Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
chainbase::database Class Reference

#include <chainbase.hpp>

Classes

struct  session
 

Public Types

enum  open_flags { read_only = 0 , read_write = 1 }
 
using database_index_row_count_multiset = std::multiset<std::pair<unsigned, std::string>>
 

Public Member Functions

 database (const bfs::path &dir, open_flags write=read_only, uint64_t shared_file_size=0, bool allow_dirty=false, pinnable_mapped_file::map_mode=pinnable_mapped_file::map_mode::mapped)
 
 ~database ()
 
 database (database &&)=default
 
databaseoperator= (database &&)=default
 
bool is_read_only () const
 
void flush ()
 
void set_require_locking (bool enable_require_locking)
 
session start_undo_session (bool enabled)
 
int64_t revision () const
 
void undo ()
 
void squash ()
 
void commit (int64_t revision)
 
void undo_all ()
 
void set_revision (uint64_t revision)
 
template<typename MultiIndexType >
void add_index ()
 
pinnable_mapped_file::segment_managerget_segment_manager ()
 
const pinnable_mapped_file::segment_managerget_segment_manager () const
 
size_t get_free_memory () const
 
template<typename MultiIndexType >
const generic_index< MultiIndexType > & get_index () const
 
template<typename MultiIndexType , typename ByIndex >
auto get_index () const -> decltype(((generic_index< MultiIndexType > *)(nullptr)) ->indices().template get< ByIndex >())
 
template<typename MultiIndexType >
generic_index< MultiIndexType > & get_mutable_index ()
 
template<typename ObjectType , typename IndexedByType , typename CompatibleKey >
const ObjectType * find (CompatibleKey &&key) const
 
template<typename ObjectType >
const ObjectType * find (oid< ObjectType > key=oid< ObjectType >()) const
 
template<typename ObjectType , typename IndexedByType , typename CompatibleKey >
const ObjectType & get (CompatibleKey &&key) const
 
template<typename ObjectType >
const ObjectType & get (const oid< ObjectType > &key=oid< ObjectType >()) const
 
template<typename ObjectType , typename Modifier >
void modify (const ObjectType &obj, Modifier &&m)
 
template<typename ObjectType >
void remove (const ObjectType &obj)
 
template<typename ObjectType , typename Constructor >
const ObjectType & create (Constructor &&con)
 
database_index_row_count_multiset row_count_per_index () const
 

Detailed Description

This class

Definition at line 252 of file chainbase.hpp.

Member Typedef Documentation

◆ database_index_row_count_multiset

using chainbase::database::database_index_row_count_multiset = std::multiset<std::pair<unsigned, std::string>>

Definition at line 260 of file chainbase.hpp.

Member Enumeration Documentation

◆ open_flags

Enumerator
read_only 
read_write 

Definition at line 255 of file chainbase.hpp.

255 {
256 read_only = 0,
257 read_write = 1
258 };

Constructor & Destructor Documentation

◆ database() [1/2]

database::database ( const bfs::path & dir,
open_flags write = read_only,
uint64_t shared_file_size = 0,
bool allow_dirty = false,
pinnable_mapped_file::map_mode db_map_mode = pinnable_mapped_file::map_mode::mapped )

Definition at line 12 of file chainbase.cpp.

13 :
14 _db_file(dir, flags & database::read_write, shared_file_size, allow_dirty, db_map_mode),
15 _read_only(flags == database::read_only)
16 {
17 }
pInfo flags

◆ ~database()

database::~database ( )

Definition at line 19 of file chainbase.cpp.

20 {
21 _index_list.clear();
22 _index_map.clear();
23 }

◆ database() [2/2]

chainbase::database::database ( database && )
default

Member Function Documentation

◆ add_index()

template<typename MultiIndexType >
void chainbase::database::add_index ( )
inline

Definition at line 344 of file chainbase.hpp.

344 {
345 const uint16_t type_id = generic_index<MultiIndexType>::value_type::type_id;
346 typedef generic_index<MultiIndexType> index_type;
347 typedef typename index_type::allocator_type index_alloc;
348
349 std::string type_name = boost::core::demangle( typeid( typename index_type::value_type ).name() );
350
351 if( !( _index_map.size() <= type_id || _index_map[ type_id ] == nullptr ) ) {
352 BOOST_THROW_EXCEPTION( std::logic_error( type_name + "::type_id is already in use" ) );
353 }
354
355 index_type* idx_ptr = nullptr;
356 if( _read_only )
357 idx_ptr = _db_file.get_segment_manager()->find_no_lock< index_type >( type_name.c_str() ).first;
358 else
359 idx_ptr = _db_file.get_segment_manager()->find< index_type >( type_name.c_str() ).first;
360 bool first_time_adding = false;
361 if( !idx_ptr ) {
362 if( _read_only ) {
363 BOOST_THROW_EXCEPTION( std::runtime_error( "unable to find index for " + type_name + " in read only database" ) );
364 }
365 first_time_adding = true;
366 idx_ptr = _db_file.get_segment_manager()->construct< index_type >( type_name.c_str() )( index_alloc( _db_file.get_segment_manager() ) );
367 }
368
369 idx_ptr->validate();
370
371 // Ensure the undo stack of added index is consistent with the other indices in the database
372 if( _index_list.size() > 0 ) {
373 auto expected_revision_range = _index_list.front()->undo_stack_revision_range();
374 auto added_index_revision_range = idx_ptr->undo_stack_revision_range();
375
376 if( added_index_revision_range.first != expected_revision_range.first ||
377 added_index_revision_range.second != expected_revision_range.second ) {
378
379 if( !first_time_adding ) {
380 BOOST_THROW_EXCEPTION( std::logic_error(
381 "existing index for " + type_name + " has an undo stack (revision range [" +
382 std::to_string(added_index_revision_range.first) + ", " + std::to_string(added_index_revision_range.second) +
383 "]) that is inconsistent with other indices in the database (revision range [" +
384 std::to_string(expected_revision_range.first) + ", " + std::to_string(expected_revision_range.second) +
385 "]); corrupted database?"
386 ) );
387 }
388
389 if( _read_only ) {
390 BOOST_THROW_EXCEPTION( std::logic_error(
391 "new index for " + type_name +
392 " requires an undo stack that is consistent with other indices in the database; cannot fix in read-only mode"
393 ) );
394 }
395
396 idx_ptr->set_revision( static_cast<uint64_t>(expected_revision_range.first) );
397 while( idx_ptr->revision() < expected_revision_range.second ) {
398 idx_ptr->start_undo_session(true).push();
399 }
400 }
401 }
402
403 if( type_id >= _index_map.size() )
404 _index_map.resize( type_id + 1 );
405
406 auto new_index = new index<index_type>( *idx_ptr );
407 _index_map[ type_id ].reset( new_index );
408 _index_list.push_back( new_index );
409 }
std::string name
segment_manager * get_segment_manager() const
multi_index_to_undo_index< MultiIndexType > generic_index
string type_name
Definition abi_def.hpp:7
unsigned short uint16_t
Definition stdint.h:125
unsigned __int64 uint64_t
Definition stdint.h:136
Here is the call graph for this function:
Here is the caller graph for this function:

◆ commit()

void database::commit ( int64_t revision)

Definition at line 57 of file chainbase.cpp.

58 {
59 for( auto& item : _index_list )
60 {
61 item->commit( revision );
62 }
63 }
int64_t revision() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ create()

template<typename ObjectType , typename Constructor >
const ObjectType & chainbase::database::create ( Constructor && con)
inline

Definition at line 519 of file chainbase.hpp.

520 {
521 CHAINBASE_REQUIRE_WRITE_LOCK("create", ObjectType);
522 typedef typename get_index_type<ObjectType>::type index_type;
523 return get_mutable_index<index_type>().emplace( std::forward<Constructor>(con) );
524 }
#define CHAINBASE_REQUIRE_WRITE_LOCK(m, t)
Definition chainbase.hpp:45
generic_index< MultiIndexType > & get_mutable_index()
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [1/2]

template<typename ObjectType , typename IndexedByType , typename CompatibleKey >
const ObjectType * chainbase::database::find ( CompatibleKey && key) const
inline

Definition at line 458 of file chainbase.hpp.

459 {
460 CHAINBASE_REQUIRE_READ_LOCK("find", ObjectType);
461 typedef typename get_index_type< ObjectType >::type index_type;
462 const auto& idx = get_index< index_type >().indices().template get< IndexedByType >();
463 auto itr = idx.find( std::forward< CompatibleKey >( key ) );
464 if( itr == idx.end() ) return nullptr;
465 return &*itr;
466 }
#define CHAINBASE_REQUIRE_READ_LOCK(m, t)
Definition chainbase.hpp:44
const generic_index< MultiIndexType > & get_index() const
const ObjectType & get(CompatibleKey &&key) const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [2/2]

template<typename ObjectType >
const ObjectType * chainbase::database::find ( oid< ObjectType > key = oid< ObjectType >()) const
inline

Definition at line 469 of file chainbase.hpp.

470 {
471 CHAINBASE_REQUIRE_READ_LOCK("find", ObjectType);
472 typedef typename get_index_type< ObjectType >::type index_type;
473 return get_index< index_type >().find( key );
474 }
Here is the call graph for this function:

◆ flush()

void chainbase::database::flush ( )

◆ get() [1/2]

template<typename ObjectType , typename IndexedByType , typename CompatibleKey >
const ObjectType & chainbase::database::get ( CompatibleKey && key) const
inline

Definition at line 477 of file chainbase.hpp.

478 {
479 CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType);
480 auto obj = find< ObjectType, IndexedByType >( std::forward< CompatibleKey >( key ) );
481 if( !obj ) {
482 std::stringstream ss;
483 ss << "unknown key (" << boost::core::demangle( typeid( key ).name() ) << "): " << key;
484 BOOST_THROW_EXCEPTION( std::out_of_range( ss.str().c_str() ) );
485 }
486 return *obj;
487 }
const ObjectType * find(CompatibleKey &&key) const
static const Segment ss(Segment::ss)
uint8_t key[16]
Definition yubico_otp.c:41
Here is the call graph for this function:

◆ get() [2/2]

template<typename ObjectType >
const ObjectType & chainbase::database::get ( const oid< ObjectType > & key = oid< ObjectType >()) const
inline

Definition at line 490 of file chainbase.hpp.

491 {
492 CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType);
493 auto obj = find< ObjectType >( key );
494 if( !obj ) {
495 std::stringstream ss;
496 ss << "unknown key (" << boost::core::demangle( typeid( key ).name() ) << "): " << key._id;
497 BOOST_THROW_EXCEPTION( std::out_of_range( ss.str().c_str() ) );
498 }
499 return *obj;
500 }
Here is the call graph for this function:

◆ get_free_memory()

size_t chainbase::database::get_free_memory ( ) const
inline

Definition at line 419 of file chainbase.hpp.

420 {
421 return _db_file.get_segment_manager()->get_free_memory();
422 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_index() [1/2]

template<typename MultiIndexType >
const generic_index< MultiIndexType > & chainbase::database::get_index ( ) const
inline

Definition at line 425 of file chainbase.hpp.

426 {
427 CHAINBASE_REQUIRE_READ_LOCK("get_index", typename MultiIndexType::value_type);
428 typedef generic_index<MultiIndexType> index_type;
429 typedef index_type* index_type_ptr;
430 assert( _index_map.size() > index_type::value_type::type_id );
431 assert( _index_map[index_type::value_type::type_id] );
432 return *index_type_ptr( _index_map[index_type::value_type::type_id]->get() );
433 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_index() [2/2]

template<typename MultiIndexType , typename ByIndex >
auto chainbase::database::get_index ( ) const -> decltype( ((generic_index<MultiIndexType>*)( nullptr ))->indices().template get<ByIndex>() )
inline

Definition at line 436 of file chainbase.hpp.

437 {
438 CHAINBASE_REQUIRE_READ_LOCK("get_index", typename MultiIndexType::value_type);
439 typedef generic_index<MultiIndexType> index_type;
440 typedef index_type* index_type_ptr;
441 assert( _index_map.size() > index_type::value_type::type_id );
442 assert( _index_map[index_type::value_type::type_id] );
443 return index_type_ptr( _index_map[index_type::value_type::type_id]->get() )->indices().template get<ByIndex>();
444 }
Here is the call graph for this function:

◆ get_mutable_index()

template<typename MultiIndexType >
generic_index< MultiIndexType > & chainbase::database::get_mutable_index ( )
inline

Definition at line 447 of file chainbase.hpp.

448 {
449 CHAINBASE_REQUIRE_WRITE_LOCK("get_mutable_index", typename MultiIndexType::value_type);
450 typedef generic_index<MultiIndexType> index_type;
451 typedef index_type* index_type_ptr;
452 assert( _index_map.size() > index_type::value_type::type_id );
453 assert( _index_map[index_type::value_type::type_id] );
454 return *index_type_ptr( _index_map[index_type::value_type::type_id]->get() );
455 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_segment_manager() [1/2]

pinnable_mapped_file::segment_manager * chainbase::database::get_segment_manager ( )
inline

Definition at line 411 of file chainbase.hpp.

411 {
412 return _db_file.get_segment_manager();
413 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_segment_manager() [2/2]

const pinnable_mapped_file::segment_manager * chainbase::database::get_segment_manager ( ) const
inline

Definition at line 415 of file chainbase.hpp.

415 {
416 return _db_file.get_segment_manager();
417 }
Here is the call graph for this function:

◆ is_read_only()

bool chainbase::database::is_read_only ( ) const
inline

Definition at line 267 of file chainbase.hpp.

267{ return _read_only; }

◆ modify()

template<typename ObjectType , typename Modifier >
void chainbase::database::modify ( const ObjectType & obj,
Modifier && m )
inline

Definition at line 503 of file chainbase.hpp.

504 {
505 CHAINBASE_REQUIRE_WRITE_LOCK("modify", ObjectType);
506 typedef typename get_index_type<ObjectType>::type index_type;
507 get_mutable_index<index_type>().modify( obj, m );
508 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator=()

database & chainbase::database::operator= ( database && )
default

◆ remove()

template<typename ObjectType >
void chainbase::database::remove ( const ObjectType & obj)
inline

Definition at line 511 of file chainbase.hpp.

512 {
513 CHAINBASE_REQUIRE_WRITE_LOCK("remove", ObjectType);
514 typedef typename get_index_type<ObjectType>::type index_type;
515 return get_mutable_index<index_type>().remove( obj );
516 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ revision()

int64_t chainbase::database::revision ( ) const
inline

Definition at line 325 of file chainbase.hpp.

325 {
326 if( _index_list.size() == 0 ) return -1;
327 return _index_list[0]->revision();
328 }
Here is the caller graph for this function:

◆ row_count_per_index()

database_index_row_count_multiset chainbase::database::row_count_per_index ( ) const
inline

Definition at line 526 of file chainbase.hpp.

526 {
528 for(const auto& ai_ptr : _index_map) {
529 if(!ai_ptr)
530 continue;
531 ret.emplace(make_pair(ai_ptr->row_count(), ai_ptr->type_name()));
532 }
533 return ret;
534 }
std::multiset< std::pair< unsigned, std::string > > database_index_row_count_multiset
CK_RV ret
Here is the caller graph for this function:

◆ set_require_locking()

void database::set_require_locking ( bool enable_require_locking)

Definition at line 25 of file chainbase.cpp.

26 {
27#ifdef CHAINBASE_CHECK_LOCKING
28 _enable_require_locking = enable_require_locking;
29#endif
30 }

◆ set_revision()

void chainbase::database::set_revision ( uint64_t revision)
inline

Definition at line 336 of file chainbase.hpp.

337 {
338 CHAINBASE_REQUIRE_WRITE_LOCK( "set_revision", uint64_t );
339 for( auto i : _index_list ) i->set_revision( revision );
340 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ squash()

void database::squash ( )

Definition at line 49 of file chainbase.cpp.

50 {
51 for( auto& item : _index_list )
52 {
53 item->squash();
54 }
55 }

◆ start_undo_session()

database::session database::start_undo_session ( bool enabled)

Definition at line 73 of file chainbase.cpp.

74 {
75 if( enabled ) {
76 vector< std::unique_ptr<abstract_session> > _sub_sessions;
77 _sub_sessions.reserve( _index_list.size() );
78 for( auto& item : _index_list ) {
79 _sub_sessions.push_back( item->start_undo_session( enabled ) );
80 }
81 return session( std::move( _sub_sessions ) );
82 } else {
83 return session();
84 }
85 }
CK_SESSION_HANDLE session
Here is the caller graph for this function:

◆ undo()

void database::undo ( )

Definition at line 41 of file chainbase.cpp.

42 {
43 for( auto& item : _index_list )
44 {
45 item->undo();
46 }
47 }
Here is the caller graph for this function:

◆ undo_all()

void database::undo_all ( )

Definition at line 65 of file chainbase.cpp.

66 {
67 for( auto& item : _index_list )
68 {
69 item->undo_all();
70 }
71 }
Here is the caller graph for this function:

The documentation for this class was generated from the following files: