Mercurial > hg > orthanc-transfers
annotate Framework/OrthancInstancesCache.cpp @ 72:d60d1a3a7357 OrthancTransfers-1.4
fix md5
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 26 Mar 2024 10:16:15 +0100 |
parents | eb6edb022a00 |
children | 1e396fb509ca |
rev | line source |
---|---|
0 | 1 /** |
2 * Transfers accelerator plugin for Orthanc | |
33
44a0430d7899
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
3 * Copyright (C) 2018-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "OrthancInstancesCache.h" | |
21 | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
22 #include <Compatibility.h> // For std::unique_ptr |
41 | 23 #include <Logging.h> |
0 | 24 |
25 namespace OrthancPlugins | |
26 { | |
27 void OrthancInstancesCache::CacheAccessor::CheckValid() const | |
28 { | |
29 if (instance_ == NULL) | |
30 { | |
31 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
32 } | |
33 } | |
34 | |
35 | |
36 OrthancInstancesCache::CacheAccessor::CacheAccessor(OrthancInstancesCache& cache, | |
37 const std::string& instanceId) : | |
38 lock_(cache.mutex_), | |
39 instance_(NULL) | |
40 { | |
41 cache.CheckInvariants(); | |
42 | |
43 if (cache.index_.Contains(instanceId)) | |
44 { | |
45 // Move the instance at the end of the LRU recycling | |
46 cache.index_.MakeMostRecent(instanceId); | |
47 | |
48 Content::const_iterator instance = cache.content_.find(instanceId); | |
49 assert(instance != cache.content_.end() && | |
50 instance->first == instanceId && | |
51 instance->second != NULL); | |
52 | |
53 instance_ = instance->second; | |
54 } | |
55 } | |
56 | |
57 | |
58 const DicomInstanceInfo& OrthancInstancesCache::CacheAccessor::GetInfo() const | |
59 { | |
60 CheckValid(); | |
61 return instance_->GetInfo(); | |
62 } | |
63 | |
64 | |
65 void OrthancInstancesCache::CacheAccessor::GetChunk(std::string& chunk, | |
66 std::string& md5, | |
67 size_t offset, | |
68 size_t size) | |
69 { | |
70 CheckValid(); | |
71 return instance_->GetChunk(chunk, md5, offset, size); | |
72 } | |
73 | |
74 | |
75 void OrthancInstancesCache::CheckInvariants() | |
76 { | |
77 #ifndef NDEBUG | |
78 size_t s = 0; | |
79 | |
80 assert(content_.size() == index_.GetSize()); | |
81 | |
82 for (Content::const_iterator it = content_.begin(); | |
83 it != content_.end(); ++it) | |
84 { | |
85 assert(it->second != NULL); | |
86 s += it->second->GetInfo().GetSize(); | |
87 | |
88 assert(index_.Contains(it->first)); | |
89 } | |
90 | |
91 assert(s == memorySize_); | |
92 | |
93 if (memorySize_ > maxMemorySize_) | |
94 { | |
95 // It is only allowed to overtake the max memory size if the | |
96 // cache contains a single, large DICOM instance | |
97 assert(index_.GetSize() == 1 && | |
98 content_.size() == 1 && | |
99 memorySize_ == (content_.begin())->second->GetInfo().GetSize()); | |
100 } | |
101 #endif | |
102 } | |
103 | |
104 | |
105 void OrthancInstancesCache::RemoveOldest() | |
106 { | |
107 CheckInvariants(); | |
108 | |
109 assert(!index_.IsEmpty()); | |
110 | |
111 std::string oldest = index_.RemoveOldest(); | |
112 | |
113 Content::iterator instance = content_.find(oldest); | |
114 assert(instance != content_.end() && | |
115 instance->second != NULL); | |
116 | |
117 memorySize_ -= instance->second->GetInfo().GetSize(); | |
118 delete instance->second; | |
119 content_.erase(instance); | |
120 } | |
121 | |
122 | |
123 void OrthancInstancesCache::Store(const std::string& instanceId, | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
124 std::unique_ptr<SourceDicomInstance>& instance) |
0 | 125 { |
126 if (instance.get() == NULL) | |
127 { | |
128 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
129 } | |
130 | |
131 if (index_.Contains(instanceId)) | |
132 { | |
133 // This instance has been read by another thread since the cache | |
134 // lookup, give up | |
135 index_.MakeMostRecent(instanceId); | |
136 return; | |
137 } | |
138 else | |
139 { | |
140 // Make room in the cache for the new instance | |
141 while (!index_.IsEmpty() && | |
142 memorySize_ + instance->GetInfo().GetSize() > maxMemorySize_) | |
143 { | |
144 RemoveOldest(); | |
145 } | |
146 | |
147 CheckInvariants(); | |
148 | |
149 index_.AddOrMakeMostRecent(instanceId); | |
150 memorySize_ += instance->GetInfo().GetSize(); | |
151 content_[instanceId] = instance.release(); | |
152 | |
153 CheckInvariants(); | |
154 } | |
155 } | |
156 | |
157 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
158 OrthancInstancesCache::OrthancInstancesCache() : |
0 | 159 memorySize_(0), |
160 maxMemorySize_(512 * MB) // 512 MB by default | |
161 { | |
162 } | |
163 | |
164 | |
165 OrthancInstancesCache::~OrthancInstancesCache() | |
166 { | |
167 CheckInvariants(); | |
168 | |
169 for (Content::iterator it = content_.begin(); | |
170 it != content_.end(); ++it) | |
171 { | |
172 assert(it->second != NULL); | |
173 delete it->second; | |
174 } | |
175 } | |
176 | |
177 | |
178 size_t OrthancInstancesCache::GetMemorySize() | |
179 { | |
180 boost::mutex::scoped_lock lock(mutex_); | |
181 return memorySize_; | |
182 } | |
183 | |
184 | |
185 size_t OrthancInstancesCache::GetMaxMemorySize() | |
186 { | |
187 boost::mutex::scoped_lock lock(mutex_); | |
188 return maxMemorySize_; | |
189 } | |
190 | |
191 | |
192 void OrthancInstancesCache::SetMaxMemorySize(size_t size) | |
193 { | |
194 if (size <= 0) | |
195 { | |
196 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
197 } | |
198 | |
199 boost::mutex::scoped_lock lock(mutex_); | |
200 | |
201 while (memorySize_ > size) | |
202 { | |
203 RemoveOldest(); | |
204 } | |
205 | |
206 maxMemorySize_ = size; | |
207 CheckInvariants(); | |
208 } | |
209 | |
210 | |
211 void OrthancInstancesCache::GetInstanceInfo(size_t& size, | |
212 std::string& md5, | |
213 const std::string& instanceId) | |
214 { | |
215 // Check whether the instance is part of the cache | |
216 { | |
217 CacheAccessor accessor(*this, instanceId); | |
218 if (accessor.IsValid()) | |
219 { | |
220 size = accessor.GetInfo().GetSize(); | |
221 md5 = accessor.GetInfo().GetMD5(); | |
222 return; | |
223 } | |
224 } | |
225 | |
226 // The instance was not in the cache, load it | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
227 std::unique_ptr<SourceDicomInstance> instance(new SourceDicomInstance(instanceId)); |
0 | 228 size = instance->GetInfo().GetSize(); |
229 md5 = instance->GetInfo().GetMD5(); | |
230 | |
231 // Store the just-loaded DICOM instance into the cache | |
232 { | |
233 boost::mutex::scoped_lock lock(mutex_); | |
234 Store(instanceId, instance); | |
235 } | |
236 } | |
237 | |
238 | |
239 void OrthancInstancesCache::GetChunk(std::string& chunk, | |
240 std::string& md5, | |
241 const std::string& instanceId, | |
242 size_t offset, | |
243 size_t size) | |
244 { | |
245 // Check whether the instance is part of the cache | |
246 { | |
247 CacheAccessor accessor(*this, instanceId); | |
248 if (accessor.IsValid()) | |
249 { | |
41 | 250 // keeping this log for future (see TODO) LOG(INFO) << "++++ Chunk for " << instanceId << " is in cache"; |
0 | 251 accessor.GetChunk(chunk, md5, offset, size); |
252 return; | |
253 } | |
254 } | |
255 | |
256 // The instance was not in the cache, load it | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
257 std::unique_ptr<SourceDicomInstance> instance(new SourceDicomInstance(instanceId)); |
41 | 258 instance->GetChunk(chunk, md5, offset, size); |
0 | 259 |
260 // Store the just-loaded DICOM instance into the cache | |
261 { | |
41 | 262 // keeping this log for future (see TODO) LOG(ERROR) << "---- Chunk for " << instanceId << " not in cache -> adding it"; |
0 | 263 boost::mutex::scoped_lock lock(mutex_); |
264 Store(instanceId, instance); | |
265 } | |
266 } | |
267 | |
268 | |
269 void OrthancInstancesCache::GetChunk(std::string& chunk, | |
270 std::string& md5, | |
271 const TransferBucket& bucket, | |
272 size_t chunkIndex) | |
273 { | |
274 GetChunk(chunk, md5, bucket.GetChunkInstanceId(chunkIndex), | |
275 bucket.GetChunkOffset(chunkIndex), | |
276 bucket.GetChunkSize(chunkIndex)); | |
277 } | |
278 } |