2013-12-11 22:30:57 +05:30
|
|
|
#ifndef PERSISTENT_DATA_DATA_STRUCTURES_BTREE_COUNTER_H
|
|
|
|
#define PERSISTENT_DATA_DATA_STRUCTURES_BTREE_COUNTER_H
|
|
|
|
|
2014-02-19 21:38:05 +05:30
|
|
|
#include "persistent-data/data-structures/btree.h"
|
2016-03-31 20:38:14 +05:30
|
|
|
#include "persistent-data/data-structures/btree_node_checker.h"
|
2013-12-11 22:30:57 +05:30
|
|
|
#include "persistent-data/block_counter.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
|
|
|
namespace btree_count_detail {
|
2016-03-31 20:38:14 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits, typename ValueCounter>
|
|
|
|
class counting_visitor : public btree<Levels, ValueTraits>::visitor {
|
2013-12-11 22:30:57 +05:30
|
|
|
public:
|
|
|
|
typedef btree<Levels, ValueTraits> tree;
|
|
|
|
|
2016-03-31 20:38:14 +05:30
|
|
|
counting_visitor(block_counter &bc, ValueCounter &vc)
|
|
|
|
: bc_(bc),
|
2020-08-11 17:11:41 +05:30
|
|
|
vc_(vc),
|
|
|
|
error_outcome_(bc.stop_on_error() ?
|
|
|
|
tree::visitor::RETHROW_EXCEPTION :
|
|
|
|
tree::visitor::EXCEPTION_HANDLED) {
|
2013-12-11 22:30:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit_internal(node_location const &l,
|
|
|
|
typename tree::internal_node const &n) {
|
2016-03-31 20:38:14 +05:30
|
|
|
return check_internal(l, n) ? visit_node(n) : false;
|
2013-12-11 22:30:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit_internal_leaf(node_location const &l,
|
|
|
|
typename tree::internal_node const &n) {
|
2016-03-31 20:38:14 +05:30
|
|
|
return check_leaf(l, n) ? visit_node(n) : false;
|
2013-12-11 22:30:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool visit_leaf(node_location const &l,
|
|
|
|
typename tree::leaf_node const &n) {
|
2016-03-31 20:38:14 +05:30
|
|
|
if (check_leaf(l, n) && visit_node(n)) {
|
2013-12-11 22:30:57 +05:30
|
|
|
unsigned nr = n.get_nr_entries();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < nr; i++) {
|
|
|
|
// FIXME: confirm l2 is correct
|
|
|
|
node_location l2(l);
|
2016-06-19 19:05:58 +05:30
|
|
|
l2.push_key(n.key_at(i));
|
2013-12-11 22:30:57 +05:30
|
|
|
vc_.visit(l2, n.value_at(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-31 20:38:14 +05:30
|
|
|
typedef typename btree<Levels, ValueTraits>::visitor::error_outcome error_outcome;
|
|
|
|
|
|
|
|
error_outcome error_accessing_node(node_location const &l, block_address b,
|
|
|
|
std::string const &what) {
|
2020-08-11 17:11:41 +05:30
|
|
|
return error_outcome_;
|
2016-03-31 20:38:14 +05:30
|
|
|
}
|
|
|
|
|
2013-12-11 22:30:57 +05:30
|
|
|
private:
|
2016-03-31 20:38:14 +05:30
|
|
|
bool check_internal(node_location const &l,
|
|
|
|
btree_detail::node_ref<block_traits> const &n) {
|
2016-04-05 14:35:28 +05:30
|
|
|
if (l.is_sub_root())
|
|
|
|
new_root(l.level());
|
|
|
|
|
2016-03-31 20:38:14 +05:30
|
|
|
if (!checker_.check_block_nr(n) ||
|
|
|
|
!checker_.check_value_size(n) ||
|
|
|
|
!checker_.check_max_entries(n) ||
|
|
|
|
!checker_.check_nr_entries(n, l.is_sub_root()) ||
|
|
|
|
!checker_.check_ordered_keys(n) ||
|
|
|
|
!checker_.check_parent_key(n, l.is_sub_root() ? boost::optional<uint64_t>() : l.key))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits2>
|
|
|
|
bool check_leaf(node_location const &l,
|
|
|
|
btree_detail::node_ref<ValueTraits2> const &n) {
|
2016-04-05 14:35:28 +05:30
|
|
|
if (l.is_sub_root())
|
|
|
|
new_root(l.level());
|
|
|
|
|
2016-03-31 20:38:14 +05:30
|
|
|
if (!checker_.check_block_nr(n) ||
|
|
|
|
!checker_.check_value_size(n) ||
|
|
|
|
!checker_.check_max_entries(n) ||
|
|
|
|
!checker_.check_nr_entries(n, l.is_sub_root()) ||
|
|
|
|
!checker_.check_ordered_keys(n) ||
|
2016-04-05 14:35:28 +05:30
|
|
|
!checker_.check_parent_key(n, l.is_sub_root() ? boost::optional<uint64_t>() : l.key) ||
|
|
|
|
!checker_.check_leaf_key(n, last_leaf_key_[l.level()]))
|
2016-03-31 20:38:14 +05:30
|
|
|
return false;
|
|
|
|
|
2016-04-05 14:35:28 +05:30
|
|
|
if (n.get_nr_entries() > 0)
|
2016-03-31 20:38:14 +05:30
|
|
|
last_leaf_key_[l.level()] = n.key_at(n.get_nr_entries() - 1);
|
|
|
|
|
2016-04-05 14:35:28 +05:30
|
|
|
return true;
|
2016-03-31 20:38:14 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void new_root(unsigned level) {
|
|
|
|
// we're starting a new subtree, so should
|
|
|
|
// reset the last_leaf value.
|
|
|
|
last_leaf_key_[level] = boost::optional<uint64_t>();
|
|
|
|
}
|
|
|
|
|
2013-12-11 22:30:57 +05:30
|
|
|
template <typename Node>
|
|
|
|
bool visit_node(Node const &n) {
|
|
|
|
block_address b = n.get_location();
|
|
|
|
bool seen = bc_.get_count(b);
|
|
|
|
bc_.inc(b);
|
|
|
|
return !seen;
|
|
|
|
}
|
|
|
|
|
|
|
|
block_counter &bc_;
|
|
|
|
ValueCounter &vc_;
|
2016-03-31 20:38:14 +05:30
|
|
|
btree_node_checker checker_;
|
|
|
|
boost::optional<uint64_t> last_leaf_key_[Levels];
|
2020-08-11 17:11:41 +05:30
|
|
|
error_outcome error_outcome_;
|
2013-12-11 22:30:57 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct noop_value_counter {
|
|
|
|
void visit(btree_detail::node_location const &loc, T const &v) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-19 21:38:05 +05:30
|
|
|
struct block_address_counter {
|
|
|
|
block_address_counter(block_counter &bc)
|
|
|
|
: bc_(bc) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void visit(btree_detail::node_location const &loc, block_address b) {
|
|
|
|
bc_.inc(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
block_counter &bc_;
|
|
|
|
};
|
|
|
|
|
2013-12-11 22:30:57 +05:30
|
|
|
// Counts how many times each metadata block is referenced in the
|
|
|
|
// tree. Blocks already referenced in the block counter are not
|
|
|
|
// walked. This walk should only be done once you're sure the tree
|
|
|
|
// is not corrupt.
|
|
|
|
template <unsigned Levels, typename ValueTraits, typename ValueCounter>
|
|
|
|
void count_btree_blocks(btree<Levels, ValueTraits> const &tree, block_counter &bc, ValueCounter &vc) {
|
2016-03-31 20:38:14 +05:30
|
|
|
btree_count_detail::counting_visitor<Levels, ValueTraits, ValueCounter> v(bc, vc);
|
2016-02-27 12:52:09 +05:30
|
|
|
tree.visit_depth_first(v);
|
|
|
|
}
|
2013-12-11 22:30:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|