Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Cache/SharedArchive.cpp @ 4295:90f91b78d708
applied log categories
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Thu, 05 Nov 2020 12:01:11 +0100 |
parents | bf7b9edf6b81 |
children | b30a8de92ad9 |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
1367 | 6 * |
7 * 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
|
8 * 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
|
9 * 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
|
10 * the License, or (at your option) any later version. |
1367 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * 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
|
15 * Lesser General Public License for more details. |
1367 | 16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
1367 | 20 **/ |
21 | |
22 | |
23 #include "../PrecompiledHeaders.h" | |
24 #include "SharedArchive.h" | |
25 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
26 #include "../Toolbox.h" |
2172
84d1d392a9ab
GenerateUuid() not available in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2142
diff
changeset
|
27 |
1367 | 28 |
29 namespace Orthanc | |
30 { | |
31 void SharedArchive::RemoveInternal(const std::string& id) | |
32 { | |
33 Archive::iterator it = archive_.find(id); | |
34 | |
35 if (it != archive_.end()) | |
36 { | |
37 delete it->second; | |
38 archive_.erase(it); | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
39 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
40 lru_.Invalidate(id); |
1367 | 41 } |
42 } | |
43 | |
44 | |
45 SharedArchive::Accessor::Accessor(SharedArchive& that, | |
46 const std::string& id) : | |
47 lock_(that.mutex_) | |
48 { | |
49 Archive::iterator it = that.archive_.find(id); | |
50 | |
51 if (it == that.archive_.end()) | |
52 { | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
53 item_ = NULL; |
1367 | 54 } |
55 else | |
56 { | |
57 that.lru_.MakeMostRecent(id); | |
58 item_ = it->second; | |
59 } | |
60 } | |
61 | |
62 | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
63 IDynamicObject& SharedArchive::Accessor::GetItem() const |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
64 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
65 if (item_ == NULL) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
66 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
67 // "IsValid()" should have been called |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
68 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
69 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
70 else |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
71 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
72 return *item_; |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
73 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
74 } |
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 |
1368 | 77 SharedArchive::SharedArchive(size_t maxSize) : |
78 maxSize_(maxSize) | |
79 { | |
80 if (maxSize == 0) | |
81 { | |
82 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
83 } | |
84 } | |
85 | |
86 | |
1367 | 87 SharedArchive::~SharedArchive() |
88 { | |
89 for (Archive::iterator it = archive_.begin(); | |
1386 | 90 it != archive_.end(); ++it) |
1367 | 91 { |
92 delete it->second; | |
93 } | |
94 } | |
95 | |
96 | |
97 std::string SharedArchive::Add(IDynamicObject* obj) | |
98 { | |
99 boost::mutex::scoped_lock lock(mutex_); | |
100 | |
101 if (archive_.size() == maxSize_) | |
102 { | |
103 // 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
|
104 RemoveInternal(lru_.GetOldest()); |
1367 | 105 } |
106 | |
2512
4dcafa8d6633
SystemToolbox::GenerateUuid moved to Toolbox::GenerateUuid
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
107 std::string id = Toolbox::GenerateUuid(); |
1367 | 108 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
|
109 |
1367 | 110 archive_[id] = obj; |
111 lru_.Add(id); | |
112 | |
113 return id; | |
114 } | |
115 | |
116 | |
117 void SharedArchive::Remove(const std::string& id) | |
118 { | |
119 boost::mutex::scoped_lock lock(mutex_); | |
120 RemoveInternal(id); | |
121 } | |
122 | |
123 | |
124 void SharedArchive::List(std::list<std::string>& items) | |
125 { | |
126 items.clear(); | |
127 | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
128 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
129 boost::mutex::scoped_lock lock(mutex_); |
1367 | 130 |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
131 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
|
132 it != archive_.end(); ++it) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
133 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
134 items.push_back(it->first); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2512
diff
changeset
|
135 } |
1367 | 136 } |
137 } | |
138 } |