annotate Core/Cache/SharedArchive.cpp @ 2426:23f5ff059ce2

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Oct 2017 12:51:11 +0200
parents a3a65de1840f
children 878b59270859
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1386
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
2244
a3a65de1840f shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2172
diff changeset
5 * Copyright (C) 2017 Osimis, Belgium
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU General Public License as
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * published by the Free Software Foundation, either version 3 of the
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * License, or (at your option) any later version.
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * In addition, as a special exception, the copyright holders of this
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * program give permission to link the code of its release with the
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * that use the same license as the "OpenSSL" library), and distribute
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * the linked executables. You must obey the GNU General Public License
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * in all respects for all of the code used other than "OpenSSL". If you
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * modify file(s) with this exception, you may extend this exception to
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * your version of the file(s), but you are not obligated to do so. If
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * you do not wish to do so, delete this exception statement from your
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * version. If you delete this exception statement from all source files
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 * in the program, then also delete it here.
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 *
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * This program is distributed in the hope that it will be useful, but
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 * General Public License for more details.
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 *
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * You should have received a copy of the GNU General Public License
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 **/
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "../PrecompiledHeaders.h"
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "SharedArchive.h"
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
2172
84d1d392a9ab GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2142
diff changeset
37 #include "../SystemToolbox.h"
84d1d392a9ab GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2142
diff changeset
38
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 namespace Orthanc
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 void SharedArchive::RemoveInternal(const std::string& id)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 Archive::iterator it = archive_.find(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 if (it != archive_.end())
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 delete it->second;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 archive_.erase(it);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 SharedArchive::Accessor::Accessor(SharedArchive& that,
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 const std::string& id) :
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 lock_(that.mutex_)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 Archive::iterator it = that.archive_.find(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 if (it == that.archive_.end())
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 throw OrthancException(ErrorCode_InexistentItem);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 else
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 that.lru_.MakeMostRecent(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 item_ = it->second;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
1368
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
72 SharedArchive::SharedArchive(size_t maxSize) :
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
73 maxSize_(maxSize)
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
74 {
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
75 if (maxSize == 0)
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
76 {
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
77 throw OrthancException(ErrorCode_ParameterOutOfRange);
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
78 }
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
79 }
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
80
b22ba8c5edbe query retrieve
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1367
diff changeset
81
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 SharedArchive::~SharedArchive()
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 for (Archive::iterator it = archive_.begin();
1386
72184773c62f cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1368
diff changeset
85 it != archive_.end(); ++it)
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 delete it->second;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 std::string SharedArchive::Add(IDynamicObject* obj)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 boost::mutex::scoped_lock lock(mutex_);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 if (archive_.size() == maxSize_)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 // The quota has been reached, remove the oldest element
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 std::string oldest = lru_.RemoveOldest();
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 RemoveInternal(oldest);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
2172
84d1d392a9ab GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2142
diff changeset
103 std::string id = SystemToolbox::GenerateUuid();
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 RemoveInternal(id); // Should never be useful because of UUID
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 archive_[id] = obj;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 lru_.Add(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 return id;
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 void SharedArchive::Remove(const std::string& id)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 boost::mutex::scoped_lock lock(mutex_);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 RemoveInternal(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 lru_.Invalidate(id);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 void SharedArchive::List(std::list<std::string>& items)
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 items.clear();
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 boost::mutex::scoped_lock lock(mutex_);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 for (Archive::const_iterator it = archive_.begin();
1386
72184773c62f cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1368
diff changeset
127 it != archive_.end(); ++it)
1367
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 {
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 items.push_back(it->first);
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
fe6e5a9f1ea2 SharedArchive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134