Skip to content

Commit 7a1c842

Browse files
LinZhihao-723jackluo923
authored andcommitted
refactor(ffi): Make get_schema_subtree_bitmap a public method of KeyValuePairLogEvent. (y-scope#581)
1 parent 1e53f2c commit 7a1c842

File tree

2 files changed

+43
-49
lines changed

2 files changed

+43
-49
lines changed

components/core/src/clp/ffi/KeyValuePairLogEvent.cpp

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,6 @@ node_type_matches_value_type(SchemaTree::Node::Type type, Value const& value) ->
153153
KeyValuePairLogEvent::NodeIdValuePairs const& node_id_value_pairs
154154
) -> bool;
155155

156-
/**
157-
* @param node_id_value_pairs
158-
* @param schema_tree
159-
* @return A result containing a bitmap where every bit corresponds to the ID of a node in the
160-
* schema tree, and the set bits correspond to the nodes in the subtree defined by all paths from
161-
* the root node to the nodes in `node_id_value_pairs`; or an error code indicating a failure:
162-
* - std::errc::result_out_of_range if a node ID in `node_id_value_pairs` doesn't exist in the
163-
* schema tree.
164-
*/
165-
[[nodiscard]] auto get_schema_subtree_bitmap(
166-
KeyValuePairLogEvent::NodeIdValuePairs const& node_id_value_pairs,
167-
SchemaTree const& schema_tree
168-
) -> OUTCOME_V2_NAMESPACE::std_result<vector<bool>>;
169-
170156
/**
171157
* Inserts the given key-value pair into the JSON object (map).
172158
* @param node The schema tree node of the key to insert.
@@ -283,38 +269,6 @@ auto is_leaf_node(
283269
return true;
284270
}
285271

286-
auto get_schema_subtree_bitmap(
287-
KeyValuePairLogEvent::NodeIdValuePairs const& node_id_value_pairs,
288-
SchemaTree const& schema_tree
289-
) -> OUTCOME_V2_NAMESPACE::std_result<vector<bool>> {
290-
auto schema_subtree_bitmap{vector<bool>(schema_tree.get_size(), false)};
291-
for (auto const& [node_id, val] : node_id_value_pairs) {
292-
if (node_id >= schema_subtree_bitmap.size()) {
293-
return std::errc::result_out_of_range;
294-
}
295-
schema_subtree_bitmap[node_id] = true;
296-
297-
// Iteratively mark the parents as true
298-
auto optional_parent_id{schema_tree.get_node(node_id).get_parent_id()};
299-
while (true) {
300-
// Ideally, we'd use this if statement as the loop condition, but clang-tidy will
301-
// complain about an unchecked `optional` access.
302-
if (false == optional_parent_id.has_value()) {
303-
// Reached the root
304-
break;
305-
}
306-
auto const parent_id{optional_parent_id.value()};
307-
if (schema_subtree_bitmap[parent_id]) {
308-
// Parent already set by other child
309-
break;
310-
}
311-
schema_subtree_bitmap[parent_id] = true;
312-
optional_parent_id = schema_tree.get_node(parent_id).get_parent_id();
313-
}
314-
}
315-
return schema_subtree_bitmap;
316-
}
317-
318272
auto insert_kv_pair_into_json_obj(
319273
SchemaTree::Node const& node,
320274
std::optional<Value> const& optional_val,
@@ -393,6 +347,36 @@ auto KeyValuePairLogEvent::create(
393347
return KeyValuePairLogEvent{std::move(schema_tree), std::move(node_id_value_pairs), utc_offset};
394348
}
395349

350+
auto KeyValuePairLogEvent::get_schema_subtree_bitmap(
351+
) const -> OUTCOME_V2_NAMESPACE::std_result<vector<bool>> {
352+
auto schema_subtree_bitmap{vector<bool>(m_schema_tree->get_size(), false)};
353+
for (auto const& [node_id, val] : m_node_id_value_pairs) {
354+
if (node_id >= schema_subtree_bitmap.size()) {
355+
return std::errc::result_out_of_range;
356+
}
357+
schema_subtree_bitmap[node_id] = true;
358+
359+
// Iteratively mark the parents as true
360+
auto optional_parent_id{m_schema_tree->get_node(node_id).get_parent_id()};
361+
while (true) {
362+
// Ideally, we'd use this if statement as the loop condition, but clang-tidy will
363+
// complain about an unchecked `optional` access.
364+
if (false == optional_parent_id.has_value()) {
365+
// Reached the root
366+
break;
367+
}
368+
auto const parent_id{optional_parent_id.value()};
369+
if (schema_subtree_bitmap[parent_id]) {
370+
// Parent already set by other child
371+
break;
372+
}
373+
schema_subtree_bitmap[parent_id] = true;
374+
optional_parent_id = m_schema_tree->get_node(parent_id).get_parent_id();
375+
}
376+
}
377+
return schema_subtree_bitmap;
378+
}
379+
396380
auto KeyValuePairLogEvent::serialize_to_json(
397381
) const -> OUTCOME_V2_NAMESPACE::std_result<nlohmann::json> {
398382
if (m_node_id_value_pairs.empty()) {
@@ -409,9 +393,7 @@ auto KeyValuePairLogEvent::serialize_to_json(
409393
// vector grows).
410394
std::stack<DfsIterator> dfs_stack;
411395

412-
auto const schema_subtree_bitmap_ret{
413-
get_schema_subtree_bitmap(m_node_id_value_pairs, *m_schema_tree)
414-
};
396+
auto const schema_subtree_bitmap_ret{get_schema_subtree_bitmap()};
415397
if (schema_subtree_bitmap_ret.has_error()) {
416398
return schema_subtree_bitmap_ret.error();
417399
}

components/core/src/clp/ffi/KeyValuePairLogEvent.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <optional>
66
#include <unordered_map>
77
#include <utility>
8+
#include <vector>
89

910
#include <json/single_include/nlohmann/json.hpp>
1011
#include <outcome/single-header/outcome.hpp>
@@ -60,6 +61,17 @@ class KeyValuePairLogEvent {
6061

6162
[[nodiscard]] auto get_utc_offset() const -> UtcOffset { return m_utc_offset; }
6263

64+
/**
65+
* @return A result containing a bitmap where every bit corresponds to the ID of a node in the
66+
* schema tree, and the set bits correspond to the nodes in the subtree defined by all paths
67+
* from the root node to the nodes in `node_id_value_pairs`; or an error code indicating a
68+
* failure:
69+
* - std::errc::result_out_of_range if a node ID in `node_id_value_pairs` doesn't exist in the
70+
* schema tree.
71+
*/
72+
[[nodiscard]] auto get_schema_subtree_bitmap(
73+
) const -> OUTCOME_V2_NAMESPACE::std_result<std::vector<bool>>;
74+
6375
/**
6476
* Serializes the log event into a `nlohmann::json` object.
6577
* @return A result containing the serialized JSON object or an error code indicating the

0 commit comments

Comments
 (0)