Mercurial > hg > orthanc-databases
annotate PostgreSQL/Plugins/PostgreSQLIndex.cpp @ 13:927264a0c137
preserve the original index
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Jul 2018 18:42:34 +0200 |
parents | 41543239072d |
children | dfc7002add9c |
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 | |
116 int hasTrigram = 0; | |
117 if (!LookupGlobalIntegerProperty(hasTrigram, *db, t, Orthanc::GlobalProperty_HasTrigramIndex) || | |
118 hasTrigram != 1) | |
119 { | |
13
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
120 /** |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
121 * Apply fix for performance issue (speed up wildcard search |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
122 * by using GIN trigrams). This implements the patch suggested |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
123 * in issue #47, BUT we also keep the original |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
124 * "DicomIdentifiersIndexValues", as it leads to better |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
125 * performance for "strict" searches (i.e. searches involving |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
126 * no wildcard). |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
127 * https://www.postgresql.org/docs/current/static/pgtrgm.html |
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
128 * 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
|
129 **/ |
0 | 130 try |
131 { | |
132 LOG(INFO) << "Trying to enable trigram matching on the PostgreSQL database to speed up wildcard searches"; | |
133 db->Execute( | |
134 "CREATE EXTENSION pg_trgm; " | |
13
927264a0c137
preserve the original index
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
12
diff
changeset
|
135 "CREATE INDEX DicomIdentifiersIndexValues2 ON DicomIdentifiers USING gin(value gin_trgm_ops);"); |
0 | 136 |
137 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasTrigramIndex, 1); | |
138 } | |
139 catch (Orthanc::OrthancException&) | |
140 { | |
141 LOG(WARNING) << "Performance warning: Your PostgreSQL server does not support trigram matching"; | |
142 LOG(WARNING) << "-> Consider installing the \"pg_trgm\" extension on the PostgreSQL server, e.g. on Debian: sudo apt install postgresql-contrib"; | |
143 } | |
144 } | |
145 | |
146 if (revision != 1) | |
147 { | |
148 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema revision: " << revision; | |
149 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
150 } | |
151 | |
152 t.Commit(); | |
153 } | |
154 | |
155 return db.release(); | |
156 } | |
157 | |
158 | |
159 PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) : | |
160 IndexBackend(new Factory(*this)), | |
161 context_(NULL), | |
162 parameters_(parameters), | |
163 clearAll_(false) | |
164 { | |
165 } | |
166 | |
167 | |
168 int64_t PostgreSQLIndex::CreateResource(const char* publicId, | |
169 OrthancPluginResourceType type) | |
170 { | |
171 DatabaseManager::CachedStatement statement( | |
172 STATEMENT_FROM_HERE, GetManager(), | |
173 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL) RETURNING internalId"); | |
174 | |
175 statement.SetParameterType("id", ValueType_Utf8String); | |
176 statement.SetParameterType("type", ValueType_Integer64); | |
177 | |
178 Dictionary args; | |
179 args.SetUtf8Value("id", publicId); | |
180 args.SetIntegerValue("type", static_cast<int>(type)); | |
181 | |
182 statement.Execute(args); | |
183 | |
184 return ReadInteger64(statement, 0); | |
185 } | |
186 } |