changeset 1509:0586ed8897f1

limit and since arguments while retrieving DICOM resources in the REST API
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 10 Aug 2015 10:01:59 +0200
parents 86394eb9f5bb
children ffc9f36103b9
files NEWS OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/IDatabaseWrapper.h OrthancServer/OrthancRestApi/OrthancRestResources.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h Plugins/Engine/OrthancPluginDatabase.cpp Plugins/Engine/OrthancPluginDatabase.h
diffstat 9 files changed, 123 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Aug 07 20:14:49 2015 +0200
+++ b/NEWS	Mon Aug 10 10:01:59 2015 +0200
@@ -1,6 +1,8 @@
 Pending changes in the mainline
 ===============================
 
+* "limit" and "since" arguments while retrieving DICOM resources in the REST API
+
 
 Version 0.9.3 (2015/08/07)
 ==========================
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Mon Aug 10 10:01:59 2015 +0200
@@ -742,6 +742,29 @@
     }
   }
 
+  void DatabaseWrapper::GetAllPublicIds(std::list<std::string>& target,
+                                        ResourceType resourceType,
+                                        size_t since,
+                                        size_t limit)
+  {
+    if (limit == 0)
+    {
+      target.clear();
+      return;
+    }
+
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT publicId FROM Resources WHERE resourceType=? LIMIT ? OFFSET ?");
+    s.BindInt(0, resourceType);
+    s.BindInt64(1, limit);
+    s.BindInt64(2, since);
+
+    target.clear();
+    while (s.Step())
+    {
+      target.push_back(s.ColumnString(0));
+    }
+  }
+
   static void UpgradeDatabase(SQLite::Connection& db,
                               EmbeddedResources::FileResourceId script)
   {
--- a/OrthancServer/DatabaseWrapper.h	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/DatabaseWrapper.h	Mon Aug 10 10:01:59 2015 +0200
@@ -170,6 +170,11 @@
     virtual void GetAllPublicIds(std::list<std::string>& target,
                                  ResourceType resourceType);
 
+    virtual void GetAllPublicIds(std::list<std::string>& target,
+                                 ResourceType resourceType,
+                                 size_t since,
+                                 size_t limit);
+
     virtual bool SelectPatientToRecycle(int64_t& internalId);
 
     virtual bool SelectPatientToRecycle(int64_t& internalId,
--- a/OrthancServer/IDatabaseWrapper.h	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/IDatabaseWrapper.h	Mon Aug 10 10:01:59 2015 +0200
@@ -81,6 +81,10 @@
     virtual void GetAllPublicIds(std::list<std::string>& target,
                                  ResourceType resourceType) = 0;
 
+    virtual void GetAllPublicIds(std::list<std::string>& target,
+                                 ResourceType resourceType,
+                                 size_t since,
+                                 size_t limit) = 0;
 
     virtual void GetChanges(std::list<ServerIndexChange>& target /*out*/,
                             bool& done /*out*/,
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Mon Aug 10 10:01:59 2015 +0200
@@ -80,7 +80,31 @@
     ServerIndex& index = OrthancRestApi::GetIndex(call);
 
     std::list<std::string> result;
-    index.GetAllUuids(result, resourceType);
+
+    if (call.HasArgument("limit") ||
+        call.HasArgument("since"))
+    {
+      if (!call.HasArgument("limit"))
+      {
+        LOG(ERROR) << "Missing \"limit\" argument for GET request against: " << call.FlattenUri();
+        throw OrthancException(ErrorCode_BadRequest);
+      }
+
+      if (!call.HasArgument("since"))
+      {
+        LOG(ERROR) << "Missing \"since\" argument for GET request against: " << call.FlattenUri();
+        throw OrthancException(ErrorCode_BadRequest);
+      }
+
+      size_t since = boost::lexical_cast<size_t>(call.GetArgument("since", ""));
+      size_t limit = boost::lexical_cast<size_t>(call.GetArgument("limit", ""));
+      index.GetAllUuids(result, resourceType, since, limit);
+    }
+    else
+    {
+      index.GetAllUuids(result, resourceType);
+    }
+
 
     AnswerListOfResources(call.GetOutput(), index, result, resourceType, call.HasArgument("expand"));
   }
--- a/OrthancServer/ServerIndex.cpp	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/ServerIndex.cpp	Mon Aug 10 10:01:59 2015 +0200
@@ -1097,6 +1097,22 @@
   }
 
 
+  void ServerIndex::GetAllUuids(std::list<std::string>& target,
+                                ResourceType resourceType,
+                                size_t since,
+                                size_t limit)
+  {
+    if (limit == 0)
+    {
+      target.clear();
+      return;
+    }
+
+    boost::mutex::scoped_lock lock(mutex_);
+    db_.GetAllPublicIds(target, resourceType, since, limit);
+  }
+
+
   template <typename T>
   static void FormatLog(Json::Value& target,
                         const std::list<T>& log,
--- a/OrthancServer/ServerIndex.h	Fri Aug 07 20:14:49 2015 +0200
+++ b/OrthancServer/ServerIndex.h	Mon Aug 10 10:01:59 2015 +0200
@@ -159,6 +159,11 @@
     void GetAllUuids(std::list<std::string>& target,
                      ResourceType resourceType);
 
+    void GetAllUuids(std::list<std::string>& target,
+                     ResourceType resourceType,
+                     size_t since,
+                     size_t limit);
+
     bool DeleteResource(Json::Value& target /* out */,
                         const std::string& uuid,
                         ResourceType expectedType);
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Fri Aug 07 20:14:49 2015 +0200
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Mon Aug 10 10:01:59 2015 +0200
@@ -326,6 +326,45 @@
   }
 
 
+  void OrthancPluginDatabase::GetAllPublicIds(std::list<std::string>& target,
+                                              ResourceType resourceType,
+                                              size_t since,
+                                              size_t limit)
+  {
+    // TODO add the corresponding primitives to the SDK
+
+    target.clear();
+
+    if (limit == 0)
+    {
+      return;
+    }
+
+    std::list<std::string> tmp;
+    GetAllPublicIds(tmp, resourceType);
+    
+    if (tmp.size() <= since)
+    {
+      // Not enough results => empty answer
+      return;
+    }
+
+    std::list<std::string>::iterator start = tmp.begin();
+    std::advance(start, since);
+
+    if (tmp.size() - since <= limit)
+    {
+      tmp.splice(start, target);
+    }
+    else
+    {
+      std::list<std::string>::iterator end = start;
+      std::advance(end, limit);
+      tmp.splice(tmp.begin(), target, start, end);
+    }
+  }
+
+
 
   void OrthancPluginDatabase::GetChanges(std::list<ServerIndexChange>& target /*out*/,
                                          bool& done /*out*/,
--- a/Plugins/Engine/OrthancPluginDatabase.h	Fri Aug 07 20:14:49 2015 +0200
+++ b/Plugins/Engine/OrthancPluginDatabase.h	Mon Aug 10 10:01:59 2015 +0200
@@ -115,6 +115,10 @@
     virtual void GetAllPublicIds(std::list<std::string>& target,
                                  ResourceType resourceType);
 
+    virtual void GetAllPublicIds(std::list<std::string>& target,
+                                 ResourceType resourceType,
+                                 size_t since,
+                                 size_t limit);
 
     virtual void GetChanges(std::list<ServerIndexChange>& target /*out*/,
                             bool& done /*out*/,