From 056c152d49c16a88c619f62e5cb3d81c3faf9dd6 Mon Sep 17 00:00:00 2001 From: "Timur A. Fatkhullin" Date: Mon, 18 May 2026 11:24:17 +0300 Subject: [PATCH] mcc_keyvalue.h: worked --- include/mcc/mcc_keyvalue.h | 139 ++++++++++++++++++++++++++---------- tests/mcc_keyvalue_test.cpp | 6 +- 2 files changed, 104 insertions(+), 41 deletions(-) diff --git a/include/mcc/mcc_keyvalue.h b/include/mcc/mcc_keyvalue.h index fd53231..432cd13 100644 --- a/include/mcc/mcc_keyvalue.h +++ b/include/mcc/mcc_keyvalue.h @@ -95,8 +95,9 @@ concept mcc_variant_valid_type_c = requires { !std::is_array_v && !std::is_vo template concept mcc_keyvalue_record_c = requires(T t) { - requires std::same_as; // immutable key!!! + requires std::same_as; // key name is immutable!!! requires mcc_variant_valid_type_c; + // requires mcc_serialization_params_c; }; @@ -122,6 +123,12 @@ template (_keyValue).key)), ...); }(std::make_index_sequence()); - _loadedKey.insert_range(_hashes); + _changedKey.insert_range(_hashes); } @@ -238,7 +245,7 @@ public: _headComment.clear(); _inlineComment.clear(); - _loadedKey.clear(); + _changedKey.clear(); auto recs = std::views::split(buffer, std::move(rec_delim)); @@ -272,7 +279,7 @@ public: return MccKeyValueHolderErrorCode::ERROR_DESERIAL; } - _loadedKey.insert(key_hash); + _changedKey.insert(key_hash); return MccKeyValueHolderErrorCode::ERROR_OK; }); @@ -300,7 +307,8 @@ public: return ec; } - template std::error_code toCharRange(R& output_buffer, @@ -311,41 +319,46 @@ public: auto write_rec = [&output_buffer, &rec_delim, &ec, &ser_params, this](this auto& self) { if constexpr (I < NUMBER_OF_RECORDS) { - if (_loadedKey.count(_hashes[I])) { - auto key = std::get(_keyValue).key; - auto val = std::get(_keyValue).value; - using val_t = std::remove_cvref_t; - - std::string buff; - auto err = MccSerializer{}(buff, val, ser_params); - if (err) { - ec = MccKeyValueHolderErrorCode::ERROR_SERIAL; + if constexpr (OPOLICY == MccKeyValueHolder::OPOLICY_CHANGED_ONLY) { + if (_changedKey.count(_hashes[I]) == 0) { + self.template operator()(); return; - } else { - size_t hash = _hashes[I]; - // write head comment - if (_headComment[hash].size()) { - for (auto const& comm : _headComment[hash]) { - if (comm.has_value()) { - std::format_to(std::back_inserter(output_buffer), "{}{}", COMM_SEQ, comm.value()); - } - std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); - } - } - - // key and value - std::format_to(std::back_inserter(output_buffer), "{}{}{}", key, KEY_VALUE_DELIM, buff); - - // inline comment - if (_inlineComment[hash].size()) { - std::format_to(std::back_inserter(output_buffer), " {}{}", COMMENT_SEQ, - _inlineComment[hash]); - } - - // record delimiter - std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); } } + // if (_changedKey.count(_hashes[I])) { + auto key = std::get(_keyValue).key; + auto val = std::get(_keyValue).value; + using val_t = std::remove_cvref_t; + + std::string buff; + auto err = MccSerializer{}(buff, val, ser_params); + if (err) { + ec = MccKeyValueHolderErrorCode::ERROR_SERIAL; + return; + } else { + size_t hash = _hashes[I]; + // write head comment + if (_headComment[hash].size()) { + for (auto const& comm : _headComment[hash]) { + if (comm.has_value()) { + std::format_to(std::back_inserter(output_buffer), "{}{}", COMM_SEQ, comm.value()); + } + std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); + } + } + + // key and value + std::format_to(std::back_inserter(output_buffer), "{}{}{}", key, KEY_VALUE_DELIM, buff); + + // inline comment + if (_inlineComment[hash].size()) { + std::format_to(std::back_inserter(output_buffer), " {}{}", COMMENT_SEQ, _inlineComment[hash]); + } + + // record delimiter + std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); + } + // } self.template operator()(); } @@ -367,7 +380,7 @@ protected: std::unordered_map>> _headComment{}; // comment string/strings before key std::unordered_map _inlineComment{}; // inline (after key=value pair) comment - std::unordered_set _loadedKey{}; // loaded keys (see fromCharRange) + std::unordered_set _changedKey{}; // loaded keys (see fromCharRange) // // NOTE: deduced this is needed here to use "forKey" method in getter and setter (const and non-const contexts)!!! @@ -395,6 +408,56 @@ protected: return MccKeyValueHolderErrorCode::ERROR_INVALID_KEY; } + + template + std::error_code writeRecord(R& output_buffer, RecDelimT rec_delim, SerParamsT const& ser_params) + { + if constexpr (I < NUMBER_OF_RECORDS) { + if constexpr (OPOLICY == MccKeyValueHolder::OPOLICY_CHANGED_ONLY) { + if (_changedKey.count(_hashes[I]) == 0) { + return MccKeyValueHolderErrorCode::ERROR_OK; + } + } + + auto key = std::get(_keyValue).key; + auto val = std::get(_keyValue).value; + using val_t = std::remove_cvref_t; + + std::string buff; + auto err = MccSerializer{}(buff, val, ser_params); + if (err) { + return MccKeyValueHolderErrorCode::ERROR_SERIAL; + } else { + size_t hash = _hashes[I]; + // write head comment + if (_headComment[hash].size()) { + for (auto const& comm : _headComment[hash]) { + if (comm.has_value()) { + std::format_to(std::back_inserter(output_buffer), "{}{}", COMM_SEQ, comm.value()); + } + std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); + } + } + + // key and value + std::format_to(std::back_inserter(output_buffer), "{}{}{}", key, KEY_VALUE_DELIM, buff); + + // inline comment + if (_inlineComment[hash].size()) { + std::format_to(std::back_inserter(output_buffer), " {}{}", COMMENT_SEQ, _inlineComment[hash]); + } + + // record delimiter + std::ranges::copy(rec_delim, std::back_inserter(output_buffer)); + } + } + + return MccKeyValueHolderErrorCode::ERROR_OK; + } }; template diff --git a/tests/mcc_keyvalue_test.cpp b/tests/mcc_keyvalue_test.cpp index 29f54f2..58bb303 100644 --- a/tests/mcc_keyvalue_test.cpp +++ b/tests/mcc_keyvalue_test.cpp @@ -22,7 +22,6 @@ cc= 2026-05-15T05:53:20.921723918 # date UTC )--"; static std::string STR1 = R"--( - aaa = dewl_ewkj23+23998 # @@ -60,13 +59,14 @@ int main() std::string buff; - err = kv.toCharRange(buff); + err = kv.toCharRange(buff); + // err = kv.toCharRange(buff); if (err) { std::println("ERR = {}", err); return 1; } - std::println("--(\n{}\n)--'", buff); + std::println("--(\n{}\n)--", buff); std::print("KEYS: "); for (auto const& k : kv.keys()) {