changeset 200:9c58b2b03cf0

refactoring of read operations
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Nov 2012 16:49:22 +0100
parents dfa2899d9960
children bee20e978835
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 4 files changed, 85 insertions(+), 248 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Nov 27 16:20:22 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Tue Nov 27 16:49:22 2012 +0100
@@ -352,6 +352,30 @@
     }
   }
 
+
+  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::AttachFile(int64_t id,
                                    AttachedFileType contentType,
                                    const std::string& fileUuid,
--- a/OrthancServer/DatabaseWrapper.h	Tue Nov 27 16:20:22 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Tue Nov 27 16:49:22 2012 +0100
@@ -103,6 +103,10 @@
                             MetadataType type,
                             const std::string& defaultValue = "");
 
+    bool GetMetadataAsInteger(int& result,
+                              int64_t id,
+                              MetadataType type);
+
     void AttachFile(int64_t id,
                     AttachedFileType contentType,
                     const std::string& fileUuid,
@@ -125,6 +129,16 @@
                     uint64_t& uncompressedSize,
                     CompressionType& compressionType);
 
+    bool LookupFile(int64_t id,
+                    AttachedFileType contentType,
+                    std::string& fileUuid,
+                    uint64_t& uncompressedSize)
+    {
+      uint64_t compressedSize;
+      CompressionType compressionType;
+      return LookupFile(id, contentType, fileUuid, compressedSize, uncompressedSize, compressionType);
+    }
+
     void SetMainDicomTags(int64_t id,
                           const DicomMap& tags);
 
--- a/OrthancServer/ServerIndex.cpp	Tue Nov 27 16:20:22 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Tue Nov 27 16:49:22 2012 +0100
@@ -176,46 +176,6 @@
     }
   }
 
-  bool ServerIndex::GetMainDicomStringTag(std::string& result,
-                                          const std::string& uuid,
-                                          const DicomTag& tag)
-  {
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
-                        "SELECT * FROM MainDicomTags WHERE uuid=? AND tagGroup=? AND tagElement=?");
-    s.BindString(0, uuid);
-    s.BindInt(1, tag.GetGroup());
-    s.BindInt(2, tag.GetElement());
-    if (!s.Step())
-    {
-      return false;
-    }
-
-    result = s.ColumnString(0);
-    return true;
-  }
-
-  bool ServerIndex::GetMainDicomIntTag(int& result,
-                                       const std::string& uuid,
-                                       const DicomTag& tag)
-  {
-    std::string s;
-    if (!GetMainDicomStringTag(s, uuid, tag))
-    {
-      return false;
-    }
-
-    try
-    {
-      result = boost::lexical_cast<int>(s);
-      return true;
-    }
-    catch (boost::bad_lexical_cast)
-    {
-      return false;
-    }
-  }
-
-
   bool ServerIndex::HasInstance(DicomInstanceHasher& hasher)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE dicomInstance=?");
@@ -304,9 +264,9 @@
 
     const DicomValue* expectedNumberOfInstances;
     if (//(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_FRAMES)) != NULL ||
-        (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
-        //(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL ||
-        (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL)
+      (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
+      //(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL ||
+      (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL)
     {
       s.BindInt(3, boost::lexical_cast<unsigned int>(expectedNumberOfInstances->AsString()));
     }
@@ -384,31 +344,6 @@
   }
 
 
-  void ServerIndex::GetMainDicomTags(DicomMap& map,
-                                     const std::string& uuid)
-  {
-    map.Clear();
-
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM MainDicomTags WHERE uuid=?");
-    s.BindString(0, uuid);
-    while (s.Step())
-    {
-      map.SetValue(s.ColumnInt(1),
-                   s.ColumnInt(2),
-                   s.ColumnString(3));
-    }
-  }
-
-  void ServerIndex::MainDicomTagsToJson(Json::Value& target,
-                                        const std::string& uuid)
-  {
-    DicomMap map;
-    GetMainDicomTags(map, uuid);
-    target["MainDicomTags"] = Json::objectValue;
-    FromDcmtkBridge::ToJson(target["MainDicomTags"], map);
-  }
-
-
   bool ServerIndex::DeleteInternal(Json::Value& target,
                                    const std::string& uuid,
                                    const std::string& tableName)
@@ -606,7 +541,8 @@
       if (isNewSeries)
       {
         if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
-            (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL)
+            (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGES_IN_ACQUISITION)) != NULL ||
+            (value = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL)
         {
           db2_->SetMetadata(series, MetadataType_Series_ExpectedNumberOfInstances, value->AsString());
         }
@@ -737,56 +673,6 @@
   }
 
 
-
-
-  SeriesStatus ServerIndex::GetSeriesStatus(const std::string& seriesUuid)
-  {
-    SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT expectedNumberOfInstances FROM Series WHERE uuid=?");
-    s1.BindString(0, seriesUuid);
-    if (!s1.Step() || s1.ColumnIsNull(0))
-    {
-      return SeriesStatus_Unknown;
-    }
-
-    int numberOfInstances = s1.ColumnInt(0);
-    if (numberOfInstances < 0)
-    {
-      return SeriesStatus_Unknown;
-    }
-
-    std::set<int> instances;
-    SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT indexInSeries FROM Instances WHERE parentSeries=?");
-    s2.BindString(0, seriesUuid);
-    while (s2.Step())
-    {
-      int index = s2.ColumnInt(0);
-      if (index <= 0 || index > numberOfInstances)
-      {
-        // Out-of-range instance index
-        return SeriesStatus_Inconsistent;
-      }
-
-      if (instances.find(index) != instances.end())
-      {
-        // Twice the same instance index
-        return SeriesStatus_Inconsistent;
-      }
-
-      instances.insert(index);
-    }
-
-    for (int i = 1; i <= numberOfInstances; i++)
-    {
-      if (instances.find(i) == instances.end())
-      {
-        return SeriesStatus_Missing;
-      }
-    }
-
-    return SeriesStatus_Complete;
-  }
-
-
   SeriesStatus ServerIndex::GetSeriesStatus(int id)
   {
     // Get the expected number of instances in this series (from the metadata)
@@ -958,21 +844,37 @@
       result["Type"] = "Series";
       result["Status"] = ToString(GetSeriesStatus(id));
 
-      std::string n = db2_->GetMetadata(id, MetadataType_Series_ExpectedNumberOfInstances);
-      if (n.size() > 0)
-      {
-        result["ExpectedNumberOfInstances"] = n;
-      }
-      else
-      {
+      int i;
+      if (db2_->GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances))
+        result["ExpectedNumberOfInstances"] = i;
+        else
         result["ExpectedNumberOfInstances"] = Json::nullValue;
-      }
+
       break;
     }
 
     case ResourceType_Instance:
+    {
       result["Type"] = "Instance";
+
+      std::string fileUuid;
+      uint64_t uncompressedSize;
+      if (!db2_->LookupFile(id, AttachedFileType_Dicom, fileUuid, uncompressedSize))
+      {
+        throw OrthancException(ErrorCode_InternalError);
+      }
+
+      result["FileSize"] = static_cast<unsigned int>(uncompressedSize);
+      result["FileUuid"] = fileUuid;
+
+      int i;
+      if (db2_->GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries))
+        result["IndexInSeries"] = i;
+      else
+        result["IndexInSeries"] = Json::nullValue;
+
       break;
+    }
 
     default:
       throw OrthancException(ErrorCode_InternalError);
@@ -986,101 +888,6 @@
   }
 
 
-  bool ServerIndex::GetInstance(Json::Value& result,
-                                const std::string& instanceUuid)
-  {
-    assert(result.type() == Json::objectValue);
-    boost::mutex::scoped_lock scoped_lock(mutex_);
-
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT parentSeries, dicomInstance, fileSize, fileUuid, indexInSeries FROM Instances WHERE uuid=?");
-    s.BindString(0, instanceUuid);
-    if (!s.Step())
-    {
-      return false;
-    }
-    else
-    {
-      result["ID"] = instanceUuid;
-      result["ParentSeries"] = s.ColumnString(0);
-      result["FileSize"] = s.ColumnInt(2);   // TODO switch to 64bit with JsonCpp 0.6?
-      result["FileUuid"] = s.ColumnString(3);
-      MainDicomTagsToJson(result, instanceUuid);
-      
-      if (s.ColumnIsNull(4))
-      {
-        result["IndexInSeries"] = Json::nullValue;
-      }
-      else
-      {
-        result["IndexInSeries"] = s.ColumnInt(4);
-      }
-
-      result["Type"] = "Instance";
-
-      return true;
-    }
-  }
-
-
-  bool ServerIndex::GetSeries(Json::Value& result,
-                              const std::string& seriesUuid)
-  {
-    return LookupResource(result, seriesUuid, ResourceType_Series);
-
-    assert(result.type() == Json::objectValue);
-    boost::mutex::scoped_lock scoped_lock(mutex_);
-
-    SQLite::Statement s1(db_, SQLITE_FROM_HERE, "SELECT parentStudy, dicomSeries, expectedNumberOfInstances FROM Series WHERE uuid=?");
-    s1.BindString(0, seriesUuid);
-    if (!s1.Step())
-    {
-      return false;
-    }
-
-    result["ID"] = seriesUuid;
-    result["ParentStudy"] = s1.ColumnString(0);
-    MainDicomTagsToJson(result, seriesUuid);
-
-    Json::Value instances(Json::arrayValue);
-    SQLite::Statement s2(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE parentSeries=?");
-    s2.BindString(0, seriesUuid);
-    while (s2.Step())
-    {
-      instances.append(s2.ColumnString(0));
-    }
-      
-    result["Instances"] = instances;
-
-    if (s1.ColumnIsNull(2))
-    {
-      result["ExpectedNumberOfInstances"] = Json::nullValue;
-    }
-    else
-    {
-      result["ExpectedNumberOfInstances"] = s1.ColumnInt(2);
-    }
-
-    result["Status"] = ToString(GetSeriesStatus(seriesUuid));
-    result["Type"] = "Series";
-
-    return true;
-  }
-
-
-  bool ServerIndex::GetStudy(Json::Value& result,
-                             const std::string& studyUuid)
-  {
-    return LookupResource(result, studyUuid, ResourceType_Study);
-  }
-
-
-  bool ServerIndex::GetPatient(Json::Value& result,
-                               const std::string& patientUuid)
-  {
-    return LookupResource(result, patientUuid, ResourceType_Patient);
-  }
-
-
   bool ServerIndex::GetFile(std::string& fileUuid,
                             CompressionType& compressionType,
                             const std::string& instanceUuid,
--- a/OrthancServer/ServerIndex.h	Tue Nov 27 16:20:22 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Tue Nov 27 16:49:22 2012 +0100
@@ -65,15 +65,6 @@
     void StoreMainDicomTags(const std::string& uuid,
                             const DicomMap& map);
 
-    bool GetMainDicomStringTag(std::string& result,
-                               const std::string& uuid,
-                               const DicomTag& tag);
-
-    bool GetMainDicomIntTag(int& result,
-                            const std::string& uuid,
-                            const DicomTag& tag);
-
-
     bool HasPatient(DicomInstanceHasher& hasher);
 
     void CreatePatient(DicomInstanceHasher& hasher,
@@ -103,12 +94,6 @@
 
     void RemoveInstance(const std::string& uuid);
 
-    void GetMainDicomTags(DicomMap& map,
-                          const std::string& uuid);
-
-    void MainDicomTagsToJson(Json::Value& target,
-                             const std::string& uuid);
-
     void MainDicomTagsToJson2(Json::Value& result,
                               int64_t resourceId);
 
@@ -128,8 +113,6 @@
 
     SeriesStatus GetSeriesStatus(int id);
 
-    SeriesStatus GetSeriesStatus(const std::string& seriesUuid);
-
   public:
     ServerIndex(const std::string& storagePath);
 
@@ -150,18 +133,31 @@
 
     uint64_t GetTotalUncompressedSize();
 
-
     bool GetInstance(Json::Value& result,
-                     const std::string& instanceUuid);
+                     const std::string& instanceUuid)
+    {
+      return LookupResource(result, instanceUuid, ResourceType_Instance);
+    }
 
     bool GetSeries(Json::Value& result,
-                   const std::string& seriesUuid);
+                   const std::string& seriesUuid)
+    {
+      return LookupResource(result, seriesUuid, ResourceType_Series);
+    }
+
 
     bool GetStudy(Json::Value& result,
-                  const std::string& studyUuid);
+                  const std::string& studyUuid)
+    {
+      return LookupResource(result, studyUuid, ResourceType_Study);
+    }
+
 
     bool GetPatient(Json::Value& result,
-                    const std::string& patientUuid);
+                    const std::string& patientUuid)
+    {
+      return LookupResource(result, patientUuid, ResourceType_Patient);
+    }
 
     bool GetFile(std::string& fileUuid,
                  CompressionType& compressionType,
@@ -199,9 +195,5 @@
                     int64_t since,
                     const std::string& filter,
                     unsigned int maxResults);
-
-    /*bool GetAllInstances(std::list<std::string>& instancesUuid,
-                         const std::string& uuid,
-                         bool clear = true);*/
   };
 }