Mercurial > hg > orthanc-databases
annotate PostgreSQL/Plugins/PostgreSQLIndex.cpp @ 62:eedd082355f9
fix for compatibility with simplified OrthancPluginCppWrapper
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 05 Dec 2018 08:57:16 +0100 |
parents | f4d5c2c70f98 |
children | 714c5d2bee76 |
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 | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU 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 "PostgreSQLIndex.h" | |
23 | |
24 #include "../../Framework/Plugins/GlobalProperties.h" | |
25 #include "../../Framework/PostgreSQL/PostgreSQLDatabase.h" | |
26 #include "../../Framework/PostgreSQL/PostgreSQLTransaction.h" | |
27 | |
28 #include <EmbeddedResources.h> // Auto-generated file | |
29 | |
30 #include <Core/Logging.h> | |
31 #include <Core/OrthancException.h> | |
32 | |
33 | |
34 namespace Orthanc | |
35 { | |
36 // Some aliases for internal properties | |
37 static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0; | |
38 } | |
39 | |
40 | |
41 namespace OrthancDatabases | |
42 { | |
43 IDatabase* PostgreSQLIndex::OpenInternal() | |
44 { | |
45 uint32_t expectedVersion = 6; | |
46 if (context_) | |
47 { | |
48 expectedVersion = OrthancPluginGetExpectedDatabaseVersion(context_); | |
49 } | |
50 else | |
51 { | |
52 // This case only occurs during unit testing | |
53 expectedVersion = 6; | |
54 } | |
55 | |
56 // Check the expected version of the database | |
57 if (expectedVersion != 6) | |
58 { | |
59 LOG(ERROR) << "This database plugin is incompatible with your version of Orthanc " | |
60 << "expecting the DB schema version " << expectedVersion | |
61 << ", but this plugin is only compatible with version 6"; | |
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin); | |
63 } | |
64 | |
65 std::auto_ptr<PostgreSQLDatabase> db(new PostgreSQLDatabase(parameters_)); | |
66 | |
67 db->Open(); | |
68 | |
12
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
69 if (parameters_.HasLock()) |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
70 { |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
71 db->AdvisoryLock(42 /* some arbitrary constant */); |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
72 } |
41543239072d
transactions for storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
73 |
0 | 74 if (clearAll_) |
75 { | |
76 db->ClearAll(); | |
77 } | |
78 | |
79 { | |
80 PostgreSQLTransaction t(*db); | |
81 | |
82 if (!db->DoesTableExist("Resources")) | |
83 { | |
84 std::string query; | |
85 | |
86 Orthanc::EmbeddedResources::GetFileResource | |
87 (query, Orthanc::EmbeddedResources::POSTGRESQL_PREPARE_INDEX); | |
88 db->Execute(query); | |
89 | |
90 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion); | |
91 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1); | |
92 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasTrigramIndex, 0); | |
93 } | |
94 | |
95 if (!db->DoesTableExist("Resources")) | |
96 { | |
97 LOG(ERROR) << "Corrupted PostgreSQL database"; | |
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
99 } | |
100 | |
101 int version = 0; | |
102 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) || | |
103 version != 6) | |
104 { | |
105 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema version: " << version; | |
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
107 } | |
108 | |
109 int revision; | |
110 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel)) | |
111 { | |
112 revision = 1; | |
113 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision); | |
114 } | |
115 | |
56
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
116 if (revision != 1) |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
117 { |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
118 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema revision: " << revision; |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
119 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
120 } |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
121 |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
122 t.Commit(); |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
123 } |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
124 |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
125 { |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
126 PostgreSQLTransaction t(*db); |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
127 |
0 | 128 int hasTrigram = 0; |
129 if (!LookupGlobalIntegerProperty(hasTrigram, *db, t, Orthanc::GlobalProperty_HasTrigramIndex) || | |
130 hasTrigram != 1) | |
131 { | |
13
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
132 /** |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
133 * Apply fix for performance issue (speed up wildcard search |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
134 * by using GIN trigrams). This implements the patch suggested |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
135 * in issue #47, BUT we also keep the original |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
136 * "DicomIdentifiersIndexValues", as it leads to better |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
137 * performance for "strict" searches (i.e. searches involving |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
138 * no wildcard). |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
139 * https://www.postgresql.org/docs/current/static/pgtrgm.html |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
140 * https://bitbucket.org/sjodogne/orthanc/issues/47/index-improvements-for-pg-plugin |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
141 **/ |
0 | 142 try |
143 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
144 // We've observed 9 minutes on DB with 100000 studies |
20 | 145 LOG(WARNING) << "Trying to enable trigram matching on the PostgreSQL database " |
57
f4d5c2c70f98
Suggestion by Karsten Hilbert: CREATE EXTENSION IF NOT EXISTS pg_trgm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
146 << "to speed up wildcard searches. This may take several minutes"; |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
147 |
0 | 148 db->Execute( |
57
f4d5c2c70f98
Suggestion by Karsten Hilbert: CREATE EXTENSION IF NOT EXISTS pg_trgm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
56
diff
changeset
|
149 "CREATE EXTENSION IF NOT EXISTS pg_trgm; " |
13
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
150 "CREATE INDEX DicomIdentifiersIndexValues2 ON DicomIdentifiers USING gin(value gin_trgm_ops);"); |
0 | 151 |
152 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasTrigramIndex, 1); | |
19 | 153 LOG(WARNING) << "Trigram index has been created"; |
56
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
154 |
4f66bc893fc3
Fix Debian issue #906771
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
155 t.Commit(); |
0 | 156 } |
157 catch (Orthanc::OrthancException&) | |
158 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
159 LOG(WARNING) << "Performance warning: Your PostgreSQL server does " |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
160 << "not support trigram matching"; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
161 LOG(WARNING) << "-> Consider installing the \"pg_trgm\" extension on the " |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
15
diff
changeset
|
162 << "PostgreSQL server, e.g. on Debian: sudo apt install postgresql-contrib"; |
0 | 163 } |
164 } | |
165 } | |
166 | |
167 return db.release(); | |
168 } | |
169 | |
170 | |
171 PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) : | |
172 IndexBackend(new Factory(*this)), | |
173 context_(NULL), | |
174 parameters_(parameters), | |
175 clearAll_(false) | |
176 { | |
177 } | |
178 | |
179 | |
180 int64_t PostgreSQLIndex::CreateResource(const char* publicId, | |
181 OrthancPluginResourceType type) | |
182 { | |
183 DatabaseManager::CachedStatement statement( | |
184 STATEMENT_FROM_HERE, GetManager(), | |
185 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL) RETURNING internalId"); | |
186 | |
187 statement.SetParameterType("id", ValueType_Utf8String); | |
188 statement.SetParameterType("type", ValueType_Integer64); | |
189 | |
190 Dictionary args; | |
191 args.SetUtf8Value("id", publicId); | |
192 args.SetIntegerValue("type", static_cast<int>(type)); | |
193 | |
194 statement.Execute(args); | |
195 | |
196 return ReadInteger64(statement, 0); | |
197 } | |
198 } |