Mercurial > hg > orthanc
comparison OrthancServer/Sources/ServerIndex.cpp @ 4585:f0bdd99f3d81 db-changes
created a ITransactionContextFactory around ServerIndex::Listener
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 09 Mar 2021 16:40:38 +0100 |
parents | b25941dcdbbe |
children | 1d96fe7e054e |
comparison
equal
deleted
inserted
replaced
4584:b25941dcdbbe | 4585:f0bdd99f3d81 |
---|---|
74 target[pos] = *it; | 74 target[pos] = *it; |
75 pos ++; | 75 pos ++; |
76 } | 76 } |
77 } | 77 } |
78 | 78 |
79 | 79 |
80 class ServerIndex::Listener : public IDatabaseListener, | 80 class ServerIndex::Listener : public IDatabaseListener, |
81 public ServerIndex::ITransactionContext | 81 public ServerIndex::ITransactionContext |
82 { | 82 { |
83 private: | 83 private: |
84 struct FileToRemove | 84 struct FileToRemove |
284 return context_.GetIndex().IsUnstableResource(id); | 284 return context_.GetIndex().IsUnstableResource(id); |
285 } | 285 } |
286 }; | 286 }; |
287 | 287 |
288 | 288 |
289 class ServerIndex::TransactionContextFactory : public ITransactionContextFactory | |
290 { | |
291 private: | |
292 class Context : public ITransactionContext | |
293 { | |
294 private: | |
295 Listener& listener_; | |
296 | |
297 public: | |
298 Context(ServerIndex& index) : | |
299 listener_(*index.listener_) | |
300 { | |
301 } | |
302 | |
303 virtual bool IsUnstableResource(int64_t id) ORTHANC_OVERRIDE | |
304 { | |
305 return listener_.IsUnstableResource(id); | |
306 } | |
307 | |
308 virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */, | |
309 ResourceType& remainingLevel /* out */) ORTHANC_OVERRIDE | |
310 { | |
311 return listener_.LookupRemainingLevel(remainingPublicId, remainingLevel); | |
312 } | |
313 | |
314 virtual void MarkAsUnstable(int64_t id, | |
315 Orthanc::ResourceType type, | |
316 const std::string& publicId) ORTHANC_OVERRIDE | |
317 { | |
318 listener_.MarkAsUnstable(id, type, publicId); | |
319 } | |
320 | |
321 virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE | |
322 { | |
323 listener_.SignalAttachmentsAdded(compressedSize); | |
324 } | |
325 | |
326 virtual void SignalChange(const ServerIndexChange& change) ORTHANC_OVERRIDE | |
327 { | |
328 listener_.SignalChange(change); | |
329 } | |
330 }; | |
331 | |
332 ServerIndex& index_; | |
333 | |
334 public: | |
335 TransactionContextFactory(ServerIndex& index) : | |
336 index_(index) | |
337 { | |
338 } | |
339 | |
340 virtual ITransactionContext* Create() | |
341 { | |
342 return new Context(index_); | |
343 } | |
344 }; | |
345 | |
346 | |
289 class ServerIndex::Transaction | 347 class ServerIndex::Transaction |
290 { | 348 { |
291 private: | 349 private: |
292 ServerIndex& index_; | 350 ServerIndex& index_; |
293 std::unique_ptr<IDatabaseWrapper::ITransaction> transaction_; | 351 std::unique_ptr<IDatabaseWrapper::ITransaction> transaction_; |
560 maxRetries_(10) | 618 maxRetries_(10) |
561 { | 619 { |
562 listener_.reset(new Listener(context)); | 620 listener_.reset(new Listener(context)); |
563 db_.SetListener(*listener_); | 621 db_.SetListener(*listener_); |
564 | 622 |
623 SetTransactionContextFactory(new TransactionContextFactory(*this)); | |
624 | |
565 // Initial recycling if the parameters have changed since the last | 625 // Initial recycling if the parameters have changed since the last |
566 // execution of Orthanc | 626 // execution of Orthanc |
567 StandaloneRecycling(maximumStorageSize_, maximumPatients_); | 627 StandaloneRecycling(maximumStorageSize_, maximumPatients_); |
568 | 628 |
569 if (db.HasFlushToDisk()) | 629 if (db.HasFlushToDisk()) |
1087 if ((readOperations == NULL && writeOperations == NULL) || | 1147 if ((readOperations == NULL && writeOperations == NULL) || |
1088 (readOperations != NULL && writeOperations != NULL)) | 1148 (readOperations != NULL && writeOperations != NULL)) |
1089 { | 1149 { |
1090 throw OrthancException(ErrorCode_InternalError); | 1150 throw OrthancException(ErrorCode_InternalError); |
1091 } | 1151 } |
1152 | |
1153 if (factory_.get() == NULL) | |
1154 { | |
1155 throw OrthancException(ErrorCode_BadSequenceOfCalls, "No transaction context was provided"); | |
1156 } | |
1092 | 1157 |
1093 unsigned int count = 0; | 1158 unsigned int count = 0; |
1094 | 1159 |
1095 for (;;) | 1160 for (;;) |
1096 { | 1161 { |
1097 try | 1162 try |
1098 { | 1163 { |
1164 std::unique_ptr<ITransactionContext> context(factory_->Create()); | |
1165 | |
1166 if (context.get() == NULL) | |
1167 { | |
1168 throw OrthancException(ErrorCode_InternalError); | |
1169 } | |
1170 | |
1099 boost::mutex::scoped_lock lock(databaseMutex_); // TODO - REMOVE | 1171 boost::mutex::scoped_lock lock(databaseMutex_); // TODO - REMOVE |
1100 | 1172 |
1101 if (readOperations != NULL) | 1173 if (readOperations != NULL) |
1102 { | 1174 { |
1103 /** | 1175 /** |
1106 * global mutex protecting the database. | 1178 * global mutex protecting the database. |
1107 **/ | 1179 **/ |
1108 | 1180 |
1109 Transaction transaction(*this, TransactionType_ReadOnly); // TODO - Only if not "TransactionType_Implicit" | 1181 Transaction transaction(*this, TransactionType_ReadOnly); // TODO - Only if not "TransactionType_Implicit" |
1110 { | 1182 { |
1111 ReadOnlyTransaction t(db_, *listener_); | 1183 ReadOnlyTransaction t(db_, *context); |
1112 readOperations->Apply(t); | 1184 readOperations->Apply(t); |
1113 } | 1185 } |
1114 transaction.Commit(); | 1186 transaction.Commit(); |
1115 } | 1187 } |
1116 else | 1188 else |
1117 { | 1189 { |
1118 assert(writeOperations != NULL); | 1190 assert(writeOperations != NULL); |
1119 | 1191 |
1120 Transaction transaction(*this, TransactionType_ReadWrite); | 1192 Transaction transaction(*this, TransactionType_ReadWrite); |
1121 { | 1193 { |
1122 assert(listener_.get() != NULL); | 1194 ReadWriteTransaction t(db_, *context); |
1123 ReadWriteTransaction t(db_, *listener_); | |
1124 writeOperations->Apply(t); | 1195 writeOperations->Apply(t); |
1125 } | 1196 } |
1126 transaction.Commit(); | 1197 transaction.Commit(); |
1127 } | 1198 } |
1128 | 1199 |
1161 } | 1232 } |
1162 } | 1233 } |
1163 } | 1234 } |
1164 | 1235 |
1165 | 1236 |
1237 void ServerIndex::SetTransactionContextFactory(ITransactionContextFactory* factory) | |
1238 { | |
1239 if (factory == NULL) | |
1240 { | |
1241 throw OrthancException(ErrorCode_NullPointer); | |
1242 } | |
1243 else if (factory_.get() != NULL) | |
1244 { | |
1245 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1246 } | |
1247 else | |
1248 { | |
1249 factory_.reset(factory); | |
1250 } | |
1251 } | |
1252 | |
1253 | |
1166 void ServerIndex::Apply(IReadOnlyOperations& operations) | 1254 void ServerIndex::Apply(IReadOnlyOperations& operations) |
1167 { | 1255 { |
1168 ApplyInternal(&operations, NULL); | 1256 ApplyInternal(&operations, NULL); |
1169 } | 1257 } |
1170 | 1258 |