# HG changeset patch # User Sebastien Jodogne # Date 1439193719 -7200 # Node ID 0586ed8897f1b80537717b667305cb9c934c329e # Parent 86394eb9f5bb4ab25517dc04ae96f7db8b57cab0 limit and since arguments while retrieving DICOM resources in the REST API diff -r 86394eb9f5bb -r 0586ed8897f1 NEWS --- 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) ========================== diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/DatabaseWrapper.cpp --- 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& 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) { diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/DatabaseWrapper.h --- 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& target, ResourceType resourceType); + virtual void GetAllPublicIds(std::list& target, + ResourceType resourceType, + size_t since, + size_t limit); + virtual bool SelectPatientToRecycle(int64_t& internalId); virtual bool SelectPatientToRecycle(int64_t& internalId, diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/IDatabaseWrapper.h --- 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& target, ResourceType resourceType) = 0; + virtual void GetAllPublicIds(std::list& target, + ResourceType resourceType, + size_t since, + size_t limit) = 0; virtual void GetChanges(std::list& target /*out*/, bool& done /*out*/, diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/OrthancRestApi/OrthancRestResources.cpp --- 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 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(call.GetArgument("since", "")); + size_t limit = boost::lexical_cast(call.GetArgument("limit", "")); + index.GetAllUuids(result, resourceType, since, limit); + } + else + { + index.GetAllUuids(result, resourceType); + } + AnswerListOfResources(call.GetOutput(), index, result, resourceType, call.HasArgument("expand")); } diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/ServerIndex.cpp --- 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& 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 static void FormatLog(Json::Value& target, const std::list& log, diff -r 86394eb9f5bb -r 0586ed8897f1 OrthancServer/ServerIndex.h --- 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& target, ResourceType resourceType); + void GetAllUuids(std::list& target, + ResourceType resourceType, + size_t since, + size_t limit); + bool DeleteResource(Json::Value& target /* out */, const std::string& uuid, ResourceType expectedType); diff -r 86394eb9f5bb -r 0586ed8897f1 Plugins/Engine/OrthancPluginDatabase.cpp --- 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& 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 tmp; + GetAllPublicIds(tmp, resourceType); + + if (tmp.size() <= since) + { + // Not enough results => empty answer + return; + } + + std::list::iterator start = tmp.begin(); + std::advance(start, since); + + if (tmp.size() - since <= limit) + { + tmp.splice(start, target); + } + else + { + std::list::iterator end = start; + std::advance(end, limit); + tmp.splice(tmp.begin(), target, start, end); + } + } + + void OrthancPluginDatabase::GetChanges(std::list& target /*out*/, bool& done /*out*/, diff -r 86394eb9f5bb -r 0586ed8897f1 Plugins/Engine/OrthancPluginDatabase.h --- 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& target, ResourceType resourceType); + virtual void GetAllPublicIds(std::list& target, + ResourceType resourceType, + size_t since, + size_t limit); virtual void GetChanges(std::list& target /*out*/, bool& done /*out*/,