Mercurial > hg > orthanc
annotate OrthancFramework/Sources/FileStorage/MemoryStorageArea.cpp @ 5559:462b8f8a619c
todo private tags in ExtraMainDicomTags
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Mon, 22 Apr 2024 10:39:33 +0200 |
parents | 48b8dae6dc77 |
children | f7adfb22e20e |
rev | line source |
---|---|
2653 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5430
diff
changeset
|
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5430
diff
changeset
|
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
2653 | 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. |
2653 | 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. |
2653 | 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/>. |
2653 | 21 **/ |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 #include "MemoryStorageArea.h" | |
26 | |
4484
64f06e7d5fc7
new abstraction IMemoryBuffer to avoid unnecessary copies of std::string buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
27 #include "../Logging.h" |
2653 | 28 #include "../OrthancException.h" |
4484
64f06e7d5fc7
new abstraction IMemoryBuffer to avoid unnecessary copies of std::string buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
29 #include "../StringMemoryBuffer.h" |
5430
b83192e7ad10
Now displaying timings when reading from/writing to disk in the verbose logs
Alain Mazy <am@osimis.io>
parents:
5398
diff
changeset
|
30 #include "../Toolbox.h" |
2653 | 31 |
32 namespace Orthanc | |
33 { | |
34 MemoryStorageArea::~MemoryStorageArea() | |
35 { | |
36 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
37 { | |
38 if (it->second != NULL) | |
39 { | |
40 delete it->second; | |
41 } | |
42 } | |
43 } | |
44 | |
45 void MemoryStorageArea::Create(const std::string& uuid, | |
46 const void* content, | |
47 size_t size, | |
48 FileContentType type) | |
49 { | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
50 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << static_cast<int>(type) |
5430
b83192e7ad10
Now displaying timings when reading from/writing to disk in the verbose logs
Alain Mazy <am@osimis.io>
parents:
5398
diff
changeset
|
51 << "\" type (size: " << Toolbox::GetHumanFileSize(size) << ")"; |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
52 |
5398
08b5516c6e5e
compatibility of OrthancFramework with latest releases of Emscripten
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
53 Mutex::ScopedLock lock(mutex_); |
2653 | 54 |
55 if (size != 0 && | |
56 content == NULL) | |
57 { | |
58 throw OrthancException(ErrorCode_NullPointer); | |
59 } | |
60 else if (content_.find(uuid) != content_.end()) | |
61 { | |
62 throw OrthancException(ErrorCode_InternalError); | |
63 } | |
64 else | |
65 { | |
66 content_[uuid] = new std::string(reinterpret_cast<const char*>(content), size); | |
67 } | |
68 } | |
69 | |
70 | |
4484
64f06e7d5fc7
new abstraction IMemoryBuffer to avoid unnecessary copies of std::string buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
71 IMemoryBuffer* MemoryStorageArea::Read(const std::string& uuid, |
64f06e7d5fc7
new abstraction IMemoryBuffer to avoid unnecessary copies of std::string buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
72 FileContentType type) |
2653 | 73 { |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
74 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
75 << static_cast<int>(type) << "\" content type"; |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
76 |
5398
08b5516c6e5e
compatibility of OrthancFramework with latest releases of Emscripten
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
77 Mutex::ScopedLock lock(mutex_); |
2653 | 78 |
79 Content::const_iterator found = content_.find(uuid); | |
80 | |
81 if (found == content_.end()) | |
82 { | |
83 throw OrthancException(ErrorCode_InexistentFile); | |
84 } | |
85 else if (found->second == NULL) | |
86 { | |
87 throw OrthancException(ErrorCode_InternalError); | |
88 } | |
89 else | |
90 { | |
4484
64f06e7d5fc7
new abstraction IMemoryBuffer to avoid unnecessary copies of std::string buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4437
diff
changeset
|
91 return StringMemoryBuffer::CreateFromCopy(*found->second); |
2653 | 92 } |
93 } | |
94 | |
95 | |
4495
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
96 IMemoryBuffer* MemoryStorageArea::ReadRange(const std::string& uuid, |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
97 FileContentType type, |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
98 uint64_t start /* inclusive */, |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
99 uint64_t end /* exclusive */) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
100 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
101 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \"" |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
102 << static_cast<int>(type) << "\" content type " |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
103 << "(range from " << start << " to " << end << ")"; |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
104 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
105 if (start > end) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
106 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
107 throw OrthancException(ErrorCode_BadRange); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
108 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
109 else if (start == end) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
110 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
111 return new StringMemoryBuffer; |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
112 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
113 else |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
114 { |
5398
08b5516c6e5e
compatibility of OrthancFramework with latest releases of Emscripten
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
115 Mutex::ScopedLock lock(mutex_); |
4495
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
116 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
117 Content::const_iterator found = content_.find(uuid); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
118 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
119 if (found == content_.end()) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
120 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
121 throw OrthancException(ErrorCode_InexistentFile); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
122 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
123 else if (found->second == NULL) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
124 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
125 throw OrthancException(ErrorCode_InternalError); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
126 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
127 else if (end > found->second->size()) |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
128 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
129 throw OrthancException(ErrorCode_BadRange); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
130 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
131 else |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
132 { |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
133 std::string range; |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
134 range.resize(end - start); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
135 assert(!range.empty()); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
136 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
137 memcpy(&range[0], &found->second[start], range.size()); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
138 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
139 return StringMemoryBuffer::CreateFromSwap(range); |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
140 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
141 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
142 } |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
143 |
fa2311f94d9f
IStorageArea::ReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4484
diff
changeset
|
144 |
4498
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
145 bool MemoryStorageArea::HasReadRange() const |
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
146 { |
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
147 return true; |
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
148 } |
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
149 |
7b99e8bb8246
IStorageArea::HasReadRange()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4495
diff
changeset
|
150 |
2653 | 151 void MemoryStorageArea::Remove(const std::string& uuid, |
152 FileContentType type) | |
153 { | |
2826
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
154 LOG(INFO) << "Deleting attachment \"" << uuid << "\" of type " << static_cast<int>(type); |
c277e0421200
unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2653
diff
changeset
|
155 |
5398
08b5516c6e5e
compatibility of OrthancFramework with latest releases of Emscripten
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
156 Mutex::ScopedLock lock(mutex_); |
2653 | 157 |
158 Content::iterator found = content_.find(uuid); | |
159 | |
160 if (found == content_.end()) | |
161 { | |
162 // Ignore second removal | |
163 } | |
164 else if (found->second == NULL) | |
165 { | |
166 throw OrthancException(ErrorCode_InternalError); | |
167 } | |
168 else | |
169 { | |
170 delete found->second; | |
171 content_.erase(found); | |
172 } | |
173 } | |
174 } |