[caching] damage visitor and checker for hint array

This commit is contained in:
Joe Thornber 2013-10-08 10:43:20 +01:00
parent 7da033d5c1
commit ddf5765f9c
2 changed files with 116 additions and 13 deletions

View File

@ -2,6 +2,7 @@
using namespace boost; using namespace boost;
using namespace caching; using namespace caching;
using namespace caching::hint_array_damage;
using namespace persistent_data; using namespace persistent_data;
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -64,6 +65,18 @@ namespace {
//-------------------------------- //--------------------------------
template <typename HA>
shared_ptr<HA>
downcast_array(shared_ptr<array_base> base) {
shared_ptr<HA> a = dynamic_pointer_cast<HA>(base);
if (!a)
throw runtime_error("internal error: couldn't cast hint array");
return a;
}
//--------------------------------
template <uint32_t WIDTH> template <uint32_t WIDTH>
shared_ptr<array_base> mk_array(transaction_manager::ptr tm, block_address root, unsigned nr_entries) { shared_ptr<array_base> mk_array(transaction_manager::ptr tm, block_address root, unsigned nr_entries) {
typedef hint_traits<WIDTH> traits; typedef hint_traits<WIDTH> traits;
@ -94,10 +107,7 @@ namespace {
typedef hint_traits<WIDTH> traits; typedef hint_traits<WIDTH> traits;
typedef array<traits> ha; typedef array<traits> ha;
shared_ptr<ha> a = dynamic_pointer_cast<ha>(base); shared_ptr<ha> a = downcast_array<ha>(base);
if (!a)
throw runtime_error("internal error: couldn't cast hint array");
data = a->get(index); data = a->get(index);
} }
@ -116,10 +126,7 @@ namespace {
typedef hint_traits<WIDTH> traits; typedef hint_traits<WIDTH> traits;
typedef array<traits> ha; typedef array<traits> ha;
shared_ptr<ha> a = dynamic_pointer_cast<ha>(base); shared_ptr<ha> a = downcast_array<ha>(base);
if (!a)
throw runtime_error("internal error: couldn't cast hint array");
a->set(index, data); a->set(index, data);
} }
@ -139,9 +146,7 @@ namespace {
typedef hint_traits<WIDTH> traits; typedef hint_traits<WIDTH> traits;
typedef array<traits> ha; typedef array<traits> ha;
shared_ptr<ha> a = dynamic_pointer_cast<ha>(base); shared_ptr<ha> a = downcast_array<ha>(base);
if (!a)
throw runtime_error("internal error: couldn't cast hint array");
a->grow(new_nr_entries, value); a->grow(new_nr_entries, value);
} }
@ -154,6 +159,62 @@ namespace {
#undef xx #undef xx
} }
} }
//--------------------------------
template <typename ValueType>
struct no_op_visitor {
virtual void visit(uint32_t index, ValueType const &v) {
}
};
class ll_damage_visitor {
public:
ll_damage_visitor(damage_visitor &v)
: v_(v) {
}
virtual void visit(array_detail::damage const &d) {
v_.visit(missing_hints(d.desc_, d.lost_keys_));
}
private:
damage_visitor &v_;
};
template <uint32_t WIDTH>
void check_hints(shared_ptr<array_base> base, damage_visitor &visitor) {
typedef hint_traits<WIDTH> traits;
typedef array<traits> ha;
shared_ptr<ha> a = downcast_array<ha>(base);
no_op_visitor<typename traits::value_type> nv;
ll_damage_visitor ll(visitor);
a->visit_values(nv, ll);
}
void check_hints_(uint32_t width, shared_ptr<array_base> base,
damage_visitor &visitor) {
switch (width) {
#define xx(n) case n: check_hints<n>(base, visitor); break
all_widths
#undef xx
}
}
}
//----------------------------------------------------------------
missing_hints::missing_hints(std::string const desc, run<uint32_t> const &keys)
: damage(desc),
keys_(keys)
{
}
void
missing_hints::visit(damage_visitor &v) const
{
v.visit(*this);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
@ -195,4 +256,10 @@ hint_array::grow(unsigned new_nr_entries, vector<unsigned char> const &value)
grow_(width_, impl_, new_nr_entries, value); grow_(width_, impl_, new_nr_entries, value);
} }
void
hint_array::check_hint_array(hint_array_damage::damage_visitor &visitor)
{
check_hints_(width_, impl_, visitor);
}
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -8,9 +8,43 @@
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace caching { namespace caching {
namespace hint_array_detail { namespace hint_array_damage {
class damage_visitor;
// FIXME: data visitor stuff class damage {
public:
damage(std::string const &desc)
: desc_(desc) {
}
virtual ~damage() {}
virtual void visit(damage_visitor &v) const = 0;
std::string get_desc() const {
return desc_;
}
private:
std::string desc_;
};
struct missing_hints : public damage {
missing_hints(std::string const desc, run<uint32_t> const &keys);
virtual void visit(damage_visitor &v) const;
run<uint32_t> keys_;
};
class damage_visitor {
public:
virtual ~damage_visitor() {}
virtual void visit(damage const &d) {
d.visit(*this);
}
virtual void visit(missing_hints const &d) = 0;
};
} }
class hint_array { class hint_array {
@ -23,6 +57,7 @@ namespace caching {
unsigned get_nr_entries() const; unsigned get_nr_entries() const;
void grow(unsigned new_nr_entries, void const *v); void grow(unsigned new_nr_entries, void const *v);
block_address get_root() const; block_address get_root() const;
@ -30,6 +65,7 @@ namespace caching {
void set_hint(unsigned index, vector<unsigned char> const &data); void set_hint(unsigned index, vector<unsigned char> const &data);
void grow(unsigned new_nr_entries, vector<unsigned char> const &value); void grow(unsigned new_nr_entries, vector<unsigned char> const &value);
void check_hint_array(hint_array_damage::damage_visitor &visitor);
private: private:
unsigned width_; unsigned width_;