annotate OrthancServer/ServerIndex.cpp @ 274:f2286c741109

patient protection in Orthanc Explorer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 08 Dec 2012 22:48:53 +0100
parents d384af918264
children 771f12042be9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
62
a70bb32802ae renaming Server
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Belgium
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
10 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
11 * In addition, as a special exception, the copyright holders of this
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
12 * program give permission to link the code of its release with the
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
15 * the linked executables. You must obey the GNU General Public License
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
19 * you do not wish to do so, delete this exception statement from your
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
20 * version. If you delete this exception statement from all source files
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 108
diff changeset
21 * in the program, then also delete it here.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "ServerIndex.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
6
9d483cf71d17 fix warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2
diff changeset
35 #ifndef NOMINMAX
2
67a6978503b7 fixing Windows build
Jodogne@Laptop
parents: 0
diff changeset
36 #define NOMINMAX
6
9d483cf71d17 fix warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2
diff changeset
37 #endif
9d483cf71d17 fix warnings
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2
diff changeset
38
8
3a584803783e build with visual studio
jodogne
parents: 6
diff changeset
39 #include "EmbeddedResources.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include "../Core/Toolbox.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include "../Core/Uuid.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include "../Core/DicomFormat/DicomArray.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include "../Core/SQLite/Transaction.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 #include "FromDcmtkBridge.h"
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
45 #include "ServerContext.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 #include <boost/lexical_cast.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 #include <stdio.h>
108
a6e41de88a53 using glog
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 84
diff changeset
49 #include <glog/logging.h>
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
62
a70bb32802ae renaming Server
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
51 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 namespace Internals
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
55 class ServerIndexListener : public IServerIndexListener
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
56 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
57 private:
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
58 ServerContext& context_;
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
59 bool hasRemainingLevel_;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
60 ResourceType remainingType_;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
61 std::string remainingPublicId_;
264
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
62 std::list<std::string> pendingFilesToRemove_;
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
63
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
64 public:
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
65 ServerIndexListener(ServerContext& context) :
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
66 context_(context),
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
67 hasRemainingLevel_(false)
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
68 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
69 assert(ResourceType_Patient < ResourceType_Study &&
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
70 ResourceType_Study < ResourceType_Series &&
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
71 ResourceType_Series < ResourceType_Instance);
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
72 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
73
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
74 void Reset()
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
75 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
76 hasRemainingLevel_ = false;
264
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
77 pendingFilesToRemove_.clear();
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
78 }
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
79
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
80 void CommitFilesToRemove()
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
81 {
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
82 for (std::list<std::string>::iterator
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
83 it = pendingFilesToRemove_.begin();
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
84 it != pendingFilesToRemove_.end(); it++)
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
85 {
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
86 context_.RemoveFile(*it);
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
87 }
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
88 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
89
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
90 virtual void SignalRemainingAncestor(ResourceType parentType,
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
91 const std::string& publicId)
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
92 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
93 LOG(INFO) << "Remaining ancestor \"" << publicId << "\" (" << parentType << ")";
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
94
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
95 if (hasRemainingLevel_)
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
96 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
97 if (parentType < remainingType_)
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
98 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
99 remainingType_ = parentType;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
100 remainingPublicId_ = publicId;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
101 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
102 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
103 else
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
104 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
105 hasRemainingLevel_ = true;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
106 remainingType_ = parentType;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
107 remainingPublicId_ = publicId;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
108 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
109 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
110
273
d384af918264 more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 272
diff changeset
111 virtual void SignalFileDeleted(const FileInfo& info)
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
112 {
273
d384af918264 more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 272
diff changeset
113 assert(Toolbox::IsUuid(info.GetUuid()));
d384af918264 more detailed signal about deleted file
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 272
diff changeset
114 pendingFilesToRemove_.push_back(info.GetUuid());
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
115 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
116
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
117 bool HasRemainingLevel() const
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
118 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
119 return hasRemainingLevel_;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
120 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
121
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
122 ResourceType GetRemainingType() const
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
123 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
124 assert(HasRemainingLevel());
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
125 return remainingType_;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
126 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
127
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
128 const std::string& GetRemainingPublicId() const
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
129 {
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
130 assert(HasRemainingLevel());
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
131 return remainingPublicId_;
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
132 }
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
133 };
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
212
f276b175dcaf delete resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 206
diff changeset
137 bool ServerIndex::DeleteResource(Json::Value& target,
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 const std::string& uuid,
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
139 ResourceType expectedType)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 {
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
141 boost::mutex::scoped_lock lock(mutex_);
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
142 listener_->Reset();
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
143
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
144 std::auto_ptr<SQLite::Transaction> t(db_->StartTransaction());
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
145 t->Begin();
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
146
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
147 int64_t id;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
148 ResourceType type;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
149 if (!db_->LookupResource(uuid, id, type) ||
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
150 expectedType != type)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 return false;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
154
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
155 db_->DeleteResource(id);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
157 if (listener_->HasRemainingLevel())
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
159 ResourceType type = listener_->GetRemainingType();
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
160 const std::string& uuid = listener_->GetRemainingPublicId();
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 target["RemainingAncestor"] = Json::Value(Json::objectValue);
204
7f4acf490179 changes api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 202
diff changeset
163 target["RemainingAncestor"]["Path"] = GetBasePath(type, uuid);
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
164 target["RemainingAncestor"]["Type"] = ToString(type);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 target["RemainingAncestor"]["ID"] = uuid;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 target["RemainingAncestor"] = Json::nullValue;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
172 t->Commit();
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
173
264
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
174 // We can remove the files once the SQLite transaction has been
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
175 // successfully committed
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
176 listener_->CommitFilesToRemove();
5b8e8b74bc8b remove files only after the sqlite transaction has succeeded
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
177
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 return true;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
182 static void FlushThread(DatabaseWrapper* db,
220
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
183 boost::mutex* mutex,
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
184 unsigned int sleep)
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
185 {
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
186 LOG(INFO) << "Starting the database flushing thread (sleep = " << sleep << ")";
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
187
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
188 while (1)
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
189 {
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
190 boost::this_thread::sleep(boost::posix_time::seconds(sleep));
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
191 boost::mutex::scoped_lock lock(*mutex);
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
192 db->FlushToDisk();
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
193 }
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
194 }
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
195
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
196
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
197 ServerIndex::ServerIndex(ServerContext& context,
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
198 const std::string& dbPath) :
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
199 maximumStorageSize_(0),
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
200 maximumPatients_(0)
186
f68c039b0571 preparing refactoring of ServerIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
201 {
226
8a26a8e85edf refactoring to read files
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 224
diff changeset
202 listener_.reset(new Internals::ServerIndexListener(context));
186
f68c039b0571 preparing refactoring of ServerIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 180
diff changeset
203
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
204 if (dbPath == ":memory:")
180
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
205 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
206 db_.reset(new DatabaseWrapper(*listener_));
180
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
207 }
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
208 else
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 {
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
210 boost::filesystem::path p = dbPath;
180
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
211
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
212 try
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
213 {
201
bee20e978835 refactoring of delete
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 200
diff changeset
214 boost::filesystem::create_directories(p);
180
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
215 }
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
216 catch (boost::filesystem::filesystem_error)
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
217 {
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
218 }
626777d01dc4 use of hashes to index dicom objects
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 179
diff changeset
219
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
220 db_.reset(new DatabaseWrapper(p.string() + "/index", *listener_));
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
222
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
223 // Initial recycling if the parameters have changed since the last
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
224 // execution of Orthanc
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
225 StandaloneRecycling();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
226
220
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
227 unsigned int sleep;
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
228 try
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
229 {
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
230 std::string sleepString = db_->GetGlobalProperty(GlobalProperty_FlushSleep);
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
231 sleep = boost::lexical_cast<unsigned int>(sleepString);
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
232 }
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
233 catch (boost::bad_lexical_cast&)
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
234 {
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
235 // By default, wait for 10 seconds before flushing
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
236 sleep = 10;
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
237 }
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
238
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
239 flushThread_ = boost::thread(FlushThread, db_.get(), &mutex_, sleep);
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
240 }
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
241
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
242
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
243 ServerIndex::~ServerIndex()
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
244 {
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
245 LOG(INFO) << "Stopping the database flushing thread";
220
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
246 /*flushThread_.terminate();
bb8c260c0092 fix for windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 212
diff changeset
247 flushThread_.join();*/
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
187
8e673a65564d refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
251 StoreStatus ServerIndex::Store(const DicomMap& dicomSummary,
232
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
252 const Attachments& attachments,
187
8e673a65564d refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
253 const std::string& remoteAet)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 {
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
255 boost::mutex::scoped_lock lock(mutex_);
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
256 listener_->Reset();
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257
178
5739b4d10a4b hashing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 147
diff changeset
258 DicomInstanceHasher hasher(dicomSummary);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 try
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
262 std::auto_ptr<SQLite::Transaction> t(db_->StartTransaction());
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
263 t->Begin();
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
265 int64_t patient, study, series, instance;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
266 ResourceType type;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
267 bool isNewSeries = false;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
268
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
269 // Do nothing if the instance already exists
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
270 if (db_->LookupResource(hasher.HashInstance(), patient, type))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
272 assert(type == ResourceType_Instance);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 return StoreStatus_AlreadyStored;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
276 // Ensure there is enough room in the storage for the new instance
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
277 uint64_t instanceSize = 0;
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
278 for (Attachments::const_iterator it = attachments.begin();
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
279 it != attachments.end(); it++)
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
280 {
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
281 instanceSize += it->GetCompressedSize();
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
282 }
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
283
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
284 Recycle(instanceSize, hasher.HashPatient());
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
285
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
286 // Create the instance
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
287 instance = db_->CreateResource(hasher.HashInstance(), ResourceType_Instance);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
288
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
289 DicomMap dicom;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
290 dicomSummary.ExtractInstanceInformation(dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
291 db_->SetMainDicomTags(instance, dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
292
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
293 // Create the patient/study/series/instance hierarchy
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
294 if (!db_->LookupResource(hasher.HashSeries(), series, type))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
296 // This is a new series
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
297 isNewSeries = true;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
298 series = db_->CreateResource(hasher.HashSeries(), ResourceType_Series);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
299 dicomSummary.ExtractSeriesInformation(dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
300 db_->SetMainDicomTags(series, dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
301 db_->AttachChild(series, instance);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
302
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
303 if (!db_->LookupResource(hasher.HashStudy(), study, type))
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
304 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
305 // This is a new study
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
306 study = db_->CreateResource(hasher.HashStudy(), ResourceType_Study);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
307 dicomSummary.ExtractStudyInformation(dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
308 db_->SetMainDicomTags(study, dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
309 db_->AttachChild(study, series);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
310
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
311 if (!db_->LookupResource(hasher.HashPatient(), patient, type))
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
312 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
313 // This is a new patient
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
314 patient = db_->CreateResource(hasher.HashPatient(), ResourceType_Patient);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
315 dicomSummary.ExtractPatientInformation(dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
316 db_->SetMainDicomTags(patient, dicom);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
317 db_->AttachChild(patient, study);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
318 }
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
319 else
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
320 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
321 assert(type == ResourceType_Patient);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
322 db_->AttachChild(patient, study);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
323 }
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
324 }
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
325 else
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
326 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
327 assert(type == ResourceType_Study);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
328 db_->AttachChild(study, series);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
329 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 else
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
333 assert(type == ResourceType_Series);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
334 db_->AttachChild(series, instance);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
337 // Attach the files to the newly created instance
232
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
338 for (Attachments::const_iterator it = attachments.begin();
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
339 it != attachments.end(); it++)
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
340 {
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
341 db_->AddAttachment(instance, *it);
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
342 }
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
343
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
344 // Attach the metadata
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
345 db_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, Toolbox::GetNowIsoString());
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
346 db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet);
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
347
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
348 const DicomValue* value;
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
349 if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL ||
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
350 (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 {
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
352 db_->SetMetadata(instance, MetadataType_Instance_IndexInSeries, value->AsString());
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
355 if (isNewSeries)
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
356 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
357 if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
358 (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL ||
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
359 (value = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL)
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
360 {
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
361 db_->SetMetadata(series, MetadataType_Series_ExpectedNumberOfInstances, value->AsString());
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
362 }
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
363 }
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
364
205
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
365 // Check whether the series of this new instance is now completed
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
366 SeriesStatus seriesStatus = GetSeriesStatus(series);
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
367 if (seriesStatus == SeriesStatus_Complete)
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
368 {
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
369 db_->LogChange(ChangeType_CompletedSeries, series, ResourceType_Series);
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
370 }
6ab754744446 logging of completed series
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 204
diff changeset
371
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
372 t->Commit();
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
373
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
374 // We can remove the files once the SQLite transaction has been
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
375 // successfully committed. Some files might have to be deleted
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
376 // because of recycling.
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
377 listener_->CommitFilesToRemove();
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
378
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 return StoreStatus_Success;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 }
62
a70bb32802ae renaming Server
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 57
diff changeset
381 catch (OrthancException& e)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 {
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
383 LOG(ERROR) << "EXCEPTION [" << e.What() << "]"
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
384 << " (SQLite status: " << db_->GetErrorMessage() << ")";
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 return StoreStatus_Failure;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390
238
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
391 void ServerIndex::ComputeStatistics(Json::Value& target)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 {
240
jodogne
parents: 239
diff changeset
393 static const uint64_t MB = 1024 * 1024;
jodogne
parents: 239
diff changeset
394
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
395 boost::mutex::scoped_lock lock(mutex_);
238
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
396 target = Json::objectValue;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397
238
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
398 uint64_t cs = db_->GetTotalCompressedSize();
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
399 uint64_t us = db_->GetTotalUncompressedSize();
239
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 238
diff changeset
400 target["TotalDiskSpace"] = boost::lexical_cast<std::string>(cs);
238
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
401 target["TotalUncompressedSize"] = boost::lexical_cast<std::string>(us);
240
jodogne
parents: 239
diff changeset
402 target["TotalDiskSpaceMB"] = boost::lexical_cast<unsigned int>(cs / MB);
jodogne
parents: 239
diff changeset
403 target["TotalUncompressedSizeMB"] = boost::lexical_cast<unsigned int>(us / MB);
238
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
404
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
405 target["CountPatients"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Patient));
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
406 target["CountStudies"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Study));
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
407 target["CountSeries"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Series));
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
408 target["CountInstances"] = static_cast<unsigned int>(db_->GetResourceCount(ResourceType_Instance));
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
409 }
e4148b0ab1d0 statistics URI
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
410
187
8e673a65564d refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
411
8e673a65564d refactoring of storing new instances
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
412
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
413 SeriesStatus ServerIndex::GetSeriesStatus(int id)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
414 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
415 // Get the expected number of instances in this series (from the metadata)
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
416 std::string s = db_->GetMetadata(id, MetadataType_Series_ExpectedNumberOfInstances);
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
417
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
418 size_t expected;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
419 try
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
420 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
421 expected = boost::lexical_cast<size_t>(s);
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
422 if (expected < 0)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
423 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
424 return SeriesStatus_Unknown;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
425 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
426 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
427 catch (boost::bad_lexical_cast&)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
428 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
429 return SeriesStatus_Unknown;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
430 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
431
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
432 // Loop over the instances of this series
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
433 std::list<int64_t> children;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
434 db_->GetChildrenInternalId(children, id);
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
435
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
436 std::set<size_t> instances;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
437 for (std::list<int64_t>::const_iterator
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
438 it = children.begin(); it != children.end(); it++)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
439 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
440 // Get the index of this instance in the series
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
441 s = db_->GetMetadata(*it, MetadataType_Instance_IndexInSeries);
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
442 size_t index;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
443 try
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
444 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
445 index = boost::lexical_cast<size_t>(s);
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
446 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
447 catch (boost::bad_lexical_cast&)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
448 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
449 return SeriesStatus_Unknown;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
450 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
451
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
452 if (index <= 0 || index > expected)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
453 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
454 // Out-of-range instance index
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
455 return SeriesStatus_Inconsistent;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
456 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
457
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
458 if (instances.find(index) != instances.end())
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
459 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
460 // Twice the same instance index
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
461 return SeriesStatus_Inconsistent;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
462 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
463
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
464 instances.insert(index);
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
465 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
466
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
467 if (instances.size() == expected)
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
468 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
469 return SeriesStatus_Complete;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
470 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
471 else
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
472 {
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
473 return SeriesStatus_Missing;
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
474 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
475 }
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
476
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
477
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
478
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
479 void ServerIndex::MainDicomTagsToJson(Json::Value& target,
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
480 int64_t resourceId)
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
481 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
482 DicomMap tags;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
483 db_->GetMainDicomTags(tags, resourceId);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
484 target["MainDicomTags"] = Json::objectValue;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
485 FromDcmtkBridge::ToJson(target["MainDicomTags"], tags);
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
486 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
487
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
488 bool ServerIndex::LookupResource(Json::Value& result,
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
489 const std::string& publicId,
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
490 ResourceType expectedType)
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
491 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
492 result = Json::objectValue;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
493
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
494 boost::mutex::scoped_lock lock(mutex_);
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
495
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
496 // Lookup for the requested resource
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
497 int64_t id;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
498 ResourceType type;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
499 if (!db_->LookupResource(publicId, id, type) ||
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
500 type != expectedType)
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
501 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
502 return false;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
503 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
504
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
505 // Find the parent resource (if it exists)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
506 if (type != ResourceType_Patient)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
507 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
508 int64_t parentId;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
509 if (!db_->LookupParent(parentId, id))
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
510 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
511 throw OrthancException(ErrorCode_InternalError);
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
512 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
513
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
514 std::string parent = db_->GetPublicId(parentId);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
515
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
516 switch (type)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
517 {
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
518 case ResourceType_Study:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
519 result["ParentPatient"] = parent;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
520 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
521
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
522 case ResourceType_Series:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
523 result["ParentStudy"] = parent;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
524 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
525
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
526 case ResourceType_Instance:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
527 result["ParentSeries"] = parent;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
528 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
529
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
530 default:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
531 throw OrthancException(ErrorCode_InternalError);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
532 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
533 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
534
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
535 // List the children resources
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
536 std::list<std::string> children;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
537 db_->GetChildrenPublicId(children, id);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
538
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
539 if (type != ResourceType_Instance)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
540 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
541 Json::Value c = Json::arrayValue;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
542
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
543 for (std::list<std::string>::const_iterator
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
544 it = children.begin(); it != children.end(); it++)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
545 {
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
546 c.append(*it);
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
547 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
548
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
549 switch (type)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
550 {
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
551 case ResourceType_Patient:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
552 result["Studies"] = c;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
553 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
554
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
555 case ResourceType_Study:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
556 result["Series"] = c;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
557 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
558
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
559 case ResourceType_Series:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
560 result["Instances"] = c;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
561 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
562
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
563 default:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
564 throw OrthancException(ErrorCode_InternalError);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
565 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
566 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
567
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
568 // Set the resource type
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
569 switch (type)
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
570 {
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
571 case ResourceType_Patient:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
572 result["Type"] = "Patient";
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
573 break;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
574
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
575 case ResourceType_Study:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
576 result["Type"] = "Study";
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
577 break;
199
dfa2899d9960 refactoring cont
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 198
diff changeset
578
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
579 case ResourceType_Series:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
580 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
581 result["Type"] = "Series";
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
582 result["Status"] = ToString(GetSeriesStatus(id));
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
583
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
584 int i;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
585 if (db_->GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances))
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
586 result["ExpectedNumberOfInstances"] = i;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
587 else
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
588 result["ExpectedNumberOfInstances"] = Json::nullValue;
200
9c58b2b03cf0 refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 199
diff changeset
589
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
590 break;
200
9c58b2b03cf0 refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 199
diff changeset
591 }
9c58b2b03cf0 refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 199
diff changeset
592
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
593 case ResourceType_Instance:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
594 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
595 result["Type"] = "Instance";
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
596
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
597 FileInfo attachment;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
598 if (!db_->LookupAttachment(attachment, id, FileContentType_Dicom))
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
599 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
600 throw OrthancException(ErrorCode_InternalError);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
601 }
200
9c58b2b03cf0 refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 199
diff changeset
602
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
603 result["FileSize"] = static_cast<unsigned int>(attachment.GetUncompressedSize());
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
604 result["FileUuid"] = attachment.GetUuid();
200
9c58b2b03cf0 refactoring of read operations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 199
diff changeset
605
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
606 int i;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
607 if (db_->GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries))
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
608 result["IndexInSeries"] = i;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
609 else
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
610 result["IndexInSeries"] = Json::nullValue;
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
611
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
612 break;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
613 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
614
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
615 default:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
616 throw OrthancException(ErrorCode_InternalError);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
617 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
618
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
619 // Record the remaining information
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
620 result["ID"] = publicId;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
621 MainDicomTagsToJson(result, id);
198
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
622
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
623 return true;
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
624 }
663cc6c46d0a before refactoring of ServerIndex::GetXXX
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 197
diff changeset
625
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626
232
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
627 bool ServerIndex::LookupAttachment(FileInfo& attachment,
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
628 const std::string& instanceUuid,
233
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 232
diff changeset
629 FileContentType contentType)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630 {
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
631 boost::mutex::scoped_lock lock(mutex_);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632
193
a1b9d1e1497b failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
633 int64_t id;
a1b9d1e1497b failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
634 ResourceType type;
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
635 if (!db_->LookupResource(instanceUuid, id, type) ||
193
a1b9d1e1497b failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
636 type != ResourceType_Instance)
192
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
637 {
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
638 throw OrthancException(ErrorCode_InternalError);
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
639 }
193
a1b9d1e1497b failed attempt to compile with linux standard base
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 192
diff changeset
640
232
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
641 if (db_->LookupAttachment(attachment, id, contentType))
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
642 {
233
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 232
diff changeset
643 assert(attachment.GetContentType() == contentType);
232
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
644 return true;
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
645 }
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
646 else
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
647 {
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
648 return false;
5368bbe813cf refactoring of attachments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 231
diff changeset
649 }
192
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
650 }
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
651
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
652
c56dc32266e0 refactoring getfile
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 191
diff changeset
653
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 void ServerIndex::GetAllUuids(Json::Value& target,
190
b6cef9d45cc3 getallpublicids
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 189
diff changeset
655 ResourceType resourceType)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656 {
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
657 boost::mutex::scoped_lock lock(mutex_);
202
1650557bd81a refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 201
diff changeset
658 db_->GetAllPublicIds(target, resourceType);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662 bool ServerIndex::GetChanges(Json::Value& target,
204
7f4acf490179 changes api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 202
diff changeset
663 int64_t since,
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
664 unsigned int maxResults)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 {
206
4453a010d0db flush to disk thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 205
diff changeset
666 boost::mutex::scoped_lock lock(mutex_);
237
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
667 db_->GetChanges(target, since, maxResults);
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
668 return true;
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
669 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670
237
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
671 bool ServerIndex::GetLastChange(Json::Value& target)
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
672 {
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
673 boost::mutex::scoped_lock lock(mutex_);
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
674 db_->GetLastChange(target);
204
7f4acf490179 changes api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 202
diff changeset
675 return true;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676 }
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
677
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
678 void ServerIndex::LogExportedResource(const std::string& publicId,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
679 const std::string& remoteModality)
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
680 {
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
681 boost::mutex::scoped_lock lock(mutex_);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
682
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
683 int64_t id;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
684 ResourceType type;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
685 if (!db_->LookupResource(publicId, id, type))
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
686 {
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
687 throw OrthancException(ErrorCode_InternalError);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
688 }
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
689
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
690 std::string patientId;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
691 std::string studyInstanceUid;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
692 std::string seriesInstanceUid;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
693 std::string sopInstanceUid;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
694
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
695 int64_t currentId = id;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
696 ResourceType currentType = type;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
697
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
698 // Iteratively go up inside the patient/study/series/instance hierarchy
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
699 bool done = false;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
700 while (!done)
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
701 {
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
702 DicomMap map;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
703 db_->GetMainDicomTags(map, currentId);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
704
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
705 switch (currentType)
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
706 {
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
707 case ResourceType_Patient:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
708 patientId = map.GetValue(DICOM_TAG_PATIENT_ID).AsString();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
709 done = true;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
710 break;
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
711
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
712 case ResourceType_Study:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
713 studyInstanceUid = map.GetValue(DICOM_TAG_STUDY_INSTANCE_UID).AsString();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
714 currentType = ResourceType_Patient;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
715 break;
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
716
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
717 case ResourceType_Series:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
718 seriesInstanceUid = map.GetValue(DICOM_TAG_SERIES_INSTANCE_UID).AsString();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
719 currentType = ResourceType_Study;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
720 break;
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
721
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
722 case ResourceType_Instance:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
723 sopInstanceUid = map.GetValue(DICOM_TAG_SOP_INSTANCE_UID).AsString();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
724 currentType = ResourceType_Series;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
725 break;
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
726
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
727 default:
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
728 throw OrthancException(ErrorCode_InternalError);
231
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
729 }
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
730
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
731 // If we have not reached the Patient level, find the parent of
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
732 // the current resource
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
733 if (!done)
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
734 {
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
735 assert(db_->LookupParent(currentId, currentId));
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
736 }
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
737 }
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
738
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
739 // No need for a SQLite::Transaction here, as we only insert 1 record
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
740 db_->LogExportedResource(type,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
741 publicId,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
742 remoteModality,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
743 patientId,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
744 studyInstanceUid,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
745 seriesInstanceUid,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
746 sopInstanceUid);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
747 }
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
748
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
749
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
750 bool ServerIndex::GetExportedResources(Json::Value& target,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
751 int64_t since,
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
752 unsigned int maxResults)
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
753 {
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
754 boost::mutex::scoped_lock lock(mutex_);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
755 db_->GetExportedResources(target, since, maxResults);
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
756 return true;
8098448bd827 export log
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 226
diff changeset
757 }
237
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
758
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
759 bool ServerIndex::GetLastExportedResource(Json::Value& target)
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
760 {
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
761 boost::mutex::scoped_lock lock(mutex_);
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
762 db_->GetLastExportedResource(target);
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
763 return true;
16a4ac70bd8a last change and export
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 233
diff changeset
764 }
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
765
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
766
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
767 bool ServerIndex::IsRecyclingNeeded(uint64_t instanceSize)
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
768 {
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
769 if (maximumStorageSize_ != 0)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
770 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
771 uint64_t currentSize = db_->GetTotalCompressedSize();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
772 if (currentSize + instanceSize > maximumStorageSize_)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
773 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
774 return true;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
775 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
776 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
777
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
778 if (maximumPatients_ != 0)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
779 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
780 uint64_t patientCount = db_->GetResourceCount(ResourceType_Patient);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
781 if (patientCount > maximumPatients_)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
782 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
783 return true;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
784 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
785 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
786
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
787 return false;
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
788 }
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
789
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
790
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
791 void ServerIndex::Recycle(uint64_t instanceSize,
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
792 const std::string& newPatientId)
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
793 {
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
794 if (!IsRecyclingNeeded(instanceSize))
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
795 {
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
796 return;
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
797 }
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
798
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
799 // Check whether other DICOM instances from this patient are
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
800 // already stored
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
801 int64_t patientToAvoid;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
802 ResourceType type;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
803 bool hasPatientToAvoid = db_->LookupResource(newPatientId, patientToAvoid, type);
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
804
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
805 if (hasPatientToAvoid && type != ResourceType_Patient)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
806 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
807 throw OrthancException(ErrorCode_InternalError);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
808 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
809
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
810 // Iteratively select patient to remove until there is enough
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
811 // space in the DICOM store
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
812 int64_t patientToRecycle;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
813 while (true)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
814 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
815 // If other instances of this patient are already in the store,
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
816 // we must avoid to recycle them
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
817 bool ok = hasPatientToAvoid ?
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
818 db_->SelectPatientToRecycle(patientToRecycle, patientToAvoid) :
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
819 db_->SelectPatientToRecycle(patientToRecycle);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
820
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
821 if (!ok)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
822 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
823 throw OrthancException(ErrorCode_FullStorage);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
824 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
825
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
826 LOG(INFO) << "Recycling one patient";
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
827 db_->DeleteResource(patientToRecycle);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
828
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
829 if (!IsRecyclingNeeded(instanceSize))
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
830 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
831 // OK, we're done
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
832 break;
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
833 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
834 }
268
4bc02e2254ec preparing ServerIndex for recycling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 264
diff changeset
835 }
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
836
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
837 void ServerIndex::SetMaximumPatientCount(unsigned int count)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
838 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
839 boost::mutex::scoped_lock lock(mutex_);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
840 maximumPatients_ = count;
270
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
841
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
842 if (count == 0)
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
843 {
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
844 LOG(WARNING) << "No limit on the number of stored patients";
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
845 }
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
846 else
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
847 {
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
848 LOG(WARNING) << "At most " << count << " patients will be stored";
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
849 }
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
850
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
851 StandaloneRecycling();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
852 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
853
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
854 void ServerIndex::SetMaximumStorageSize(uint64_t size)
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
855 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
856 boost::mutex::scoped_lock lock(mutex_);
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
857 maximumStorageSize_ = size;
270
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
858
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
859 if (size == 0)
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
860 {
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
861 LOG(WARNING) << "No limit on the size of the storage area";
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
862 }
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
863 else
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
864 {
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
865 LOG(WARNING) << "At most " << (size / (1024 * 1024)) << "MB will be used for the storage area";
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
866 }
e6a4c4329481 parameters for storage capacity
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 269
diff changeset
867
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
868 StandaloneRecycling();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
869 }
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
870
272
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
871 void ServerIndex::StandaloneRecycling()
269
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
872 {
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
873 // WARNING: No mutex here, do not include this as a public method
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
874 std::auto_ptr<SQLite::Transaction> t(db_->StartTransaction());
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
875 t->Begin();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
876 Recycle(0, "");
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
877 t->Commit();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
878 listener_->CommitFilesToRemove();
f6fdf5abe751 recycling up and running
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 268
diff changeset
879 }
272
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
880
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
881
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
882 bool ServerIndex::IsProtectedPatient(const std::string& publicId)
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
883 {
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
884 boost::mutex::scoped_lock lock(mutex_);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
885
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
886 // Lookup for the requested resource
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
887 int64_t id;
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
888 ResourceType type;
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
889 if (!db_->LookupResource(publicId, id, type) ||
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
890 type != ResourceType_Patient)
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
891 {
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
892 throw OrthancException(ErrorCode_ParameterOutOfRange);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
893 }
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
894
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
895 return db_->IsProtectedPatient(id);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
896 }
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
897
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
898
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
899 void ServerIndex::SetProtectedPatient(const std::string& publicId,
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
900 bool isProtected)
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
901 {
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
902 boost::mutex::scoped_lock lock(mutex_);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
903
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
904 // Lookup for the requested resource
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
905 int64_t id;
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
906 ResourceType type;
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
907 if (!db_->LookupResource(publicId, id, type) ||
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
908 type != ResourceType_Patient)
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
909 {
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
910 throw OrthancException(ErrorCode_ParameterOutOfRange);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
911 }
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
912
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
913 // No need for a SQLite::Transaction here, as we only make 1 write to the DB
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
914 db_->SetProtectedPatient(id, isProtected);
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
915
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
916 if (isProtected)
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
917 LOG(INFO) << "Patient " << publicId << " has been protected";
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
918 else
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
919 LOG(INFO) << "Patient " << publicId << " has been unprotected";
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
920 }
337c506461d2 protection from rest api
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 270
diff changeset
921
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
922 }