comparison OrthancServer/Sources/ServerIndex.cpp @ 4590:4a0bf1019335 db-changes

simplification of ServerIndex::Listener as ServerIndex::TransactionContext
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 12 Mar 2021 16:04:09 +0100
parents bec74e29f86b
children ff8170d17d90
comparison
equal deleted inserted replaced
4589:bec74e29f86b 4590:4a0bf1019335
49 49
50 static const uint64_t MEGA_BYTES = 1024 * 1024; 50 static const uint64_t MEGA_BYTES = 1024 * 1024;
51 51
52 namespace Orthanc 52 namespace Orthanc
53 { 53 {
54 class ServerIndex::Listener : public StatelessDatabaseOperations::ITransactionContext 54 class ServerIndex::TransactionContext : public StatelessDatabaseOperations::ITransactionContext
55 { 55 {
56 private: 56 private:
57 struct FileToRemove 57 struct FileToRemove
58 { 58 {
59 private: 59 private:
83 ResourceType remainingType_; 83 ResourceType remainingType_;
84 std::string remainingPublicId_; 84 std::string remainingPublicId_;
85 std::list<FileToRemove> pendingFilesToRemove_; 85 std::list<FileToRemove> pendingFilesToRemove_;
86 std::list<ServerIndexChange> pendingChanges_; 86 std::list<ServerIndexChange> pendingChanges_;
87 uint64_t sizeOfFilesToRemove_; 87 uint64_t sizeOfFilesToRemove_;
88 bool insideTransaction_;
89 uint64_t sizeOfAddedAttachments_; 88 uint64_t sizeOfAddedAttachments_;
90 89
91 void Reset() 90 void Reset()
92 { 91 {
93 sizeOfFilesToRemove_ = 0; 92 sizeOfFilesToRemove_ = 0;
94 hasRemainingLevel_ = false; 93 hasRemainingLevel_ = false;
95 pendingFilesToRemove_.clear(); 94 pendingFilesToRemove_.clear();
96 pendingChanges_.clear(); 95 pendingChanges_.clear();
97 sizeOfAddedAttachments_ = 0; 96 sizeOfAddedAttachments_ = 0;
98 }
99
100 public:
101 explicit Listener(ServerContext& context) :
102 context_(context),
103 insideTransaction_(false)
104 {
105 Reset();
106 assert(ResourceType_Patient < ResourceType_Study &&
107 ResourceType_Study < ResourceType_Series &&
108 ResourceType_Series < ResourceType_Instance);
109 }
110
111 void StartTransaction()
112 {
113 Reset();
114 insideTransaction_ = true;
115 }
116
117 void EndTransaction()
118 {
119 insideTransaction_ = false;
120 }
121
122 uint64_t GetSizeOfFilesToRemove()
123 {
124 return sizeOfFilesToRemove_;
125 } 97 }
126 98
127 void CommitFilesToRemove() 99 void CommitFilesToRemove()
128 { 100 {
129 for (std::list<FileToRemove>::const_iterator 101 for (std::list<FileToRemove>::const_iterator
150 { 122 {
151 context_.SignalChange(*it); 123 context_.SignalChange(*it);
152 } 124 }
153 } 125 }
154 126
127 public:
128 explicit TransactionContext(ServerContext& context) :
129 context_(context)
130 {
131 Reset();
132 assert(ResourceType_Patient < ResourceType_Study &&
133 ResourceType_Study < ResourceType_Series &&
134 ResourceType_Series < ResourceType_Instance);
135 }
136
155 virtual void SignalRemainingAncestor(ResourceType parentType, 137 virtual void SignalRemainingAncestor(ResourceType parentType,
156 const std::string& publicId) ORTHANC_OVERRIDE 138 const std::string& publicId) ORTHANC_OVERRIDE
157 { 139 {
158 LOG(TRACE) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")"; 140 LOG(TRACE) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")";
159 141
190 { 172 {
191 LOG(TRACE) << "Change related to resource " << change.GetPublicId() << " of type " 173 LOG(TRACE) << "Change related to resource " << change.GetPublicId() << " of type "
192 << EnumerationToString(change.GetResourceType()) << ": " 174 << EnumerationToString(change.GetResourceType()) << ": "
193 << EnumerationToString(change.GetChangeType()); 175 << EnumerationToString(change.GetChangeType());
194 176
195 if (insideTransaction_) 177 pendingChanges_.push_back(change);
196 {
197 pendingChanges_.push_back(change);
198 }
199 else
200 {
201 context_.SignalChange(change);
202 }
203 } 178 }
204 179
205 virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE 180 virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE
206 { 181 {
207 sizeOfAddedAttachments_ += compressedSize; 182 sizeOfAddedAttachments_ += compressedSize;
208 }
209
210 bool HasRemainingLevel() const
211 {
212 return hasRemainingLevel_;
213 }
214
215 ResourceType GetRemainingType() const
216 {
217 assert(HasRemainingLevel());
218 return remainingType_;
219 }
220
221 const std::string& GetRemainingPublicId() const
222 {
223 assert(HasRemainingLevel());
224 return remainingPublicId_;
225 }
226
227 uint64_t GetSizeOfAddedAttachments() const
228 {
229 return sizeOfAddedAttachments_;
230 } 183 }
231 184
232 virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */, 185 virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */,
233 ResourceType& remainingLevel /* out */) ORTHANC_OVERRIDE 186 ResourceType& remainingLevel /* out */) ORTHANC_OVERRIDE
234 { 187 {
235 if (HasRemainingLevel()) 188 if (hasRemainingLevel_)
236 { 189 {
237 remainingPublicId = GetRemainingPublicId(); 190 remainingPublicId = remainingPublicId_;
238 remainingLevel = GetRemainingType(); 191 remainingLevel = remainingType_;
239 return true; 192 return true;
240 } 193 }
241 else 194 else
242 { 195 {
243 return false; 196 return false;
267 CommitChanges(); 220 CommitChanges();
268 } 221 }
269 222
270 virtual int64_t GetCompressedSizeDelta() ORTHANC_OVERRIDE 223 virtual int64_t GetCompressedSizeDelta() ORTHANC_OVERRIDE
271 { 224 {
272 return (static_cast<int64_t>(GetSizeOfAddedAttachments()) - 225 return (static_cast<int64_t>(sizeOfAddedAttachments_) -
273 static_cast<int64_t>(GetSizeOfFilesToRemove())); 226 static_cast<int64_t>(sizeOfFilesToRemove_));
274 } 227 }
275 }; 228 };
276 229
277 230
278 class ServerIndex::TransactionContextFactory : public ITransactionContextFactory 231 class ServerIndex::TransactionContextFactory : public ITransactionContextFactory
279 { 232 {
280 private: 233 private:
281 class Context : public ITransactionContext 234 ServerContext& context_;
282 {
283 private:
284 Listener& listener_;
285
286 public:
287 Context(ServerIndex& index) :
288 listener_(*index.listener_)
289 {
290 listener_.StartTransaction();
291 }
292
293 ~Context()
294 {
295 listener_.EndTransaction();
296 }
297
298 virtual bool IsUnstableResource(int64_t id) ORTHANC_OVERRIDE
299 {
300 return listener_.IsUnstableResource(id);
301 }
302
303 virtual bool LookupRemainingLevel(std::string& remainingPublicId /* out */,
304 ResourceType& remainingLevel /* out */) ORTHANC_OVERRIDE
305 {
306 return listener_.LookupRemainingLevel(remainingPublicId, remainingLevel);
307 }
308
309 virtual void MarkAsUnstable(int64_t id,
310 Orthanc::ResourceType type,
311 const std::string& publicId) ORTHANC_OVERRIDE
312 {
313 listener_.MarkAsUnstable(id, type, publicId);
314 }
315
316 virtual void SignalAttachmentsAdded(uint64_t compressedSize) ORTHANC_OVERRIDE
317 {
318 listener_.SignalAttachmentsAdded(compressedSize);
319 }
320
321 virtual void SignalChange(const ServerIndexChange& change) ORTHANC_OVERRIDE
322 {
323 listener_.SignalChange(change);
324 }
325
326 virtual void Commit() ORTHANC_OVERRIDE
327 {
328 listener_.Commit();
329 }
330
331 virtual int64_t GetCompressedSizeDelta() ORTHANC_OVERRIDE
332 {
333 return listener_.GetCompressedSizeDelta();
334 }
335
336 virtual void SignalRemainingAncestor(ResourceType parentType,
337 const std::string& publicId) ORTHANC_OVERRIDE
338 {
339 listener_.SignalRemainingAncestor(parentType, publicId);
340 }
341
342 virtual void SignalAttachmentDeleted(const FileInfo& info) ORTHANC_OVERRIDE
343 {
344 listener_.SignalAttachmentDeleted(info);
345 }
346
347 virtual void SignalResourceDeleted(ResourceType type,
348 const std::string& publicId) ORTHANC_OVERRIDE
349 {
350 listener_.SignalResourceDeleted(type, publicId);
351 }
352 };
353
354 ServerIndex& index_;
355 235
356 public: 236 public:
357 TransactionContextFactory(ServerIndex& index) : 237 TransactionContextFactory(ServerContext& context) :
358 index_(index) 238 context_(context)
359 { 239 {
360 } 240 }
361 241
362 virtual ITransactionContext* Create() 242 virtual ITransactionContext* Create()
363 { 243 {
364 return new Context(index_); 244 return new TransactionContext(context_);
365 } 245 }
366 }; 246 };
367 247
368 248
369 class ServerIndex::UnstableResourcePayload 249 class ServerIndex::UnstableResourcePayload
450 StatelessDatabaseOperations(db), 330 StatelessDatabaseOperations(db),
451 done_(false), 331 done_(false),
452 maximumStorageSize_(0), 332 maximumStorageSize_(0),
453 maximumPatients_(0) 333 maximumPatients_(0)
454 { 334 {
455 listener_.reset(new Listener(context)); 335 SetTransactionContextFactory(new TransactionContextFactory(context));
456
457 SetTransactionContextFactory(new TransactionContextFactory(*this));
458 336
459 // Initial recycling if the parameters have changed since the last 337 // Initial recycling if the parameters have changed since the last
460 // execution of Orthanc 338 // execution of Orthanc
461 StandaloneRecycling(maximumStorageSize_, maximumPatients_); 339 StandaloneRecycling(maximumStorageSize_, maximumPatients_);
462 340