comparison PostgreSQL/Plugins/PostgreSQLIndex.cpp @ 576:b0533b703c2c attach-custom-data

merged find-refactoring -> attach-custom-data
author Alain Mazy <am@orthanc.team>
date Tue, 01 Oct 2024 16:02:48 +0200
parents 7453fc5bef1a 58fce5aebb31
children
comparison
equal deleted inserted replaced
571:7453fc5bef1a 576:b0533b703c2c
41 // Some aliases for internal properties 41 // Some aliases for internal properties
42 static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0; 42 static const GlobalProperty GlobalProperty_HasTrigramIndex = GlobalProperty_DatabaseInternal0;
43 static const GlobalProperty GlobalProperty_HasCreateInstance = GlobalProperty_DatabaseInternal1; 43 static const GlobalProperty GlobalProperty_HasCreateInstance = GlobalProperty_DatabaseInternal1;
44 static const GlobalProperty GlobalProperty_HasFastCountResources = GlobalProperty_DatabaseInternal2; 44 static const GlobalProperty GlobalProperty_HasFastCountResources = GlobalProperty_DatabaseInternal2;
45 static const GlobalProperty GlobalProperty_GetLastChangeIndex = GlobalProperty_DatabaseInternal3; 45 static const GlobalProperty GlobalProperty_GetLastChangeIndex = GlobalProperty_DatabaseInternal3;
46 static const GlobalProperty GlobalProperty_HasComputeStatisticsReadOnly = GlobalProperty_DatabaseInternal4;
46 } 47 }
47 48
48 49
49 namespace OrthancDatabases 50 namespace OrthancDatabases
50 { 51 {
170 << "to speed up wildcard searches. This may take several minutes"; 171 << "to speed up wildcard searches. This may take several minutes";
171 needToRunUpgradeV1toV2 = true; 172 needToRunUpgradeV1toV2 = true;
172 } 173 }
173 174
174 int property = 0; 175 int property = 0;
175 if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER, 176 // these extensions are not installed anymore from v6.0 of the plugin (but the plugin is fast to compute the size and count the resources)
176 Orthanc::GlobalProperty_HasFastCountResources) || 177 // if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER,
177 property != 1) 178 // Orthanc::GlobalProperty_HasFastCountResources) ||
178 { 179 // property != 1)
179 needToRunUpgradeV1toV2 = true; 180 // {
180 } 181 // needToRunUpgradeV1toV2 = true;
181 if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER, 182 // }
182 Orthanc::GlobalProperty_GetTotalSizeIsFast) || 183 // if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER,
183 property != 1) 184 // Orthanc::GlobalProperty_GetTotalSizeIsFast) ||
184 { 185 // property != 1)
185 needToRunUpgradeV1toV2 = true; 186 // {
186 } 187 // needToRunUpgradeV1toV2 = true;
188 // }
187 if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER, 189 if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER,
188 Orthanc::GlobalProperty_GetLastChangeIndex) || 190 Orthanc::GlobalProperty_GetLastChangeIndex) ||
189 property != 1) 191 property != 1)
190 { 192 {
191 needToRunUpgradeV1toV2 = true; 193 needToRunUpgradeV1toV2 = true;
224 Orthanc::EmbeddedResources::GetFileResource 226 Orthanc::EmbeddedResources::GetFileResource
225 (query, Orthanc::EmbeddedResources::POSTGRESQL_UPGRADE_REV2_TO_REV3); 227 (query, Orthanc::EmbeddedResources::POSTGRESQL_UPGRADE_REV2_TO_REV3);
226 t.GetDatabaseTransaction().ExecuteMultiLines(query); 228 t.GetDatabaseTransaction().ExecuteMultiLines(query);
227 229
228 // apply all idempotent changes that are in the PrepareIndex (update triggers + set Patch level to 3) 230 // apply all idempotent changes that are in the PrepareIndex (update triggers + set Patch level to 3)
231 ApplyPrepareIndex(t, manager);
232 }
233
234 if (!LookupGlobalIntegerProperty(property, manager, MISSING_SERVER_IDENTIFIER,
235 Orthanc::GlobalProperty_HasComputeStatisticsReadOnly) ||
236 property != 1)
237 {
238 // apply all idempotent changes that are in the PrepareIndex. In this case, we are just interested by
239 // ComputeStatisticsReadOnly() that does not need to be uninstalled in case of downgrade.
229 ApplyPrepareIndex(t, manager); 240 ApplyPrepareIndex(t, manager);
230 } 241 }
231 } 242 }
232 243
233 t.Commit(); 244 t.Commit();
257 } 268 }
258 269
259 270
260 uint64_t PostgreSQLIndex::GetTotalCompressedSize(DatabaseManager& manager) 271 uint64_t PostgreSQLIndex::GetTotalCompressedSize(DatabaseManager& manager)
261 { 272 {
262 // Fast version if extension "./FastTotalSize.sql" is installed
263 uint64_t result; 273 uint64_t result;
264 274
265 { 275 {
266 DatabaseManager::CachedStatement statement( 276 DatabaseManager::CachedStatement statement(
267 STATEMENT_FROM_HERE, manager, 277 STATEMENT_FROM_HERE, manager,
268 "SELECT * FROM UpdateSingleStatistic(0)"); 278 "SELECT * FROM ComputeStatisticsReadOnly(0)");
269 279
270 statement.Execute(); 280 statement.Execute();
271 281
272 result = static_cast<uint64_t>(statement.ReadInteger64(0)); 282 if (statement.IsNull(0))
283 {
284 return 0;
285 }
286 else
287 {
288 result = static_cast<uint64_t>(statement.ReadInteger64(0));
289 }
273 } 290 }
274 291
275 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added 292 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added
276 //assert(result == IndexBackend::GetTotalCompressedSize(manager)); 293 //assert(result == IndexBackend::GetTotalCompressedSize(manager));
277 return result; 294 return result;
278 } 295 }
279 296
280 297
281 uint64_t PostgreSQLIndex::GetTotalUncompressedSize(DatabaseManager& manager) 298 uint64_t PostgreSQLIndex::GetTotalUncompressedSize(DatabaseManager& manager)
282 { 299 {
283 // Fast version if extension "./FastTotalSize.sql" is installed
284 uint64_t result; 300 uint64_t result;
285 301
286 { 302 {
287 DatabaseManager::CachedStatement statement( 303 DatabaseManager::CachedStatement statement(
288 STATEMENT_FROM_HERE, manager, 304 STATEMENT_FROM_HERE, manager,
289 "SELECT * FROM UpdateSingleStatistic(1)"); 305 "SELECT * FROM ComputeStatisticsReadOnly(1)");
290 306
291 statement.Execute(); 307 statement.Execute();
292 308
293 result = static_cast<uint64_t>(statement.ReadInteger64(0)); 309 if (statement.IsNull(0))
310 {
311 return 0;
312 }
313 else
314 {
315 result = static_cast<uint64_t>(statement.ReadInteger64(0));
316 }
294 } 317 }
295 318
296 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added 319 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added
297 // assert(result == IndexBackend::GetTotalUncompressedSize(manager)); 320 // assert(result == IndexBackend::GetTotalUncompressedSize(manager));
298 return result; 321 return result;
656 679
657 680
658 uint64_t PostgreSQLIndex::GetResourcesCount(DatabaseManager& manager, 681 uint64_t PostgreSQLIndex::GetResourcesCount(DatabaseManager& manager,
659 OrthancPluginResourceType resourceType) 682 OrthancPluginResourceType resourceType)
660 { 683 {
661 // Optimized version thanks to the "FastCountResources.sql" extension
662
663 assert(OrthancPluginResourceType_Patient == 0 && 684 assert(OrthancPluginResourceType_Patient == 0 &&
664 OrthancPluginResourceType_Study == 1 && 685 OrthancPluginResourceType_Study == 1 &&
665 OrthancPluginResourceType_Series == 2 && 686 OrthancPluginResourceType_Series == 2 &&
666 OrthancPluginResourceType_Instance == 3); 687 OrthancPluginResourceType_Instance == 3);
667 688
668 uint64_t result; 689 uint64_t result;
669 690
670 { 691 {
671 DatabaseManager::StandaloneStatement statement( 692 DatabaseManager::StandaloneStatement statement(
672 manager, 693 manager,
673 std::string("SELECT * FROM UpdateSingleStatistic(") + boost::lexical_cast<std::string>(resourceType + 2) + ")"); // For an explanation of the "+ 2" below, check out "PrepareIndex.sql" 694 std::string("SELECT * FROM ComputeStatisticsReadOnly(") + boost::lexical_cast<std::string>(resourceType + 2) + ")"); // For an explanation of the "+ 2" below, check out "PrepareIndex.sql"
674 695
675 statement.Execute(); 696 statement.Execute();
676 697
677 result = static_cast<uint64_t>(statement.ReadInteger64(0)); 698 if (statement.IsNull(0))
699 {
700 return 0;
701 }
702 else
703 {
704 result = static_cast<uint64_t>(statement.ReadInteger64(0));
705 }
678 } 706 }
679 707
680 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added 708 // disabled because this is not alway true while transactions are being executed in READ COMITTED TRANSACTION. This is however true when no files are being delete/added
681 assert(result == IndexBackend::GetResourcesCount(manager, resourceType)); 709 // assert(result == IndexBackend::GetResourcesCount(manager, resourceType));
682 710
683 return result; 711 return result;
684 } 712 }
685 713
686 714