comparison PostgreSQL/Plugins/PostgreSQLIndex.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 41543239072d
comparison
equal deleted inserted replaced
-1:000000000000 0:7cea966b6829
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
69 if (clearAll_)
70 {
71 db->ClearAll();
72 }
73
74 {
75 PostgreSQLTransaction t(*db);
76
77 if (!db->DoesTableExist("Resources"))
78 {
79 std::string query;
80
81 Orthanc::EmbeddedResources::GetFileResource
82 (query, Orthanc::EmbeddedResources::POSTGRESQL_PREPARE_INDEX);
83 db->Execute(query);
84
85 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion, expectedVersion);
86 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, 1);
87 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasTrigramIndex, 0);
88 }
89
90 t.Commit();
91 }
92
93 {
94 PostgreSQLTransaction t(*db);
95
96 if (!db->DoesTableExist("Resources"))
97 {
98 LOG(ERROR) << "Corrupted PostgreSQL database";
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
100 }
101
102 int version = 0;
103 if (!LookupGlobalIntegerProperty(version, *db, t, Orthanc::GlobalProperty_DatabaseSchemaVersion) ||
104 version != 6)
105 {
106 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema version: " << version;
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
108 }
109
110 int revision;
111 if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel))
112 {
113 revision = 1;
114 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision);
115 }
116
117 int hasTrigram = 0;
118 if (!LookupGlobalIntegerProperty(hasTrigram, *db, t, Orthanc::GlobalProperty_HasTrigramIndex) ||
119 hasTrigram != 1)
120 {
121 // Apply fix for performance issue (speed up wildcard search by using GIN trigrams)
122 // https://www.postgresql.org/docs/current/static/pgtrgm.html
123 try
124 {
125 LOG(INFO) << "Trying to enable trigram matching on the PostgreSQL database to speed up wildcard searches";
126 db->Execute(
127 "CREATE EXTENSION pg_trgm; "
128 "CREATE INDEX DicomIdentifiersIndexValues_new ON DicomIdentifiers USING gin(value gin_trgm_ops); "
129 "DROP INDEX DicomIdentifiersIndexValues; "
130 "ALTER INDEX DicomIdentifiersIndexValues_new RENAME TO DicomIdentifiersIndexValues;");
131
132 SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_HasTrigramIndex, 1);
133 }
134 catch (Orthanc::OrthancException&)
135 {
136 LOG(WARNING) << "Performance warning: Your PostgreSQL server does not support trigram matching";
137 LOG(WARNING) << "-> Consider installing the \"pg_trgm\" extension on the PostgreSQL server, e.g. on Debian: sudo apt install postgresql-contrib";
138 }
139 }
140
141 if (revision != 1)
142 {
143 LOG(ERROR) << "PostgreSQL plugin is incompatible with database schema revision: " << revision;
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
145 }
146
147 t.Commit();
148 }
149
150 return db.release();
151 }
152
153
154 PostgreSQLIndex::PostgreSQLIndex(const PostgreSQLParameters& parameters) :
155 IndexBackend(new Factory(*this)),
156 context_(NULL),
157 parameters_(parameters),
158 clearAll_(false)
159 {
160 }
161
162
163 int64_t PostgreSQLIndex::CreateResource(const char* publicId,
164 OrthancPluginResourceType type)
165 {
166 DatabaseManager::CachedStatement statement(
167 STATEMENT_FROM_HERE, GetManager(),
168 "INSERT INTO Resources VALUES(${}, ${type}, ${id}, NULL) RETURNING internalId");
169
170 statement.SetParameterType("id", ValueType_Utf8String);
171 statement.SetParameterType("type", ValueType_Integer64);
172
173 Dictionary args;
174 args.SetUtf8Value("id", publicId);
175 args.SetIntegerValue("type", static_cast<int>(type));
176
177 statement.Execute(args);
178
179 return ReadInteger64(statement, 0);
180 }
181 }