changeset 5588:1b0fc6685f57 find-refactoring

add index of resources into FindResponse
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 03 May 2024 17:20:27 +0200
parents 0f5586c498d1
children b51ee994cd6f
files OrthancServer/Sources/Database/FindResponse.cpp OrthancServer/Sources/Database/FindResponse.h OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp
diffstat 3 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Fri May 03 17:06:42 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Fri May 03 17:20:27 2024 +0200
@@ -364,6 +364,11 @@
     {
       throw OrthancException(ErrorCode_NullPointer);
     }
+    else if (!items_.empty() &&
+             items_[0]->GetLevel() != item->GetLevel())
+    {
+      throw OrthancException(ErrorCode_BadParameterType, "A find response must only contain resources of the same type");
+    }
     else
     {
       const std::string& id = item->GetIdentifier();
@@ -395,18 +400,18 @@
   }
 
 
-  const FindResponse::Item* FindResponse::LookupItem(const std::string& id) const
+  FindResponse::Item& FindResponse::GetItem(const std::string& id)
   {
     Index::const_iterator found = index_.find(id);
 
     if (found == index_.end())
     {
-      return NULL;
+      throw OrthancException(ErrorCode_InexistentItem);
     }
     else
     {
       assert(found->second != NULL);
-      return found->second;
+      return *found->second;
     }
   }
 }
--- a/OrthancServer/Sources/Database/FindResponse.h	Fri May 03 17:06:42 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.h	Fri May 03 17:20:27 2024 +0200
@@ -109,6 +109,11 @@
       {
       }
 
+      ResourceType GetLevel() const
+      {
+        return level_;
+      }
+
       const std::string& GetIdentifier() const
       {
         return identifier_;
@@ -204,6 +209,16 @@
 
     const Item& GetItem(size_t index) const;
 
-    const Item* LookupItem(const std::string& id) const;
+    Item& GetItem(const std::string& id);
+
+    const Item& GetItem(const std::string& id) const
+    {
+      return const_cast<FindResponse&>(*this).GetItem(id);
+    }
+
+    bool HasItem(const std::string& id) const
+    {
+      return (index_.find(id) != index_.end());
+    }
   };
 }
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Fri May 03 17:06:42 2024 +0200
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Fri May 03 17:20:27 2024 +0200
@@ -1178,9 +1178,8 @@
         }
         else
         {
-          std::map<std::string, FindResponse::Item*> items;  // cache to the response items
-
-          {// first create a temporary table that with the filtered and ordered results
+          {
+            // first create a temporary table that with the filtered and ordered results
             sqlLookup = "CREATE TEMPORARY TABLE FilteredResourcesIds AS " + sqlLookup;
 
             SQLite::Statement statement(db_, SQLITE_FROM_HERE_DYNAMIC(sqlLookup), sqlLookup);
@@ -1196,10 +1195,7 @@
             while (statement.Step())
             {
               const std::string resourceId = statement.ColumnString(0);
-
-              FindResponse::Item* item = new FindResponse::Item(request.GetLevel(), resourceId);
-              items[resourceId] = item;
-              response.Add(item);
+              response.Add(new FindResponse::Item(request.GetLevel(), resourceId));
             }
           }
 
@@ -1215,10 +1211,11 @@
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              items[resourceId]->AddStringDicomTag(request.GetLevel(),
-                                                   statement.ColumnInt(1),
-                                                   statement.ColumnInt(2),
-                                                   statement.ColumnString(3));
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).AddStringDicomTag(request.GetLevel(),
+                                                             statement.ColumnInt(1),
+                                                             statement.ColumnInt(2),
+                                                             statement.ColumnString(3));
             }
           }
 
@@ -1234,7 +1231,8 @@
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              items[resourceId]->AddChildIdentifier(GetChildResourceType(request.GetLevel()), statement.ColumnString(1));
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).AddChildIdentifier(GetChildResourceType(request.GetLevel()), statement.ColumnString(1));
             }
           }
 
@@ -1250,7 +1248,8 @@
             {
               const std::string& resourceId = statement.ColumnString(0);
               const std::string& parentId = statement.ColumnString(1);
-              items[resourceId]->SetParentIdentifier(parentId);
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).SetParentIdentifier(parentId);
             }
           }
 
@@ -1264,8 +1263,9 @@
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              items[resourceId]->AddMetadata(static_cast<MetadataType>(statement.ColumnInt(1)),
-                                             statement.ColumnString(2));
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).AddMetadata(static_cast<MetadataType>(statement.ColumnInt(1)),
+                                                       statement.ColumnString(2));
             }
           }
 
@@ -1279,7 +1279,8 @@
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              items[resourceId]->AddLabel(statement.ColumnString(1));
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).AddLabel(statement.ColumnString(1));
             }
           }
 
@@ -1301,7 +1302,9 @@
                                              static_cast<CompressionType>(statement.ColumnInt(4)),
                                              statement.ColumnInt64(5),
                                              statement.ColumnString(7));
-              items[resourceId]->AddAttachment(attachment);
+
+              assert(response.HasItem(resourceId));
+              response.GetItem(resourceId).AddAttachment(attachment);
             };
           }