Add utility class binary_block_counter

This commit is contained in:
Ming-Hung Tsai 2016-02-27 15:22:09 +08:00
parent b22495997a
commit d068ec8082
2 changed files with 39 additions and 2 deletions

View File

@ -20,6 +20,7 @@
#define BLOCK_COUNTER_H #define BLOCK_COUNTER_H
#include "block.h" #include "block.h"
#include "run_set.h"
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -32,7 +33,9 @@ namespace persistent_data {
public: public:
typedef std::map<block_address, unsigned> count_map; typedef std::map<block_address, unsigned> count_map;
void inc(block_address b) { virtual ~block_counter() {}
virtual void inc(block_address b) {
count_map::iterator it = counts_.find(b); count_map::iterator it = counts_.find(b);
if (it == counts_.end()) if (it == counts_.end())
counts_.insert(make_pair(b, 1)); counts_.insert(make_pair(b, 1));
@ -40,7 +43,7 @@ namespace persistent_data {
it->second++; it->second++;
} }
unsigned get_count(block_address b) const { virtual unsigned get_count(block_address b) const {
count_map::const_iterator it = counts_.find(b); count_map::const_iterator it = counts_.find(b);
return (it == counts_.end()) ? 0 : it->second; return (it == counts_.end()) ? 0 : it->second;
} }
@ -52,6 +55,29 @@ namespace persistent_data {
private: private:
count_map counts_; count_map counts_;
}; };
//----------------------------------------------------------------
// Little helper class that keeps track of which blocks
// are referenced.
//----------------------------------------------------------------
class binary_block_counter : public block_counter {
public:
virtual ~binary_block_counter() {}
virtual void inc(block_address b) {
visited_.add(b);
}
virtual unsigned get_count(block_address b) const {
return visited_.member(b) ? 1 : 0;
}
base::run_set<block_address> const& get_visited() const {
return visited_;
}
private:
base::run_set<block_address> visited_;
};
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -100,6 +100,17 @@ namespace persistent_data {
btree_count_detail::counting_visitor<NoopValueVisitor, noop_damage_visitor, Levels, ValueTraits, ValueCounter> v(noop_vv, noop_dv, bc, vc); btree_count_detail::counting_visitor<NoopValueVisitor, noop_damage_visitor, Levels, ValueTraits, ValueCounter> v(noop_vv, noop_dv, bc, vc);
tree.visit_depth_first(v); tree.visit_depth_first(v);
} }
template <unsigned Levels, typename ValueTraits>
void count_btree_blocks(btree<Levels, ValueTraits> const &tree, block_counter &bc) {
typedef noop_value_visitor<typename ValueTraits::value_type> NoopValueVisitor;
NoopValueVisitor noop_vv;
noop_damage_visitor noop_dv;
typedef noop_value_counter<typename ValueTraits::value_type> NoopValueCounter;
NoopValueCounter vc;
btree_count_detail::counting_visitor<NoopValueVisitor, noop_damage_visitor, Levels, ValueTraits, NoopValueCounter> v(noop_vv, noop_dv, bc, vc);
tree.visit_depth_first(v);
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------