Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Refactor database for RAII compliance
Browse files Browse the repository at this point in the history
Make chainbase::database implement RAII by having the constructor open and the destructor close.
As long as the database is constructed, it should be properly open and ready to use.
  • Loading branch information
nathanielhourt committed May 29, 2017
1 parent ea2e42d commit f1a150f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 39 deletions.
9 changes: 5 additions & 4 deletions include/chainbase/chainbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,12 @@ namespace chainbase {
read_write = 1
};

void open( const bfs::path& dir, uint32_t write = read_only, uint64_t shared_file_size = 0 );
bool is_open()const;
void close();
database(const bfs::path& dir, open_flags write = read_only, uint64_t shared_file_size = 0);
~database();
database(database&&) = default;
database& operator=(database&&) = default;
bool is_read_only() const { return _read_only; }
void flush();
void wipe( const bfs::path& dir );
void set_require_locking( bool enable_require_locking );

#ifdef CHAINBASE_CHECK_LOCKING
Expand Down
38 changes: 10 additions & 28 deletions src/chainbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ namespace chainbase {
bool windows = false;
};

void database::open( const bfs::path& dir, uint32_t flags, uint64_t shared_file_size ) {

database::database(const bfs::path& dir, open_flags flags, uint64_t shared_file_size) {
bool write = flags & database::read_write;

if( !bfs::exists( dir ) ) {
if( !write ) BOOST_THROW_EXCEPTION( std::runtime_error( "database file not found at " + dir.native() ) );
if (!bfs::exists(dir)) {
if(!write) BOOST_THROW_EXCEPTION( std::runtime_error( "database file not found at " + dir.native() ) );
}

bfs::create_directories( dir );
if( _data_dir != dir ) close();
bfs::create_directories(dir);

_data_dir = dir;
auto abs_path = bfs::absolute( dir / "shared_memory.bin" );
Expand Down Expand Up @@ -105,19 +103,7 @@ namespace chainbase {
}
}

bool database::is_open() const
{
return _segment && _meta && !_data_dir.empty();
}

void database::flush() {
if( _segment )
_segment->flush();
if( _meta )
_meta->flush();
}

void database::close()
database::~database()
{
_segment.reset();
_meta.reset();
Expand All @@ -126,15 +112,11 @@ namespace chainbase {
_data_dir = bfs::path();
}

void database::wipe( const bfs::path& dir )
{
_segment.reset();
_meta.reset();
bfs::remove_all( dir / "shared_memory.bin" );
bfs::remove_all( dir / "shared_memory.meta" );
_data_dir = bfs::path();
_index_list.clear();
_index_map.clear();
void database::flush() {
if( _segment )
_segment->flush();
if( _meta )
_meta->flush();
}

void database::set_require_locking( bool enable_require_locking )
Expand Down
9 changes: 2 additions & 7 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,8 @@ BOOST_AUTO_TEST_CASE( open_and_create ) {
try {
std::cerr << temp.native() << " \n";

chainbase::database db;
BOOST_CHECK_THROW( db.open( temp ), std::runtime_error ); /// temp does not exist

db.open( temp, database::read_write, 1024*1024*8 );

chainbase::database db2; /// open an already created db
db2.open( temp );
chainbase::database db(temp, database::read_write, 1024*1024*8);
chainbase::database db2(temp); /// open an already created db
BOOST_CHECK_THROW( db2.add_index< book_index >(), std::runtime_error ); /// index does not exist in read only database

db.add_index< book_index >();
Expand Down

0 comments on commit f1a150f

Please sign in to comment.