Mercurial > hg > orthanc-databases
changeset 72:8dd29af7c844 db-changes
new extension: FastTotalSize
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 Jan 2019 14:43:35 +0100 |
parents | d40c5fecd160 |
children | aa81c1c80c75 |
files | PostgreSQL/CMakeLists.txt PostgreSQL/Plugins/CreateInstance.sql PostgreSQL/Plugins/FastTotalSize.sql PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/PostgreSQLIndex.h |
diffstat | 5 files changed, 104 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/PostgreSQL/CMakeLists.txt Fri Jan 04 13:51:52 2019 +0100 +++ b/PostgreSQL/CMakeLists.txt Fri Jan 04 14:43:35 2019 +0100 @@ -54,6 +54,7 @@ EmbedResources( POSTGRESQL_PREPARE_INDEX ${CMAKE_SOURCE_DIR}/Plugins/PrepareIndex.sql POSTGRESQL_CREATE_INSTANCE ${CMAKE_SOURCE_DIR}/Plugins/CreateInstance.sql + POSTGRESQL_FAST_TOTAL_SIZE ${CMAKE_SOURCE_DIR}/Plugins/FastTotalSize.sql ) add_library(OrthancPostgreSQLIndex SHARED
--- a/PostgreSQL/Plugins/CreateInstance.sql Fri Jan 04 13:51:52 2019 +0100 +++ b/PostgreSQL/Plugins/CreateInstance.sql Fri Jan 04 14:43:35 2019 +0100 @@ -10,7 +10,8 @@ OUT patientKey BIGINT, OUT studyKey BIGINT, OUT seriesKey BIGINT, - OUT instanceKey BIGINT) AS $$ + OUT instanceKey BIGINT) AS $body$ + BEGIN SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3; @@ -63,4 +64,5 @@ isNewInstance := 1; END IF; END; -$$ LANGUAGE plpgsql; + +$body$ LANGUAGE plpgsql;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PostgreSQL/Plugins/FastTotalSize.sql Fri Jan 04 14:43:35 2019 +0100 @@ -0,0 +1,41 @@ +CREATE TABLE GlobalIntegers( + key INTEGER PRIMARY KEY, + value BIGINT); + +INSERT INTO GlobalIntegers +SELECT 0, CAST(COALESCE(SUM(compressedSize), 0) AS BIGINT) FROM AttachedFiles; + +INSERT INTO GlobalIntegers +SELECT 1, CAST(COALESCE(SUM(uncompressedSize), 0) AS BIGINT) FROM AttachedFiles; + + + +CREATE FUNCTION AttachedFileIncrementSizeFunc() +RETURNS TRIGGER AS $body$ +BEGIN + UPDATE GlobalIntegers SET value = value + new.compressedSize WHERE key = 0; + UPDATE GlobalIntegers SET value = value + new.uncompressedSize WHERE key = 1; + RETURN NULL; +END; +$body$ LANGUAGE plpgsql; + +CREATE FUNCTION AttachedFileDecrementSizeFunc() +RETURNS TRIGGER AS $body$ +BEGIN + UPDATE GlobalIntegers SET value = value - old.compressedSize WHERE key = 0; + UPDATE GlobalIntegers SET value = value - old.uncompressedSize WHERE key = 1; + RETURN NULL; +END; +$body$ LANGUAGE plpgsql; + + + +CREATE TRIGGER AttachedFileIncrementSize +AFTER INSERT ON AttachedFiles +FOR EACH ROW +EXECUTE PROCEDURE AttachedFileIncrementSizeFunc(); + +CREATE TRIGGER AttachedFileDecrementSize +AFTER DELETE ON AttachedFiles +FOR EACH ROW +EXECUTE PROCEDURE AttachedFileDecrementSizeFunc();
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp Fri Jan 04 13:51:52 2019 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp Fri Jan 04 14:43:35 2019 +0100 @@ -187,6 +187,27 @@ } } + { + PostgreSQLTransaction t(*db); + + int hasFastTotalSize = 0; + if (!LookupGlobalIntegerProperty(hasFastTotalSize, *db, t, + Orthanc::GlobalProperty_GetTotalSizeIsFast) || + hasFastTotalSize != 1) + { + LOG(INFO) << "Installing the FastTotalSize extension"; + + std::string query; + Orthanc::EmbeddedResources::GetFileResource + (query, Orthanc::EmbeddedResources::POSTGRESQL_FAST_TOTAL_SIZE); + db->Execute(query); + + SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_GetTotalSizeIsFast, 1); + + t.Commit(); + } + } + return db.release(); } @@ -220,6 +241,34 @@ } + uint64_t PostgreSQLIndex::GetTotalCompressedSize() + { + // Fast version if extension "./FastTotalSize.sql" is installed + DatabaseManager::CachedStatement statement( + STATEMENT_FROM_HERE, GetManager(), + "SELECT value FROM GlobalIntegers WHERE key = 0"); + + statement.SetReadOnly(true); + statement.Execute(); + + return static_cast<uint64_t>(ReadInteger64(statement, 0)); + } + + + uint64_t PostgreSQLIndex::GetTotalUncompressedSize() + { + // Fast version if extension "./FastTotalSize.sql" is installed + DatabaseManager::CachedStatement statement( + STATEMENT_FROM_HERE, GetManager(), + "SELECT value FROM GlobalIntegers WHERE key = 1"); + + statement.SetReadOnly(true); + statement.Execute(); + + return static_cast<uint64_t>(ReadInteger64(statement, 0)); + } + + void PostgreSQLIndex::CreateInstance(OrthancPluginCreateInstanceResult& result, const char* hashPatient, const char* hashStudy,
--- a/PostgreSQL/Plugins/PostgreSQLIndex.h Fri Jan 04 13:51:52 2019 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLIndex.h Fri Jan 04 14:43:35 2019 +0100 @@ -71,9 +71,14 @@ } virtual int64_t CreateResource(const char* publicId, - OrthancPluginResourceType type); + OrthancPluginResourceType type) + ORTHANC_OVERRIDE; - virtual bool HasCreateInstance() const + virtual uint64_t GetTotalCompressedSize() ORTHANC_OVERRIDE; + + virtual uint64_t GetTotalUncompressedSize() ORTHANC_OVERRIDE; + + virtual bool HasCreateInstance() const ORTHANC_OVERRIDE { return true; } @@ -83,7 +88,8 @@ const char* hashPatient, const char* hashStudy, const char* hashSeries, - const char* hashInstance); + const char* hashInstance) + ORTHANC_OVERRIDE; #endif }; }