[btree_damage_visitor] factor out non-template functions

This commit is contained in:
Ming-Hung Tsai 2020-02-15 17:58:36 +08:00 committed by Joe Thornber
parent e6a4ba53f0
commit d6a8c03aa2
3 changed files with 55 additions and 71 deletions

View File

@ -80,6 +80,7 @@ SOURCE=\
persistent-data/data-structures/bitset.cc \
persistent-data/data-structures/bloom_filter.cc \
persistent-data/data-structures/btree.cc \
persistent-data/data-structures/btree_damage_visitor.cc \
persistent-data/data-structures/btree_node_checker.cc \
persistent-data/error_set.cc \
persistent-data/file_utils.cc \

View File

@ -16,13 +16,13 @@ damage_tracker::bad_node()
damaged_ = true;
}
maybe_range64
damage_tracker::maybe_run64
damage_tracker::good_internal(block_address begin)
{
maybe_range64 r;
maybe_run64 r;
if (damaged_) {
r = maybe_range64(range64(damage_begin_, begin));
r = maybe_run64(run64(damage_begin_, begin));
damaged_ = false;
}
@ -30,13 +30,13 @@ damage_tracker::good_internal(block_address begin)
return r;
}
maybe_range64
damage_tracker::good_leaf(uint64_t begin, uint64_t end)
damage_tracker::maybe_run64
damage_tracker::good_leaf(block_address begin, block_address end)
{
maybe_range64 r;
maybe_run64 r;
if (damaged_) {
r = maybe_range64(range64(damage_begin_, begin));
r = maybe_run64(run64(damage_begin_, begin));
damaged_ = false;
}
@ -44,13 +44,49 @@ damage_tracker::good_leaf(uint64_t begin, uint64_t end)
return r;
}
maybe_range64
damage_tracker::maybe_run64
damage_tracker::end()
{
maybe_run64 r;
if (damaged_)
return maybe_range64(damage_begin_);
r = maybe_run64(damage_begin_);
else
return maybe_range64();
r = maybe_run64();
damaged_ = false;
damage_begin_ = 0;
return r;
}
//----------------------------------------------------------------
path_tracker::path_tracker()
{
// We push an empty path, to ensure there
// is always a current_path.
paths_.push_back(btree_path());
}
btree_path const *
path_tracker::next_path(btree_path const &p)
{
if (p != current_path()) {
if (paths_.size() == 2)
paths_.pop_front();
paths_.push_back(p);
return &paths_.front();
}
return NULL;
}
btree_path const &
path_tracker::current_path() const
{
return paths_.back();
}
//----------------------------------------------------------------

View File

@ -38,57 +38,20 @@ namespace persistent_data {
// trackers if you have a multilayer tree.
class damage_tracker {
public:
damage_tracker()
: damaged_(false),
damage_begin_(0) {
}
damage_tracker();
typedef run<uint64_t> run64;
typedef boost::optional<run64> maybe_run64;
void bad_node() {
damaged_ = true;
}
void bad_node();
maybe_run64 good_internal(block_address begin) {
maybe_run64 r;
if (damaged_) {
r = maybe_run64(run64(damage_begin_, begin));
damaged_ = false;
}
damage_begin_ = begin;
return r;
}
maybe_run64 good_internal(block_address begin);
// remember 'end' is the one-past-the-end value, so
// take the last key in the leaf and add one.
maybe_run64 good_leaf(block_address begin, block_address end) {
maybe_run64 r;
maybe_run64 good_leaf(block_address begin, block_address end);
if (damaged_) {
r = maybe_run64(run64(damage_begin_, begin));
damaged_ = false;
}
damage_begin_ = end;
return r;
}
maybe_run64 end() {
maybe_run64 r;
if (damaged_)
r = maybe_run64(damage_begin_);
else
r = maybe_run64();
damaged_ = false;
damage_begin_ = 0;
return r;
}
maybe_run64 end();
private:
bool damaged_;
@ -99,28 +62,12 @@ namespace persistent_data {
// different sub tree (by looking at the btree_path).
class path_tracker {
public:
path_tracker() {
// We push an empty path, to ensure there
// is always a current_path.
paths_.push_back(btree_path());
}
path_tracker();
// returns the old path if the tree has changed.
btree_path const *next_path(btree_path const &p) {
if (p != current_path()) {
if (paths_.size() == 2)
paths_.pop_front();
paths_.push_back(p);
btree_path const *next_path(btree_path const &p);
return &paths_.front();
}
return NULL;
}
btree_path const &current_path() const {
return paths_.back();
}
btree_path const &current_path() const;
private:
std::list<btree_path> paths_;