Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions score/kvs/kvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "score/kvs/kvs.h"

#include "score/assert.hpp"
#include "score/kvs/internal/kvs_helper.h"
#include "score/os/errno.h"
#include "score/os/fcntl.h"
Expand Down Expand Up @@ -136,14 +137,21 @@ score::Result<Kvs> Kvs::Open(const InstanceId& instance_id,
const KvsLoad& kvs_load,
const std::string& working_dir,
const std::optional<size_t>& max_storage_bytes,
const std::size_t snapshot_max_count) noexcept
const std::size_t snapshot_max_count,
std::unique_ptr<score::filesystem::Filesystem> filesystem,
std::unique_ptr<score::json::IJsonParser> parser,
std::unique_ptr<score::json::IJsonWriter> writer) noexcept
{
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(filesystem != nullptr, "Kvs::Open: filesystem must not be null");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you mandate to have them as mandatory args .

why not makeing them optional parameters with defult value = nullptr then in testing scenario we can pass them but in normal use cases not passing them to open function ?

SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(parser != nullptr, "Kvs::Open: parser must not be null");
SCORE_LANGUAGE_FUTURECPP_PRECONDITION_PRD_MESSAGE(writer != nullptr, "Kvs::Open: writer must not be null");

const score::filesystem::Path base_path{working_dir};
const score::filesystem::Path filename_prefix{base_path / ("kvs_" + std::to_string(instance_id.id))};
const score::filesystem::Path filename_default{filename_prefix.Native() + std::string{kDefaultSuffix}};
const score::filesystem::Path filename_kvs{filename_prefix.Native() + "_" + std::to_string(snapshot_id.id)};

Kvs kvs;
Kvs kvs{std::move(filesystem), std::move(parser), std::move(writer)};
kvs.max_storage_bytes_ = max_storage_bytes;
score::Result<KeyValueMap> default_res{};

Expand Down Expand Up @@ -532,12 +540,13 @@ score::Result<size_t> Kvs::CalculatePotentialSize() const noexcept
return current_size_res.value() + buf.size() + kHashFileSize;
}

Kvs::Kvs() noexcept
Kvs::Kvs(std::unique_ptr<score::filesystem::Filesystem> filesystem,
std::unique_ptr<score::json::IJsonParser> parser,
std::unique_ptr<score::json::IJsonWriter> writer) noexcept
: max_snapshots_{kDefaultMaxSnapshots},
filesystem_{
std::make_unique<score::filesystem::Filesystem>(score::filesystem::FilesystemFactory{}.CreateInstance())},
parser_{std::make_unique<score::json::JsonParser>()},
writer_{std::make_unique<score::json::JsonWriter>()},
filesystem_{std::move(filesystem)},
parser_{std::move(parser)},
writer_{std::move(writer)},
logger_{std::make_unique<score::mw::log::Logger>("SKVS")}
{
}
Expand Down
27 changes: 23 additions & 4 deletions score/kvs/kvs.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,22 @@ class Kvs final
/// Values above kMaxSnapshotsUpperBound are clamped to it (a warning is logged).
/// A value of 0 disables snapshot operations.
/// The current KVS data is still persisted by Flush().
/// @param filesystem Filesystem handler for file operations.
/// @param parser JSON parser for reading KVS data.
/// @param writer JSON writer for writing KVS data.
/// @pre `filesystem`, `parser` and `writer` are not null.
/// A null pointer aborts through the S-CORE assertion handler.
/// @return The opened Kvs object on success, or an ErrorCode on failure.
static score::Result<Kvs> Open(const InstanceId& instance_id,
const SnapshotId& snapshot_id,
const KvsDefaults& defaults,
const KvsLoad& kvs_load,
const std::string& working_dir,
const std::optional<size_t>& max_storage_bytes,
const std::size_t snapshot_max_count) noexcept;
const std::size_t snapshot_max_count,
std::unique_ptr<score::filesystem::Filesystem> filesystem,
std::unique_ptr<score::json::IJsonParser> parser,
std::unique_ptr<score::json::IJsonWriter> writer) noexcept;

/// @brief Resets the key-value store to its initial state.
/// @return Nothing on success, or an ErrorCode on failure.
Expand Down Expand Up @@ -341,7 +349,16 @@ class Kvs final
const std::size_t snapshot_max_count) noexcept
{
return Kvs::Open(
instance_id, snapshot_id, defaults, kvs_load, working_dir, max_storage_bytes, snapshot_max_count);
instance_id,
snapshot_id,
defaults,
kvs_load,
working_dir,
max_storage_bytes,
snapshot_max_count,
std::make_unique<score::filesystem::Filesystem>(score::filesystem::FilesystemFactory{}.CreateInstance()),
std::make_unique<score::json::JsonParser>(),
std::make_unique<score::json::JsonWriter>());
}

/// @deprecated Use `Reset()` instead.
Expand Down Expand Up @@ -471,10 +488,12 @@ class Kvs final
}

private:
/// @brief Constructs an empty KVS.
/// @brief Constructs an empty KVS with the given filesystem, JSON parser and JSON writer.
///
/// Private to prevent direct instantiation, use Open() instead.
Kvs() noexcept;
Kvs(std::unique_ptr<score::filesystem::Filesystem> filesystem,
std::unique_ptr<score::json::IJsonParser> parser,
std::unique_ptr<score::json::IJsonWriter> writer) noexcept;

/// @brief Rotates the snapshots, ensuring that the maximum count is maintained.
score::Result<void> SnapshotRotate() const noexcept;
Expand Down
11 changes: 10 additions & 1 deletion score/kvs/kvs_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,16 @@ score::Result<Kvs> KvsBuilder::Build() const noexcept
const std::string working_dir = working_dir_.empty() ? std::string{"./"} : working_dir_;

return Kvs::Open(
instance_id_, snapshot_id_, defaults_, kvs_load_, working_dir, maximum_storage_bytes_, max_snapshots_);
instance_id_,
snapshot_id_,
defaults_,
kvs_load_,
working_dir,
maximum_storage_bytes_,
max_snapshots_,
std::make_unique<score::filesystem::Filesystem>(score::filesystem::FilesystemFactory{}.CreateInstance()),
std::make_unique<score::json::JsonParser>(),
std::make_unique<score::json::JsonWriter>());
}

} // namespace score::kvs
26 changes: 12 additions & 14 deletions score/kvs/kvs_deprecated_api_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ DISABLE_WARNING_POP

score::Result<Kvs> OpenKvs()
{
return Kvs::Open(kInstanceId,
kSnapshotId,
KvsDefaults::kOptional,
KvsLoad::kOptional,
std::string(kDataDir),
std::nullopt,
kDefaultMaxSnapshots);
return Kvs::Open(
kInstanceId,
kSnapshotId,
KvsDefaults::kOptional,
KvsLoad::kOptional,
std::string(kDataDir),
std::nullopt,
kDefaultMaxSnapshots,
std::make_unique<score::filesystem::Filesystem>(score::filesystem::FilesystemFactory{}.CreateInstance()),
std::make_unique<score::json::JsonParser>(),
std::make_unique<score::json::JsonWriter>());
}

void ExpectSame(const score::Result<void>& old_result, const score::Result<void>& new_result)
Expand Down Expand Up @@ -141,13 +145,7 @@ TEST(kvs_deprecated_api, kvs_open)
std::nullopt,
kDefaultMaxSnapshots)};
DISABLE_WARNING_POP
auto new_result{Kvs::Open(kInstanceId,
kSnapshotId,
KvsDefaults::kOptional,
KvsLoad::kOptional,
std::string(kDataDir),
std::nullopt,
kDefaultMaxSnapshots)};
auto new_result{OpenKvs()};
ASSERT_TRUE(result);
ASSERT_TRUE(new_result);
EXPECT_EQ(result.value().GetAllKeys().value(), new_result.value().GetAllKeys().value());
Expand Down
Loading
Loading