first pass at cache
This commit is contained in:
parent
ad8a70ce76
commit
97f8d913e2
253
cache.h
Normal file
253
cache.h
Normal file
@ -0,0 +1,253 @@
|
||||
#ifndef CACHE_H
|
||||
#define CACHE_H
|
||||
|
||||
#include <boost/intrusive/circular_list_algorithms.hpp>
|
||||
#include <boost/intrusive/rbtree_algorithms.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <stdexcept>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace base {
|
||||
// FIXME: move somewhere more useful
|
||||
template <typename T>
|
||||
struct deleter {
|
||||
void operator()(T *t) {
|
||||
delete t;
|
||||
}
|
||||
};
|
||||
|
||||
// ValueTraits needs to define value_type, key_type and a get_key()
|
||||
// static function. Commonly you will want value_type to be a
|
||||
// shared_ptr, with any teardown specific stuff in the destructor.
|
||||
|
||||
template <typename ValueTraits>
|
||||
class cache {
|
||||
public:
|
||||
typedef typename ValueTraits::value_type value_type;
|
||||
typedef typename ValueTraits::key_type key_type;
|
||||
|
||||
cache(unsigned max_entries);
|
||||
~cache();
|
||||
|
||||
void insert(value_type const &v);
|
||||
|
||||
boost::optional<value_type> get(key_type const &k);
|
||||
void put(value_type const &k);
|
||||
|
||||
private:
|
||||
void make_space();
|
||||
|
||||
struct value_entry {
|
||||
// FIXME: this means the cached object must have a
|
||||
// default constructor also, which is a shame.
|
||||
// so we can construct the headers.
|
||||
value_entry()
|
||||
: ref_count_(0) {
|
||||
}
|
||||
|
||||
explicit value_entry(value_type v)
|
||||
: ref_count_(0),
|
||||
v_(v) {
|
||||
}
|
||||
|
||||
struct {
|
||||
value_entry *next_, *prev_;
|
||||
} lru_;
|
||||
|
||||
struct {
|
||||
value_entry *parent_, *left_, *right_;
|
||||
int color_;
|
||||
} lookup_;
|
||||
|
||||
unsigned ref_count_;
|
||||
value_type v_;
|
||||
};
|
||||
|
||||
struct value_ptr_cmp {
|
||||
bool operator() (value_entry const *lhs, value_entry const *rhs) {
|
||||
key_type k1 = ValueTraits::get_key(lhs->v_);
|
||||
key_type k2 = ValueTraits::get_key(rhs->v_);
|
||||
|
||||
return k1 < k2;
|
||||
}
|
||||
};
|
||||
|
||||
struct key_value_ptr_cmp {
|
||||
bool operator() (key_type const &k1, value_entry const *rhs) {
|
||||
key_type k2 = ValueTraits::get_key(rhs->v_);
|
||||
return k1 < k2;
|
||||
}
|
||||
|
||||
bool operator() (value_entry const *lhs, key_type const &k2) {
|
||||
key_type k1 = ValueTraits::get_key(lhs->v_);
|
||||
return k1 < k2;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct list_node_traits {
|
||||
typedef value_entry node;
|
||||
typedef value_entry *node_ptr;
|
||||
typedef const value_entry *const_node_ptr;
|
||||
|
||||
static node_ptr get_next(const_node_ptr n) {
|
||||
return n->lru_.next_;
|
||||
}
|
||||
|
||||
static void set_next(node_ptr n, node_ptr next) {
|
||||
n->lru_.next_ = next;
|
||||
}
|
||||
|
||||
static node_ptr get_previous(const_node_ptr n) {
|
||||
return n->lru_.prev_;
|
||||
}
|
||||
|
||||
static void set_previous(node_ptr n, node_ptr prev) {
|
||||
n->lru_.prev_ = prev;
|
||||
}
|
||||
};
|
||||
|
||||
struct rbtree_node_traits {
|
||||
typedef value_entry node;
|
||||
typedef value_entry *node_ptr;
|
||||
typedef const value_entry * const_node_ptr;
|
||||
typedef int color;
|
||||
|
||||
static node_ptr get_parent(const_node_ptr n) {
|
||||
return n->lookup_.parent_;
|
||||
}
|
||||
|
||||
static void set_parent(node_ptr n, node_ptr parent) {
|
||||
n->lookup_.parent_ = parent;
|
||||
}
|
||||
|
||||
static node_ptr get_left(const_node_ptr n) {
|
||||
return n->lookup_.left_;
|
||||
}
|
||||
|
||||
static void set_left(node_ptr n, node_ptr left) {
|
||||
n->lookup_.left_ = left;
|
||||
}
|
||||
|
||||
static node_ptr get_right(const_node_ptr n) {
|
||||
return n->lookup_.right_;
|
||||
}
|
||||
|
||||
static void set_right(node_ptr n, node_ptr right) {
|
||||
n->lookup_.right_ = right;
|
||||
}
|
||||
|
||||
static int get_color(const_node_ptr n) {
|
||||
return n->lookup_.color_;
|
||||
}
|
||||
|
||||
static void set_color(node_ptr n, color c) {
|
||||
n->lookup_.color_ = c;
|
||||
}
|
||||
|
||||
static color red() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static color black() {
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
typedef boost::intrusive::circular_list_algorithms<list_node_traits> lru_algo;
|
||||
typedef boost::intrusive::rbtree_algorithms<rbtree_node_traits> lookup_algo;
|
||||
|
||||
unsigned max_entries_;
|
||||
unsigned current_entries_;
|
||||
|
||||
value_entry lru_header_;
|
||||
value_entry lookup_header_;
|
||||
};
|
||||
|
||||
template <typename ValueTraits>
|
||||
cache<ValueTraits>::cache(unsigned max_entries)
|
||||
: max_entries_(max_entries),
|
||||
current_entries_(0) {
|
||||
lru_algo::init_header(&lru_header_);
|
||||
lookup_algo::init_header(&lookup_header_);
|
||||
}
|
||||
|
||||
template <typename ValueTraits>
|
||||
cache<ValueTraits>::~cache() {
|
||||
deleter<value_entry> d;
|
||||
lookup_algo::clear_and_dispose(&lookup_header_, d);
|
||||
}
|
||||
|
||||
template <typename ValueTraits>
|
||||
void
|
||||
cache<ValueTraits>::insert(value_type const &v) {
|
||||
make_space();
|
||||
|
||||
value_entry *node = new value_entry(v);
|
||||
try {
|
||||
lru_algo::link_after(&lru_header_, node);
|
||||
try {
|
||||
value_ptr_cmp cmp;
|
||||
lookup_algo::insert_equal(&lookup_header_, &lookup_header_, node, cmp);
|
||||
current_entries_++;
|
||||
|
||||
} catch (...) {
|
||||
lru_algo::unlink(node);
|
||||
throw;
|
||||
}
|
||||
|
||||
} catch (...) {
|
||||
delete node;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename ValueTraits>
|
||||
boost::optional<typename ValueTraits::value_type>
|
||||
cache<ValueTraits>::get(key_type const &k) {
|
||||
key_value_ptr_cmp cmp;
|
||||
value_entry *node = lookup_algo::find(&lookup_header_, k, cmp);
|
||||
if (node == &lookup_header_)
|
||||
return boost::optional<value_type>();
|
||||
|
||||
if (!node->ref_count_++)
|
||||
lru_algo::unlink(node);
|
||||
return boost::optional<value_type>(node->v_);
|
||||
}
|
||||
|
||||
template <typename ValueTraits>
|
||||
void
|
||||
cache<ValueTraits>::put(value_type const &v) {
|
||||
// FIXME: the lookup will go once we use a proper hook
|
||||
key_value_ptr_cmp cmp;
|
||||
key_type k = ValueTraits::get_key(v);
|
||||
value_entry *node = lookup_algo::find(&lookup_header_, k, cmp);
|
||||
if (node == &lookup_header_)
|
||||
throw std::runtime_error("invalid put");
|
||||
|
||||
if (!--node->ref_count_)
|
||||
lru_algo::link_after(&lru_header_, node);
|
||||
}
|
||||
|
||||
template <typename ValueTraits>
|
||||
void
|
||||
cache<ValueTraits>::make_space() {
|
||||
if (current_entries_ == max_entries_) {
|
||||
value_entry *node = lru_header_.lru_.prev_;
|
||||
if (node == &lru_header_)
|
||||
throw std::runtime_error("cache full");
|
||||
|
||||
lru_algo::unlink(node);
|
||||
lookup_algo::unlink(node);
|
||||
delete node;
|
||||
current_entries_--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
@ -1,6 +1,7 @@
|
||||
TEST_SOURCE=\
|
||||
unit-tests/block_t.cc \
|
||||
unit-tests/btree_t.cc \
|
||||
unit-tests/cache_t.cc \
|
||||
unit-tests/endian_t.cc \
|
||||
unit-tests/space_map_t.cc \
|
||||
unit-tests/space_map_disk_t.cc \
|
||||
@ -19,6 +20,9 @@ unit-tests/block_t: unit-tests/block_t.o
|
||||
unit-tests/btree_t: unit-tests/btree_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
unit-tests/cache_t: unit-tests/cache_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
unit-tests/space_map_t: unit-tests/space_map_t.o $(OBJECTS)
|
||||
g++ $(CPPFLAGS) -o $@ $+ $(LIBS)
|
||||
|
||||
|
144
unit-tests/cache_t.cc
Normal file
144
unit-tests/cache_t.cc
Normal file
@ -0,0 +1,144 @@
|
||||
#include "cache.h"
|
||||
|
||||
#define BOOST_TEST_MODULE CacheTests
|
||||
#include <boost/test/included/unit_test.hpp>
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
using namespace boost;
|
||||
using namespace base;
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
struct Thing {
|
||||
Thing()
|
||||
: key_(0),
|
||||
desc_("default constructed") {
|
||||
}
|
||||
|
||||
Thing(unsigned n)
|
||||
: key_(n),
|
||||
desc_("foo bar") {
|
||||
}
|
||||
|
||||
unsigned key_;
|
||||
string desc_;
|
||||
};
|
||||
|
||||
struct ThingTraits {
|
||||
typedef unsigned key_type;
|
||||
typedef Thing value_type;
|
||||
|
||||
static key_type get_key(value_type const &t) {
|
||||
return t.key_;
|
||||
}
|
||||
};
|
||||
|
||||
struct SharedThingTraits {
|
||||
typedef unsigned key_type;
|
||||
typedef shared_ptr<Thing> value_type;
|
||||
|
||||
static key_type get_key(value_type const &p) {
|
||||
return p->key_;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cache_creation)
|
||||
{
|
||||
cache<ThingTraits> c(16);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cache_caches)
|
||||
{
|
||||
unsigned const COUNT = 16;
|
||||
cache<ThingTraits> c(COUNT);
|
||||
|
||||
for (unsigned i = 0; i < COUNT; i++)
|
||||
c.insert(Thing(i));
|
||||
|
||||
for (unsigned i = 0; i < COUNT; i++)
|
||||
BOOST_ASSERT(c.get(i));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(cache_drops_elements)
|
||||
{
|
||||
unsigned const COUNT = 1024;
|
||||
unsigned const CACHE_SIZE = 16;
|
||||
cache<ThingTraits> c(CACHE_SIZE);
|
||||
|
||||
for (unsigned i = 0; i < COUNT; i++)
|
||||
c.insert(Thing(i));
|
||||
|
||||
for (unsigned i = 0; i < COUNT - CACHE_SIZE; i++)
|
||||
BOOST_ASSERT(!c.get(i));
|
||||
|
||||
for (unsigned i = COUNT - CACHE_SIZE; i < COUNT; i++)
|
||||
BOOST_ASSERT(c.get(i));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(held_entries_count_towards_the_cache_limit)
|
||||
{
|
||||
unsigned const CACHE_SIZE = 16;
|
||||
cache<ThingTraits> c(CACHE_SIZE);
|
||||
|
||||
unsigned i;
|
||||
for (i = 0; i < CACHE_SIZE; i++) {
|
||||
c.insert(Thing(i));
|
||||
c.get(i);
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(c.insert(Thing(i)), runtime_error);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(put_works)
|
||||
{
|
||||
unsigned const CACHE_SIZE = 16;
|
||||
cache<ThingTraits> c(CACHE_SIZE);
|
||||
|
||||
unsigned i;
|
||||
for (i = 0; i < CACHE_SIZE; i++) {
|
||||
c.insert(Thing(i));
|
||||
c.get(i);
|
||||
c.put(Thing(i));
|
||||
}
|
||||
|
||||
// should succeed
|
||||
c.insert(Thing(i));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(multiple_gets_works)
|
||||
{
|
||||
unsigned const CACHE_SIZE = 16;
|
||||
cache<ThingTraits> c(CACHE_SIZE);
|
||||
|
||||
unsigned i;
|
||||
for (i = 0; i < CACHE_SIZE; i++) {
|
||||
c.insert(Thing(i));
|
||||
c.get(i);
|
||||
c.get(i);
|
||||
c.put(Thing(i));
|
||||
}
|
||||
|
||||
BOOST_CHECK_THROW(c.insert(Thing(i)), runtime_error);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(shared_ptr_cache_works)
|
||||
{
|
||||
unsigned const CACHE_SIZE = 16;
|
||||
cache<SharedThingTraits> c(CACHE_SIZE);
|
||||
|
||||
for (unsigned i = 0; i < CACHE_SIZE; i++) {
|
||||
c.insert(shared_ptr<Thing>(new Thing(i)));
|
||||
optional<shared_ptr<Thing> > maybe_ptr = c.get(i);
|
||||
|
||||
BOOST_ASSERT(maybe_ptr);
|
||||
BOOST_ASSERT((*maybe_ptr)->key_ == i);
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
Loading…
Reference in New Issue
Block a user