[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/bitset.cc \
persistent-data/data-structures/bloom_filter.cc \ persistent-data/data-structures/bloom_filter.cc \
persistent-data/data-structures/btree.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/data-structures/btree_node_checker.cc \
persistent-data/error_set.cc \ persistent-data/error_set.cc \
persistent-data/file_utils.cc \ persistent-data/file_utils.cc \

View File

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