changeset 1237:0f3716b88af7

cleaning
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Dec 2014 15:33:16 +0100
parents 21d84e3acc0d
children 6c07108ff1e2
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h UnitTestsSources/ServerIndexTests.cpp
diffstat 5 files changed, 94 insertions(+), 79 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Dec 05 14:53:30 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Fri Dec 05 15:33:16 2014 +0100
@@ -259,7 +259,8 @@
       throw OrthancException(ErrorCode_InternalError);
     }
 
-    LogChange(id, changeType, type, publicId);
+    ServerIndexChange change(changeType, type, publicId);
+    LogChange(id, change);
     return id;
   }
 
@@ -449,28 +450,6 @@
   }
 
 
-  bool DatabaseWrapper::GetMetadataAsInteger(int& result,
-                                             int64_t id,
-                                             MetadataType type)
-  {
-    std::string s = GetMetadata(id, type, "");
-    if (s.size() == 0)
-    {
-      return false;
-    }
-
-    try
-    {
-      result = boost::lexical_cast<int>(s);
-      return true;
-    }
-    catch (boost::bad_lexical_cast&)
-    {
-      return false;
-    }
-  }
-
-
 
   void DatabaseWrapper::AddAttachment(int64_t id,
                                       const FileInfo& attachment)
@@ -1046,33 +1025,6 @@
   }
 
 
-  uint64_t DatabaseWrapper::IncrementGlobalSequence(GlobalProperty property)
-  {
-    std::string oldValue;
-
-    if (LookupGlobalProperty(oldValue, property))
-    {
-      uint64_t oldNumber;
-
-      try
-      {
-        oldNumber = boost::lexical_cast<uint64_t>(oldValue);
-        SetGlobalProperty(property, boost::lexical_cast<std::string>(oldNumber + 1));
-        return oldNumber + 1;
-      }
-      catch (boost::bad_lexical_cast&)
-      {
-        throw OrthancException(ErrorCode_InternalError);
-      }
-    }
-    else
-    {
-      // Initialize the sequence at "1"
-      SetGlobalProperty(property, "1");
-      return 1;
-    }
-  }
-
 
   void DatabaseWrapper::ClearTable(const std::string& tableName)
   {
--- a/OrthancServer/DatabaseWrapper.h	Fri Dec 05 14:53:30 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Fri Dec 05 15:33:16 2014 +0100
@@ -122,10 +122,6 @@
                             MetadataType type,
                             const std::string& defaultValue);
 
-    bool GetMetadataAsInteger(int& result,
-                              int64_t id,
-                              MetadataType type);
-
     void AddAttachment(int64_t id,
                        const FileInfo& attachment);
 
@@ -155,15 +151,6 @@
                                int64_t id);
 
     void LogChange(int64_t internalId,
-                   ChangeType changeType,
-                   ResourceType resourceType,
-                   const std::string& publicId)
-    {
-      ServerIndexChange change(changeType, resourceType, publicId);
-      LogChange(internalId, change);
-    }
-
-    void LogChange(int64_t internalId,
                    const ServerIndexChange& change);
 
     void GetChanges(Json::Value& target,
@@ -229,8 +216,6 @@
       db_.FlushToDisk();
     }
 
-    uint64_t IncrementGlobalSequence(GlobalProperty property);
-
     void ClearTable(const std::string& tableName);
 
     bool IsExistingResource(int64_t internalId);
--- a/OrthancServer/ServerIndex.cpp	Fri Dec 05 14:53:30 2014 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Dec 05 15:33:16 2014 +0100
@@ -418,6 +418,60 @@
   }
 
 
+
+
+  bool ServerIndex::GetMetadataAsInteger(int& result,
+                                         int64_t id,
+                                         MetadataType type)
+  {
+    std::string s = db_->GetMetadata(id, type, "");
+    if (s.size() == 0)
+    {
+      return false;
+    }
+
+    try
+    {
+      result = boost::lexical_cast<int>(s);
+      return true;
+    }
+    catch (boost::bad_lexical_cast&)
+    {
+      return false;
+    }
+  }
+
+
+
+  uint64_t ServerIndex::IncrementGlobalSequenceInternal(GlobalProperty property)
+  {
+    std::string oldValue;
+
+    if (db_->LookupGlobalProperty(oldValue, property))
+    {
+      uint64_t oldNumber;
+
+      try
+      {
+        oldNumber = boost::lexical_cast<uint64_t>(oldValue);
+        db_->SetGlobalProperty(property, boost::lexical_cast<std::string>(oldNumber + 1));
+        return oldNumber + 1;
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+    else
+    {
+      // Initialize the sequence at "1"
+      db_->SetGlobalProperty(property, "1");
+      return 1;
+    }
+  }
+
+
+
   ServerIndex::ServerIndex(ServerContext& context,
                            const std::string& dbPath) : 
     done_(false),
@@ -672,7 +726,7 @@
       SeriesStatus seriesStatus = GetSeriesStatus(series);
       if (seriesStatus == SeriesStatus_Complete)
       {
-        db_->LogChange(series, ChangeType_CompletedSeries, ResourceType_Series, hasher.HashSeries());
+        LogChange(series, ChangeType_CompletedSeries, ResourceType_Series, hasher.HashSeries());
       }
 
       // Mark the parent resources of this instance as unstable
@@ -883,7 +937,7 @@
         result["Status"] = EnumerationToString(GetSeriesStatus(id));
 
         int i;
-        if (db_->GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances))
+        if (GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances))
           result["ExpectedNumberOfInstances"] = i;
         else
           result["ExpectedNumberOfInstances"] = Json::nullValue;
@@ -905,7 +959,7 @@
         result["FileUuid"] = attachment.GetUuid();
 
         int i;
-        if (db_->GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries))
+        if (GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries))
           result["IndexInSeries"] = i;
         else
           result["IndexInSeries"] = Json::nullValue;
@@ -1446,7 +1500,7 @@
     std::auto_ptr<SQLite::ITransaction> transaction(db_->StartTransaction());
 
     transaction->Begin();
-    uint64_t seq = db_->IncrementGlobalSequence(sequence);
+    uint64_t seq = IncrementGlobalSequenceInternal(sequence);
     transaction->Commit();
 
     return seq;
@@ -1468,7 +1522,7 @@
       throw OrthancException(ErrorCode_UnknownResource);
     }
 
-    db_->LogChange(id, changeType, type, publicId);
+    LogChange(id, changeType, type, publicId);
 
     transaction->Commit();
   }
@@ -1668,15 +1722,15 @@
           switch (payload.GetResourceType())
           {
             case ResourceType_Patient:
-              that->db_->LogChange(id, ChangeType_StablePatient, ResourceType_Patient, payload.GetPublicId());
+              that->LogChange(id, ChangeType_StablePatient, ResourceType_Patient, payload.GetPublicId());
               break;
 
             case ResourceType_Study:
-              that->db_->LogChange(id, ChangeType_StableStudy, ResourceType_Study, payload.GetPublicId());
+              that->LogChange(id, ChangeType_StableStudy, ResourceType_Study, payload.GetPublicId());
               break;
 
             case ResourceType_Series:
-              that->db_->LogChange(id, ChangeType_StableSeries, ResourceType_Series, payload.GetPublicId());
+              that->LogChange(id, ChangeType_StableSeries, ResourceType_Series, payload.GetPublicId());
               break;
 
             default:
@@ -1706,7 +1760,7 @@
     unstableResources_.AddOrMakeMostRecent(id, payload);
     //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id;
 
-    db_->LogChange(id, ChangeType_NewChildInstance, type, publicId);
+    LogChange(id, ChangeType_NewChildInstance, type, publicId);
   }
 
 
--- a/OrthancServer/ServerIndex.h	Fri Dec 05 14:53:30 2014 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Dec 05 15:33:16 2014 +0100
@@ -103,6 +103,22 @@
                                /* in  */ int64_t id,
                                /* in  */ ResourceType type);
 
+    bool GetMetadataAsInteger(int& result,
+                              int64_t id,
+                              MetadataType type);
+
+    void LogChange(int64_t internalId,
+                   ChangeType changeType,
+                   ResourceType resourceType,
+                   const std::string& publicId)
+    {
+      ServerIndexChange change(changeType, resourceType, publicId);
+      db_->LogChange(internalId, change);
+    }
+
+    uint64_t IncrementGlobalSequenceInternal(GlobalProperty property);
+
+
   public:
     ServerIndex(ServerContext& context,
                 const std::string& dbPath);
--- a/UnitTestsSources/ServerIndexTests.cpp	Fri Dec 05 14:53:30 2014 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Fri Dec 05 15:33:16 2014 +0100
@@ -540,12 +540,20 @@
 
 
 
-TEST_P(DatabaseWrapperTest, Sequence)
+TEST(ServerIndex, Sequence)
 {
-  ASSERT_EQ(1u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
-  ASSERT_EQ(2u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
-  ASSERT_EQ(3u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
-  ASSERT_EQ(4u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
+  const std::string path = "UnitTestsStorage";
+
+  Toolbox::RemoveFile(path + "/index");
+  FilesystemStorage storage(path);
+  ServerContext context(":memory:");   // The SQLite DB is in memory
+  context.SetStorageArea(storage);
+  ServerIndex& index = context.GetIndex();
+
+  ASSERT_EQ(1u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
+  ASSERT_EQ(2u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
+  ASSERT_EQ(3u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
+  ASSERT_EQ(4u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence));
 }