Introduce a mode enum to block_io<>

This commit is contained in:
Joe Thornber 2013-01-11 20:16:40 +00:00
parent 2b86309961
commit 779f8e1fd4
3 changed files with 33 additions and 8 deletions

View File

@ -82,8 +82,12 @@ namespace persistent_data {
class block_io : private boost::noncopyable { class block_io : private boost::noncopyable {
public: public:
typedef boost::shared_ptr<block_io> ptr; typedef boost::shared_ptr<block_io> ptr;
enum mode {
READ_ONLY,
READ_WRITE,
};
block_io(std::string const &path, block_address nr_blocks, bool writeable = false); block_io(std::string const &path, block_address nr_blocks, mode m);
~block_io(); ~block_io();
block_address get_nr_blocks() const { block_address get_nr_blocks() const {
@ -96,6 +100,7 @@ namespace persistent_data {
private: private:
int fd_; int fd_;
block_address nr_blocks_; block_address nr_blocks_;
mode mode_;
bool writeable_; bool writeable_;
}; };
@ -107,7 +112,7 @@ namespace persistent_data {
block_manager(std::string const &path, block_manager(std::string const &path,
block_address nr_blocks, block_address nr_blocks,
unsigned max_concurrent_locks, unsigned max_concurrent_locks,
bool writeable = false); typename block_io<BlockSize>::mode m);
class validator { class validator {
public: public:

View File

@ -36,12 +36,28 @@ using namespace std;
//---------------------------------------------------------------- //----------------------------------------------------------------
template <uint32_t BlockSize> template <uint32_t BlockSize>
block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, bool writeable) block_io<BlockSize>::block_io(std::string const &path, block_address nr_blocks, mode m)
: nr_blocks_(nr_blocks), : nr_blocks_(nr_blocks),
writeable_(writeable) mode_(m)
{ {
int fd_mode = O_DIRECT | O_SYNC;
switch (m) {
case READ_ONLY:
fd_mode |= O_RDONLY;
break;
case READ_WRITE:
fd_mode |= O_RDWR;
break;
default:
throw runtime_error("unsupported mode");
break;
}
// fd_ = ::open(path.c_str(), writeable ? (O_RDWR | O_CREAT) : O_RDONLY, 0666); // fd_ = ::open(path.c_str(), writeable ? (O_RDWR | O_CREAT) : O_RDONLY, 0666);
fd_ = ::open(path.c_str(), O_DIRECT | O_SYNC | (writeable ? O_RDWR : O_RDONLY), 0666); fd_ = ::open(path.c_str(), fd_mode, 0666);
if (fd_ < 0) if (fd_ < 0)
throw std::runtime_error("couldn't open file"); throw std::runtime_error("couldn't open file");
} }
@ -237,8 +253,8 @@ 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,
bool writeable) typename block_io<BlockSize>::mode mode)
: io_(new block_io<BlockSize>(path, nr_blocks, writeable)), : io_(new block_io<BlockSize>(path, nr_blocks, mode)),
cache_(max(64u, max_concurrent_blocks)), cache_(max(64u, max_concurrent_blocks)),
tracker_(0, nr_blocks) tracker_(0, nr_blocks)
{ {

View File

@ -91,7 +91,11 @@ namespace {
transaction_manager::ptr transaction_manager::ptr
open_tm(string const &dev_path, bool writeable) { open_tm(string const &dev_path, bool writeable) {
block_address nr_blocks = get_nr_blocks(dev_path); block_address nr_blocks = get_nr_blocks(dev_path);
block_manager<>::ptr bm(new block_manager<>(dev_path, nr_blocks, 1, writeable)); typename block_io<>::mode m = writeable ?
block_io<>::READ_WRITE :
block_io<>::READ_ONLY;
block_manager<>::ptr bm(new block_manager<>(dev_path, nr_blocks, 1, m));
space_map::ptr sm(new core_map(nr_blocks)); space_map::ptr sm(new core_map(nr_blocks));
sm->inc(SUPERBLOCK_LOCATION); sm->inc(SUPERBLOCK_LOCATION);
transaction_manager::ptr tm(new transaction_manager(bm, sm)); transaction_manager::ptr tm(new transaction_manager(bm, sm));