annotate OrthancFramework/Sources/FileStorage/MemoryStorageArea.cpp @ 4325:b96aedfa8cc1

unit tests now running in WebAssembly
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 24 Nov 2020 16:21:29 +0100
parents bf7b9edf6b81
children d9473bd5ed43
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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.
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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.
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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/>.
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "../PrecompiledHeaders.h"
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "MemoryStorageArea.h"
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 #include "../OrthancException.h"
2826
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
27 #include "../Logging.h"
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace Orthanc
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 MemoryStorageArea::~MemoryStorageArea()
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 if (it->second != NULL)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 delete it->second;
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 void MemoryStorageArea::Create(const std::string& uuid,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 const void* content,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 size_t size,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 FileContentType type)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
2826
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
47 LOG(INFO) << "Creating attachment \"" << uuid << "\" of \"" << static_cast<int>(type)
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
48 << "\" type (size: " << (size / (1024 * 1024) + 1) << "MB)";
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
49
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 boost::mutex::scoped_lock lock(mutex_);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 if (size != 0 &&
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 content == NULL)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 throw OrthancException(ErrorCode_NullPointer);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 else if (content_.find(uuid) != content_.end())
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 throw OrthancException(ErrorCode_InternalError);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 else
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 content_[uuid] = new std::string(reinterpret_cast<const char*>(content), size);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 void MemoryStorageArea::Read(std::string& content,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 const std::string& uuid,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 FileContentType type)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 {
2826
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
72 LOG(INFO) << "Reading attachment \"" << uuid << "\" of \""
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
73 << static_cast<int>(type) << "\" content type";
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
74
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 boost::mutex::scoped_lock lock(mutex_);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 Content::const_iterator found = content_.find(uuid);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 if (found == content_.end())
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 throw OrthancException(ErrorCode_InexistentFile);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 else if (found->second == NULL)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 throw OrthancException(ErrorCode_InternalError);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 else
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 content.assign(*found->second);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 void MemoryStorageArea::Remove(const std::string& uuid,
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 FileContentType type)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 {
2826
c277e0421200 unit testing of overwriting
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2653
diff changeset
97 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
98
2653
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 boost::mutex::scoped_lock lock(mutex_);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 Content::iterator found = content_.find(uuid);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 if (found == content_.end())
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 // Ignore second removal
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 else if (found->second == NULL)
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 throw OrthancException(ErrorCode_InternalError);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 else
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 delete found->second;
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 content_.erase(found);
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 }
d7815540bd81 MemoryStorageArea
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 }