changeset 87:48d445f756db db-changes

new extension implemented for MySQL: GetLastChangeIndex
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 10 Jan 2019 20:39:36 +0100
parents d16e94157efe
children eb08ec14fb04
files MySQL/CMakeLists.txt MySQL/NEWS MySQL/Plugins/GetLastChangeIndex.sql MySQL/Plugins/MySQLIndex.cpp MySQL/Plugins/MySQLIndex.h
diffstat 5 files changed, 47 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MySQL/CMakeLists.txt	Thu Jan 10 18:04:27 2019 +0100
+++ b/MySQL/CMakeLists.txt	Thu Jan 10 20:39:36 2019 +0100
@@ -52,7 +52,8 @@
 
 
 EmbedResources(
-  MYSQL_PREPARE_INDEX ${CMAKE_SOURCE_DIR}/Plugins/PrepareIndex.sql
+  MYSQL_PREPARE_INDEX          ${CMAKE_SOURCE_DIR}/Plugins/PrepareIndex.sql
+  MYSQL_GET_LAST_CHANGE_INDEX  ${CMAKE_SOURCE_DIR}/Plugins/GetLastChangeIndex.sql
   )
 
 add_library(OrthancMySQLIndex SHARED
--- a/MySQL/NEWS	Thu Jan 10 18:04:27 2019 +0100
+++ b/MySQL/NEWS	Thu Jan 10 20:39:36 2019 +0100
@@ -1,6 +1,7 @@
 Pending changes in the mainline
 ===============================
 
+* Database optimizations by implementing new primitives of Orthanc SDK 1.5.2
 * Characters "$" and "_" are allowed in MySQL database identifiers
 * Fix serialization of jobs if many of them
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MySQL/Plugins/GetLastChangeIndex.sql	Thu Jan 10 20:39:36 2019 +0100
@@ -0,0 +1,16 @@
+CREATE TABLE GlobalIntegers(
+       property INTEGER PRIMARY KEY,
+       value BIGINT
+       );
+
+
+INSERT INTO GlobalIntegers
+SELECT 0, COALESCE(MAX(seq), 0) FROM Changes;
+
+
+CREATE TRIGGER ChangeAdded
+AFTER INSERT ON Changes
+FOR EACH ROW
+BEGIN
+  UPDATE GlobalIntegers SET value = new.seq WHERE property = 0@
+END;
--- a/MySQL/Plugins/MySQLIndex.cpp	Thu Jan 10 18:04:27 2019 +0100
+++ b/MySQL/Plugins/MySQLIndex.cpp	Thu Jan 10 20:39:36 2019 +0100
@@ -128,7 +128,19 @@
         SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision);
       }
 
-      if (revision != 2)
+      if (revision == 2)
+      {
+        std::string query;
+
+        Orthanc::EmbeddedResources::GetFileResource
+          (query, Orthanc::EmbeddedResources::MYSQL_GET_LAST_CHANGE_INDEX);
+        db->Execute(query, true);
+        
+        revision = 3;
+        SetGlobalIntegerProperty(*db, t, Orthanc::GlobalProperty_DatabasePatchLevel, revision);
+      }
+
+      if (revision != 3)
       {
         LOG(ERROR) << "MySQL plugin is incompatible with database schema revision: " << revision;
         throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);        
@@ -261,4 +273,17 @@
 
     SignalDeletedFiles();
   }
+
+  
+  int64_t MySQLIndex::GetLastChangeIndex()
+  {
+    DatabaseManager::CachedStatement statement(
+      STATEMENT_FROM_HERE, GetManager(),
+      "SELECT value FROM GlobalIntegers WHERE property = 0");
+    
+    statement.SetReadOnly(true);
+    statement.Execute();
+
+    return ReadInteger64(statement, 0);
+  }
 }
--- a/MySQL/Plugins/MySQLIndex.h	Thu Jan 10 18:04:27 2019 +0100
+++ b/MySQL/Plugins/MySQLIndex.h	Thu Jan 10 20:39:36 2019 +0100
@@ -74,5 +74,7 @@
                                    OrthancPluginResourceType type);
 
     virtual void DeleteResource(int64_t id);
+
+    virtual int64_t GetLastChangeIndex();
   };
 }