Skip to content

Commit 8603d18

Browse files
committed
cpp impl: Add basic datatype support + dir parameter
Implement datatypes and add a DIR parameter to align with Rust (+updated Unittests)
1 parent 19ae62c commit 8603d18

File tree

5 files changed

+750
-294
lines changed

5 files changed

+750
-294
lines changed

src/cpp/inc/kvs.hpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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,7 +242,6 @@ 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.
@@ -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+
* Important: It needs to end with "/" to be a valid directory path.
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
@@ -553,7 +560,7 @@ class KvsBuilder final {
553560
* @brief Constructs a KvsBuilder for the given KVS instance.
554561
* @param instance_id Unique identifier for the KVS instance.
555562
*/
556-
explicit KvsBuilder(std::string&& process_name, const InstanceId& instance_id);
563+
explicit KvsBuilder(const InstanceId& instance_id);
557564

558565
/**
559566
* @brief Specify whether default values must be loaded.
@@ -569,6 +576,13 @@ class KvsBuilder final {
569576
*/
570577
KvsBuilder& need_kvs_flag(bool flag);
571578

579+
/**
580+
* @brief Specify the directory where KVS files are stored.
581+
* @param dir The directory path as a string.
582+
* @return Reference to this builder (for chaining).
583+
*/
584+
KvsBuilder& dir(std::string&& dir_path);
585+
572586
/**
573587
* @brief Builds and opens the Kvs instance with the configured options.
574588
*
@@ -582,7 +596,7 @@ class KvsBuilder final {
582596
InstanceId instance_id; ///< ID of the KVS instance
583597
bool need_defaults; ///< Whether default values are required
584598
bool need_kvs; ///< Whether an existing KVS is required
585-
std::string process_name; ///< Process name for the KVS files
599+
std::string directory; ///< Directory where to store the KVS Files
586600
};
587601

588602
#endif /* SCORE_LIB_KVS_KVS_HPP */

0 commit comments

Comments
 (0)