Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rebase refactor branch to master #1

Merged
merged 2 commits into from
Jun 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 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 Expand Up @@ -839,7 +840,8 @@ namespace chainbase {
{
CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType);
auto obj = find< ObjectType, IndexedByType >( std::forward< CompatibleKey >( key ) );
if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key" ) );
if( !obj )
BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key" ) );
return *obj;
}

Expand All @@ -848,7 +850,8 @@ namespace chainbase {
{
CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType);
auto obj = find< ObjectType >( key );
if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key") );
if( !obj )
BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key") );
return *obj;
}

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