2011-06-23 19:17:08 +05:30
|
|
|
#ifndef BLOCK_H
|
|
|
|
#define BLOCK_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2011-07-14 18:35:07 +05:30
|
|
|
#include <string.h>
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
uint32_t const MD_BLOCK_SIZE = 4096;
|
|
|
|
|
2011-07-14 18:35:07 +05:30
|
|
|
class count_adjuster {
|
|
|
|
public:
|
|
|
|
count_adjuster(unsigned &c)
|
|
|
|
: c_(c) {
|
|
|
|
c_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
~count_adjuster() {
|
|
|
|
c_--;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned &c_;
|
|
|
|
};
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
typedef uint64_t block_address;
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <uint32_t BlockSize = MD_BLOCK_SIZE>
|
2011-06-23 19:17:08 +05:30
|
|
|
class block_manager : private boost::noncopyable {
|
|
|
|
public:
|
|
|
|
typedef boost::shared_ptr<block_manager> ptr;
|
|
|
|
|
2011-07-13 19:39:33 +05:30
|
|
|
block_manager(std::string const &path, block_address nr_blocks);
|
2011-06-23 19:17:08 +05:30
|
|
|
~block_manager();
|
|
|
|
|
|
|
|
typedef unsigned char buffer[BlockSize];
|
|
|
|
typedef unsigned char const const_buffer[BlockSize];
|
|
|
|
|
|
|
|
class block;
|
|
|
|
|
2011-07-13 19:39:33 +05:30
|
|
|
class validator {
|
2011-06-23 19:17:08 +05:30
|
|
|
public:
|
2011-07-13 19:39:33 +05:30
|
|
|
typedef boost::shared_ptr<validator> ptr;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-13 19:39:33 +05:30
|
|
|
virtual ~validator() {}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
virtual void check(block const &b) const = 0;
|
|
|
|
virtual void prepare(block &b) const = 0;
|
|
|
|
};
|
|
|
|
|
2011-08-31 17:48:28 +05:30
|
|
|
class noop_validator : public validator {
|
|
|
|
public:
|
|
|
|
void check(block const &b) const {}
|
|
|
|
void prepare(block &b) const {}
|
|
|
|
};
|
|
|
|
|
2011-08-31 17:52:37 +05:30
|
|
|
enum block_type {
|
|
|
|
BT_SUPERBLOCK,
|
|
|
|
BT_NORMAL
|
|
|
|
};
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
struct block {
|
2011-07-13 19:39:33 +05:30
|
|
|
typedef boost::shared_ptr<block> ptr;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
block(block_address location,
|
2011-07-14 18:35:07 +05:30
|
|
|
const_buffer &data,
|
|
|
|
unsigned &count,
|
|
|
|
unsigned &type_count,
|
2011-08-31 17:52:37 +05:30
|
|
|
block_type bt,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v)
|
2011-06-23 19:17:08 +05:30
|
|
|
: location_(location),
|
2011-07-14 18:35:07 +05:30
|
|
|
adjuster_(count),
|
|
|
|
type_adjuster_(type_count),
|
2011-07-13 19:39:33 +05:30
|
|
|
validator_(v),
|
2011-08-31 17:52:37 +05:30
|
|
|
bt_(bt) {
|
2011-07-14 18:35:07 +05:30
|
|
|
::memcpy(data_, data, sizeof(data));
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address location_;
|
2011-07-14 18:35:07 +05:30
|
|
|
count_adjuster adjuster_;
|
|
|
|
count_adjuster type_adjuster_;
|
2011-06-23 19:17:08 +05:30
|
|
|
buffer data_;
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr validator_;
|
2011-08-31 17:52:37 +05:30
|
|
|
block_type bt_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
class read_ref {
|
|
|
|
public:
|
2011-07-13 19:39:33 +05:30
|
|
|
read_ref(typename block::ptr b);
|
2011-06-23 19:17:08 +05:30
|
|
|
virtual ~read_ref() {}
|
|
|
|
|
|
|
|
block_address get_location() const;
|
|
|
|
const_buffer &data() const;
|
|
|
|
|
|
|
|
protected:
|
2011-07-14 18:35:07 +05:30
|
|
|
friend class block_manager;
|
2011-07-13 19:39:33 +05:30
|
|
|
typename block::ptr block_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
// Inherited from read_ref, since you can read a block that's write
|
|
|
|
// locked.
|
|
|
|
class write_ref : public read_ref {
|
|
|
|
public:
|
2011-07-13 19:39:33 +05:30
|
|
|
write_ref(typename block::ptr b);
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
using read_ref::data;
|
|
|
|
buffer &data();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Locking methods
|
|
|
|
read_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
read_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator())) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
boost::optional<read_ref>
|
2011-06-27 15:15:30 +05:30
|
|
|
read_try_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator())) const;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
write_ref
|
2011-06-27 15:15:30 +05:30
|
|
|
write_lock_zero(block_address location,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
|
|
|
// The super block is the one that should be written last.
|
|
|
|
// Unlocking this block triggers the following events:
|
|
|
|
//
|
|
|
|
// i) synchronous write of all dirty blocks _except_ the
|
|
|
|
// superblock.
|
|
|
|
//
|
|
|
|
// ii) synchronous write of superblock
|
|
|
|
//
|
|
|
|
// If any locks are held at the time of the superblock
|
|
|
|
// being unlocked then an exception will be thrown.
|
|
|
|
write_ref superblock(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
write_ref superblock_zero(block_address b,
|
2011-08-31 17:48:28 +05:30
|
|
|
typename validator::ptr v =
|
|
|
|
typename validator::ptr(new noop_validator()));
|
2011-07-14 18:35:07 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address get_nr_blocks() const;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
private:
|
2011-07-13 19:39:33 +05:30
|
|
|
void check(block_address b) const;
|
|
|
|
|
2011-07-14 18:35:07 +05:30
|
|
|
void read_buffer(block_address location, buffer &buf) const;
|
|
|
|
void write_buffer(block_address location, const_buffer &buf);
|
|
|
|
void zero_buffer(buffer &buf) const;
|
|
|
|
void read_release(block *b) const;
|
|
|
|
void write_release(block *b);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
enum lock_type {
|
|
|
|
READ_LOCK,
|
|
|
|
WRITE_LOCK
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_lock(block_address b, lock_type t) const;
|
|
|
|
void unregister_lock(block_address b, lock_type t) const;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
int fd_;
|
2011-07-13 19:39:33 +05:30
|
|
|
block_address nr_blocks_;
|
2011-07-14 18:35:07 +05:30
|
|
|
mutable unsigned lock_count_;
|
|
|
|
mutable unsigned superblock_count_;
|
|
|
|
mutable unsigned ordinary_count_;
|
2011-07-22 20:39:56 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
typedef std::map<block_address, std::pair<lock_type, unsigned> > held_map;
|
|
|
|
mutable held_map held_locks_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:15:30 +05:30
|
|
|
#include "block.tcc"
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|