Skip to content

Commit

Permalink
Removed key specific print into btree and create custom overrider for…
Browse files Browse the repository at this point in the history
… that
  • Loading branch information
hkadayam committed Aug 2, 2024
1 parent 69992b6 commit 8bdda61
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 140 deletions.
4 changes: 2 additions & 2 deletions src/include/homestore/btree/btree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/include/homestore/btree/btree.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/include/homestore/btree/detail/btree_common.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/include/homestore/btree/detail/btree_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 35 additions & 1 deletion src/include/homestore/btree/detail/btree_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/include/homestore/btree/detail/btree_remove_impl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/include/homestore/btree/detail/prefix_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
72 changes: 3 additions & 69 deletions src/include/homestore/btree/detail/simple_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
59 changes: 0 additions & 59 deletions src/include/homestore/btree/detail/varlen_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion src/tests/btree_helpers/btree_test_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }

Expand Down

0 comments on commit 8bdda61

Please sign in to comment.