From 8bdda6139b9e673586cdd13e5ba4be9084171411 Mon Sep 17 00:00:00 2001 From: Harihara Kadayam Date: Fri, 2 Aug 2024 10:22:33 -0700 Subject: [PATCH] Removed key specific print into btree and create custom overrider for that --- src/include/homestore/btree/btree.hpp | 4 +- src/include/homestore/btree/btree.ipp | 4 +- .../homestore/btree/detail/btree_common.ipp | 9 +-- .../homestore/btree/detail/btree_internal.hpp | 3 + .../homestore/btree/detail/btree_node.hpp | 36 +++++++++- .../btree/detail/btree_remove_impl.ipp | 2 +- .../homestore/btree/detail/prefix_node.hpp | 1 - .../homestore/btree/detail/simple_node.hpp | 72 +------------------ .../homestore/btree/detail/varlen_node.hpp | 59 --------------- src/tests/btree_helpers/btree_test_helper.hpp | 20 +++++- 10 files changed, 70 insertions(+), 140 deletions(-) diff --git a/src/include/homestore/btree/btree.hpp b/src/include/homestore/btree/btree.hpp index 87e7eb351..2ef1e1d44 100644 --- a/src/include/homestore/btree/btree.hpp +++ b/src/include/homestore/btree/btree.hpp @@ -121,7 +121,7 @@ class Btree { nlohmann::json get_status(int log_level) const; void dump_tree_to_file(const std::string& file = "") const; - std::string to_string_keys() const; + std::string to_custom_string(to_string_cb_t< K, V > const& cb) const; std::string visualize_tree_keys(const std::string& file) const; uint64_t count_keys(bnodeid_t bnodeid) const; @@ -201,7 +201,7 @@ class Btree { uint64_t get_btree_node_cnt() const; uint64_t get_child_node_cnt(bnodeid_t bnodeid) const; void to_string(bnodeid_t bnodeid, std::string& buf) const; - void to_string_keys(bnodeid_t bnodeid, std::string& buf) const; + void to_custom_string_internal(bnodeid_t bnodeid, std::string& buf, to_string_cb_t< K, V > const& cb) const; void to_dot_keys(bnodeid_t bnodeid, std::string& buf, std::map< uint32_t, std::vector< uint64_t > >& l_map, std::map< uint64_t, BtreeVisualizeVariables >& info_map) const; void validate_sanity_child(const BtreeNodePtr& parent_node, uint32_t ind) const; diff --git a/src/include/homestore/btree/btree.ipp b/src/include/homestore/btree/btree.ipp index 6b10dbdd9..35c42ab97 100644 --- a/src/include/homestore/btree/btree.ipp +++ b/src/include/homestore/btree/btree.ipp @@ -323,10 +323,10 @@ void Btree< K, V >::dump_tree_to_file(const std::string& file) const { } template < typename K, typename V > -std::string Btree< K, V >::to_string_keys() const { +std::string Btree< K, V >::to_custom_string(to_string_cb_t< K, V > const& cb) const { std::string buf; m_btree_lock.lock_shared(); - to_string_keys(m_root_node_info.bnode_id(), buf); + to_custom_string_internal(m_root_node_info.bnode_id(), buf, cb); m_btree_lock.unlock_shared(); return buf; diff --git a/src/include/homestore/btree/detail/btree_common.ipp b/src/include/homestore/btree/detail/btree_common.ipp index d497287a0..b21305497 100644 --- a/src/include/homestore/btree/detail/btree_common.ipp +++ b/src/include/homestore/btree/detail/btree_common.ipp @@ -147,23 +147,24 @@ void Btree< K, V >::to_string(bnodeid_t bnodeid, std::string& buf) const { } template < typename K, typename V > -void Btree< K, V >::to_string_keys(bnodeid_t bnodeid, std::string& buf) const { +void Btree< K, V >::to_custom_string_internal(bnodeid_t bnodeid, std::string& buf, + to_string_cb_t< K, V > const& cb) const { BtreeNodePtr node; locktype_t acq_lock = locktype_t::READ; if (read_and_lock_node(bnodeid, node, acq_lock, acq_lock, nullptr) != btree_status_t::success) { return; } - fmt::format_to(std::back_inserter(buf), "{}\n", node->to_string_keys()); + fmt::format_to(std::back_inserter(buf), "{}\n", node->to_custom_string(cb)); if (!node->is_leaf()) { uint32_t i = 0; while (i < node->total_entries()) { BtreeLinkInfo p; node->get_nth_value(i, &p, false); - to_string_keys(p.bnode_id(), buf); + to_custom_string_internal(p.bnode_id(), buf, cb); ++i; } - if (node->has_valid_edge()) { to_string_keys(node->edge_id(), buf); } + if (node->has_valid_edge()) { to_custom_string_internal(node->edge_id(), buf, cb); } } unlock_node(node, acq_lock); } diff --git a/src/include/homestore/btree/detail/btree_internal.hpp b/src/include/homestore/btree/detail/btree_internal.hpp index 352b06e04..a77bfe5ac 100644 --- a/src/include/homestore/btree/detail/btree_internal.hpp +++ b/src/include/homestore/btree/detail/btree_internal.hpp @@ -215,6 +215,9 @@ class BtreeNode; void intrusive_ptr_add_ref(BtreeNode* node); void intrusive_ptr_release(BtreeNode* node); +template < typename K, typename V > +using to_string_cb_t = std::function< std::string(std::vector< std::pair< K, V > > const&) >; + ENUM(btree_event_t, uint8_t, READ, MUTATE, REMOVE, SPLIT, REPAIR, MERGE); struct trace_route_entry { diff --git a/src/include/homestore/btree/detail/btree_node.hpp b/src/include/homestore/btree/detail/btree_node.hpp index b1c22ba9a..84f70aa05 100644 --- a/src/include/homestore/btree/detail/btree_node.hpp +++ b/src/include/homestore/btree/detail/btree_node.hpp @@ -354,6 +354,41 @@ class BtreeNode : public sisl::ObjLifeCounter< BtreeNode > { void lock_acknowledge() { m_trans_hdr.upgraders.decrement(1); } bool any_upgrade_waiters() const { return (!m_trans_hdr.upgraders.testz()); } + template < typename K, typename V > + std::string to_custom_string(to_string_cb_t< K, V > const& cb) const { + std::string snext = + (this->next_bnode() == empty_bnodeid) ? "" : fmt::format(" next_node={}", this->next_bnode()); + auto str = fmt::format("id={}.{} level={} nEntries={} {}{} node_gen={} ", this->node_id(), this->link_version(), + this->level(), this->total_entries(), (this->is_leaf() ? "LEAF" : "INTERIOR"), snext, + this->node_gen()); + if (this->has_valid_edge()) { + fmt::format_to(std::back_inserter(str), " edge={}.{}", this->edge_info().m_bnodeid, + this->edge_info().m_link_version); + } + + if (this->total_entries() == 0) { + fmt::format_to(std::back_inserter(str), " [EMPTY] "); + return str; + } else if (this->is_leaf()) { + std::vector< std::pair< K, V > > entries; + for (uint32_t i{0}; i < this->total_entries(); ++i) { + V v; + get_nth_value(i, &v, false); + entries.emplace_back(std::make_pair(get_nth_key< K >(i, false), v)); + } + fmt::format_to(std::back_inserter(str), " Keys=[{}]", cb(entries)); + return str; + } else { + fmt::format_to(std::back_inserter(str), " Keys=["); + for (uint32_t i{0}; i < this->total_entries(); ++i) { + fmt::format_to(std::back_inserter(str), "{}{}", get_nth_key< K >(i, false).to_string(), + (i == this->total_entries() - 1) ? "" : ", "); + } + fmt::format_to(std::back_inserter(str), "]"); + } + return str; + } + public: // Public method which needs to be implemented by variants virtual btree_status_t insert(uint32_t ind, const BtreeKey& key, const BtreeValue& val) = 0; @@ -391,7 +426,6 @@ class BtreeNode : public sisl::ObjLifeCounter< BtreeNode > { } virtual std::string to_string(bool print_friendly = false) const = 0; - virtual std::string to_string_keys(bool print_friendly = false) const = 0; virtual std::string to_dot_keys() const = 0; protected: diff --git a/src/include/homestore/btree/detail/btree_remove_impl.ipp b/src/include/homestore/btree/detail/btree_remove_impl.ipp index 6b20473d3..82213dcc6 100644 --- a/src/include/homestore/btree/detail/btree_remove_impl.ipp +++ b/src/include/homestore/btree/detail/btree_remove_impl.ipp @@ -119,7 +119,7 @@ retry: goto retry; } else if (ret == btree_status_t::merge_not_required) { BT_NODE_LOG(DEBUG, my_node, "merge is not required for child = {} keys: {}", curr_idx, - child_node->to_string_keys()); + child_node->to_string()); } } } diff --git a/src/include/homestore/btree/detail/prefix_node.hpp b/src/include/homestore/btree/detail/prefix_node.hpp index 3bdd06ba7..072b4f654 100644 --- a/src/include/homestore/btree/detail/prefix_node.hpp +++ b/src/include/homestore/btree/detail/prefix_node.hpp @@ -644,7 +644,6 @@ class FixedPrefixNode : public VariantNode< K, V > { return str; } - std::string to_string_keys(bool print_friendly = false) const override { return "NOT Supported"; } std::string to_dot_keys() const override { return "NOT Supported"; } private: diff --git a/src/include/homestore/btree/detail/simple_node.hpp b/src/include/homestore/btree/detail/simple_node.hpp index 97ea6a393..85318c9b9 100644 --- a/src/include/homestore/btree/detail/simple_node.hpp +++ b/src/include/homestore/btree/detail/simple_node.hpp @@ -215,11 +215,9 @@ class SimpleNode : public VariantNode< K, V > { (print_friendly ? "------------------------------------------------------------\n" : ""), this->node_id(), this->level(), this->total_entries(), (this->is_leaf() ? "LEAF" : "INTERIOR"), snext); - if (!this->is_leaf() && (this->has_valid_edge())) { - auto sedge = this->has_valid_edge() ? "edge:" + std::to_string(this->edge_info().m_bnodeid) + "." + - std::to_string(this->edge_info().m_link_version) - : ""; - fmt::format_to(std::back_inserter(str), "{}", sedge); + if (this->has_valid_edge()) { + fmt::format_to(std::back_inserter(str), " edge={}.{}", this->edge_info().m_bnodeid, + this->edge_info().m_link_version); } for (uint32_t i{0}; i < this->total_entries(); ++i) { @@ -230,70 +228,6 @@ class SimpleNode : public VariantNode< K, V > { return str; } - std::string to_string_keys(bool print_friendly = false) const override { - // FIXME: Implement this, key may not be a unit32_t - // return ""; - // #if 0 - std::string delimiter = print_friendly ? "\n" : "\t"; - std::string snext = this->next_bnode() == empty_bnodeid ? "" : fmt::format("next_node={}", this->next_bnode()); - auto str = fmt::format("{}{}.{} level:{} nEntries={} {} {} node_gen={} ", - print_friendly ? "------------------------------------------------------------\n" : "", - this->node_id(), this->link_version(), this->level(), this->total_entries(), - (this->is_leaf() ? "LEAF" : "INTERIOR"), snext, this->node_gen()); - if (!this->is_leaf() && (this->has_valid_edge())) { - fmt::format_to(std::back_inserter(str), "edge_id={}.{}", this->edge_info().m_bnodeid, - this->edge_info().m_link_version); - } - if (this->total_entries() == 0) { - fmt::format_to(std::back_inserter(str), " [EMPTY] "); - return str; - } - if (!this->is_leaf()) { - fmt::format_to(std::back_inserter(str), " ["); - for (uint32_t i{0}; i < this->total_entries(); ++i) { - uint32_t cur_key = BtreeNode::get_nth_key< K >(i, false).key(); - BtreeLinkInfo child_info; - get_nth_value(i, &child_info, false /* copy */); - fmt::format_to(std::back_inserter(str), "{}.{} {}", cur_key, child_info.link_version(), - i == this->total_entries() - 1 ? "" : ", "); - } - fmt::format_to(std::back_inserter(str), "]"); - return str; - } - uint32_t prev_key = BtreeNode::get_nth_key< K >(0, false).key(); - uint32_t cur_key = prev_key; - uint32_t last_key = BtreeNode::get_nth_key< K >(this->total_entries() - 1, false).key(); - if (last_key - prev_key == this->total_entries() - 1) { - if (this->total_entries() == 1) - fmt::format_to(std::back_inserter(str), "{}[{}]", delimiter, prev_key); - else - fmt::format_to(std::back_inserter(str), "{}[{}-{}]", delimiter, prev_key, last_key); - return str; - } - fmt::format_to(std::back_inserter(str), "{}0 - [{}", delimiter, prev_key); - uint32_t start_interval_key = prev_key; - for (uint32_t i{1}; i < this->total_entries(); ++i) { - cur_key = BtreeNode::get_nth_key< K >(i, false).key(); - if (cur_key != prev_key + 1) { - if (start_interval_key == prev_key) { - fmt::format_to(std::back_inserter(str), "-{}]{}{}- [{}", prev_key, delimiter, i, cur_key); - } else { - fmt::format_to(std::back_inserter(str), "]{}{}- [{}", delimiter, i, cur_key); - } - start_interval_key = cur_key; - } - prev_key = cur_key; - } - - if (start_interval_key == prev_key) { - fmt::format_to(std::back_inserter(str), "]"); - } else { - fmt::format_to(std::back_inserter(str), "-{}]", cur_key); - } - return str; - // #endif - } - std::string to_dot_keys() const override { std::string str; std::string snext = this->next_bnode() == empty_bnodeid ? "" : fmt::format("next_node={}", this->next_bnode()); diff --git a/src/include/homestore/btree/detail/varlen_node.hpp b/src/include/homestore/btree/detail/varlen_node.hpp index 120838dce..fcb7ff79c 100644 --- a/src/include/homestore/btree/detail/varlen_node.hpp +++ b/src/include/homestore/btree/detail/varlen_node.hpp @@ -508,65 +508,6 @@ class VariableNode : public VariantNode< K, V > { return str; } - std::string to_string_keys(bool print_friendly = false) const override { -#if 0 - std::string delimiter = print_friendly ? "\n" : "\t"; - auto str = fmt::format("{}{}.{} nEntries={} {} ", - print_friendly ? "------------------------------------------------------------\n" : "", - this->node_id(), this->link_version(), this->total_entries(), (this->is_leaf() ? "LEAF" : "INTERIOR")); - if (!this->is_leaf() && (this->has_valid_edge())) { - fmt::format_to(std::back_inserter(str), "edge_id={}.{}", this->edge_info().m_bnodeid, - this->edge_info().m_link_version); - } - if (this->total_entries() == 0) { - fmt::format_to(std::back_inserter(str), " [EMPTY] "); - return str; - } - if (!this->is_leaf()) { - fmt::format_to(std::back_inserter(str), " ["); - for (uint32_t i{0}; i < this->total_entries(); ++i) { - uint32_t cur_key = BtreeNode::get_nth_key< K >(i, false).key(); - BtreeLinkInfo child_info; - get_nth_value(i, &child_info, false /* copy */); - fmt::format_to(std::back_inserter(str), "{}.{} {}", cur_key, child_info.link_version(), i == this->total_entries() - 1 ? "" : ", "); - } - fmt::format_to(std::back_inserter(str), "]"); - return str; - } - uint32_t prev_key = BtreeNode::get_nth_key< K >(0, false).key(); - uint32_t cur_key = prev_key; - uint32_t last_key = BtreeNode::get_nth_key< K >(this->total_entries() - 1, false).key(); - if (last_key - prev_key == this->total_entries() - 1) { - if (this->total_entries() == 1) - fmt::format_to(std::back_inserter(str), "{}[{}]", delimiter, prev_key); - else - fmt::format_to(std::back_inserter(str), "{}[{}-{}]", delimiter, prev_key, last_key); - return str; - } - fmt::format_to(std::back_inserter(str), "{}0 - [{}", delimiter, prev_key); - uint32_t start_interval_key = prev_key; - for (uint32_t i{1}; i < this->total_entries(); ++i) { - cur_key = BtreeNode::get_nth_key< K >(i, false).key(); - if (cur_key != prev_key + 1) { - if (start_interval_key == prev_key) { - fmt::format_to(std::back_inserter(str), "-{}]{}{}- [{}", prev_key, delimiter, i, cur_key); - } else { - fmt::format_to(std::back_inserter(str), "]{}{}- [{}", delimiter, i, cur_key); - } - start_interval_key = cur_key; - } - prev_key = cur_key; - } - - if (start_interval_key == prev_key) { - fmt::format_to(std::back_inserter(str), "]"); - } else { - fmt::format_to(std::back_inserter(str), "-{}]", cur_key); - } - return str; -#endif - return {}; - } std::string to_dot_keys() const override { return "NOT Supported"; } /*int compare_nth_key_range(const BtreeKeyRange& range, uint32_t ind) const { diff --git a/src/tests/btree_helpers/btree_test_helper.hpp b/src/tests/btree_helpers/btree_test_helper.hpp index dd5508bea..298235068 100644 --- a/src/tests/btree_helpers/btree_test_helper.hpp +++ b/src/tests/btree_helpers/btree_test_helper.hpp @@ -376,7 +376,25 @@ struct BtreeTestHelper { void dump_to_file(const std::string& file = "") const { m_bt->dump_tree_to_file(file); } void print_keys(const std::string& preamble = "") const { - LOGINFO("{}{}", preamble.empty() ? "" : preamble + ":\n", m_bt->to_string_keys()); + auto print_key_range = [](std::vector< std::pair< K, V > > const& kvs) -> std::string { + uint32_t start = 0; + std::string str; + for (uint32_t i{1}; i <= kvs.size(); ++i) { + if ((i == kvs.size()) || (kvs[i].first.key() != kvs[i - 1].first.key() + 1)) { + if ((i - start) > 1) { + fmt::format_to(std::back_inserter(str), "{}-{}{}", kvs[start].first.key(), + kvs[i - 1].first.key(), (i == kvs.size()) ? "" : ", "); + } else { + fmt::format_to(std::back_inserter(str), "{}{}", kvs[start].first.key(), + (i == kvs.size()) ? "" : ", "); + } + start = i; + } + } + return str; + }; + + LOGINFO("{}{}", preamble.empty() ? "" : preamble + ":\n", m_bt->to_custom_string(print_key_range)); } void visualize_keys(const std::string& file) const { m_bt->visualize_tree_keys(file); }