comparison OrthancServer/StorageCommitmentReports.cpp @ 3736:0540b54324f1 storage-commitment

StorageCommitmentReports
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Mar 2020 17:43:49 +0100
parents
children f29843323daf
comparison
equal deleted inserted replaced
3735:77183afbf55e 3736:0540b54324f1
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "PrecompiledHeadersServer.h"
35 #include "StorageCommitmentReports.h"
36
37 #include "../Core/OrthancException.h"
38
39 namespace Orthanc
40 {
41 void StorageCommitmentReports::Report::MarkAsComplete()
42 {
43 if (isComplete_)
44 {
45 throw OrthancException(ErrorCode_BadSequenceOfCalls);
46 }
47 else
48 {
49 isComplete_ = true;
50 }
51 }
52
53 void StorageCommitmentReports::Report::AddSuccess(const std::string& sopClassUid,
54 const std::string& sopInstanceUid)
55 {
56 if (isComplete_)
57 {
58 throw OrthancException(ErrorCode_BadSequenceOfCalls);
59 }
60 else
61 {
62 Success success;
63 success.sopClassUid_ = sopClassUid;
64 success.sopInstanceUid_ = sopInstanceUid;
65 successes_.push_back(success);
66 }
67 }
68
69 void StorageCommitmentReports::Report::AddFailure(const std::string& sopClassUid,
70 const std::string& sopInstanceUid,
71 StorageCommitmentFailureReason reason)
72 {
73 if (isComplete_)
74 {
75 throw OrthancException(ErrorCode_BadSequenceOfCalls);
76 }
77 else
78 {
79 Failure failure;
80 failure.sopClassUid_ = sopClassUid;
81 failure.sopInstanceUid_ = sopInstanceUid;
82 failure.reason_ = reason;
83 failures_.push_back(failure);
84 }
85 }
86
87
88 StorageCommitmentReports::Report::Status StorageCommitmentReports::Report::GetStatus() const
89 {
90 if (!isComplete_)
91 {
92 return Status_Pending;
93 }
94 else if (failures_.empty())
95 {
96 return Status_Success;
97 }
98 else
99 {
100 return Status_Failure;
101 }
102 }
103
104
105 StorageCommitmentReports::~StorageCommitmentReports()
106 {
107 while (!content_.IsEmpty())
108 {
109 Report* report = NULL;
110 content_.RemoveOldest(report);
111
112 assert(report != NULL);
113 delete report;
114 }
115 }
116
117
118 void StorageCommitmentReports::Store(const std::string& transactionUid,
119 Report* report)
120 {
121 std::unique_ptr<Report> protection(report);
122
123 boost::mutex::scoped_lock lock(mutex_);
124
125 {
126 Report* previous = NULL;
127 if (content_.Contains(transactionUid, previous))
128 {
129 assert(previous != NULL);
130 delete previous;
131
132 content_.Invalidate(transactionUid);
133 }
134 }
135
136 assert(maxSize_ == 0 ||
137 content_.GetSize() <= maxSize_);
138
139 if (maxSize_ != 0 &&
140 content_.GetSize() == maxSize_)
141 {
142 assert(!content_.IsEmpty());
143
144 Report* oldest = NULL;
145 content_.RemoveOldest(oldest);
146
147 assert(oldest != NULL);
148 delete oldest;
149 }
150
151 assert(maxSize_ == 0 ||
152 content_.GetSize() < maxSize_);
153
154 content_.Add(transactionUid, protection.release());
155 }
156
157
158 StorageCommitmentReports::Accessor::Accessor(StorageCommitmentReports& that,
159 const std::string& transactionUid) :
160 lock_(that.mutex_),
161 transactionUid_(transactionUid)
162 {
163 if (that.content_.Contains(transactionUid, report_))
164 {
165 that.content_.MakeMostRecent(transactionUid);
166 }
167 else
168 {
169 report_ = NULL;
170 }
171 }
172
173 const StorageCommitmentReports::Report&
174 StorageCommitmentReports::Accessor::GetReport() const
175 {
176 if (report_ == NULL)
177 {
178 throw OrthancException(ErrorCode_BadSequenceOfCalls);
179 }
180 else
181 {
182 return *report_;
183 }
184 }
185 }