diff --git a/score/kvs/docs/detailed_design/class_diagram_cpp.puml b/score/kvs/docs/detailed_design/class_diagram_cpp.puml index 0cb83d52b..9e67f82bf 100644 --- a/score/kvs/docs/detailed_design/class_diagram_cpp.puml +++ b/score/kvs/docs/detailed_design/class_diagram_cpp.puml @@ -39,28 +39,28 @@ class KvsBuilder <> { interface IKvsBackend <> { + LoadKvs(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + LoadDefaults(instance_id : InstanceId) : Result - + Flush(instance_id : InstanceId, kvs : KvsMap) : ResultBlank + + Flush(instance_id : InstanceId, kvs : KvsMap) : Result + SnapshotCount(instance_id : InstanceId) : Result + SnapshotRestore(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + SnapshotMaxCount() : size_t } interface IKvs <> { - + Reset() : ResultBlank - + ResetKey(key : string) : ResultBlank + + Reset() : Result + + ResetKey(key : string) : Result + GetAllKeys() : Result> + KeyExists(key : string) : Result + GetValue(key : string) : Result + GetDefaultValue(key : string) : Result + IsValueDefault(key : string) : Result - + SetValue(key : string, value : KvsValue) : ResultBlank - + RemoveKey(key : string) : ResultBlank - + RemoveAllKeys() : ResultBlank - + DiscardPendingChanges() : ResultBlank - + Flush() : ResultBlank + + SetValue(key : string, value : KvsValue) : Result + + RemoveKey(key : string) : Result + + RemoveAllKeys() : Result + + DiscardPendingChanges() : Result + + Flush() : Result + SnapshotCount() : Result + SnapshotMaxCount() : size_t - + SnapshotRestore(snapshot_id : SnapshotId) : ResultBlank + + SnapshotRestore(snapshot_id : SnapshotId) : Result + GetStorageFileSize() : Result } @@ -72,7 +72,7 @@ KvsMock -[hidden]right- Kvs class MockBackend <> { + LoadKvs(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + LoadDefaults(instance_id : InstanceId) : Result - + Flush(instance_id : InstanceId, kvs : KvsMap) : ResultBlank + + Flush(instance_id : InstanceId, kvs : KvsMap) : Result + SnapshotCount(instance_id : InstanceId) : Result + SnapshotRestore(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + SnapshotMaxCount() : size_t @@ -90,7 +90,7 @@ class JsonBackend <> { + JsonBackend(instance_id : InstanceId, filename_prefix : path, filesystem : score::filesystem, directory : string, max_snapshot_count : size_t) + LoadKvs(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + LoadDefaults(instance_id : InstanceId) : Result - + Flush(instance_id : InstanceId, kvs : KvsMap) : ResultBlank + + Flush(instance_id : InstanceId, kvs : KvsMap) : Result + SnapshotCount(instance_id : InstanceId) : Result + SnapshotRestore(instance_id : InstanceId, snapshot_id : SnapshotId) : Result + SnapshotMaxCount() : size_t @@ -103,21 +103,21 @@ class JsonBackend <> { } class KvsMock <> { - + Reset() : ResultBlank - + ResetKey(key : string) : ResultBlank + + Reset() : Result + + ResetKey(key : string) : Result + GetAllKeys() : Result> + KeyExists(key : string) : Result + GetValue(key : string) : Result + GetDefaultValue(key : string) : Result + IsValueDefault(key : string) : Result - + SetValue(key : string, value : KvsValue) : ResultBlank - + RemoveKey(key : string) : ResultBlank - + RemoveAllKeys() : ResultBlank - + DiscardPendingChanges() : ResultBlank - + Flush() : ResultBlank + + SetValue(key : string, value : KvsValue) : Result + + RemoveKey(key : string) : Result + + RemoveAllKeys() : Result + + DiscardPendingChanges() : Result + + Flush() : Result + SnapshotCount() : Result + SnapshotMaxCount() : size_t - + SnapshotRestore(snapshot_id : SnapshotId) : ResultBlank + + SnapshotRestore(snapshot_id : SnapshotId) : Result + GetStorageFileSize() : Result } @@ -127,21 +127,21 @@ class Kvs <> { - logger : ptr - backend : IKvsBackend __ - + Reset() : ResultBlank - + ResetKey(key : string) : ResultBlank + + Reset() : Result + + ResetKey(key : string) : Result + GetAllKeys() : Result> + KeyExists(key : string) : Result + GetValue(key : string) : Result + GetDefaultValue(key : string) : Result + IsValueDefault(key : string) : Result - + SetValue(key : string, value : KvsValue) : ResultBlank - + RemoveKey(key : string) : ResultBlank - + RemoveAllKeys() : ResultBlank - + DiscardPendingChanges() : ResultBlank - + Flush() : ResultBlank + + SetValue(key : string, value : KvsValue) : Result + + RemoveKey(key : string) : Result + + RemoveAllKeys() : Result + + DiscardPendingChanges() : Result + + Flush() : Result + SnapshotCount() : Result + SnapshotMaxCount() : size_t - + SnapshotRestore(snapshot_id : SnapshotId) : ResultBlank + + SnapshotRestore(snapshot_id : SnapshotId) : Result + GetStorageFileSize() : Result } diff --git a/score/kvs/kvs.cpp b/score/kvs/kvs.cpp index a72c54647..e1b6f33f6 100644 --- a/score/kvs/kvs.cpp +++ b/score/kvs/kvs.cpp @@ -262,14 +262,14 @@ score::Result Kvs::open(const InstanceId& instance_id, } /* Reset KVS to initial state*/ -score::ResultBlank Kvs::reset() +score::Result Kvs::reset() { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { kvs.clear(); - result = score::ResultBlank{}; + result = score::Result{}; } else { @@ -373,9 +373,9 @@ score::Result Kvs::get_default_value(const std::string_view key) } /* Resets a Key to its default value (Deletes written key if default is available) */ -score::ResultBlank Kvs::reset_key(const std::string_view key) +score::Result Kvs::reset_key(const std::string_view key) { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock_kvs(kvs_mutex, std::try_to_lock); if (!lock_kvs.owns_lock()) { @@ -395,11 +395,11 @@ score::ResultBlank Kvs::reset_key(const std::string_view key) { (void)kvs.erase( std::string(key)); /* Return Value ignored, since its already secured, that the key exists*/ - result = score::ResultBlank{}; + result = score::Result{}; } else { - result = score::ResultBlank{}; + result = score::Result{}; } } } @@ -423,14 +423,14 @@ score::Result Kvs::is_value_default(const std::string_view key) const } /* Set the value for a key*/ -score::ResultBlank Kvs::set_value(const std::string_view key, const KvsValue& value) +score::Result Kvs::set_value(const std::string_view key, const KvsValue& value) { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { kvs.insert_or_assign(std::string(key), value); - result = score::ResultBlank{}; + result = score::Result{}; } else { @@ -441,16 +441,16 @@ score::ResultBlank Kvs::set_value(const std::string_view key, const KvsValue& va } /* Remove a key-value pair*/ -score::ResultBlank Kvs::remove_key(const std::string_view key) +score::Result Kvs::remove_key(const std::string_view key) { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { const auto erased = kvs.erase(std::string(key)); if (erased > 0U) { - result = score::ResultBlank{}; + result = score::Result{}; } else { @@ -465,14 +465,14 @@ score::ResultBlank Kvs::remove_key(const std::string_view key) return result; } -score::ResultBlank Kvs::remove_all_keys() +score::Result Kvs::remove_all_keys() { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { kvs.clear(); - result = score::ResultBlank{}; + result = score::Result{}; } else { @@ -483,9 +483,9 @@ score::ResultBlank Kvs::remove_all_keys() } /* Drop all in-memory changes by reloading the persisted KVS file */ -score::ResultBlank Kvs::discard_pending_changes() +score::Result Kvs::discard_pending_changes() { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { @@ -501,7 +501,7 @@ score::ResultBlank Kvs::discard_pending_changes() else { kvs = std::move(data_res.value()); - result = score::ResultBlank{}; + result = score::Result{}; } } else @@ -513,7 +513,7 @@ score::ResultBlank Kvs::discard_pending_changes() } /* Helper: write data to a file and ensure it reaches physical storage.*/ -score::ResultBlank Kvs::write_and_sync(const std::string& path, const void* data, std::size_t size) +score::Result Kvs::write_and_sync(const std::string& path, const void* data, std::size_t size) { auto file_deleter = [](std::FILE* f) { if (f != nullptr) @@ -553,9 +553,9 @@ score::ResultBlank Kvs::write_and_sync(const std::string& path, const void* data } /* Helper Function to write JSON data to a file for flush process (also adds Hash file)*/ -score::ResultBlank Kvs::write_json_data(const std::string& buf) +score::Result Kvs::write_json_data(const std::string& buf) { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); score::filesystem::Path json_path{filename_prefix.Native() + "_0.json"}; score::filesystem::Path dir = json_path.ParentPath(); if (!dir.Empty()) @@ -591,9 +591,9 @@ score::ResultBlank Kvs::write_json_data(const std::string& buf) } /* Flush the key-value store*/ -score::ResultBlank Kvs::flush() +score::Result Kvs::flush() { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); /* Create JSON Object */ score::json::Object root_obj; bool error = false; @@ -695,9 +695,9 @@ size_t Kvs::snapshot_max_count() const } /* Rotate Snapshots */ -score::ResultBlank Kvs::snapshot_rotate() +score::Result Kvs::snapshot_rotate() { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { @@ -744,7 +744,7 @@ score::ResultBlank Kvs::snapshot_rotate() } if (!error) { - result = score::ResultBlank{}; + result = score::Result{}; } } else @@ -756,9 +756,9 @@ score::ResultBlank Kvs::snapshot_rotate() } /* Restore the key-value store from a snapshot*/ -score::ResultBlank Kvs::snapshot_restore(const SnapshotId& snapshot_id) +score::Result Kvs::snapshot_restore(const SnapshotId& snapshot_id) { - score::ResultBlank result = score::MakeUnexpected(ErrorCode::UnmappedError); + score::Result result = score::MakeUnexpected(ErrorCode::UnmappedError); std::unique_lock lock(kvs_mutex, std::try_to_lock); if (lock.owns_lock()) { @@ -789,7 +789,7 @@ score::ResultBlank Kvs::snapshot_restore(const SnapshotId& snapshot_id) else { kvs = std::move(data_res.value()); - result = score::ResultBlank{}; + result = score::Result{}; } } } diff --git a/score/kvs/kvs.hpp b/score/kvs/kvs.hpp index 224574818..099096197 100644 --- a/score/kvs/kvs.hpp +++ b/score/kvs/kvs.hpp @@ -129,8 +129,7 @@ enum class OpenJsonNeedFile * - `writer`: A unique pointer to a JSON writer for writing KVS data. * * ----------------Notice---------------- - * - Blank should be used instead of void for Result class - * Refer: "Blank and score::ResultBlank shall be used for `T` instead of `void`" in result.h + * - `void` should be used instead of the deprecated blank Result alias. * A KVS Object is not copyable, but it can be moved. * */ @@ -181,7 +180,7 @@ class Kvs final * @brief Resets a key-value-storage to its initial state * */ - score::ResultBlank reset(); + score::Result reset(); /** * @brief Retrieves all keys stored in the key-value store. @@ -246,7 +245,7 @@ class Kvs final * - On success: Returns a blank score::Result. * - On failure: Returns an ErrorCode describing the error. */ - score::ResultBlank reset_key(const std::string_view key); + score::Result reset_key(const std::string_view key); /** * @brief Checks if the specified key wasn't set yet and uses its default value. @@ -273,7 +272,7 @@ class Kvs final * - On success: Returns a blank score::Result. * - On failure: Returns an ErrorCode describing the error. */ - score::ResultBlank set_value(const std::string_view key, const KvsValue& value); + score::Result set_value(const std::string_view key, const KvsValue& value); /** * @brief Removes a key-value pair from the store based on the specified key. @@ -284,7 +283,7 @@ class Kvs final * - On success: Returns a blank score::Result. * - On failure: Returns an ErrorCode describing the error. */ - score::ResultBlank remove_key(const std::string_view key); + score::Result remove_key(const std::string_view key); /** * @brief Removes all key-value pairs from the store. @@ -293,7 +292,7 @@ class Kvs final * - On success: Returns a blank score::Result. * - On failure: Returns an ErrorCode describing the error. */ - score::ResultBlank remove_all_keys(); + score::Result remove_all_keys(); /** * @brief Discards all pending changes to the key-value store. @@ -310,7 +309,7 @@ class Kvs final * is re-read, this includes storage errors such as `KvsHashFileReadError`, * `ValidationFailed` and `JsonParserError`. */ - score::ResultBlank discard_pending_changes(); + score::Result discard_pending_changes(); /** * @brief Flushes the key-value store, ensuring that all pending changes @@ -320,7 +319,7 @@ class Kvs final * - On success: Returns a blank score::Result. * - On failure: Returns an ErrorCode describing the error. */ - score::ResultBlank flush(); + score::Result flush(); /** * @brief Retrieves the number of snapshots currently stored in the key-value store. @@ -349,17 +348,17 @@ class Kvs final * restoration process fails, an appropriate error code is returned. * * @param snapshot_id The identifier of the snapshot to restore from. - * @return score::ResultBlank + * @return score::Result * - On success: An empty score::Result indicating the restoration was successful. * - On failure: An error code describing the reason for the failure. */ - score::ResultBlank snapshot_restore(const SnapshotId& snapshot_id); + score::Result snapshot_restore(const SnapshotId& snapshot_id); /** * @brief Retrieves the filename associated with a given snapshot ID in the key-value store. * * @param snapshot_id The identifier of the snapshot for which the filename is to be retrieved. - * @return score::ResultBlank + * @return score::Result * - On success: A score::filesystem::Path with the filename (path) associated with the * snapshot ID. * - On failure: An error code describing the reason for the failure. @@ -374,7 +373,7 @@ class Kvs final * store metadata or integrity information for the snapshot. * * @param snapshot_id The identifier of the snapshot for which the hash filename is requested. - * @return score::ResultBlank + * @return score::Result * - On success: A score::filesystem::Path with the filename (path) of the hash file * associated with the snapshot ID. * - On failure: An error code describing the reason for the failure. @@ -423,12 +422,12 @@ class Kvs final std::unique_ptr logger; /* Private Methods */ - score::ResultBlank snapshot_rotate(); + score::Result snapshot_rotate(); score::Result> parse_json_data(const std::string& data); score::Result> open_json(const score::filesystem::Path& prefix, OpenJsonNeedFile need_file); - score::ResultBlank write_json_data(const std::string& buf); - score::ResultBlank write_and_sync(const std::string& path, const void* data, std::size_t size); + score::Result write_json_data(const std::string& buf); + score::Result write_and_sync(const std::string& path, const void* data, std::size_t size); }; } /* namespace score::mw::per::kvs */ diff --git a/score/kvs/tests/test_kvs.cpp b/score/kvs/tests/test_kvs.cpp index 3a6c4644e..c1db1aa11 100644 --- a/score/kvs/tests/test_kvs.cpp +++ b/score/kvs/tests/test_kvs.cpp @@ -666,7 +666,7 @@ TEST(kvs_write_json_data, write_json_data_filesystem_failure) ASSERT_NE(standard_mock, nullptr); EXPECT_CALL(*standard_mock, CreateDirectories(::testing::_)) .WillOnce(::testing::Return( - score::ResultBlank(score::MakeUnexpected(score::filesystem::ErrorCode::kCouldNotCreateDirectory)))); + score::Result(score::MakeUnexpected(score::filesystem::ErrorCode::kCouldNotCreateDirectory)))); kvs.value().filesystem = std::make_unique(std::move(mock_filesystem)); auto result = kvs->write_json_data(kvs_json);