changeset 237:16a4ac70bd8a

last change and export
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 30 Nov 2012 15:45:15 +0100
parents 6d9be2b470b4
children e4148b0ab1d0
files Core/RestApi/RestApi.h NEWS OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h
diffstat 7 files changed, 107 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/Core/RestApi/RestApi.h	Fri Nov 30 15:09:16 2012 +0100
+++ b/Core/RestApi/RestApi.h	Fri Nov 30 15:45:15 2012 +0100
@@ -99,6 +99,11 @@
       {
         return HttpHandler::GetArgument(*getArguments_, name, defaultValue);
       }
+
+      bool HasArgument(const std::string& name) const
+      {
+        return getArguments_->find(name) != getArguments_->end();
+      }
     };
 
     class PutCall : public SharedCall
--- a/NEWS	Fri Nov 30 15:09:16 2012 +0100
+++ b/NEWS	Fri Nov 30 15:45:15 2012 +0100
@@ -15,6 +15,7 @@
 Minor changes
 -------------
 
+* "last" flag to retrieve the last change from the "/changes" URI
 * Generate a sample configuration file from command line
 * "CompletedSeries" event in the changes API
 * Thread to continuously flush DB to disk (SQLite checkpoints for
--- a/OrthancServer/DatabaseWrapper.cpp	Fri Nov 30 15:09:16 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.cpp	Fri Nov 30 15:45:15 2012 +0100
@@ -509,16 +509,13 @@
   }
 
 
-  void DatabaseWrapper::GetChanges(Json::Value& target,
-                                   int64_t since,
-                                   unsigned int maxResults)
+  void DatabaseWrapper::GetChangesInternal(Json::Value& target,
+                                           SQLite::Statement& s,
+                                           int64_t since,
+                                           unsigned int maxResults)
   {
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?");
-    s.BindInt(0, since);
-    s.BindInt(1, maxResults + 1);
-
     Json::Value changes = Json::arrayValue;
-    int64_t last = 0;
+    int64_t last = since;
 
     while (changes.size() < maxResults && s.Step())
     {
@@ -543,8 +540,25 @@
 
     target = Json::objectValue;
     target["Changes"] = changes;
-    target["PendingChanges"] = (changes.size() == maxResults && s.Step());
-    target["LastSeq"] = static_cast<int>(last);
+    target["Done"] = !(changes.size() == maxResults && s.Step());
+    target["Last"] = static_cast<int>(last);
+  }
+
+
+  void DatabaseWrapper::GetChanges(Json::Value& target,
+                                   int64_t since,
+                                   unsigned int maxResults)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?");
+    s.BindInt(0, since);
+    s.BindInt(1, maxResults + 1);
+    GetChangesInternal(target, s, since, maxResults);
+  }
+
+  void DatabaseWrapper::GetLastChange(Json::Value& target)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1");
+    GetChangesInternal(target, s, 0, 1);
   }
 
 
@@ -574,16 +588,12 @@
 
 
   void DatabaseWrapper::GetExportedResources(Json::Value& target,
+                                             SQLite::Statement& s,
                                              int64_t since,
                                              unsigned int maxResults)
   {
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
-                        "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?");
-    s.BindInt(0, since);
-    s.BindInt(1, maxResults + 1);
-
     Json::Value changes = Json::arrayValue;
-    int64_t last = 0;
+    int64_t last = since;
 
     while (changes.size() < maxResults && s.Step())
     {
@@ -625,9 +635,29 @@
     }
 
     target = Json::objectValue;
-    target["Changes"] = changes;
-    target["PendingChanges"] = (changes.size() == maxResults && s.Step());
-    target["LastSeq"] = static_cast<int>(last);
+    target["Exports"] = changes;
+    target["Done"] = !(changes.size() == maxResults && s.Step());
+    target["Last"] = static_cast<int>(last);
+  }
+
+
+  void DatabaseWrapper::GetExportedResources(Json::Value& target,
+                                             int64_t since,
+                                             unsigned int maxResults)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT * FROM ExportedResources WHERE seq>? ORDER BY seq LIMIT ?");
+    s.BindInt(0, since);
+    s.BindInt(1, maxResults + 1);
+    GetExportedResources(target, s, since, maxResults);
+  }
+
+    
+  void DatabaseWrapper::GetLastExportedResource(Json::Value& target)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
+                        "SELECT * FROM ExportedResources ORDER BY seq DESC LIMIT 1");
+    GetExportedResources(target, s, 0, 1);
   }
 
 
--- a/OrthancServer/DatabaseWrapper.h	Fri Nov 30 15:09:16 2012 +0100
+++ b/OrthancServer/DatabaseWrapper.h	Fri Nov 30 15:45:15 2012 +0100
@@ -62,6 +62,16 @@
 
     void Open();
 
+    void GetChangesInternal(Json::Value& target,
+                            SQLite::Statement& s,
+                            int64_t since,
+                            unsigned int maxResults);
+
+    void GetExportedResources(Json::Value& target,
+                              SQLite::Statement& s,
+                              int64_t since,
+                              unsigned int maxResults);
+
   public:
     void SetGlobalProperty(GlobalProperty property,
                            const std::string& value);
@@ -139,6 +149,8 @@
                     int64_t since,
                     unsigned int maxResults);
 
+    void GetLastChange(Json::Value& target);
+
     void LogExportedResource(ResourceType resourceType,
                              const std::string& publicId,
                              const std::string& remoteModality,
@@ -153,6 +165,8 @@
                               int64_t since,
                               unsigned int maxResults);
 
+    void GetLastExportedResource(Json::Value& target);
+
     int64_t GetTableRecordCount(const std::string& table);
     
     uint64_t GetTotalCompressedSize();
--- a/OrthancServer/OrthancRestApi.cpp	Fri Nov 30 15:09:16 2012 +0100
+++ b/OrthancServer/OrthancRestApi.cpp	Fri Nov 30 15:45:15 2012 +0100
@@ -350,10 +350,19 @@
  
   static void GetSinceAndLimit(int64_t& since,
                                unsigned int& limit,
+                               bool& last,
                                const RestApi::GetCall& call)
   {
     static const unsigned int MAX_RESULTS = 100;
-        
+    
+    if (call.HasArgument("last"))
+    {
+      last = true;
+      return;
+    }
+
+    last = false;
+
     try
     {
       since = boost::lexical_cast<int64_t>(call.GetArgument("since", "0"));
@@ -377,10 +386,12 @@
     //std::string filter = GetArgument(getArguments, "filter", "");
     int64_t since;
     unsigned int limit;
-    GetSinceAndLimit(since, limit, call);
+    bool last;
+    GetSinceAndLimit(since, limit, last, call);
 
     Json::Value result;
-    if (context.GetIndex().GetChanges(result, since, limit))
+    if ((!last && context.GetIndex().GetChanges(result, since, limit)) ||
+        ( last && context.GetIndex().GetLastChange(result)))
     {
       call.GetOutput().AnswerJson(result);
     }
@@ -393,10 +404,12 @@
 
     int64_t since;
     unsigned int limit;
-    GetSinceAndLimit(since, limit, call);
+    bool last;
+    GetSinceAndLimit(since, limit, last, call);
 
     Json::Value result;
-    if (context.GetIndex().GetExportedResources(result, since, limit))
+    if ((!last && context.GetIndex().GetExportedResources(result, since, limit)) ||
+        ( last && context.GetIndex().GetLastExportedResource(result)))
     {
       call.GetOutput().AnswerJson(result);
     }
@@ -619,7 +632,7 @@
     Register("/", ServeRoot);
     Register("/system", GetSystemInformation);
     Register("/changes", GetChanges);
-    Register("/exported", GetExports);
+    Register("/exports", GetExports);
 
     Register("/instances", UploadDicomFile);
     Register("/instances", ListResources<ResourceType_Instance>);
--- a/OrthancServer/ServerIndex.cpp	Fri Nov 30 15:09:16 2012 +0100
+++ b/OrthancServer/ServerIndex.cpp	Fri Nov 30 15:45:15 2012 +0100
@@ -617,9 +617,14 @@
                                unsigned int maxResults)
   {
     boost::mutex::scoped_lock lock(mutex_);
+    db_->GetChanges(target, since, maxResults);
+    return true;
+  }
 
-    db_->GetChanges(target, since, maxResults);
-
+  bool ServerIndex::GetLastChange(Json::Value& target)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    db_->GetLastChange(target);
     return true;
   }
 
@@ -703,4 +708,11 @@
     db_->GetExportedResources(target, since, maxResults);
     return true;
   }
+
+  bool ServerIndex::GetLastExportedResource(Json::Value& target)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    db_->GetLastExportedResource(target);
+    return true;
+  }
 }
--- a/OrthancServer/ServerIndex.h	Fri Nov 30 15:09:16 2012 +0100
+++ b/OrthancServer/ServerIndex.h	Fri Nov 30 15:45:15 2012 +0100
@@ -102,11 +102,16 @@
                     int64_t since,
                     unsigned int maxResults);
 
+    bool GetLastChange(Json::Value& target);
+
     void LogExportedResource(const std::string& publicId,
                              const std::string& remoteModality);
 
     bool GetExportedResources(Json::Value& target,
                               int64_t since,
                               unsigned int maxResults);
+
+    bool GetLastExportedResource(Json::Value& target);
+
   };
 }