Mercurial > hg > orthanc-databases
annotate MySQL/Plugins/MySQLIndex.cpp @ 87:48d445f756db db-changes
new extension implemented for MySQL: GetLastChangeIndex
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 10 Jan 2019 20:39:36 +0100 |
parents | b96446b8718b |
children | 3f31e3fa5114 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
67 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "MySQLIndex.h" | |
23 | |
24 #include "../../Framework/Plugins/GlobalProperties.h" | |
25 #include "../../Framework/MySQL/MySQLDatabase.h" | |
26 #include "../../Framework/MySQL/MySQLTransaction.h" | |
27 | |
28 #include <EmbeddedResources.h> // Auto-generated file | |
29 | |
30 #include <Core/Logging.h> | |
31 #include <Core/OrthancException.h> | |
32 | |
33 #include <ctype.h> | |
34 | |
35 namespace OrthancDatabases | |
36 { | |
37 IDatabase* MySQLIndex::OpenInternal() | |
38 { | |
39 uint32_t expectedVersion = 6; | |
40 if (context_) | |
41 { | |
42 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_); | |
43 } | |
44 else | |
45 { | |
46 // This case only occurs during unit testing | |
47 expectedVersion = 6; | |
48 } | |
49 | |
50 // Check the expected version of the database | |
51 if (expectedVersion != 6) | |
52 { | |
53 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
54 << "expecting the DB schema version " << expectedVersion | |
55 << ", but this plugin is only compatible with version 6"; | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
57 } | |
58 | |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
59 if (!MySQLDatabase::IsValidDatabaseIdentifier(parameters_.GetDatabase())) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
60 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
62 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
63 |
0 | 64 if (clearAll_) |
65 { | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
66 MySQLDatabase::ClearDatabase(parameters_); |
0 | 67 } |
68 | |
69 std::auto_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_)); | |
70 | |
71 db->Open(); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
72 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
73 db->Execute("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", false); |
0 | 74 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
75 if (parameters_.HasLock()) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
76 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
77 db->AdvisoryLock(42 /* some arbitrary constant */); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
78 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
79 |
0 | 80 { |
81 MySQLTransaction t(*db); | |
82 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
83 db->Execute("ALTER DATABASE " + parameters_.GetDatabase() + |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
84 " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci", false); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
85 |
0 | 86 if (!db->DoesTableExist(t, "Resources")) |
87 { | |
88 std::string query; | |
89 | |
90 Orthanc::EmbeddedResources::GetFileResource | |
91 (query, Orthanc::EmbeddedResources::MYSQL_PREPARE_INDEX); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
92 db->Execute(query, true); |
0 | 93 |
94 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); | |
95 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1); | |
96 } | |
97 | |
98 if (!db->DoesTableExist(t, "Resources")) | |
99 { | |
100 LOG(ERROR) << "Corrupted MySQL database"; | |
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
102 } | |
103 | |
104 int version = 0; | |
105 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) || | |
106 version != 6) | |
107 { | |
53 | 108 LOG(ERROR) << "MySQL plugin is incompatible with database schema version: " << version; |
0 | 109 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
110 } | |
111 | |
112 int revision; | |
113 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel)) | |
114 { | |
115 revision = 1; | |
116 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); | |
117 } | |
118 | |
84
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
119 if (revision == 1) |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
120 { |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
121 // The serialization of jobs as a global property can lead to |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
122 // very long values => switch to the LONGTEXT type that can |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
123 // store up to 4GB: |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
124 // https://stackoverflow.com/a/13932834/881731 |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
125 db->Execute("ALTER TABLE GlobalProperties MODIFY value LONGTEXT", false); |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
126 |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
127 revision = 2; |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
128 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
129 } |
b96446b8718b
Fix serialization of jobs if many of them
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
130 |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
131 if (revision == 2) |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
132 { |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
133 std::string query; |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
134 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
135 Orthanc::EmbeddedResources::GetFileResource |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
136 (query, Orthanc::EmbeddedResources::MYSQL_GET_LAST_CHANGE_INDEX); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
137 db->Execute(query, true); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
138 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
139 revision = 3; |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
140 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
141 } |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
142 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
143 if (revision != 3) |
0 | 144 { |
53 | 145 LOG(ERROR) << "MySQL plugin is incompatible with database schema revision: " << revision; |
0 | 146 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
147 } | |
148 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
149 t.Commit(); |
0 | 150 } |
151 | |
152 return db.release(); | |
153 } | |
154 | |
155 | |
156 MySQLIndex::MySQLIndex(const MySQLParameters& parameters) : | |
157 IndexBackend(new Factory(*this)), | |
158 context_(NULL), | |
159 parameters_(parameters), | |
160 clearAll_(false) | |
161 { | |
162 } | |
163 | |
164 | |
165 int64_t MySQLIndex::CreateResource(const char* publicId, | |
166 OrthancPluginResourceType type) | |
167 { | |
168 { | |
169 DatabaseManager::CachedStatement statement( | |
170 STATEMENT_FROM_HERE, GetManager(), | |
171 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL)"); | |
172 | |
173 statement.SetParameterType("id", ValueType_Utf8String); | |
174 statement.SetParameterType("type", ValueType_Integer64); | |
175 | |
176 Dictionary args; | |
177 args.SetUtf8Value("id", publicId); | |
178 args.SetIntegerValue("type", static_cast<int>(type)); | |
179 | |
180 statement.Execute(args); | |
181 } | |
182 | |
183 { | |
184 DatabaseManager::CachedStatement statement( | |
185 STATEMENT_FROM_HERE, GetManager(), | |
186 "SELECT LAST_INSERT_ID()"); | |
187 | |
188 statement.Execute(); | |
189 | |
190 return ReadInteger64(statement, 0); | |
191 } | |
192 } | |
193 | |
194 | |
195 void MySQLIndex::DeleteResource(int64_t id) | |
196 { | |
197 ClearDeletedFiles(); | |
198 | |
199 // Recursive exploration of resources to be deleted, from the "id" | |
200 // resource to the top of the tree of resources | |
201 | |
202 bool done = false; | |
203 | |
204 while (!done) | |
205 { | |
206 int64_t parentId; | |
207 | |
208 { | |
209 DatabaseManager::CachedStatement lookupSiblings( | |
210 STATEMENT_FROM_HERE, GetManager(), | |
211 "SELECT parentId FROM Resources " | |
212 "WHERE parentId = (SELECT parentId FROM Resources WHERE internalId=${id});"); | |
213 | |
214 lookupSiblings.SetParameterType("id", ValueType_Integer64); | |
215 | |
216 Dictionary args; | |
217 args.SetIntegerValue("id", id); | |
218 | |
219 lookupSiblings.Execute(args); | |
220 | |
221 if (lookupSiblings.IsDone()) | |
222 { | |
223 // "id" is a root node | |
224 done = true; | |
225 } | |
226 else | |
227 { | |
228 parentId = ReadInteger64(lookupSiblings, 0); | |
229 lookupSiblings.Next(); | |
230 | |
231 if (lookupSiblings.IsDone()) | |
232 { | |
233 // "id" has no sibling node, recursively remove | |
234 done = false; | |
235 id = parentId; | |
236 } | |
237 else | |
238 { | |
239 // "id" has at least one sibling node: the parent node is the remaining ancestor | |
240 done = true; | |
241 | |
242 DatabaseManager::CachedStatement parent( | |
243 STATEMENT_FROM_HERE, GetManager(), | |
244 "SELECT publicId, resourceType FROM Resources WHERE internalId=${id};"); | |
245 | |
246 parent.SetParameterType("id", ValueType_Integer64); | |
247 | |
248 Dictionary args; | |
249 args.SetIntegerValue("id", parentId); | |
250 | |
251 parent.Execute(args); | |
252 | |
253 GetOutput().SignalRemainingAncestor( | |
254 ReadString(parent, 0), | |
255 static_cast<OrthancPluginResourceType>(ReadInteger32(parent, 1))); | |
256 } | |
257 } | |
258 } | |
259 } | |
260 | |
261 { | |
262 DatabaseManager::CachedStatement deleteHierarchy( | |
263 STATEMENT_FROM_HERE, GetManager(), | |
264 "DELETE FROM Resources WHERE internalId IN (SELECT * FROM (SELECT internalId FROM Resources WHERE internalId=${id} OR parentId=${id} OR parentId IN (SELECT internalId FROM Resources WHERE parentId=${id}) OR parentId IN (SELECT internalId FROM Resources WHERE parentId IN (SELECT internalId FROM Resources WHERE parentId=${id}))) as t);"); | |
265 | |
266 deleteHierarchy.SetParameterType("id", ValueType_Integer64); | |
267 | |
268 Dictionary args; | |
269 args.SetIntegerValue("id", id); | |
270 | |
271 deleteHierarchy.Execute(args); | |
272 } | |
273 | |
274 SignalDeletedFiles(); | |
275 } | |
87
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
276 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
277 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
278 int64_t MySQLIndex::GetLastChangeIndex() |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
279 { |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
280 DatabaseManager::CachedStatement statement( |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
281 STATEMENT_FROM_HERE, GetManager(), |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
282 "SELECT value FROM GlobalIntegers WHERE property = 0"); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
283 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
284 statement.SetReadOnly(true); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
285 statement.Execute(); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
286 |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
287 return ReadInteger64(statement, 0); |
48d445f756db
new extension implemented for MySQL: GetLastChangeIndex
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
84
diff
changeset
|
288 } |
0 | 289 } |