Skip to content

Commit 534fe61

Browse files
committed
cpp impl: update json datatype format
Update json datatype format + unittests + prepare json mock library usage
1 parent b23c691 commit 534fe61

File tree

4 files changed

+205
-175
lines changed

4 files changed

+205
-175
lines changed

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,14 @@ class Kvs final {
549549

550550
/* Flush on exit flag for written Keys */
551551
std::atomic<bool> flush_on_exit;
552-
552+
553+
/* Json handling */
554+
std::unique_ptr<score::json::JsonParser> parser;
555+
std::unique_ptr<score::json::JsonWriter> writer;
556+
557+
score::Result<std::unordered_map<std::string, KvsValue>> parse_json_data(const std::string& data);
558+
score::Result<std::unordered_map<std::string, KvsValue>> open_json(const std::string& prefix, OpenJsonNeedFile need_file);
559+
score::ResultBlank write_json_data(const std::string& filename_prefix, const std::string& buf);
553560
};
554561

555562

src/cpp/src/kvs.cpp

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -106,78 +106,78 @@ score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any){
106106
score::Result<KvsValue> result = score::MakeUnexpected(MyErrorCode::UnmappedError);
107107
if (auto o = any.As<score::json::Object>(); o.has_value()) {
108108
const auto& objAny = o.value().get();
109-
auto type = objAny.find("type");
110-
auto value = objAny.find("value");
109+
auto type = objAny.find("t");
110+
auto value = objAny.find("v");
111111
if (type != objAny.end() && value != objAny.end()) {
112112
if (auto typeStr = type->second.As<std::string>(); typeStr.has_value()) {
113113
const std::string_view typeStrV = typeStr.value().get();
114114
const score::json::Any& valueAny = value->second;
115115

116-
if (typeStrV == "I32") {
116+
if (typeStrV == "i32") {
117117
if (auto n = valueAny.As<int32_t>(); n.has_value()){
118118
result = KvsValue(static_cast<int32_t>(n.value()));
119119
}
120120
else {
121121
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
122122
}
123123
}
124-
else if (typeStrV == "U32") {
124+
else if (typeStrV == "u32") {
125125
if (auto n = valueAny.As<uint32_t>(); n.has_value()) {
126126
result = KvsValue(static_cast<uint32_t>(n.value()));
127127
}
128128
else {
129129
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
130130
}
131131
}
132-
else if (typeStrV == "I64") {
132+
else if (typeStrV == "i64") {
133133
if (auto n = valueAny.As<int64_t>(); n.has_value()) {
134134
result = KvsValue(static_cast<int64_t>(n.value()));
135135
}
136136
else {
137137
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
138138
}
139139
}
140-
else if (typeStrV == "U64") {
140+
else if (typeStrV == "u64") {
141141
if (auto n = valueAny.As<uint64_t>(); n.has_value()) {
142142
result = KvsValue(static_cast<uint64_t>(n.value()));
143143
}
144144
else {
145145
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
146146
}
147147
}
148-
else if (typeStrV == "F64") {
148+
else if (typeStrV == "f64") {
149149
if (auto n = valueAny.As<double>(); n.has_value()) {
150150
result = KvsValue(n.value());
151151
}
152152
else {
153153
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
154154
}
155155
}
156-
else if (typeStrV == "Boolean") {
156+
else if (typeStrV == "bool") {
157157
if (auto b = valueAny.As<bool>(); b.has_value()) {
158158
result = KvsValue(b.value());
159159
}
160160
else {
161161
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
162162
}
163163
}
164-
else if (typeStrV == "String") {
164+
else if (typeStrV == "str") {
165165
if (auto s = valueAny.As<std::string>(); s.has_value()) {
166166
result = KvsValue(s.value().get());
167167
}
168168
else {
169169
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
170170
}
171171
}
172-
else if (typeStrV == "Null") {
172+
else if (typeStrV == "null") {
173173
if (valueAny.As<score::json::Null>().has_value()) {
174174
result = KvsValue(nullptr);
175175
}
176176
else {
177177
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
178178
}
179179
}
180-
else if (typeStrV == "Array") {
180+
else if (typeStrV == "arr") {
181181
if (auto l = valueAny.As<score::json::List>(); l.has_value()) {
182182
KvsValue::Array arr;
183183
bool error = false;
@@ -197,7 +197,7 @@ score::Result<KvsValue> any_to_kvsvalue(const score::json::Any& any){
197197
result = score::MakeUnexpected(MyErrorCode::InvalidValueType);
198198
}
199199
}
200-
else if (typeStrV == "Object") {
200+
else if (typeStrV == "obj") {
201201
if (auto obj = valueAny.As<score::json::Object>(); obj.has_value()) {
202202
KvsValue::Object map;
203203
bool error = false;
@@ -239,47 +239,47 @@ score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv) {
239239
score::json::Object obj;
240240
switch (kv.getType()) {
241241
case KvsValue::Type::I32: {
242-
obj.emplace("type", score::json::Any(std::string("I32")));
243-
obj.emplace("value", score::json::Any(static_cast<int32_t>(std::get<int32_t>(kv.getValue()))));
242+
obj.emplace("t", score::json::Any(std::string("i32")));
243+
obj.emplace("v", score::json::Any(static_cast<int32_t>(std::get<int32_t>(kv.getValue()))));
244244
break;
245245
}
246246
case KvsValue::Type::U32: {
247-
obj.emplace("type", score::json::Any(std::string("U32")));
248-
obj.emplace("value", score::json::Any(static_cast<uint32_t>(std::get<uint32_t>(kv.getValue()))));
247+
obj.emplace("t", score::json::Any(std::string("u32")));
248+
obj.emplace("v", score::json::Any(static_cast<uint32_t>(std::get<uint32_t>(kv.getValue()))));
249249
break;
250250
}
251251
case KvsValue::Type::I64: {
252-
obj.emplace("type", score::json::Any(std::string("I64")));
253-
obj.emplace("value", score::json::Any(static_cast<int64_t>(std::get<int64_t>(kv.getValue()))));
252+
obj.emplace("t", score::json::Any(std::string("i64")));
253+
obj.emplace("v", score::json::Any(static_cast<int64_t>(std::get<int64_t>(kv.getValue()))));
254254
break;
255255
}
256256
case KvsValue::Type::U64: {
257-
obj.emplace("type", score::json::Any(std::string("U64")));
258-
obj.emplace("value", score::json::Any(static_cast<uint64_t>(std::get<uint64_t>(kv.getValue()))));
257+
obj.emplace("t", score::json::Any(std::string("u64")));
258+
obj.emplace("v", score::json::Any(static_cast<uint64_t>(std::get<uint64_t>(kv.getValue()))));
259259
break;
260260
}
261261
case KvsValue::Type::F64: {
262-
obj.emplace("type", score::json::Any(std::string("F64")));
263-
obj.emplace("value", score::json::Any(std::get<double>(kv.getValue())));
262+
obj.emplace("t", score::json::Any(std::string("f64")));
263+
obj.emplace("v", score::json::Any(std::get<double>(kv.getValue())));
264264
break;
265265
}
266266
case KvsValue::Type::Boolean: {
267-
obj.emplace("type", score::json::Any(std::string("Boolean")));
268-
obj.emplace("value", score::json::Any(std::get<bool>(kv.getValue())));
267+
obj.emplace("t", score::json::Any(std::string("bool")));
268+
obj.emplace("v", score::json::Any(std::get<bool>(kv.getValue())));
269269
break;
270270
}
271271
case KvsValue::Type::String: {
272-
obj.emplace("type", score::json::Any(std::string("String")));
273-
obj.emplace("value", score::json::Any(std::get<std::string>(kv.getValue())));
272+
obj.emplace("t", score::json::Any(std::string("str")));
273+
obj.emplace("v", score::json::Any(std::get<std::string>(kv.getValue())));
274274
break;
275275
}
276276
case KvsValue::Type::Null: {
277-
obj.emplace("type", score::json::Any(std::string("Null")));
278-
obj.emplace("value", score::json::Any(score::json::Null{}));
277+
obj.emplace("t", score::json::Any(std::string("null")));
278+
obj.emplace("v", score::json::Any(score::json::Null{}));
279279
break;
280280
}
281281
case KvsValue::Type::Array: {
282-
obj.emplace("type", score::json::Any(std::string("Array")));
282+
obj.emplace("t", score::json::Any(std::string("arr")));
283283
score::json::List list;
284284
for (auto& elem : std::get<KvsValue::Array>(kv.getValue())) {
285285
auto conv = kvsvalue_to_any(elem);
@@ -291,12 +291,12 @@ score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv) {
291291
list.push_back(std::move(conv.value()));
292292
}
293293
if (!error) {
294-
obj.emplace("value", score::json::Any(std::move(list)));
294+
obj.emplace("v", score::json::Any(std::move(list)));
295295
}
296296
break;
297297
}
298298
case KvsValue::Type::Object: {
299-
obj.emplace("type", score::json::Any(std::string("Object")));
299+
obj.emplace("t", score::json::Any(std::string("obj")));
300300
score::json::Object inner_obj;
301301
for (auto& [key, value] : std::get<KvsValue::Object>(kv.getValue())) {
302302
auto conv = kvsvalue_to_any(value);
@@ -308,7 +308,7 @@ score::Result<score::json::Any> kvsvalue_to_any(const KvsValue& kv) {
308308
inner_obj.emplace(key, std::move(conv.value()));
309309
}
310310
if (!error) {
311-
obj.emplace("value", score::json::Any(std::move(inner_obj)));
311+
obj.emplace("v", score::json::Any(std::move(inner_obj)));
312312
}
313313
break;
314314
}
@@ -396,6 +396,9 @@ std::string_view MyErrorDomain::MessageFor(score::result::ErrorCode const& code)
396396
case MyErrorCode::InvalidValueType:
397397
msg = "Invalid value type";
398398
break;
399+
case MyErrorCode::InvalidArgument:
400+
msg = "Invalid argument";
401+
break;
399402
default:
400403
msg = "Unknown Error!";
401404
break;
@@ -462,12 +465,16 @@ Kvs::~Kvs(){
462465

463466
Kvs::Kvs()
464467
: flush_on_exit(false)
468+
, parser(std::make_unique<score::json::JsonParser>())
469+
, writer(std::make_unique<score::json::JsonWriter>())
465470
{
466471
}
467472

468473
Kvs::Kvs(Kvs&& other) noexcept
469474
: filename_prefix(std::move(other.filename_prefix))
470475
, flush_on_exit(other.flush_on_exit.load(std::memory_order_relaxed))
476+
, parser(std::move(other.parser)) /* Not absolutely necessary, because a new JSON writer/parser object would also be okay*/
477+
, writer(std::move(other.writer))
471478
{
472479
{
473480
std::lock_guard<std::mutex> lock(other.kvs_mutex);
@@ -501,16 +508,20 @@ Kvs& Kvs::operator=(Kvs&& other) noexcept
501508
kvs = std::move(other.kvs);
502509
}
503510
default_values = std::move(other.default_values);
511+
512+
/* Transfer ownership of JSON parser and writer
513+
Not absolutely necessary, because a new JSON writer/parser object would also be okay*/
514+
parser = std::move(other.parser);
515+
writer = std::move(other.writer);
504516
}
505517
return *this;
506518
}
507519

508520
/* Helper Function to parse JSON data for open_json*/
509-
score::Result<std::unordered_map<std::string, KvsValue>> parse_json_data(const std::string& data) {
521+
score::Result<std::unordered_map<std::string, KvsValue>> Kvs::parse_json_data(const std::string& data) {
510522

511523
score::Result<unordered_map<std::string, KvsValue>> result = score::MakeUnexpected(MyErrorCode::UnmappedError);
512-
score::json::JsonParser parser;
513-
auto any_res = parser.FromBuffer(data);
524+
auto any_res = parser->FromBuffer(data);
514525

515526
if (!any_res) {
516527
result = score::MakeUnexpected(MyErrorCode::JsonParserError);
@@ -545,7 +556,7 @@ score::Result<std::unordered_map<std::string, KvsValue>> parse_json_data(const s
545556
}
546557

547558
/* Open and read JSON File */
548-
score::Result<std::unordered_map<string, KvsValue>> open_json(const string& prefix, OpenJsonNeedFile need_file)
559+
score::Result<std::unordered_map<string, KvsValue>> Kvs::open_json(const string& prefix, OpenJsonNeedFile need_file)
549560
{
550561
string json_file = prefix + ".json";
551562
string hash_file = prefix + ".hash";
@@ -616,15 +627,15 @@ score::Result<Kvs> Kvs::open(const InstanceId& instance_id, OpenNeedDefaults nee
616627

617628
score::Result<Kvs> result = score::MakeUnexpected(MyErrorCode::UnmappedError); /* Redundant initialization needed, since Resul<KVS> would call the implicitly-deleted default constructor of KVS */
618629

619-
620-
auto default_res = open_json(
630+
Kvs kvs; /* Create KVS instance */
631+
auto default_res = kvs.open_json(
621632
filename_default,
622633
need_defaults == OpenNeedDefaults::Required ? OpenJsonNeedFile::Required : OpenJsonNeedFile::Optional);
623634
if (!default_res){
624635
result = score::MakeUnexpected(static_cast<MyErrorCode>(*default_res.error())); /* Dereferences the Error class to its underlying code -> error.h*/
625636
}
626637
else{
627-
auto kvs_res = open_json(
638+
auto kvs_res = kvs.open_json(
628639
filename_kvs,
629640
need_kvs == OpenNeedKvs::Required ? OpenJsonNeedFile::Required : OpenJsonNeedFile::Optional);
630641
if (!kvs_res){
@@ -633,7 +644,6 @@ score::Result<Kvs> Kvs::open(const InstanceId& instance_id, OpenNeedDefaults nee
633644
cout << "opened KVS: instance '" << instance_id.id << "'" << endl;
634645
cout << "max snapshot count: " << KVS_MAX_SNAPSHOTS << endl;
635646

636-
Kvs kvs;
637647
kvs.kvs = std::move(kvs_res.value());
638648
kvs.default_values = std::move(default_res.value());
639649
kvs.filename_prefix = filename_prefix;
@@ -813,7 +823,7 @@ score::ResultBlank Kvs::remove_key(const string_view key) {
813823
}
814824

815825
/* Helper Function to write JSON data to a file for flush process (also adds Hash file)*/
816-
score::ResultBlank write_json_data(const std::string& filename_prefix, const std::string& buf)
826+
score::ResultBlank Kvs::write_json_data(const std::string& filename_prefix, const std::string& buf)
817827
{
818828
score::ResultBlank result = score::MakeUnexpected(MyErrorCode::UnmappedError);
819829
const std::string fn_json = filename_prefix + "_0.json";
@@ -878,8 +888,7 @@ score::ResultBlank Kvs::flush() {
878888

879889
if(!error){
880890
/* Serialize Buffer */
881-
score::json::JsonWriter writer;
882-
auto buf_res = writer.ToBuffer(root_obj);
891+
auto buf_res = writer->ToBuffer(root_obj);
883892
if (!buf_res) {
884893
result = score::MakeUnexpected(MyErrorCode::JsonGeneratorError);
885894
}else{

0 commit comments

Comments
 (0)