Skip to content

Commit 19b69dc

Browse files
authored
Merge pull request #54 from joshualicht/datatypes
CPP: Support basic types + Add Dir Parameter
2 parents 3e664ce + 62c88c5 commit 19b69dc

File tree

14 files changed

+1180
-868
lines changed

14 files changed

+1180
-868
lines changed

.github/workflows/check.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ jobs:
7070
run: |
7171
bazel coverage //:test_kvs_cpp \
7272
--collect_code_coverage \
73-
--instrument_test_targets \
7473
--combined_report=lcov \
7574
--experimental_generate_llvm_lcov \
7675
--nocache_test_results \

BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ alias(
5151

5252
test_suite(
5353
name = "test_kvs_cpp",
54-
tests = ["//tests/cpp:test_kvs_cpp"],
54+
tests = ["//src/cpp/tests:test_kvs_cpp"],
5555
visibility = ["//visibility:public"],
5656
)
5757

5858
test_suite(
5959
name = "bm_kvs_cpp",
60-
tests = ["//tests/cpp:bm_kvs_cpp"],
60+
tests = ["//src/cpp/tests:bm_kvs_cpp"],
6161
visibility = ["//visibility:public"],
6262
)

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ archive_override(
9595
bazel_dep(name = "score-baselibs", version = "0.0.0")
9696
git_override(
9797
module_name = "score-baselibs",
98-
commit = "f0a394a602986ddf7abac6a238b9d44535a4b597",
98+
commit = "ae349b99cafc1e79d98c0391a851fc5664c04ebc",
9999
remote = "https://github.com/eclipse-score/baselibs.git",
100100
)

src/cpp/BUILD

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,19 @@
1212
# *******************************************************************************
1313

1414
# The filegroup is used to collect all source files for the tests.
15-
filegroup(
16-
name = "kvs_srcs",
17-
srcs = glob([
18-
"src/*.cpp",
19-
"inc/**/*.hpp",
20-
]),
21-
visibility = [
22-
"//tests/cpp:__pkg__",
23-
],
24-
)
2515

2616
cc_library(
2717
name = "kvs_cpp",
28-
srcs = glob(["src/*.cpp"]),
29-
hdrs = glob([
30-
"inc/**/*.hpp",
31-
]),
18+
srcs = [
19+
"inc/internal/kvs_helper.hpp",
20+
"src/kvs.cpp",
21+
],
22+
hdrs = ["inc/kvs.hpp"],
3223
includes = ["inc"],
3324
visibility = ["//:__pkg__"],
3425
deps = [
3526
"@score-baselibs//score/filesystem:filesystem",
36-
"@score-baselibs//score/json:interface",
37-
"@score-baselibs//score/json:json_parser",
27+
"@score-baselibs//score/json",
3828
"@score-baselibs//score/result:result",
3929
],
4030
)

src/cpp/inc/internal/kvs_helper.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ uint32_t parse_hash_adler32(std::istream& in);
2525
uint32_t calculate_hash_adler32(const std::string& data);
2626
std::array<uint8_t,4> get_hash_bytes_adler32(uint32_t hash);
2727
std::array<uint8_t,4> get_hash_bytes(const std::string& data);
28-
score::Result<std::unordered_map<std::string, KvsValue>> parse_json_data(const std::string& data);
29-
score::Result<std::unordered_map<std::string, KvsValue>> open_json(const std::string& prefix, OpenJsonNeedFile need_file);
30-
score::ResultBlank write_json_data(const std::string& filename_prefix, const std::string& buf);
3128
score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any);
3229
score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv);
3330

src/cpp/inc/kvs.hpp

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ enum class MyErrorCode : score::result::ErrorCode {
9090
MutexLockFailed,
9191

9292
/* Invalid value type*/
93-
InvalidValueType
93+
InvalidValueType,
9494
};
9595

9696
class MyErrorDomain final : public score::result::ErrorDomain
@@ -180,7 +180,11 @@ class KvsValue final{
180180

181181
/* Enum to represent the type of the value*/
182182
enum class Type {
183-
Number,
183+
i32,
184+
u32,
185+
i64,
186+
u64,
187+
f64,
184188
Boolean,
185189
String,
186190
Null,
@@ -189,7 +193,11 @@ class KvsValue final{
189193
};
190194

191195
/* Constructors for each type*/
192-
explicit KvsValue(double number) : value(number), type(Type::Number) {}
196+
explicit KvsValue(int32_t number) : value(number), type(Type::i32) {}
197+
explicit KvsValue(uint32_t number) : value(number), type(Type::u32) {}
198+
explicit KvsValue(int64_t number) : value(number), type(Type::i64) {}
199+
explicit KvsValue(uint64_t number) : value(number), type(Type::u64) {}
200+
explicit KvsValue(double number) : value(number), type(Type::f64) {}
193201
explicit KvsValue(bool boolean) : value(boolean), type(Type::Boolean) {}
194202
explicit KvsValue(const std::string& str) : value(str), type(Type::String) {}
195203
explicit KvsValue(std::nullptr_t) : value(nullptr), type(Type::Null) {}
@@ -200,13 +208,13 @@ class KvsValue final{
200208
Type getType() const { return type; }
201209

202210
/* Access the underlying value (use std::get to retrieve the value)*/
203-
const std::variant<double, bool, std::string, std::nullptr_t, Array, Object>& getValue() const {
211+
const std::variant<int32_t, uint32_t, int64_t, uint64_t, double, bool, std::string, std::nullptr_t, Array, Object>& getValue() const {
204212
return value;
205213
}
206214

207215
private:
208216
/* The underlying value*/
209-
std::variant<double, bool, std::string, std::nullptr_t, Array, Object> value;
217+
std::variant<int32_t, uint32_t, int64_t, uint64_t, double, bool, std::string, std::nullptr_t, Array, Object> value;
210218

211219
/* The type of the value*/
212220
Type type;
@@ -234,15 +242,14 @@ class KvsValue final{
234242
* - `reset_key`: Resets a key to its default value if available.
235243
* - `has_default_value`: Checks if a default value exists for a specific key.
236244
* - `set_value`: Sets the value for a specific key in the KVS.
237-
* - `set_default_value`: Sets a default value for a specific key.
238245
* - `remove_key`: Removes a specific key from the KVS.
239246
* - `flush`: Flushes the KVS to storage.
240247
* - `flush_default`: Flushes the default values to storage.
241248
* - `snapshot_count`: Retrieves the number of available snapshots.
242249
* - `snapshot_max_count`: Retrieves the maximum number of snapshots allowed.
243250
* - `snapshot_restore`: Restores the KVS from a specified snapshot.
244251
* - `get_kvs_filename`: Retrieves the filename associated with a snapshot.
245-
* - `get_kvs_hash_filename`: Retrieves the hash filename associated with a snapshot.
252+
* - `get_hash_filename`: Retrieves the hash filename associated with a snapshot.
246253
*
247254
* Private Members:
248255
* - `kvs_mutex`: A mutex for ensuring thread safety.
@@ -264,7 +271,7 @@ class KvsValue final{
264271
*
265272
* int main() {
266273
* // Open kvs
267-
* auto open_res = KvsBuilder("Process_Name", 0)
274+
* auto open_res = KvsBuilder(0)
268275
* .need_defaults_flag(true)
269276
* .need_kvs_flag(true)
270277
* .build();
@@ -305,8 +312,6 @@ class Kvs final {
305312
* It allows the caller to specify whether default values and an existing KVS are required
306313
* or optional during the opening process.
307314
*
308-
* @param process_name The name of the process that is opening the KVS. This is used to create a unique directory for the KVS instance.
309-
* Important: It must be unique for each application to avoid conflicts.
310315
* @param id The instance ID of the KVS. This uniquely identifies the KVS instance.
311316
* @param need_defaults A flag of type OpenNeedDefaults indicating whether default values
312317
* are required or optional.
@@ -315,6 +320,8 @@ class Kvs final {
315320
* @param need_kvs A flag of type OpenNeedKvs indicating whether the KVS is required or optional.
316321
* - OpenNeedKvs::Required: The KVS must already exist.
317322
* - OpenNeedKvs::Optional: An empty KVS will be used if no KVS exists.
323+
* @param dir The directory path where the KVS files are located. It is passed as an rvalue reference to avoid unnecessary copying.
324+
* Use "" or "." for the current directory.
318325
* @return A Result object containing either:
319326
* - A Kvs object if the operation is successful.
320327
* - An ErrorCode if an error occurs during the operation.
@@ -326,7 +333,7 @@ class Kvs final {
326333
* - ErrorCode::ValidationFailed: Validation of the KVS data failed.
327334
* - ErrorCode::ResourceBusy: The KVS resource is currently in use.
328335
*/
329-
static score::Result<Kvs> open(const std::string&& process_name, const InstanceId& id, OpenNeedDefaults need_defaults, OpenNeedKvs need_kvs);
336+
static score::Result<Kvs> open(const InstanceId& instance_id, OpenNeedDefaults need_defaults, OpenNeedKvs need_kvs, const std::string&& dir);
330337

331338
/**
332339
* @brief Sets whether the key-value store should flush its contents to
@@ -468,9 +475,11 @@ class Kvs final {
468475
/**
469476
* @brief Retrieves the number of snapshots currently stored in the key-value store.
470477
*
471-
* @return The total count of snapshots as a size_t value.
478+
* @return A score::Result object that indicates the success or failure of the operation.
479+
* - On success: The total count of snapshots as a size_t value.
480+
* - On failure: Returns an ErrorCode describing the error.
472481
*/
473-
size_t snapshot_count() const;
482+
score::Result<size_t> snapshot_count() const;
474483

475484

476485
/**
@@ -503,9 +512,11 @@ class Kvs final {
503512
* @brief Retrieves the filename associated with a given snapshot ID in the key-value store.
504513
*
505514
* @param snapshot_id The identifier of the snapshot for which the filename is to be retrieved.
506-
* @return A String with the filename associated with the snapshot ID.
515+
* @return score::ResultBlank
516+
* - On success: A score::filesystem::Path with the filename (path) associated with the snapshot ID.
517+
* - On failure: An error code describing the reason for the failure.
507518
*/
508-
std::string get_kvs_filename(const SnapshotId& snapshot_id) const;
519+
score::Result<score::filesystem::Path> get_kvs_filename(const SnapshotId& snapshot_id) const;
509520

510521

511522
/**
@@ -516,9 +527,11 @@ class Kvs final {
516527
* store metadata or integrity information for the snapshot.
517528
*
518529
* @param snapshot_id The identifier of the snapshot for which the hash filename is requested.
519-
* @return A String with the filename of the hash file associated with the snapshot ID.
530+
* @return score::ResultBlank
531+
* - On success: A score::filesystem::Path with the filename (path) of the hash file associated with the snapshot ID.
532+
* - On failure: An error code describing the reason for the failure.
520533
*/
521-
std::string get_kvs_hash_filename(const SnapshotId& snapshot_id) const;
534+
score::Result<score::filesystem::Path> get_hash_filename(const SnapshotId& snapshot_id) const;
522535

523536
private:
524537
/* Private constructor to prevent direct instantiation */
@@ -535,11 +548,21 @@ class Kvs final {
535548
std::unordered_map<std::string, KvsValue> default_values;
536549

537550
/* Filename prefix */
538-
std::string filename_prefix;
551+
score::filesystem::Path filename_prefix;
539552

540553
/* Flush on exit flag for written Keys */
541554
std::atomic<bool> flush_on_exit;
542-
555+
556+
/* Filesystem handling */
557+
std::unique_ptr<score::filesystem::Filesystem> filesystem;
558+
559+
/* Json handling */
560+
std::unique_ptr<score::json::IJsonParser> parser;
561+
std::unique_ptr<score::json::IJsonWriter> writer;
562+
563+
score::Result<std::unordered_map<std::string, KvsValue>> parse_json_data(const std::string& data);
564+
score::Result<std::unordered_map<std::string, KvsValue>> open_json(const score::filesystem::Path& prefix, OpenJsonNeedFile need_file);
565+
score::ResultBlank write_json_data(const std::string& buf);
543566
};
544567

545568

@@ -553,7 +576,7 @@ class KvsBuilder final {
553576
* @brief Constructs a KvsBuilder for the given KVS instance.
554577
* @param instance_id Unique identifier for the KVS instance.
555578
*/
556-
explicit KvsBuilder(std::string&& process_name, const InstanceId& instance_id);
579+
explicit KvsBuilder(const InstanceId& instance_id);
557580

558581
/**
559582
* @brief Specify whether default values must be loaded.
@@ -569,20 +592,29 @@ class KvsBuilder final {
569592
*/
570593
KvsBuilder& need_kvs_flag(bool flag);
571594

595+
/**
596+
* @brief Specify the directory where KVS files are stored.
597+
* @param dir The directory path as a string.
598+
* Use "" or "." for the current directory.
599+
*
600+
* @return Reference to this builder (for chaining).
601+
*/
602+
KvsBuilder& dir(std::string&& dir_path);
603+
572604
/**
573605
* @brief Builds and opens the Kvs instance with the configured options.
574606
*
575607
* Internally calls Kvs::open() with the selected flags and directory.
576608
*
577609
* @return A score::Result<Kvs> containing the opened store or an ErrorCode.
578610
*/
579-
score::Result<Kvs> build() const;
611+
score::Result<Kvs> build();
580612

581613
private:
582614
InstanceId instance_id; ///< ID of the KVS instance
583615
bool need_defaults; ///< Whether default values are required
584616
bool need_kvs; ///< Whether an existing KVS is required
585-
std::string process_name; ///< Process name for the KVS files
617+
std::string directory; ///< Directory where to store the KVS Files
586618
};
587619

588620
#endif /* SCORE_LIB_KVS_KVS_HPP */

0 commit comments

Comments
 (0)