Remove some global 'using namespace's

This commit is contained in:
Joe Thornber 2013-05-28 13:48:10 +01:00
parent 3066487814
commit 6706493304
9 changed files with 395 additions and 387 deletions

View File

@ -30,13 +30,9 @@
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
// FIXME: remove these from a header!
using namespace boost;
using namespace persistent_data;
using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
// FIXME: give this namesace a name
namespace { namespace {
using namespace std; using namespace std;
@ -105,433 +101,435 @@ namespace {
} }
}; };
template <uint32_t BlockSize> namespace persistent_data {
block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, mode m) template <uint32_t BlockSize>
: nr_blocks_(nr_blocks), block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, mode m)
mode_(m) : nr_blocks_(nr_blocks),
{ mode_(m)
off_t file_size = nr_blocks * BlockSize; {
off_t file_size = nr_blocks * BlockSize;
switch (m) { switch (m) {
case READ_ONLY: case READ_ONLY:
fd_ = open_block_file(path, file_size, false); fd_ = open_block_file(path, file_size, false);
break; break;
case READ_WRITE: case READ_WRITE:
fd_ = open_block_file(path, file_size, true); fd_ = open_block_file(path, file_size, true);
break; break;
case CREATE: case CREATE:
fd_ = create_block_file(path, file_size); fd_ = create_block_file(path, file_size);
break; break;
default: default:
throw runtime_error("unsupported mode"); throw runtime_error("unsupported mode");
} }
} }
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_io<BlockSize>::~block_io() block_io<BlockSize>::~block_io()
{ {
if (::close(fd_) < 0) if (::close(fd_) < 0)
syscall_failed("close"); syscall_failed("close");
} }
template <uint32_t BlockSize> template <uint32_t BlockSize>
void void
block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buffer) const block_io<BlockSize>::read_buffer(block_address location, buffer<BlockSize> &buffer) const
{ {
off_t r; off_t r;
r = ::lseek(fd_, BlockSize * location, SEEK_SET); r = ::lseek(fd_, BlockSize * location, SEEK_SET);
if (r == (off_t) -1) if (r == (off_t) -1)
throw std::runtime_error("lseek failed"); throw std::runtime_error("lseek failed");
ssize_t n; ssize_t n;
size_t remaining = BlockSize; size_t remaining = BlockSize;
unsigned char *buf = buffer.raw(); unsigned char *buf = buffer.raw();
do { do {
n = ::read(fd_, buf, remaining); n = ::read(fd_, buf, remaining);
if (n > 0) { if (n > 0) {
remaining -= n; remaining -= n;
buf += n; buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0)
throw std::runtime_error("read failed");
}
template <uint32_t BlockSize>
void
block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> const &buffer)
{
off_t r;
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
unsigned char const *buf = buffer.raw();
do {
n = ::write(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0) {
std::ostringstream out;
out << "write failed to block " << location
<< ", block size = " << BlockSize
<< ", remaining = " << remaining
<< ", n = " << n
<< ", errno = " << errno
<< ", fd_ = " << fd_
<< std::endl;
throw std::runtime_error(out.str());
} }
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0)
throw std::runtime_error("read failed");
}
template <uint32_t BlockSize>
void
block_io<BlockSize>::write_buffer(block_address location, buffer<BlockSize> const &buffer)
{
off_t r;
r = ::lseek(fd_, BlockSize * location, SEEK_SET);
if (r == (off_t) -1)
throw std::runtime_error("lseek failed");
ssize_t n;
size_t remaining = BlockSize;
unsigned char const *buf = buffer.raw();
do {
n = ::write(fd_, buf, remaining);
if (n > 0) {
remaining -= n;
buf += n;
}
} while (remaining && ((n > 0) || (n == EINTR) || (n == EAGAIN)));
if (n < 0) {
std::ostringstream out;
out << "write failed to block " << location
<< ", block size = " << BlockSize
<< ", remaining = " << remaining
<< ", n = " << n
<< ", errno = " << errno
<< ", fd_ = " << fd_
<< std::endl;
throw std::runtime_error(out.str());
} }
}
//---------------------------------------------------------------- //----------------------------------------------------------------
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io, block_manager<BlockSize>::block::block(typename block_io<BlockSize>::ptr io,
block_address location, block_address location,
block_type bt, block_type bt,
typename validator::ptr v, typename validator::ptr v,
bool zero) bool zero)
: io_(io), : io_(io),
location_(location), location_(location),
data_(new buffer<BlockSize>()), data_(new buffer<BlockSize>()),
validator_(v), validator_(v),
bt_(bt), bt_(bt),
dirty_(false) dirty_(false)
{ {
if (zero) { if (zero) {
// FIXME: duplicate memset // FIXME: duplicate memset
memset(data_->raw(), 0, BlockSize); memset(data_->raw(), 0, BlockSize);
dirty_ = true; // redundant? dirty_ = true; // redundant?
} else { } else {
io_->read_buffer(location_, *data_); io_->read_buffer(location_, *data_);
validator_->check(*data_, location_);
}
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block::~block()
{
flush();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::flush()
{
if (dirty_) {
validator_->prepare(*data_, location_);
io_->write_buffer(location_, *data_);
dirty_ = false;
}
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::change_validator(typename block_manager<BlockSize>::validator::ptr v,
bool check)
{
if (v.get() != validator_.get()) {
if (dirty_)
// It may have already happened, by calling
// this we ensure we're consistent.
validator_->prepare(*data_, location_);
validator_ = v;
if (check)
validator_->check(*data_, location_); validator_->check(*data_, location_);
}
}
template <uint32_t BlockSize>
block_manager<BlockSize>::block::~block()
{
flush();
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::flush()
{
if (dirty_) {
validator_->prepare(*data_, location_);
io_->write_buffer(location_, *data_);
dirty_ = false;
}
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::block::change_validator(typename block_manager<BlockSize>::validator::ptr v,
bool check)
{
if (v.get() != validator_.get()) {
if (dirty_)
// It may have already happened, by calling
// this we ensure we're consistent.
validator_->prepare(*data_, location_);
validator_ = v;
if (check)
validator_->check(*data_, location_);
}
} }
}
//---------------------------------------------------------------- //----------------------------------------------------------------
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm, block_manager<BlockSize>::read_ref::read_ref(block_manager<BlockSize> const &bm,
typename block::ptr b) typename block::ptr b)
: bm_(&bm), : bm_(&bm),
block_(b), block_(b),
holders_(new unsigned) holders_(new unsigned)
{ {
*holders_ = 1; *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_)
{
(*holders_)++;
}
template <uint32_t BlockSize>
block_manager<BlockSize>::read_ref::~read_ref()
{
if (!--(*holders_)) {
if (block_->bt_ == BT_SUPERBLOCK) {
bm_->flush();
bm_->cache_.put(block_);
bm_->flush();
} else
bm_->cache_.put(block_);
bm_->tracker_.unlock(block_->location_);
delete holders_;
} }
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref const & block_manager<BlockSize>::read_ref::read_ref(read_ref const &rhs)
block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs) : bm_(rhs.bm_),
{ block_(rhs.block_),
if (this != &rhs) { holders_(rhs.holders_)
block_ = rhs.block_; {
bm_ = rhs.bm_;
holders_ = rhs.holders_;
(*holders_)++; (*holders_)++;
} }
return *this; template <uint32_t BlockSize>
} block_manager<BlockSize>::read_ref::~read_ref()
{
if (!--(*holders_)) {
if (block_->bt_ == BT_SUPERBLOCK) {
bm_->flush();
bm_->cache_.put(block_);
bm_->flush();
} else
bm_->cache_.put(block_);
template <uint32_t BlockSize> bm_->tracker_.unlock(block_->location_);
block_address delete holders_;
block_manager<BlockSize>::read_ref::get_location() const }
{ }
return block_->location_;
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
buffer<BlockSize> const & typename block_manager<BlockSize>::read_ref const &
block_manager<BlockSize>::read_ref::data() const block_manager<BlockSize>::read_ref::operator =(read_ref const &rhs)
{ {
return *block_->data_; if (this != &rhs) {
} block_ = rhs.block_;
bm_ = rhs.bm_;
holders_ = rhs.holders_;
(*holders_)++;
}
return *this;
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::read_ref::get_location() const
{
return block_->location_;
}
template <uint32_t BlockSize>
buffer<BlockSize> const &
block_manager<BlockSize>::read_ref::data() const
{
return *block_->data_;
}
//-------------------------------- //--------------------------------
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm, block_manager<BlockSize>::write_ref::write_ref(block_manager<BlockSize> const &bm,
typename block::ptr b) typename block::ptr b)
: read_ref(bm, b) : read_ref(bm, b)
{ {
b->dirty_ = true; b->dirty_ = true;
} }
template <uint32_t BlockSize> template <uint32_t BlockSize>
buffer<BlockSize> & buffer<BlockSize> &
block_manager<BlockSize>::write_ref::data() block_manager<BlockSize>::write_ref::data()
{ {
return *read_ref::block_->data_; return *read_ref::block_->data_;
} }
//---------------------------------------------------------------- //----------------------------------------------------------------
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_manager<BlockSize>::block_manager(std::string const &path, block_manager<BlockSize>::block_manager(std::string const &path,
block_address nr_blocks, block_address nr_blocks,
unsigned max_concurrent_blocks, unsigned max_concurrent_blocks,
typename block_io<BlockSize>::mode mode) typename block_io<BlockSize>::mode mode)
: io_(new block_io<BlockSize>(path, nr_blocks, mode)), : io_(new block_io<BlockSize>(path, nr_blocks, mode)),
cache_(max(1024u, max_concurrent_blocks)), cache_(max(1024u, max_concurrent_blocks)),
tracker_(0, nr_blocks) tracker_(0, nr_blocks)
{ {
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
block_manager<BlockSize>::read_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v) const
{
tracker_.read_lock(location);
try {
check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location);
if (cached_block) {
typename block::ptr cb = *cached_block;
cb->check_read_lockable();
cb->change_validator(v);
return read_ref(*this, *cached_block);
}
typename block::ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return read_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
}
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
tracker_.write_lock(location);
try {
check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location);
if (cached_block) {
typename block::ptr cb = *cached_block;
cb->check_write_lockable();
cb->change_validator(v);
return write_ref(*this, *cached_block);
}
typename block::ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
} template <uint32_t BlockSize>
typename block_manager<BlockSize>::read_ref
block_manager<BlockSize>::read_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v) const
{
tracker_.read_lock(location);
try {
check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location);
template <uint32_t BlockSize> if (cached_block) {
typename block_manager<BlockSize>::write_ref typename block::ptr cb = *cached_block;
block_manager<BlockSize>::write_lock_zero(block_address location, cb->check_read_lockable();
typename block_manager<BlockSize>::validator::ptr v) cb->change_validator(v);
{
tracker_.write_lock(location);
try {
check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location); return read_ref(*this, *cached_block);
if (cached_block) { }
typename block::ptr cb = *cached_block;
cb->check_write_lockable();
cb->change_validator(v, false);
memset((*cached_block)->data_->raw(), 0, BlockSize);
return write_ref(*this, *cached_block); typename block::ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return read_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
}
}
template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::write_lock(block_address location,
typename block_manager<BlockSize>::validator::ptr v)
{
tracker_.write_lock(location);
try {
check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location);
if (cached_block) {
typename block::ptr cb = *cached_block;
cb->check_write_lockable();
cb->change_validator(v);
return write_ref(*this, *cached_block);
}
typename block::ptr b(new block(io_, location, BT_NORMAL, v));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
typename block::ptr b(new block(io_, location, BT_NORMAL, v, true));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock(block_address location, block_manager<BlockSize>::write_lock_zero(block_address location,
typename block_manager<BlockSize>::validator::ptr v) typename block_manager<BlockSize>::validator::ptr v)
{ {
tracker_.superblock_lock(location); tracker_.write_lock(location);
try { try {
check(location); check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location); boost::optional<typename block::ptr> cached_block = cache_.get(location);
if (cached_block) {
typename block::ptr cb = *cached_block;
cb->check_write_lockable();
cb->change_validator(v, false);
memset((*cached_block)->data_->raw(), 0, BlockSize);
if (cached_block) { return write_ref(*this, *cached_block);
typename block::ptr cb = *cached_block; }
cb->check_write_lockable();
cb->bt_ = BT_SUPERBLOCK;
cb->change_validator(v);
return write_ref(*this, *cached_block); typename block::ptr b(new block(io_, location, BT_NORMAL, v, true));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
typename block::ptr b(new block(io_, location, BT_SUPERBLOCK, v));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
typename block_manager<BlockSize>::write_ref typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::superblock_zero(block_address location, block_manager<BlockSize>::superblock(block_address location,
typename block_manager<BlockSize>::validator::ptr v) typename block_manager<BlockSize>::validator::ptr v)
{ {
tracker_.superblock_lock(location); tracker_.superblock_lock(location);
try { try {
check(location); check(location);
boost::optional<typename block::ptr> cached_block = cache_.get(location); boost::optional<typename block::ptr> cached_block = cache_.get(location);
if (cached_block) { if (cached_block) {
typename block::ptr cb = *cached_block; typename block::ptr cb = *cached_block;
cb->check_write_lockable(); cb->check_write_lockable();
cb->bt_ = BT_SUPERBLOCK; cb->bt_ = BT_SUPERBLOCK;
cb->change_validator(v, false); cb->change_validator(v);
memset(cb->data_->raw(), 0, BlockSize); // FIXME: add a zero method to buffer
return write_ref(*this, *cached_block); return write_ref(*this, *cached_block);
}
typename block::ptr b(new block(io_, location, BT_SUPERBLOCK, v));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
typename block::ptr b(new block(io_, location, BT_SUPERBLOCK, v, true));
cache_.insert(b);
return write_ref(*this, b);
} catch (...) {
tracker_.unlock(location);
throw;
} }
}
template <uint32_t BlockSize> template <uint32_t BlockSize>
void typename block_manager<BlockSize>::write_ref
block_manager<BlockSize>::check(block_address b) const block_manager<BlockSize>::superblock_zero(block_address location,
{ typename block_manager<BlockSize>::validator::ptr v)
if (b >= io_->get_nr_blocks()) {
throw std::runtime_error("block address out of bounds"); tracker_.superblock_lock(location);
} try {
check(location);
template <uint32_t BlockSize> boost::optional<typename block::ptr> cached_block = cache_.get(location);
block_address
block_manager<BlockSize>::get_nr_blocks() const
{
return io_->get_nr_blocks();
}
template <uint32_t BlockSize> if (cached_block) {
void typename block::ptr cb = *cached_block;
block_manager<BlockSize>::write_block(typename block::ptr b) const cb->check_write_lockable();
{ cb->bt_ = BT_SUPERBLOCK;
b->flush(); cb->change_validator(v, false);
} memset(cb->data_->raw(), 0, BlockSize); // FIXME: add a zero method to buffer
template <uint32_t BlockSize> return write_ref(*this, *cached_block);
void }
block_manager<BlockSize>::flush() const
{
cache_.iterate_unheld(
boost::bind(&block_manager<BlockSize>::write_block, this, _1));
}
template <uint32_t BlockSize> typename block::ptr b(new block(io_, location, BT_SUPERBLOCK, v, true));
bool cache_.insert(b);
block_manager<BlockSize>::is_locked(block_address b) const return write_ref(*this, b);
{
return tracker_.is_locked(b); } catch (...) {
tracker_.unlock(location);
throw;
}
}
template <uint32_t BlockSize>
void
block_manager<BlockSize>::check(block_address b) const
{
if (b >= io_->get_nr_blocks())
throw std::runtime_error("block address out of bounds");
}
template <uint32_t BlockSize>
block_address
block_manager<BlockSize>::get_nr_blocks() const
{
return io_->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
{
cache_.iterate_unheld(
boost::bind(&block_manager<BlockSize>::write_block, this, _1));
}
template <uint32_t BlockSize>
bool
block_manager<BlockSize>::is_locked(block_address b) const
{
return tracker_.is_locked(b);
}
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -380,7 +380,7 @@ namespace persistent_data {
private: private:
template <typename ValueTraits2, typename Search> template <typename ValueTraits2, typename Search>
optional<typename ValueTraits2::value_type> boost::optional<typename ValueTraits2::value_type>
lookup_raw(btree_detail::ro_spine &spine, block_address block, uint64_t key) const; lookup_raw(btree_detail::ro_spine &spine, block_address block, uint64_t key) const;
template <typename ValueTraits2> template <typename ValueTraits2>

View File

@ -24,9 +24,10 @@
#include <iostream> #include <iostream>
// FIXME: remove
using namespace base; using namespace base;
using namespace btree_detail;
using namespace persistent_data; using namespace persistent_data;
using namespace btree_detail;
using namespace std; using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -22,6 +22,8 @@
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace { namespace {
using namespace persistent_data;
class sm_careful_alloc : public checked_space_map { class sm_careful_alloc : public checked_space_map {
public: public:
typedef std::shared_ptr<sm_careful_alloc> ptr; typedef std::shared_ptr<sm_careful_alloc> ptr;

View File

@ -11,9 +11,11 @@ using namespace base;
//---------------------------------------------------------------- //----------------------------------------------------------------
block_address persistent_data::block_address
thin_provisioning::get_nr_blocks(string const &path) thin_provisioning::get_nr_blocks(string const &path)
{ {
using namespace persistent_data;
struct stat info; struct stat info;
block_address nr_blocks; block_address nr_blocks;

View File

@ -6,7 +6,7 @@
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace thin_provisioning { namespace thin_provisioning {
block_address get_nr_blocks(string const &path); persistent_data::block_address get_nr_blocks(string const &path);
} }
//---------------------------------------------------------------- //----------------------------------------------------------------

View File

@ -120,10 +120,10 @@ namespace thin_provisioning {
}; };
} }
block_manager<>::validator::ptr superblock_validator(); persistent_data::block_manager<>::validator::ptr superblock_validator();
superblock_detail::superblock read_superblock(block_manager<>::ptr bm); superblock_detail::superblock read_superblock(persistent_data::block_manager<>::ptr bm);
void check_superblock(block_manager<>::ptr bm, void check_superblock(persistent_data::block_manager<>::ptr bm,
superblock_detail::damage_visitor &visitor); superblock_detail::damage_visitor &visitor);
} }

View File

@ -2,6 +2,8 @@
#include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/core.h"
using namespace persistent_data;
//---------------------------------------------------------------- //----------------------------------------------------------------
void test::zero_block(block_manager<>::ptr bm, block_address b) void test::zero_block(block_manager<>::ptr bm, block_address b)
@ -10,7 +12,8 @@ void test::zero_block(block_manager<>::ptr bm, block_address b)
memset(&wr.data(), 0, sizeof(wr.data())); memset(&wr.data(), 0, sizeof(wr.data()));
} }
transaction_manager::ptr test::open_temporary_tm(block_manager<>::ptr bm) transaction_manager::ptr
test::open_temporary_tm(block_manager<>::ptr bm)
{ {
space_map::ptr sm(new core_map(bm->get_nr_blocks())); space_map::ptr sm(new core_map(bm->get_nr_blocks()));
transaction_manager::ptr tm(new transaction_manager(bm, sm)); transaction_manager::ptr tm(new transaction_manager(bm, sm));

View File

@ -22,6 +22,8 @@
//---------------------------------------------------------------- //----------------------------------------------------------------
namespace test { namespace test {
using namespace persistent_data;
unsigned const MAX_HELD_LOCKS = 16; unsigned const MAX_HELD_LOCKS = 16;
template <uint32_t BlockSize> template <uint32_t BlockSize>