changeset 179:78e28d0098d9

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Nov 2012 11:30:16 +0100
parents 5739b4d10a4b
children 626777d01dc4
files Core/DicomFormat/DicomInstanceHasher.cpp Core/DicomFormat/DicomInstanceHasher.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 4 files changed, 79 insertions(+), 62 deletions(-) [+]
line wrap: on
line diff
--- a/Core/DicomFormat/DicomInstanceHasher.cpp	Fri Nov 09 11:13:29 2012 +0100
+++ b/Core/DicomFormat/DicomInstanceHasher.cpp	Fri Nov 09 11:30:16 2012 +0100
@@ -39,44 +39,56 @@
   DicomInstanceHasher::DicomInstanceHasher(const DicomMap& instance)
   {
     patientId_ = instance.GetValue(DICOM_TAG_PATIENT_ID).AsString();
-    instanceUid_ = instance.GetValue(DICOM_TAG_SOP_INSTANCE_UID).AsString();
+    studyUid_ = instance.GetValue(DICOM_TAG_STUDY_INSTANCE_UID).AsString();
     seriesUid_ = instance.GetValue(DICOM_TAG_SERIES_INSTANCE_UID).AsString();
-    studyUid_ = instance.GetValue(DICOM_TAG_STUDY_INSTANCE_UID).AsString();
+    instanceUid_ = instance.GetValue(DICOM_TAG_SOP_INSTANCE_UID).AsString();
 
     if (patientId_.size() == 0 ||
-        instanceUid_.size() == 0 ||
+        studyUid_.size() == 0 ||
         seriesUid_.size() == 0 ||
-        studyUid_.size() == 0)
+        instanceUid_.size() == 0)
     {
       throw OrthancException(ErrorCode_BadFileFormat);
     }
   }
 
-  std::string DicomInstanceHasher::HashPatient() const
+  const std::string& DicomInstanceHasher::HashPatient()
   {
-    std::string s;
-    Toolbox::ComputeSHA1(s, patientId_);
-    return s;
+    if (patientHash_.size() == 0)
+    {
+      Toolbox::ComputeSHA1(patientHash_, patientId_);
+    }
+
+    return patientHash_;
   }
 
-  std::string DicomInstanceHasher::HashStudy() const
+  const std::string& DicomInstanceHasher::HashStudy()
   {
-    std::string s;
-    Toolbox::ComputeSHA1(s, patientId_ + "|" + studyUid_);
-    return s;
+    if (studyHash_.size() == 0)
+    {
+      Toolbox::ComputeSHA1(studyHash_, patientId_ + "|" + studyUid_);
+    }
+
+    return studyHash_;
   }
 
-  std::string DicomInstanceHasher::HashSeries() const
+  const std::string& DicomInstanceHasher::HashSeries()
   {
-    std::string s;
-    Toolbox::ComputeSHA1(s, patientId_ + "|" + studyUid_ + "|" + seriesUid_);
-    return s;
+    if (seriesHash_.size() == 0)
+    {
+      Toolbox::ComputeSHA1(seriesHash_, patientId_ + "|" + studyUid_ + "|" + seriesUid_);
+    }
+
+    return seriesHash_;
   }
 
-  std::string DicomInstanceHasher::HashInstance() const
+  const std::string& DicomInstanceHasher::HashInstance()
   {
-    std::string s;
-    Toolbox::ComputeSHA1(s, patientId_ + "|" + studyUid_ + "|" + seriesUid_ + "|" + instanceUid_);
-    return s;
+    if (instanceHash_.size() == 0)
+    {
+      Toolbox::ComputeSHA1(instanceHash_, patientId_ + "|" + studyUid_ + "|" + seriesUid_ + "|" + instanceUid_);
+    }
+
+    return instanceHash_;
   }
 }
--- a/Core/DicomFormat/DicomInstanceHasher.h	Fri Nov 09 11:13:29 2012 +0100
+++ b/Core/DicomFormat/DicomInstanceHasher.h	Fri Nov 09 11:30:16 2012 +0100
@@ -50,9 +50,14 @@
   {
   private:
     std::string patientId_;
-    std::string instanceUid_;
+    std::string studyUid_;
     std::string seriesUid_;
-    std::string studyUid_;
+    std::string instanceUid_;
+
+    std::string patientHash_;
+    std::string studyHash_;
+    std::string seriesHash_;
+    std::string instanceHash_;
 
   public:
     DicomInstanceHasher(const DicomMap& instance);
@@ -62,9 +67,9 @@
       return patientId_;
     }
 
-    const std::string& GetInstanceUid() const
+    const std::string& GetStudyUid() const
     {
-      return instanceUid_;
+      return studyUid_;
     }
 
     const std::string& GetSeriesUid() const
@@ -72,17 +77,17 @@
       return seriesUid_;
     }
 
-    const std::string& GetStudyUid() const
+    const std::string& GetInstanceUid() const
     {
-      return studyUid_;
+      return instanceUid_;
     }
 
-    std::string HashPatient() const;
+    const std::string& HashPatient();
 
-    std::string HashStudy() const;
+    const std::string& HashStudy();
 
-    std::string HashSeries() const;
+    const std::string& HashSeries();
 
-    std::string HashInstance() const;
+    const std::string& HashInstance();
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Fri Nov 09 11:13:29 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Nov 09 11:30:16 2012 +0100
@@ -42,7 +42,6 @@
 #include "../Core/Toolbox.h"
 #include "../Core/Uuid.h"
 #include "../Core/DicomFormat/DicomArray.h"
-#include "../Core/DicomFormat/DicomInstanceHasher.h"
 #include "../Core/SQLite/Transaction.h"
 #include "FromDcmtkBridge.h"
 
@@ -207,10 +206,10 @@
   }
 
   bool ServerIndex::HasInstance(std::string& instanceUuid,
-                                const std::string& dicomInstance)
+                                const DicomInstanceHasher& hasher)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE dicomInstance=?");
-    s.BindString(0, dicomInstance);
+    s.BindString(0, hasher.GetInstanceUid());
     if (s.Step())
     {
       instanceUuid = s.ColumnString(0);
@@ -234,7 +233,7 @@
 
 
   std::string ServerIndex::CreateInstance(const std::string& parentSeriesUuid,
-                                          const std::string& dicomInstance,
+                                          const DicomInstanceHasher& hasher,
                                           const DicomMap& dicomSummary,
                                           const std::string& fileUuid,
                                           uint64_t fileSize,
@@ -251,7 +250,7 @@
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Instances VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
     s.BindString(0, instanceUuid);
     s.BindString(1, parentSeriesUuid);
-    s.BindString(2, dicomInstance);
+    s.BindString(2, hasher.GetInstanceUid());
     s.BindString(3, fileUuid);
     s.BindInt64(4, fileSize);
     s.BindString(5, jsonUuid);
@@ -287,10 +286,10 @@
   }
 
   bool ServerIndex::HasSeries(std::string& seriesUuid,
-                              const std::string& dicomSeries)
+                              const DicomInstanceHasher& hasher)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE dicomSeries=?");
-    s.BindString(0, dicomSeries);
+    s.BindString(0, hasher.GetSeriesUid());
     if (s.Step())
     {
       seriesUuid = s.ColumnString(0);
@@ -303,7 +302,7 @@
   }
 
   std::string ServerIndex::CreateSeries(const std::string& parentStudyUuid,
-                                        const std::string& dicomSeries,
+                                        const DicomInstanceHasher& hasher,
                                         const DicomMap& dicomSummary)
   {
     std::string seriesUuid = Toolbox::GenerateUuid();
@@ -316,7 +315,7 @@
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Series VALUES(?, ?, ?, ?)");
     s.BindString(0, seriesUuid);
     s.BindString(1, parentStudyUuid);
-    s.BindString(2, dicomSeries);
+    s.BindString(2, hasher.GetSeriesUid());
 
     const DicomValue* expectedNumberOfInstances;
     if (//(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_FRAMES)) != NULL ||
@@ -343,10 +342,10 @@
   }
 
   bool ServerIndex::HasStudy(std::string& studyUuid,
-                             const std::string& dicomStudy)
+                             const DicomInstanceHasher& hasher)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE dicomStudy=?");
-    s.BindString(0, dicomStudy);
+    s.BindString(0, hasher.GetStudyUid());
     if (s.Step())
     {
       studyUuid = s.ColumnString(0);
@@ -359,7 +358,7 @@
   }
 
   std::string ServerIndex::CreateStudy(const std::string& parentPatientUuid,
-                                       const std::string& dicomStudy,
+                                       const DicomInstanceHasher& hasher,
                                        const DicomMap& dicomSummary)
   {
     std::string studyUuid = Toolbox::GenerateUuid();
@@ -372,7 +371,7 @@
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Studies VALUES(?, ?, ?)");
     s.BindString(0, studyUuid);
     s.BindString(1, parentPatientUuid);
-    s.BindString(2, dicomStudy);
+    s.BindString(2, hasher.GetStudyUid());
     s.Run();
 
     RecordChange("studies", studyUuid);
@@ -387,10 +386,10 @@
 
 
   bool ServerIndex::HasPatient(std::string& patientUuid,
-                               const std::string& dicomPatientId)
+                               const DicomInstanceHasher& hasher)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Patients WHERE dicomPatientId=?");
-    s.BindString(0, dicomPatientId);
+    s.BindString(0,hasher.GetPatientId());
     if (s.Step())
     {
       patientUuid = s.ColumnString(0);
@@ -402,7 +401,7 @@
     }
   }
 
-  std::string ServerIndex::CreatePatient(const std::string& patientId,
+  std::string ServerIndex::CreatePatient(const DicomInstanceHasher& hasher, 
                                          const DicomMap& dicomSummary)
   {
     std::string patientUuid = Toolbox::GenerateUuid();
@@ -538,43 +537,43 @@
       SQLite::Transaction t(db_);
       t.Begin();
 
-      if (HasInstance(instanceUuid, hasher.GetInstanceUid()))
+      if (HasInstance(instanceUuid, hasher))
       {
         return StoreStatus_AlreadyStored;
         // TODO: Check consistency?
       }
 
       std::string patientUuid;
-      if (HasPatient(patientUuid, hasher.GetPatientId()))
+      if (HasPatient(patientUuid, hasher))
       {
         // TODO: Check consistency?
       }
       else
       {
-        patientUuid = CreatePatient(hasher.GetPatientId(), dicomSummary);
+        patientUuid = CreatePatient(hasher, dicomSummary);
       }
 
       std::string studyUuid;
-      if (HasStudy(studyUuid, hasher.GetStudyUid()))
+      if (HasStudy(studyUuid, hasher))
       {
         // TODO: Check consistency?
       }
       else
       {
-        studyUuid = CreateStudy(patientUuid, hasher.GetStudyUid(), dicomSummary);
+        studyUuid = CreateStudy(patientUuid, hasher, dicomSummary);
       }
 
       std::string seriesUuid;
-      if (HasSeries(seriesUuid, hasher.GetSeriesUid()))
+      if (HasSeries(seriesUuid, hasher))
       {
         // TODO: Check consistency?
       }
       else
       {
-        seriesUuid = CreateSeries(studyUuid, hasher.GetSeriesUid(), dicomSummary);
+        seriesUuid = CreateSeries(studyUuid, hasher, dicomSummary);
       }
 
-      instanceUuid = CreateInstance(seriesUuid, hasher.GetInstanceUid(), dicomSummary, fileUuid, 
+      instanceUuid = CreateInstance(seriesUuid, hasher, dicomSummary, fileUuid, 
                                     uncompressedFileSize, jsonUuid, distantAet);
       
       t.Commit();
--- a/OrthancServer/ServerIndex.h	Fri Nov 09 11:13:29 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Nov 09 11:30:16 2012 +0100
@@ -36,6 +36,7 @@
 #include "../Core/SQLite/Connection.h"
 #include "../Core/DicomFormat/DicomMap.h"
 #include "../Core/FileStorage.h"
+#include "../Core/DicomFormat/DicomInstanceHasher.h"
 
 
 namespace Orthanc
@@ -94,13 +95,13 @@
                             const DicomTag& tag);
 
     bool HasInstance(std::string& instanceUuid,
-                     const std::string& dicomInstance);
+                     const DicomInstanceHasher& hasher);
 
     void RecordChange(const std::string& resourceType,
                       const std::string& uuid);
 
     std::string CreateInstance(const std::string& parentSeriesUuid,
-                               const std::string& dicomInstance,
+                               const DicomInstanceHasher& hasher,
                                const DicomMap& dicomSummary,
                                const std::string& fileUuid,
                                uint64_t fileSize,
@@ -110,23 +111,23 @@
     void RemoveInstance(const std::string& uuid);
 
     bool HasSeries(std::string& seriesUuid,
-                   const std::string& dicomSeries);
+                   const DicomInstanceHasher& hasher);
 
     std::string CreateSeries(const std::string& parentStudyUuid,
-                             const std::string& dicomSeries,
+                             const DicomInstanceHasher& hasher,
                              const DicomMap& dicomSummary);
 
     bool HasStudy(std::string& studyUuid,
-                  const std::string& dicomStudy);
+                  const DicomInstanceHasher& hasher);
 
     std::string CreateStudy(const std::string& parentPatientUuid,
-                            const std::string& dicomStudy,
+                            const DicomInstanceHasher& hasher,
                             const DicomMap& dicomSummary);
 
     bool HasPatient(std::string& patientUuid,
-                    const std::string& dicomPatientId);
+                    const DicomInstanceHasher& hasher);
 
-    std::string CreatePatient(const std::string& patientId,
+    std::string CreatePatient(const DicomInstanceHasher& hasher,
                               const DicomMap& dicomSummary);
 
     void GetMainDicomTags(DicomMap& map,