changeset 1413:cde379b9d1d2 loader-cache-refactoring

LoaderCache is now extensible + factored the uuid normalizing
author Benjamin Golinvaux <bgo@osimis.io>
date Mon, 04 May 2020 18:26:35 +0200
parents 08fd094f9582
children 4f2e14275b09 0171302051fa
files Framework/Loaders/LoaderCache.cpp Framework/Loaders/LoaderCache.h UnitTestsSources/UnitTestsMain.cpp
diffstat 3 files changed, 93 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Loaders/LoaderCache.cpp	Mon May 04 13:34:46 2020 +0200
+++ b/Framework/Loaders/LoaderCache.cpp	Mon May 04 18:26:35 2020 +0200
@@ -53,10 +53,8 @@
   {
     try
     {
-      
       // normalize keys a little
-      seriesUuid = Orthanc::Toolbox::StripSpaces(seriesUuid);
-      Orthanc::Toolbox::ToLowerCase(seriesUuid);
+      NormalizeUuid(seriesUuid);
 
       // find in cache
       if (seriesVolumeProgressiveLoaders_.find(seriesUuid) == seriesVolumeProgressiveLoaders_.end())
@@ -104,6 +102,9 @@
 
   boost::shared_ptr<OrthancMultiframeVolumeLoader> LoaderCache::GetMultiframeVolumeLoader(std::string instanceUuid)
   {
+    // normalize keys a little
+    NormalizeUuid(instanceUuid);
+
     // if the loader is not available, let's trigger its creation
     if(multiframeVolumeLoaders_.find(instanceUuid) == multiframeVolumeLoaders_.end())
     {
@@ -119,8 +120,7 @@
     try
     {
       // normalize keys a little
-      instanceUuid = Orthanc::Toolbox::StripSpaces(instanceUuid);
-      Orthanc::Toolbox::ToLowerCase(instanceUuid);
+      NormalizeUuid(instanceUuid);
 
       // find in cache
       if (dicomVolumeImageMPRSlicers_.find(instanceUuid) == dicomVolumeImageMPRSlicers_.end())
@@ -194,8 +194,7 @@
     try
     {
       // normalize keys a little
-      inInstanceUuid = Orthanc::Toolbox::StripSpaces(inInstanceUuid);
-      Orthanc::Toolbox::ToLowerCase(inInstanceUuid);
+      NormalizeUuid(inInstanceUuid);
 
       std::string initiallyVisibleStructuresKey = 
         SortAndJoin(initiallyVisibleStructures);
@@ -275,4 +274,15 @@
     DebugDisplayObjRefCountsInMap("dicomVolumeImageMPRSlicers_", dicomVolumeImageMPRSlicers_);
     DebugDisplayObjRefCountsInMap("dicomStructureSetLoaders_", dicomStructureSetLoaders_);
   }
+
+  /**
+  This method could have been called StripSpacesAndChangeToLower but we might want to 
+  add some UUID validation to the argument
+  */
+  void LoaderCache::NormalizeUuid(std::string& uuid)
+  {
+    std::string temp = Orthanc::Toolbox::StripSpaces(uuid);
+    Orthanc::Toolbox::ToLowerCase(temp);
+    uuid.swap(temp);
+  }
 }
--- a/Framework/Loaders/LoaderCache.h	Mon May 04 13:34:46 2020 +0200
+++ b/Framework/Loaders/LoaderCache.h	Mon May 04 18:26:35 2020 +0200
@@ -41,6 +41,9 @@
   class LoaderCache
   {
   public:
+
+    virtual ~LoaderCache() {}
+
     LoaderCache(OrthancStone::ILoadersContext& loadersContext);
 
     boost::shared_ptr<OrthancSeriesVolumeProgressiveLoader>
@@ -59,7 +62,12 @@
 
     void ClearCache();
 
-  private:
+    /**
+    Service method static and exposed for unit tests.
+    */
+    static void NormalizeUuid(std::string& uuid);
+
+  protected:
     
     void DebugDisplayObjRefCounts();
 
--- a/UnitTestsSources/UnitTestsMain.cpp	Mon May 04 13:34:46 2020 +0200
+++ b/UnitTestsSources/UnitTestsMain.cpp	Mon May 04 18:26:35 2020 +0200
@@ -25,6 +25,7 @@
 #include "../Framework/Toolbox/FiniteProjectiveCamera.h"
 #include "../Framework/Toolbox/GeometryToolbox.h"
 #include "../Framework/Volumes/ImageBuffer3D.h"
+#include "../Framework/Loaders/LoaderCache.h"
 
 #include <Core/HttpClient.h>
 #include <Core/Images/ImageProcessing.h>
@@ -811,6 +812,72 @@
   ASSERT_DOUBLE_EQ(1.3671875, v[1]); 
 }
 
+TEST(LoaderCache, NormalizeUuid)
+{
+  std::string ref("44ca5051-14ef-4d2f-8bd7-db20bfb61fbb");
+
+  {
+    std::string u("44ca5051-14ef-4d2f-8bd7-db20bfb61fbb");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space left
+  {
+    std::string u("  44ca5051-14ef-4d2f-8bd7-db20bfb61fbb");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space right
+  {
+    std::string u("44ca5051-14ef-4d2f-8bd7-db20bfb61fbb  ");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space l & r 
+  {
+    std::string u("  44ca5051-14ef-4d2f-8bd7-db20bfb61fbb  ");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space left + case 
+  {
+    std::string u("  44CA5051-14ef-4d2f-8bd7-dB20bfb61fbb");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space right + case 
+  {
+    std::string u("44ca5051-14EF-4D2f-8bd7-db20bfb61fbB  ");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // space l & r + case
+  {
+    std::string u("  44cA5051-14Ef-4d2f-8bD7-db20bfb61fbb  ");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_EQ(ref, u);
+  }
+
+  // no
+  {
+    std::string u("  44ca5051-14ef-4d2f-8bd7-  db20bfb61fbb");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_NE(ref, u);
+  }
+
+  // no
+  {
+    std::string u("44ca5051-14ef-4d2f-8bd7-db20bfb61fb");
+    OrthancStone::LoaderCache::NormalizeUuid(u);
+    ASSERT_NE(ref, u);
+  }
+}
 
 int main(int argc, char **argv)
 {