changeset 1241:90d2f320862d

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 05 Dec 2014 17:22:53 +0100
parents 62c35e4b67db
children 58e6a89c3ef4
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ServerIndex.cpp UnitTestsSources/ServerIndexTests.cpp
diffstat 4 files changed, 48 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Dec 05 17:12:35 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Fri Dec 05 17:22:53 2014 +0100
@@ -336,16 +336,17 @@
     s.Run();
   }
 
-  void DatabaseWrapper::GetChildren(Json::Value& childrenPublicIds,
+
+  void DatabaseWrapper::GetChildren(std::list<std::string>& childrenPublicIds,
                                     int64_t id)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE parentId=?");
     s.BindInt64(0, id);
 
-    childrenPublicIds = Json::arrayValue;
+    childrenPublicIds.clear();
     while (s.Step())
     {
-      childrenPublicIds.append(s.ColumnString(0));
+      childrenPublicIds.push_back(s.ColumnString(0));
     }
   }
 
@@ -788,16 +789,16 @@
     return static_cast<uint64_t>(s.ColumnInt64(0));
   }
 
-  void DatabaseWrapper::GetAllPublicIds(Json::Value& target,
+  void DatabaseWrapper::GetAllPublicIds(std::list<std::string>& target,
                                         ResourceType resourceType)
   {
     SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=?");
     s.BindInt(0, resourceType);
 
-    target = Json::arrayValue;
+    target.clear();
     while (s.Step())
     {
-      target.append(s.ColumnString(0));
+      target.push_back(s.ColumnString(0));
     }
   }
 
--- a/OrthancServer/DatabaseWrapper.h	Fri Dec 05 17:12:35 2014 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Fri Dec 05 17:22:53 2014 +0100
@@ -96,9 +96,6 @@
     void AttachChild(int64_t parent,
                      int64_t child);
 
-    void GetChildren(Json::Value& childrenPublicIds,
-                     int64_t id);
-
     void DeleteResource(int64_t id);
 
     void SetMetadata(int64_t id,
@@ -134,9 +131,6 @@
     void GetMainDicomTags(DicomMap& map,
                           int64_t id);
 
-    bool GetParentPublicId(std::string& result,
-                           int64_t id);
-
     void GetChildrenPublicId(std::list<std::string>& result,
                              int64_t id);
 
@@ -168,16 +162,13 @@
 
     void GetLastExportedResource(Json::Value& target);
 
-    // For unit testing only!
-    int64_t GetTableRecordCount(const std::string& table);
-    
     uint64_t GetTotalCompressedSize();
     
     uint64_t GetTotalUncompressedSize();
 
     uint64_t GetResourceCount(ResourceType resourceType);
 
-    void GetAllPublicIds(Json::Value& target,
+    void GetAllPublicIds(std::list<std::string>& target,
                          ResourceType resourceType);
 
     bool SelectPatientToRecycle(int64_t& internalId);
@@ -223,5 +214,21 @@
 
     void GetAllMetadata(std::map<MetadataType, std::string>& result,
                         int64_t id);
+
+
+
+
+    /**
+     * The methods declared below are for unit testing only!
+     **/
+
+    void GetChildren(std::list<std::string>& childrenPublicIds,
+                     int64_t id);
+
+    int64_t GetTableRecordCount(const std::string& table);
+    
+    bool GetParentPublicId(std::string& result,
+                           int64_t id);
+
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Fri Dec 05 17:12:35 2014 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Dec 05 17:22:53 2014 +0100
@@ -1022,8 +1022,19 @@
   void ServerIndex::GetAllUuids(Json::Value& target,
                                 ResourceType resourceType)
   {
-    boost::mutex::scoped_lock lock(mutex_);
-    db_->GetAllPublicIds(target, resourceType);
+    std::list<std::string> lst;
+
+    {
+      boost::mutex::scoped_lock lock(mutex_);
+      db_->GetAllPublicIds(lst, resourceType);
+    }
+
+    target = Json::arrayValue;
+    for (std::list<std::string>::const_iterator
+           it = lst.begin(); it != lst.end(); it++)
+    {
+      target.append(*it);
+    }
   }
 
 
--- a/UnitTestsSources/ServerIndexTests.cpp	Fri Dec 05 17:12:35 2014 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Fri Dec 05 17:22:53 2014 +0100
@@ -156,15 +156,15 @@
   ASSERT_EQ(ResourceType_Study, index_->GetResourceType(a[6]));
 
   {
-    Json::Value t;
+    std::list<std::string> t;
     index_->GetAllPublicIds(t, ResourceType_Patient);
 
     ASSERT_EQ(1u, t.size());
-    ASSERT_EQ("a", t[0u].asString());
+    ASSERT_EQ("a", t.front());
 
     index_->GetAllPublicIds(t, ResourceType_Series);
     ASSERT_EQ(1u, t.size());
-    ASSERT_EQ("c", t[0u].asString());
+    ASSERT_EQ("c", t.front());
 
     index_->GetAllPublicIds(t, ResourceType_Study);
     ASSERT_EQ(2u, t.size());
@@ -353,25 +353,25 @@
   index_->AttachChild(a[5], a[7]);
 
   {
-    Json::Value j;
+    std::list<std::string> j;
     index_->GetChildren(j, a[0]);
     ASSERT_EQ(2u, j.size());
-    ASSERT_TRUE((j[0u] == "b" && j[1u] == "f") ||
-                (j[1u] == "b" && j[0u] == "f"));
+    ASSERT_TRUE((j.front() == "b" && j.back() == "f") ||
+                (j.back() == "b" && j.front() == "f"));
 
     index_->GetChildren(j, a[1]);
     ASSERT_EQ(2u, j.size());
-    ASSERT_TRUE((j[0u] == "c" && j[1u] == "g") ||
-                (j[1u] == "c" && j[0u] == "g"));
+    ASSERT_TRUE((j.front() == "c" && j.back() == "g") ||
+                (j.back() == "c" && j.front() == "g"));
 
     index_->GetChildren(j, a[2]);
     ASSERT_EQ(2u, j.size());
-    ASSERT_TRUE((j[0u] == "d" && j[1u] == "e") ||
-                (j[1u] == "d" && j[0u] == "e"));
+    ASSERT_TRUE((j.front() == "d" && j.back() == "e") ||
+                (j.back() == "d" && j.front() == "e"));
 
     index_->GetChildren(j, a[3]); ASSERT_EQ(0u, j.size());
     index_->GetChildren(j, a[4]); ASSERT_EQ(0u, j.size());
-    index_->GetChildren(j, a[5]); ASSERT_EQ(1u, j.size()); ASSERT_EQ("h", j[0u].asString());
+    index_->GetChildren(j, a[5]); ASSERT_EQ(1u, j.size()); ASSERT_EQ("h", j.front());
     index_->GetChildren(j, a[6]); ASSERT_EQ(0u, j.size());
     index_->GetChildren(j, a[7]); ASSERT_EQ(0u, j.size());
   }