Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Cache/SharedArchive.cpp @ 5437:85da6dcd0e08
merge
author | Alain Mazy <am@osimis.io> |
---|---|
date | Wed, 22 Nov 2023 09:39:35 +0100 |
parents | b5c502bcaf99 |
children | 48b8dae6dc77 |
rev | line source |
---|---|
1367 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1367 | 4 * Department, University Hospital of Liege, Belgium |
5185
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium |
0ea402b4d901
upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4870
diff
changeset
|
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1367 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
1367 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
1367 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
1367 | 21 **/ |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 #include "SharedArchive.h" | |
26 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
27 #include "../Toolbox.h" |
2172
84d1d392a9ab
GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
28 |
1367 | 29 |
30 namespace Orthanc | |
31 { | |
32 void SharedArchive::RemoveInternal(const std::string& id) | |
33 { | |
34 Archive::iterator it = archive_.find(id); | |
35 | |
36 if (it != archive_.end()) | |
37 { | |
38 delete it->second; | |
39 archive_.erase(it); | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
40 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
41 lru_.Invalidate(id); |
1367 | 42 } |
43 } | |
44 | |
45 | |
46 SharedArchive::Accessor::Accessor(SharedArchive& that, | |
47 const std::string& id) : | |
48 lock_(that.mutex_) | |
49 { | |
50 Archive::iterator it = that.archive_.find(id); | |
51 | |
52 if (it == that.archive_.end()) | |
53 { | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
54 item_ = NULL; |
1367 | 55 } |
56 else | |
57 { | |
58 that.lru_.MakeMostRecent(id); | |
59 item_ = it->second; | |
60 } | |
61 } | |
62 | |
4300 | 63 bool SharedArchive::Accessor::IsValid() const |
64 { | |
65 return item_ != NULL; | |
66 } | |
67 | |
1367 | 68 |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
69 IDynamicObject& SharedArchive::Accessor::GetItem() const |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
70 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
71 if (item_ == NULL) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
72 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
73 // "IsValid()" should have been called |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
74 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
75 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
76 else |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
77 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
78 return *item_; |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
79 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
80 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
81 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
82 |
1368 | 83 SharedArchive::SharedArchive(size_t maxSize) : |
84 maxSize_(maxSize) | |
85 { | |
86 if (maxSize == 0) | |
87 { | |
88 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
89 } | |
90 } | |
91 | |
92 | |
1367 | 93 SharedArchive::~SharedArchive() |
94 { | |
95 for (Archive::iterator it = archive_.begin(); | |
1386 | 96 it != archive_.end(); ++it) |
1367 | 97 { |
98 delete it->second; | |
99 } | |
100 } | |
101 | |
102 | |
103 std::string SharedArchive::Add(IDynamicObject* obj) | |
104 { | |
5310
b5c502bcaf99
added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
105 boost::recursive_mutex::scoped_lock lock(mutex_); |
1367 | 106 |
107 if (archive_.size() == maxSize_) | |
108 { | |
109 // The quota has been reached, remove the oldest element | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
110 RemoveInternal(lru_.GetOldest()); |
1367 | 111 } |
112 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
113 std::string id = Toolbox::GenerateUuid(); |
1367 | 114 RemoveInternal(id); // Should never be useful because of UUID |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
115 |
1367 | 116 archive_[id] = obj; |
117 lru_.Add(id); | |
118 | |
119 return id; | |
120 } | |
121 | |
122 | |
123 void SharedArchive::Remove(const std::string& id) | |
124 { | |
5310
b5c502bcaf99
added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
125 boost::recursive_mutex::scoped_lock lock(mutex_); |
1367 | 126 RemoveInternal(id); |
127 } | |
128 | |
129 | |
130 void SharedArchive::List(std::list<std::string>& items) | |
131 { | |
132 items.clear(); | |
133 | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
134 { |
5310
b5c502bcaf99
added a route to DELETE /jobs/../archive
Alain Mazy <am@osimis.io>
parents:
5185
diff
changeset
|
135 boost::recursive_mutex::scoped_lock lock(mutex_); |
1367 | 136 |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
137 for (Archive::const_iterator it = archive_.begin(); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
138 it != archive_.end(); ++it) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
139 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
140 items.push_back(it->first); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
141 } |
1367 | 142 } |
143 } | |
144 } |