comparison OrthancFramework/Sources/FileStorage/StorageAccessor.cpp @ 5048:22966345eaba

skip StorageCache when verifying md5 or when re-compressing attachments
author Alain Mazy <am@osimis.io>
date Wed, 29 Jun 2022 13:15:20 +0200
parents 207f259c41c5
children 5c997c72603c
comparison
equal deleted inserted replaced
5047:207f259c41c5 5048:22966345eaba
60 } 60 }
61 } 61 }
62 }; 62 };
63 63
64 64
65 StorageAccessor::StorageAccessor(IStorageArea &area, StorageCache& cache) : 65 StorageAccessor::StorageAccessor(IStorageArea &area, StorageCache* cache) :
66 area_(area), 66 area_(area),
67 cache_(cache), 67 cache_(cache),
68 metrics_(NULL) 68 metrics_(NULL)
69 { 69 {
70 } 70 }
71 71
72 StorageAccessor::StorageAccessor(IStorageArea &area, 72 StorageAccessor::StorageAccessor(IStorageArea &area,
73 StorageCache& cache, 73 StorageCache* cache,
74 MetricsRegistry &metrics) : 74 MetricsRegistry &metrics) :
75 area_(area), 75 area_(area),
76 cache_(cache), 76 cache_(cache),
77 metrics_(&metrics) 77 metrics_(&metrics)
78 { 78 {
99 case CompressionType_None: 99 case CompressionType_None:
100 { 100 {
101 MetricsTimer timer(*this, METRICS_CREATE); 101 MetricsTimer timer(*this, METRICS_CREATE);
102 102
103 area_.Create(uuid, data, size, type); 103 area_.Create(uuid, data, size, type);
104 cache_.Add(uuid, type, data, size); 104
105 if (cache_ != NULL)
106 {
107 cache_->Add(uuid, type, data, size);
108 }
105 109
106 return FileInfo(uuid, type, size, md5); 110 return FileInfo(uuid, type, size, md5);
107 } 111 }
108 112
109 case CompressionType_ZlibWithSize: 113 case CompressionType_ZlibWithSize:
131 { 135 {
132 area_.Create(uuid, NULL, 0, type); 136 area_.Create(uuid, NULL, 0, type);
133 } 137 }
134 } 138 }
135 139
136 cache_.Add(uuid, type, data, size); // always add uncompressed data to cache 140 if (cache_ != NULL)
141 {
142 cache_->Add(uuid, type, data, size); // always add uncompressed data to cache
143 }
144
137 return FileInfo(uuid, type, size, md5, 145 return FileInfo(uuid, type, size, md5,
138 CompressionType_ZlibWithSize, compressed.size(), compressedMD5); 146 CompressionType_ZlibWithSize, compressed.size(), compressedMD5);
139 } 147 }
140 148
141 default: 149 default:
154 162
155 163
156 void StorageAccessor::Read(std::string& content, 164 void StorageAccessor::Read(std::string& content,
157 const FileInfo& info) 165 const FileInfo& info)
158 { 166 {
159 if (!cache_.Fetch(content, info.GetUuid(), info.GetContentType())) 167 if (cache_ == NULL ||
168 !cache_->Fetch(content, info.GetUuid(), info.GetContentType()))
160 { 169 {
161 switch (info.GetCompressionType()) 170 switch (info.GetCompressionType())
162 { 171 {
163 case CompressionType_None: 172 case CompressionType_None:
164 { 173 {
190 throw OrthancException(ErrorCode_NotImplemented); 199 throw OrthancException(ErrorCode_NotImplemented);
191 } 200 }
192 } 201 }
193 202
194 // always store the uncompressed data in cache 203 // always store the uncompressed data in cache
195 cache_.Add(info.GetUuid(), info.GetContentType(), content); 204 if (cache_ != NULL)
205 {
206 cache_->Add(info.GetUuid(), info.GetContentType(), content);
207 }
196 } 208 }
197 209
198 // TODO Check the validity of the uncompressed MD5? 210 // TODO Check the validity of the uncompressed MD5?
199 } 211 }
200 212
201 213
202 void StorageAccessor::ReadRaw(std::string& content, 214 void StorageAccessor::ReadRaw(std::string& content,
203 const FileInfo& info) 215 const FileInfo& info)
204 { 216 {
205 if (!cache_.Fetch(content, info.GetUuid(), info.GetContentType())) 217 if (cache_ == NULL || !cache_->Fetch(content, info.GetUuid(), info.GetContentType()))
206 { 218 {
207 MetricsTimer timer(*this, METRICS_READ); 219 MetricsTimer timer(*this, METRICS_READ);
208 std::unique_ptr<IMemoryBuffer> buffer(area_.Read(info.GetUuid(), info.GetContentType())); 220 std::unique_ptr<IMemoryBuffer> buffer(area_.Read(info.GetUuid(), info.GetContentType()));
209 buffer->MoveToString(content); 221 buffer->MoveToString(content);
210 } 222 }
212 224
213 225
214 void StorageAccessor::Remove(const std::string& fileUuid, 226 void StorageAccessor::Remove(const std::string& fileUuid,
215 FileContentType type) 227 FileContentType type)
216 { 228 {
217 cache_.Invalidate(fileUuid, type); 229 if (cache_ != NULL)
230 {
231 cache_->Invalidate(fileUuid, type);
232 }
218 233
219 { 234 {
220 MetricsTimer timer(*this, METRICS_REMOVE); 235 MetricsTimer timer(*this, METRICS_REMOVE);
221 area_.Remove(fileUuid, type); 236 area_.Remove(fileUuid, type);
222 } 237 }
232 void StorageAccessor::ReadStartRange(std::string& target, 247 void StorageAccessor::ReadStartRange(std::string& target,
233 const std::string& fileUuid, 248 const std::string& fileUuid,
234 FileContentType contentType, 249 FileContentType contentType,
235 uint64_t end /* exclusive */) 250 uint64_t end /* exclusive */)
236 { 251 {
237 if (!cache_.FetchStartRange(target, fileUuid, contentType, end)) 252 if (cache_ == NULL || !cache_->FetchStartRange(target, fileUuid, contentType, end))
238 { 253 {
239 MetricsTimer timer(*this, METRICS_READ); 254 MetricsTimer timer(*this, METRICS_READ);
240 std::unique_ptr<IMemoryBuffer> buffer(area_.ReadRange(fileUuid, contentType, 0, end)); 255 std::unique_ptr<IMemoryBuffer> buffer(area_.ReadRange(fileUuid, contentType, 0, end));
241 assert(buffer->GetSize() == end); 256 assert(buffer->GetSize() == end);
242 buffer->MoveToString(target); 257 buffer->MoveToString(target);
243 258
244 cache_.AddStartRange(fileUuid, contentType, target); 259 if (cache_ != NULL)
260 {
261 cache_->AddStartRange(fileUuid, contentType, target);
262 }
245 } 263 }
246 } 264 }
247 265
248 266
249 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1 267 #if ORTHANC_ENABLE_CIVETWEB == 1 || ORTHANC_ENABLE_MONGOOSE == 1
250 void StorageAccessor::SetupSender(BufferHttpSender& sender, 268 void StorageAccessor::SetupSender(BufferHttpSender& sender,
251 const FileInfo& info, 269 const FileInfo& info,
252 const std::string& mime) 270 const std::string& mime)
253 { 271 {
254 if (!cache_.Fetch(sender.GetBuffer(), info.GetUuid(), info.GetContentType())) 272 if (cache_ == NULL || !cache_->Fetch(sender.GetBuffer(), info.GetUuid(), info.GetContentType()))
255 { 273 {
256 MetricsTimer timer(*this, METRICS_READ); 274 MetricsTimer timer(*this, METRICS_READ);
257 std::unique_ptr<IMemoryBuffer> buffer(area_.Read(info.GetUuid(), info.GetContentType())); 275 std::unique_ptr<IMemoryBuffer> buffer(area_.Read(info.GetUuid(), info.GetContentType()));
258 buffer->MoveToString(sender.GetBuffer()); 276 buffer->MoveToString(sender.GetBuffer());
259 277
260 cache_.Add(info.GetUuid(), info.GetContentType(), sender.GetBuffer()); 278 if (cache_ != NULL)
279 {
280 cache_->Add(info.GetUuid(), info.GetContentType(), sender.GetBuffer());
281 }
261 } 282 }
262 283
263 sender.SetContentType(mime); 284 sender.SetContentType(mime);
264 285
265 const char* extension; 286 const char* extension;