Mercurial > hg > orthanc
changeset 1240:62c35e4b67db
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Dec 2014 17:12:35 +0100 |
parents | 92c6b3b57699 |
children | 90d2f320862d |
files | OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndexChange.h |
diffstat | 4 files changed, 108 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- 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<ServerIndexChange>& 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<ChangeType>(s.ColumnInt(1)); - int64_t internalId = s.ColumnInt64(2); ResourceType resourceType = static_cast<ResourceType>(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<int>(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<int>(last); + done = !(target.size() == maxResults && s.Step()); } - void DatabaseWrapper::GetChanges(Json::Value& target, + void DatabaseWrapper::GetChanges(std::list<ServerIndexChange>& 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<ServerIndexChange>& 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); }
--- 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<ServerIndexChange>& 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<ServerIndexChange>& target /* out */, + bool& done /* out */, int64_t since, unsigned int maxResults); - void GetLastChange(Json::Value& target); + void GetLastChange(std::list<ServerIndexChange>& target /* out */); void LogExportedResource(ResourceType resourceType, const std::string& publicId,
--- 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<ServerIndexChange>& changes, + bool done, + int64_t since) + { + Json::Value items = Json::arrayValue; + for (std::list<ServerIndexChange>::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<int>(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<ServerIndexChange> 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<ServerIndexChange> 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) {
--- 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 <string> +#include <json/value.h> 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<int>(seq_); + item["ChangeType"] = EnumerationToString(changeType_); + item["ResourceType"] = EnumerationToString(resourceType_); + item["ID"] = publicId_; + item["Path"] = GetBasePath(resourceType_, publicId_); + item["Date"] = date_; + } }; }