Mercurial > hg > orthanc
comparison Plugins/Engine/OrthancPluginDatabase.cpp @ 3019:8336204d95dc db-changes
refactoring computation of disk size for recycling
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Dec 2018 18:07:40 +0100 |
parents | bfee0b9f3209 |
children | d207f6ac1f86 |
comparison
equal
deleted
inserted
replaced
3018:e3b5c07146a3 | 3019:8336204d95dc |
---|---|
45 | 45 |
46 #include <cassert> | 46 #include <cassert> |
47 | 47 |
48 namespace Orthanc | 48 namespace Orthanc |
49 { | 49 { |
50 class OrthancPluginDatabase::Transaction : public IDatabaseWrapper::ITransaction | |
51 { | |
52 private: | |
53 OrthancPluginDatabase& that_; | |
54 | |
55 void CheckSuccess(OrthancPluginErrorCode code) const | |
56 { | |
57 if (code != OrthancPluginErrorCode_Success) | |
58 { | |
59 that_.errorDictionary_.LogError(code, true); | |
60 throw OrthancException(static_cast<ErrorCode>(code)); | |
61 } | |
62 } | |
63 | |
64 public: | |
65 Transaction(OrthancPluginDatabase& that) : | |
66 that_(that) | |
67 { | |
68 } | |
69 | |
70 virtual void Begin() | |
71 { | |
72 CheckSuccess(that_.backend_.startTransaction(that_.payload_)); | |
73 } | |
74 | |
75 virtual void Rollback() | |
76 { | |
77 CheckSuccess(that_.backend_.rollbackTransaction(that_.payload_)); | |
78 } | |
79 | |
80 virtual void Commit(int64_t diskSizeDelta) | |
81 { | |
82 if (that_.fastGetTotalSize_) | |
83 { | |
84 CheckSuccess(that_.backend_.commitTransaction(that_.payload_)); | |
85 } | |
86 else | |
87 { | |
88 if (static_cast<int64_t>(that_.currentDiskSize_) + diskSizeDelta < 0) | |
89 { | |
90 throw OrthancException(ErrorCode_DatabasePlugin); | |
91 } | |
92 | |
93 uint64_t newDiskSize = (that_.currentDiskSize_ + diskSizeDelta); | |
94 | |
95 assert(newDiskSize == that_.GetTotalCompressedSize()); | |
96 | |
97 CheckSuccess(that_.backend_.commitTransaction(that_.payload_)); | |
98 | |
99 // The transaction has succeeded, we can commit the new disk size | |
100 that_.currentDiskSize_ = newDiskSize; | |
101 } | |
102 } | |
103 }; | |
104 | |
105 | |
50 static FileInfo Convert(const OrthancPluginAttachment& attachment) | 106 static FileInfo Convert(const OrthancPluginAttachment& attachment) |
51 { | 107 { |
52 return FileInfo(attachment.uuid, | 108 return FileInfo(attachment.uuid, |
53 static_cast<FileContentType>(attachment.contentType), | 109 static_cast<FileContentType>(attachment.contentType), |
54 attachment.uncompressedSize, | 110 attachment.uncompressedSize, |
187 | 243 |
188 memcpy(&extensions_, extensions, size); | 244 memcpy(&extensions_, extensions, size); |
189 } | 245 } |
190 | 246 |
191 | 247 |
248 void OrthancPluginDatabase::Open() | |
249 { | |
250 CheckSuccess(backend_.open(payload_)); | |
251 | |
252 { | |
253 Transaction transaction(*this); | |
254 transaction.Begin(); | |
255 | |
256 std::string tmp; | |
257 fastGetTotalSize_ = | |
258 (LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) && | |
259 tmp == "1"); | |
260 | |
261 if (fastGetTotalSize_) | |
262 { | |
263 currentDiskSize_ = 0; // Unused | |
264 } | |
265 else | |
266 { | |
267 // This is the case of database plugins using Orthanc SDK <= 1.5.2 | |
268 LOG(WARNING) << "Consider upgrading your database index plugin for best performance"; | |
269 currentDiskSize_ = GetTotalCompressedSize(); | |
270 } | |
271 | |
272 transaction.Commit(0); | |
273 } | |
274 } | |
275 | |
276 | |
192 void OrthancPluginDatabase::AddAttachment(int64_t id, | 277 void OrthancPluginDatabase::AddAttachment(int64_t id, |
193 const FileInfo& attachment) | 278 const FileInfo& attachment) |
194 { | 279 { |
195 OrthancPluginAttachment tmp; | 280 OrthancPluginAttachment tmp; |
196 tmp.uuid = attachment.GetUuid().c_str(); | 281 tmp.uuid = attachment.GetUuid().c_str(); |
784 { | 869 { |
785 CheckSuccess(backend_.setProtectedPatient(payload_, internalId, isProtected)); | 870 CheckSuccess(backend_.setProtectedPatient(payload_, internalId, isProtected)); |
786 } | 871 } |
787 | 872 |
788 | 873 |
789 class OrthancPluginDatabase::Transaction : public SQLite::ITransaction | 874 IDatabaseWrapper::ITransaction* OrthancPluginDatabase::StartTransaction() |
790 { | 875 { |
791 private: | 876 return new Transaction(*this); |
792 const OrthancPluginDatabaseBackend& backend_; | |
793 void* payload_; | |
794 PluginsErrorDictionary& errorDictionary_; | |
795 | |
796 void CheckSuccess(OrthancPluginErrorCode code) | |
797 { | |
798 if (code != OrthancPluginErrorCode_Success) | |
799 { | |
800 errorDictionary_.LogError(code, true); | |
801 throw OrthancException(static_cast<ErrorCode>(code)); | |
802 } | |
803 } | |
804 | |
805 public: | |
806 Transaction(const OrthancPluginDatabaseBackend& backend, | |
807 void* payload, | |
808 PluginsErrorDictionary& errorDictionary) : | |
809 backend_(backend), | |
810 payload_(payload), | |
811 errorDictionary_(errorDictionary) | |
812 { | |
813 } | |
814 | |
815 virtual void Begin() | |
816 { | |
817 CheckSuccess(backend_.startTransaction(payload_)); | |
818 } | |
819 | |
820 virtual void Rollback() | |
821 { | |
822 CheckSuccess(backend_.rollbackTransaction(payload_)); | |
823 } | |
824 | |
825 virtual void Commit() | |
826 { | |
827 CheckSuccess(backend_.commitTransaction(payload_)); | |
828 } | |
829 }; | |
830 | |
831 | |
832 SQLite::ITransaction* OrthancPluginDatabase::StartTransaction() | |
833 { | |
834 return new Transaction(backend_, payload_, errorDictionary_); | |
835 } | 877 } |
836 | 878 |
837 | 879 |
838 static void ProcessEvent(IDatabaseListener& listener, | 880 static void ProcessEvent(IDatabaseListener& listener, |
839 const _OrthancPluginDatabaseAnswer& answer) | 881 const _OrthancPluginDatabaseAnswer& answer) |
890 void OrthancPluginDatabase::Upgrade(unsigned int targetVersion, | 932 void OrthancPluginDatabase::Upgrade(unsigned int targetVersion, |
891 IStorageArea& storageArea) | 933 IStorageArea& storageArea) |
892 { | 934 { |
893 if (extensions_.upgradeDatabase != NULL) | 935 if (extensions_.upgradeDatabase != NULL) |
894 { | 936 { |
895 Transaction transaction(backend_, payload_, errorDictionary_); | 937 Transaction transaction(*this); |
896 transaction.Begin(); | 938 transaction.Begin(); |
897 | 939 |
898 OrthancPluginErrorCode code = extensions_.upgradeDatabase( | 940 OrthancPluginErrorCode code = extensions_.upgradeDatabase( |
899 payload_, targetVersion, | 941 payload_, targetVersion, |
900 reinterpret_cast<OrthancPluginStorageArea*>(&storageArea)); | 942 reinterpret_cast<OrthancPluginStorageArea*>(&storageArea)); |
901 | 943 |
902 if (code == OrthancPluginErrorCode_Success) | 944 if (code == OrthancPluginErrorCode_Success) |
903 { | 945 { |
904 transaction.Commit(); | 946 transaction.Commit(0); |
905 } | 947 } |
906 else | 948 else |
907 { | 949 { |
908 transaction.Rollback(); | 950 transaction.Rollback(); |
909 errorDictionary_.LogError(code, true); | 951 errorDictionary_.LogError(code, true); |