# HG changeset patch # User Sebastien Jodogne # Date 1417789996 -3600 # Node ID 0f3716b88af7b3e702b60ddb9357204886830607 # Parent 21d84e3acc0d8be11c8fc1d32939cdfcd69d359a cleaning diff -r 21d84e3acc0d -r 0f3716b88af7 OrthancServer/DatabaseWrapper.cpp --- a/OrthancServer/DatabaseWrapper.cpp Fri Dec 05 14:53:30 2014 +0100 +++ b/OrthancServer/DatabaseWrapper.cpp Fri Dec 05 15:33:16 2014 +0100 @@ -259,7 +259,8 @@ throw OrthancException(ErrorCode_InternalError); } - LogChange(id, changeType, type, publicId); + ServerIndexChange change(changeType, type, publicId); + LogChange(id, change); return id; } @@ -449,28 +450,6 @@ } - bool DatabaseWrapper::GetMetadataAsInteger(int& result, - int64_t id, - MetadataType type) - { - std::string s = GetMetadata(id, type, ""); - if (s.size() == 0) - { - return false; - } - - try - { - result = boost::lexical_cast(s); - return true; - } - catch (boost::bad_lexical_cast&) - { - return false; - } - } - - void DatabaseWrapper::AddAttachment(int64_t id, const FileInfo& attachment) @@ -1046,33 +1025,6 @@ } - uint64_t DatabaseWrapper::IncrementGlobalSequence(GlobalProperty property) - { - std::string oldValue; - - if (LookupGlobalProperty(oldValue, property)) - { - uint64_t oldNumber; - - try - { - oldNumber = boost::lexical_cast(oldValue); - SetGlobalProperty(property, boost::lexical_cast(oldNumber + 1)); - return oldNumber + 1; - } - catch (boost::bad_lexical_cast&) - { - throw OrthancException(ErrorCode_InternalError); - } - } - else - { - // Initialize the sequence at "1" - SetGlobalProperty(property, "1"); - return 1; - } - } - void DatabaseWrapper::ClearTable(const std::string& tableName) { diff -r 21d84e3acc0d -r 0f3716b88af7 OrthancServer/DatabaseWrapper.h --- a/OrthancServer/DatabaseWrapper.h Fri Dec 05 14:53:30 2014 +0100 +++ b/OrthancServer/DatabaseWrapper.h Fri Dec 05 15:33:16 2014 +0100 @@ -122,10 +122,6 @@ MetadataType type, const std::string& defaultValue); - bool GetMetadataAsInteger(int& result, - int64_t id, - MetadataType type); - void AddAttachment(int64_t id, const FileInfo& attachment); @@ -155,15 +151,6 @@ int64_t id); void LogChange(int64_t internalId, - ChangeType changeType, - ResourceType resourceType, - const std::string& publicId) - { - ServerIndexChange change(changeType, resourceType, publicId); - LogChange(internalId, change); - } - - void LogChange(int64_t internalId, const ServerIndexChange& change); void GetChanges(Json::Value& target, @@ -229,8 +216,6 @@ db_.FlushToDisk(); } - uint64_t IncrementGlobalSequence(GlobalProperty property); - void ClearTable(const std::string& tableName); bool IsExistingResource(int64_t internalId); diff -r 21d84e3acc0d -r 0f3716b88af7 OrthancServer/ServerIndex.cpp --- a/OrthancServer/ServerIndex.cpp Fri Dec 05 14:53:30 2014 +0100 +++ b/OrthancServer/ServerIndex.cpp Fri Dec 05 15:33:16 2014 +0100 @@ -418,6 +418,60 @@ } + + + bool ServerIndex::GetMetadataAsInteger(int& result, + int64_t id, + MetadataType type) + { + std::string s = db_->GetMetadata(id, type, ""); + if (s.size() == 0) + { + return false; + } + + try + { + result = boost::lexical_cast(s); + return true; + } + catch (boost::bad_lexical_cast&) + { + return false; + } + } + + + + uint64_t ServerIndex::IncrementGlobalSequenceInternal(GlobalProperty property) + { + std::string oldValue; + + if (db_->LookupGlobalProperty(oldValue, property)) + { + uint64_t oldNumber; + + try + { + oldNumber = boost::lexical_cast(oldValue); + db_->SetGlobalProperty(property, boost::lexical_cast(oldNumber + 1)); + return oldNumber + 1; + } + catch (boost::bad_lexical_cast&) + { + throw OrthancException(ErrorCode_InternalError); + } + } + else + { + // Initialize the sequence at "1" + db_->SetGlobalProperty(property, "1"); + return 1; + } + } + + + ServerIndex::ServerIndex(ServerContext& context, const std::string& dbPath) : done_(false), @@ -672,7 +726,7 @@ SeriesStatus seriesStatus = GetSeriesStatus(series); if (seriesStatus == SeriesStatus_Complete) { - db_->LogChange(series, ChangeType_CompletedSeries, ResourceType_Series, hasher.HashSeries()); + LogChange(series, ChangeType_CompletedSeries, ResourceType_Series, hasher.HashSeries()); } // Mark the parent resources of this instance as unstable @@ -883,7 +937,7 @@ result["Status"] = EnumerationToString(GetSeriesStatus(id)); int i; - if (db_->GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances)) + if (GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances)) result["ExpectedNumberOfInstances"] = i; else result["ExpectedNumberOfInstances"] = Json::nullValue; @@ -905,7 +959,7 @@ result["FileUuid"] = attachment.GetUuid(); int i; - if (db_->GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries)) + if (GetMetadataAsInteger(i, id, MetadataType_Instance_IndexInSeries)) result["IndexInSeries"] = i; else result["IndexInSeries"] = Json::nullValue; @@ -1446,7 +1500,7 @@ std::auto_ptr transaction(db_->StartTransaction()); transaction->Begin(); - uint64_t seq = db_->IncrementGlobalSequence(sequence); + uint64_t seq = IncrementGlobalSequenceInternal(sequence); transaction->Commit(); return seq; @@ -1468,7 +1522,7 @@ throw OrthancException(ErrorCode_UnknownResource); } - db_->LogChange(id, changeType, type, publicId); + LogChange(id, changeType, type, publicId); transaction->Commit(); } @@ -1668,15 +1722,15 @@ switch (payload.GetResourceType()) { case ResourceType_Patient: - that->db_->LogChange(id, ChangeType_StablePatient, ResourceType_Patient, payload.GetPublicId()); + that->LogChange(id, ChangeType_StablePatient, ResourceType_Patient, payload.GetPublicId()); break; case ResourceType_Study: - that->db_->LogChange(id, ChangeType_StableStudy, ResourceType_Study, payload.GetPublicId()); + that->LogChange(id, ChangeType_StableStudy, ResourceType_Study, payload.GetPublicId()); break; case ResourceType_Series: - that->db_->LogChange(id, ChangeType_StableSeries, ResourceType_Series, payload.GetPublicId()); + that->LogChange(id, ChangeType_StableSeries, ResourceType_Series, payload.GetPublicId()); break; default: @@ -1706,7 +1760,7 @@ unstableResources_.AddOrMakeMostRecent(id, payload); //LOG(INFO) << "Unstable resource: " << EnumerationToString(type) << " " << id; - db_->LogChange(id, ChangeType_NewChildInstance, type, publicId); + LogChange(id, ChangeType_NewChildInstance, type, publicId); } diff -r 21d84e3acc0d -r 0f3716b88af7 OrthancServer/ServerIndex.h --- a/OrthancServer/ServerIndex.h Fri Dec 05 14:53:30 2014 +0100 +++ b/OrthancServer/ServerIndex.h Fri Dec 05 15:33:16 2014 +0100 @@ -103,6 +103,22 @@ /* in */ int64_t id, /* in */ ResourceType type); + bool GetMetadataAsInteger(int& result, + int64_t id, + MetadataType type); + + void LogChange(int64_t internalId, + ChangeType changeType, + ResourceType resourceType, + const std::string& publicId) + { + ServerIndexChange change(changeType, resourceType, publicId); + db_->LogChange(internalId, change); + } + + uint64_t IncrementGlobalSequenceInternal(GlobalProperty property); + + public: ServerIndex(ServerContext& context, const std::string& dbPath); diff -r 21d84e3acc0d -r 0f3716b88af7 UnitTestsSources/ServerIndexTests.cpp --- a/UnitTestsSources/ServerIndexTests.cpp Fri Dec 05 14:53:30 2014 +0100 +++ b/UnitTestsSources/ServerIndexTests.cpp Fri Dec 05 15:33:16 2014 +0100 @@ -540,12 +540,20 @@ -TEST_P(DatabaseWrapperTest, Sequence) +TEST(ServerIndex, Sequence) { - ASSERT_EQ(1u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); - ASSERT_EQ(2u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); - ASSERT_EQ(3u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); - ASSERT_EQ(4u, index_->IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); + const std::string path = "UnitTestsStorage"; + + Toolbox::RemoveFile(path + "/index"); + FilesystemStorage storage(path); + ServerContext context(":memory:"); // The SQLite DB is in memory + context.SetStorageArea(storage); + ServerIndex& index = context.GetIndex(); + + ASSERT_EQ(1u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); + ASSERT_EQ(2u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); + ASSERT_EQ(3u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); + ASSERT_EQ(4u, index.IncrementGlobalSequence(GlobalProperty_AnonymizationSequence)); }