# HG changeset patch # User Sebastien Jodogne # Date 1417795955 -3600 # Node ID 62c35e4b67db008a9b4341520d7653a252c26bc6 # Parent 92c6b3b576998ba52265d12fde1f5ad07f7e2357 refactoring diff -r 92c6b3b57699 -r 62c35e4b67db OrthancServer/DatabaseWrapper.cpp --- a/OrthancServer/DatabaseWrapper.cpp Fri Dec 05 16:22:18 2014 +0100 +++ b/OrthancServer/DatabaseWrapper.cpp Fri Dec 05 17:12:35 2014 +0100 @@ -601,13 +601,11 @@ { if (change.GetChangeType() <= ChangeType_INTERNAL_LastLogged) { - const boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); - SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Changes VALUES(NULL, ?, ?, ?, ?)"); s.BindInt(0, change.GetChangeType()); s.BindInt64(1, internalId); s.BindInt(2, change.GetResourceType()); - s.BindString(3, boost::posix_time::to_iso_string(now)); + s.BindString(3, change.GetDate()); s.Run(); } @@ -615,56 +613,44 @@ } - void DatabaseWrapper::GetChangesInternal(Json::Value& target, + void DatabaseWrapper::GetChangesInternal(std::list& target, + bool& done, SQLite::Statement& s, - int64_t since, unsigned int maxResults) { - Json::Value changes = Json::arrayValue; - int64_t last = since; - - while (changes.size() < maxResults && s.Step()) + while (target.size() < maxResults && s.Step()) { int64_t seq = s.ColumnInt64(0); ChangeType changeType = static_cast(s.ColumnInt(1)); - int64_t internalId = s.ColumnInt64(2); ResourceType resourceType = static_cast(s.ColumnInt(3)); const std::string& date = s.ColumnString(4); + + int64_t internalId = s.ColumnInt64(2); std::string publicId = GetPublicId(internalId); - Json::Value item = Json::objectValue; - item["Seq"] = static_cast(seq); - item["ChangeType"] = EnumerationToString(changeType); - item["ResourceType"] = EnumerationToString(resourceType); - item["ID"] = publicId; - item["Path"] = GetBasePath(resourceType, publicId); - item["Date"] = date; - last = seq; - - changes.append(item); + target.push_back(ServerIndexChange(seq, changeType, resourceType, publicId, date)); } - target = Json::objectValue; - target["Changes"] = changes; - target["Done"] = !(changes.size() == maxResults && s.Step()); - target["Last"] = static_cast(last); + done = !(target.size() == maxResults && s.Step()); } - void DatabaseWrapper::GetChanges(Json::Value& target, + void DatabaseWrapper::GetChanges(std::list& target, + bool& done, int64_t since, unsigned int maxResults) { SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes WHERE seq>? ORDER BY seq LIMIT ?"); s.BindInt64(0, since); s.BindInt(1, maxResults + 1); - GetChangesInternal(target, s, since, maxResults); + GetChangesInternal(target, done, s, maxResults); } - void DatabaseWrapper::GetLastChange(Json::Value& target) + void DatabaseWrapper::GetLastChange(std::list& target) { + bool done; // Ignored SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT * FROM Changes ORDER BY seq DESC LIMIT 1"); - GetChangesInternal(target, s, 0, 1); + GetChangesInternal(target, done, s, 1); } diff -r 92c6b3b57699 -r 62c35e4b67db OrthancServer/DatabaseWrapper.h --- a/OrthancServer/DatabaseWrapper.h Fri Dec 05 16:22:18 2014 +0100 +++ b/OrthancServer/DatabaseWrapper.h Fri Dec 05 17:12:35 2014 +0100 @@ -62,9 +62,9 @@ void Open(); - void GetChangesInternal(Json::Value& target, + void GetChangesInternal(std::list& target, + bool& done, SQLite::Statement& s, - int64_t since, unsigned int maxResults); void GetExportedResourcesInternal(Json::Value& target, @@ -146,11 +146,12 @@ void LogChange(int64_t internalId, const ServerIndexChange& change); - void GetChanges(Json::Value& target, + void GetChanges(std::list& target /* out */, + bool& done /* out */, int64_t since, unsigned int maxResults); - void GetLastChange(Json::Value& target); + void GetLastChange(std::list& target /* out */); void LogExportedResource(ResourceType resourceType, const std::string& publicId, diff -r 92c6b3b57699 -r 62c35e4b67db OrthancServer/ServerIndex.cpp --- a/OrthancServer/ServerIndex.cpp Fri Dec 05 16:22:18 2014 +0100 +++ b/OrthancServer/ServerIndex.cpp Fri Dec 05 17:12:35 2014 +0100 @@ -1027,22 +1027,60 @@ } + static void FormatChanges(Json::Value& target, + const std::list& changes, + bool done, + int64_t since) + { + Json::Value items = Json::arrayValue; + for (std::list::const_iterator + it = changes.begin(); it != changes.end(); it++) + { + Json::Value item; + it->Format(item); + items.append(item); + } + + target = Json::objectValue; + target["Changes"] = items; + target["Done"] = done; + + int64_t last = (changes.size() == 0 ? since : changes.back().GetSeq()); + target["Last"] = static_cast(last); + } + + bool ServerIndex::GetChanges(Json::Value& target, int64_t since, unsigned int maxResults) { - boost::mutex::scoped_lock lock(mutex_); - db_->GetChanges(target, since, maxResults); + std::list changes; + bool done; + + { + boost::mutex::scoped_lock lock(mutex_); + db_->GetChanges(changes, done, since, maxResults); + } + + FormatChanges(target, changes, done, since); return true; } + bool ServerIndex::GetLastChange(Json::Value& target) { - boost::mutex::scoped_lock lock(mutex_); - db_->GetLastChange(target); + std::list changes; + + { + boost::mutex::scoped_lock lock(mutex_); + db_->GetLastChange(changes); + } + + FormatChanges(target, changes, true, 0); return true; } + void ServerIndex::LogExportedResource(const std::string& publicId, const std::string& remoteModality) { diff -r 92c6b3b57699 -r 62c35e4b67db OrthancServer/ServerIndexChange.h --- a/OrthancServer/ServerIndexChange.h Fri Dec 05 16:22:18 2014 +0100 +++ b/OrthancServer/ServerIndexChange.h Fri Dec 05 17:12:35 2014 +0100 @@ -33,28 +33,57 @@ #pragma once #include "ServerEnumerations.h" +#include "../Core/Toolbox.h" #include +#include namespace Orthanc { struct ServerIndexChange { private: + int64_t seq_; ChangeType changeType_; ResourceType resourceType_; std::string publicId_; + std::string date_; public: ServerIndexChange(ChangeType changeType, ResourceType resourceType, - const std::string& publicId) : + const std::string& publicId) : + seq_(-1), changeType_(changeType), resourceType_(resourceType), - publicId_(publicId) + publicId_(publicId), + date_(Toolbox::GetNowIsoString()) { } + ServerIndexChange(int64_t seq, + ChangeType changeType, + ResourceType resourceType, + const std::string& publicId, + const std::string& date) : + seq_(seq), + changeType_(changeType), + resourceType_(resourceType), + publicId_(publicId), + date_(date) + { + } + + int64_t GetSeq() const + { + return seq_; + } + + void SetSeq(int64_t seq) + { + seq_ = seq; + } + ChangeType GetChangeType() const { return changeType_; @@ -69,5 +98,21 @@ { return publicId_; } + + const std::string& GetDate() const + { + return date_; + } + + void Format(Json::Value& item) const + { + item = Json::objectValue; + item["Seq"] = static_cast(seq_); + item["ChangeType"] = EnumerationToString(changeType_); + item["ResourceType"] = EnumerationToString(resourceType_); + item["ID"] = publicId_; + item["Path"] = GetBasePath(resourceType_, publicId_); + item["Date"] = date_; + } }; }