Beginning to work

This commit is contained in:
Joe Thornber
2014-07-25 16:14:24 +01:00
parent 7e870ea5a6
commit 11469a2fda
5 changed files with 79 additions and 268 deletions

View File

@@ -20,7 +20,6 @@
#define BLOCK_H
#include "block-cache/block_cache.h"
#include "block-cache/buffer.h"
#include <stdint.h>
#include <map>
@@ -61,61 +60,28 @@ namespace persistent_data {
BT_NORMAL
};
// FIXME: eventually this will disappear to be replaced with block_cache::block
struct block : private boost::noncopyable {
typedef boost::shared_ptr<block> ptr;
block(block_cache &bc,
block_address location,
block_type bt,
typename bcache::validator::ptr v,
bool zero = false);
~block();
void check_read_lockable() const {
// FIXME: finish
}
void check_write_lockable() const {
// FIXME: finish
}
block_type get_type() const;
uint64_t get_location() const;
void const *get_data() const;
void *get_data();
void mark_dirty();
void unlock();
private:
void check_not_unlocked() const;
block_cache &bc_;
block_cache::block *internal_;
block_type bt_;
bool dirty_;
bool unlocked_;
};
typedef void (*put_behaviour_fn)(block_cache &, block_cache::block &);
class read_ref {
public:
static uint32_t const BLOCK_SIZE = BlockSize;
read_ref(block_manager<BlockSize> const &bm,
typename block::ptr b);
read_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn);
read_ref(read_ref const &rhs);
virtual ~read_ref();
read_ref const &operator =(read_ref const &rhs);
block_address get_location() const;
void const * data() const;
void const *data() const;
protected:
block_manager<BlockSize> const *bm_;
typename block::ptr block_;
block_cache &bc_;
block_cache::block &b_;
put_behaviour_fn fn_;
unsigned *holders_;
};
@@ -123,13 +89,24 @@ namespace persistent_data {
// locked.
class write_ref : public read_ref {
public:
write_ref(block_manager<BlockSize> const &bm,
typename block::ptr b);
write_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn);
using read_ref::data;
void *data();
};
class super_ref : public write_ref {
public:
super_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn);
using read_ref::data;
using write_ref::data;
};
// Locking methods
read_ref
read_lock(block_address location,
@@ -174,11 +151,9 @@ namespace persistent_data {
private:
void check(block_address b) const;
void write_block(typename block::ptr b) const;
int fd_;
// FIXME: the mutable is a fudge to allow flush() to be const, which I'm not sure is necc.
mutable block_cache bc_;
};

View File

@@ -105,102 +105,39 @@ namespace {
};
namespace persistent_data {
inline void read_put(block_cache &bc, block_cache::block &b) {
bc.put(b, 0);
}
inline void write_put(block_cache &bc, block_cache::block &b) {
bc.put(b, block_cache::PF_DIRTY);
}
inline void super_put(block_cache &bc, block_cache::block &b) {
bc.flush();
bc.put(b, block_cache::PF_DIRTY);
bc.flush();
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block::block(block_cache &bc,
block_address location,
block_type bt,
typename validator::ptr v,
bool zero)
block_manager<BlockSize>::read_ref::read_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn)
: bc_(bc),
bt_(bt),
dirty_(false),
unlocked_(false)
{
if (zero) {
internal_ = &bc.get(location, block_cache::GF_ZERO | block_cache::GF_CAN_BLOCK, v);
dirty_ = true;
} else {
internal_ = &bc.get(location, block_cache::GF_CAN_BLOCK, v);
}
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block::~block()
{
if (!unlocked_)
unlock();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::unlock()
{
bc_.put(*internal_, dirty_ ? block_cache::PF_DIRTY : 0);
unlocked_ = true;
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::block_type
block_manager<BlockSize>::block::get_type() const
{
return bt_;
}
template <uint32_t BlockSize>
uint64_t
block_manager<BlockSize>::block::get_location() const
{
check_not_unlocked();
return internal_->get_index();
}
template <uint32_t BlockSize>
void const *
block_manager<BlockSize>::block::get_data() const
{
return internal_->get_data();
}
template <uint32_t BlockSize>
void *
block_manager<BlockSize>::block::get_data()
{
return internal_->get_data();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::mark_dirty()
{
check_not_unlocked();
dirty_ = true;
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::check_not_unlocked() const
{
if (unlocked_)
throw std::runtime_error("block prematurely unlocked");
}
//----------------------------------------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm,
typename block::ptr b)
: bm_(&bm),
block_(b),
holders_(new unsigned)
b_(b),
fn_(fn),
holders_(new unsigned)
{
*holders_ = 1;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
: bm_(rhs.bm_),
block_(rhs.block_),
holders_(rhs.holders_)
: bc_(rhs.bc_),
b_(rhs.b_),
fn_(rhs.fn_),
holders_(rhs.holders_)
{
(*holders_)++;
}
@@ -209,13 +146,7 @@ namespace persistent_data {
block_manager<BlockSize>::read_ref::~read_ref()
{
if (!--(*holders_)) {
if (block_->get_type() == BT_SUPERBLOCK) {
bm_->flush();
block_->unlock();
bm_->flush();
} else
block_->unlock();
fn_(bc_, b_);
delete holders_;
}
}
@@ -225,8 +156,9 @@ namespace persistent_data {
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
{
if (this != &rhs) {
block_ = rhs.block_;
bm_ = rhs.bm_;
bc_ = rhs.bc_;
b_ = rhs.b_;
fn_ = rhs.fn_;
holders_ = rhs.holders_;
(*holders_)++;
}
@@ -238,31 +170,40 @@ namespace persistent_data {
block_address
block_manager<BlockSize>::read_ref::get_location() const
{
return block_->get_location();
return b_.get_index();
}
template <uint32_t BlockSize>
void const *
block_manager<BlockSize>::read_ref::data() const
{
return block_->get_data();
return b_.get_data();
}
//--------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm,
typename block::ptr b)
: read_ref(bm, b)
block_manager<BlockSize>::write_ref::write_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn)
: read_ref(bc, b, fn)
{
b->mark_dirty();
}
template <uint32_t BlockSize>
void *
block_manager<BlockSize>::write_ref::data()
{
return read_ref::block_->get_data();
return read_ref::b_.get_data();
}
//--------------------------------
template <uint32_t BlockSize>
block_manager<BlockSize>::super_ref::super_ref(block_cache &bc,
block_cache::block &b,
put_behaviour_fn fn)
: write_ref(bc, b, fn) {
}
//----------------------------------------------------------------
@@ -282,8 +223,8 @@ namespace persistent_data {
block_manager<BlockSize>::read_lock(block_address location,
typename bcache::validator::ptr v) const
{
typename block::ptr b(new block(bc_, location, BT_NORMAL, v, false));
return read_ref(*this, b);
block_cache::block &b = bc_.get(location, block_cache::GF_CAN_BLOCK, v);
return read_ref(bc_, b, read_put);
}
template <uint32_t BlockSize>
@@ -291,8 +232,8 @@ namespace persistent_data {
block_manager<BlockSize>::write_lock(block_address location,
typename bcache::validator::ptr v)
{
typename block::ptr b(new block(bc_, location, BT_NORMAL, v, false));
return write_ref(*this, b);
block_cache::block &b = bc_.get(location, block_cache::GF_CAN_BLOCK, v);
return write_ref(bc_, b, write_put);
}
template <uint32_t BlockSize>
@@ -300,8 +241,8 @@ namespace persistent_data {
block_manager<BlockSize>::write_lock_zero(block_address location,
typename bcache::validator::ptr v)
{
typename block::ptr b(new block(bc_, location, BT_NORMAL, v, true));
return write_ref(*this, b);
block_cache::block &b = bc_.get(location, block_cache::GF_CAN_BLOCK | block_cache::GF_ZERO, v);
return write_ref(bc_, b, write_put);
}
template <uint32_t BlockSize>
@@ -309,8 +250,8 @@ namespace persistent_data {
block_manager<BlockSize>::superblock(block_address location,
typename bcache::validator::ptr v)
{
typename block::ptr b(new block(bc_, location, BT_SUPERBLOCK, v, false));
return write_ref(*this, b);
block_cache::block &b = bc_.get(location, block_cache::GF_CAN_BLOCK, v);
return super_ref(bc_, b, super_put);
}
template <uint32_t BlockSize>
@@ -318,8 +259,8 @@ namespace persistent_data {
block_manager<BlockSize>::superblock_zero(block_address location,
typename bcache::validator::ptr v)
{
typename block::ptr b(new block(bc_, location, BT_SUPERBLOCK, v, true));
return write_ref(*this, b);
block_cache::block &b = bc_.get(location, block_cache::GF_CAN_BLOCK | block_cache::GF_ZERO, v);
return super_ref(bc_, b, super_put);
}
template <uint32_t BlockSize>
@@ -329,13 +270,6 @@ namespace persistent_data {
return bc_.get_nr_blocks();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::write_block(typename block::ptr b) const
{
b->flush();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::flush() const