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