Skip to content

Commit

Permalink
Update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 committed Aug 9, 2024
1 parent bc565f5 commit 2393038
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions components/core/src/clp/ffi/KeyValuePairLogEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
#include <memory>
#include <string>
#include <system_error>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <outcome/single-header/outcome.hpp>

Expand All @@ -16,6 +17,7 @@

using clp::ir::EightByteEncodedTextAst;
using clp::ir::FourByteEncodedTextAst;
using std::string;

namespace clp::ffi {
namespace {
Expand All @@ -39,7 +41,7 @@ auto is_valid_value_type(SchemaTreeNode::Type type, Value const& value) -> bool
case SchemaTreeNode::Type::UnstructuredArray:
return value.is<FourByteEncodedTextAst>() || value.is<EightByteEncodedTextAst>();
case SchemaTreeNode::Type::Str:
return value.is<std::string>() || value.is<FourByteEncodedTextAst>()
return value.is<string>() || value.is<FourByteEncodedTextAst>()
|| value.is<EightByteEncodedTextAst>();
default:
return false;
Expand All @@ -52,9 +54,15 @@ auto KeyValuePairLogEvent::create(
KeyValuePairs kv_pairs,
UtcOffset utc_offset
) -> OUTCOME_V2_NAMESPACE::std_result<KeyValuePairLogEvent> {
std::unordered_map<SchemaTreeNode::id_t, std::unordered_set<string>> key_sets;
try {
for (auto const& [key_id, value] : kv_pairs) {
auto const type{schema_tree->get_node(key_id).get_type()};
if (SchemaTree::cRootId == key_id) {
return std::errc::protocol_error;
}

auto const& node{schema_tree->get_node(key_id)};
auto const type{node.get_type()};
if (false == value.has_value()) {
// Empty value
if (SchemaTreeNode::Type::Obj != type) {
Expand All @@ -63,6 +71,17 @@ auto KeyValuePairLogEvent::create(
} else if (false == is_valid_value_type(type, value.value())) {
return std::errc::protocol_error;
}

auto const parent_id{node.get_parent_id()};
auto const key_name{node.get_key_name()};
if (key_sets.contains(parent_id)) {
if (key_sets.at(parent_id).contains({key_name.begin(), key_name.end()})) {
// The key is duplicated
return std::errc::protocol_not_supported;
}
} else {
key_sets.emplace(parent_id, std::unordered_set{string{key_name}});
}
}
} catch (SchemaTree::OperationFailed const& ex) {
return std::errc::operation_not_permitted;
Expand Down
2 changes: 2 additions & 0 deletions components/core/src/clp/ffi/KeyValuePairLogEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class KeyValuePairLogEvent {
* - std::errc::operation_not_permitted if the key ID doesn't represent a valid node in the
* schema tree.
* - std::errc::protocol_error if the schema tree node type doesn't match the value's type.
* - std::errc::protocol_not_supported if the same key appears more than once under a parent
* node.
*/
[[nodiscard]] static auto
create(std::shared_ptr<SchemaTree> schema_tree, KeyValuePairs kv_pairs, UtcOffset utc_offset
Expand Down

0 comments on commit 2393038

Please sign in to comment.