stldb::Database — Class which allows applications to connect to and use an STLdb database.
// In header: </Users/bobw/workspace/stldb_lib/stldb/Database.h> template<typename ManagedRegionType> class Database { public: // types typedef ManagedRegionType RegionType; // construct/copy/destruct Database(create_or_recover_t, const char *, const char *, std::size_t, void *, const char *, const char *, std::size_t, bool, std::list< container_proxy_type * >); Database(open_create_or_recover_t, const char *, const char *, std::size_t, void *, const char *, const char *, std::size_t, bool, std::list< container_proxy_type * >); Database(open_or_create_t, const char *, const char *, std::size_t, void *, const char *, const char *, std::size_t, bool, std::list< container_proxy_type * >); Database(open_or_recover_t, const char *, const char *, std::size_t, void *, const char *, const char *, std::size_t, bool, std::list< container_proxy_type * >); Database(open_only_t, const char *, const char *, void *, std::list< container_proxy_type * >); ~Database(); // public member functions Transaction * beginTransaction(Transaction * = NULL) ; exclusive_transaction * begin_exclusive_transaction(exclusive_transaction * = NULL) ; int commit(Transaction *, bool = false) ; int rollback(Transaction *) ; transaction_id_t checkpoint() ; std::vector< boost::filesystem::path > get_archivable_logs() ; std::vector< checkpoint_file_info > get_archivable_checkpoints() ; std::vector< boost::filesystem::path > get_current_logs() ; std::map< std::string, checkpoint_file_info > get_current_checkpoints() ; ManagedRegionType & getRegion() ; template<typename ContainerType> ContainerType * getContainer(const char *) ; void * add_container(container_proxy_type *) ; template<typename ContainerType> bool remove_container(const char *) ; container_proxy_type * getContainerProxy(std::string) ; const char * get_database_name() const; const char * get_database_directory() const; const char * get_checkpoint_directory() const; const char * get_logging_directory() const; std::size_t get_max_logfile_size() const; bool get_synchronous_logging() const; database_stats get_stats(bool = false) ; bool check_integrity() ; void close(bool = false) ; void set_invalid(bool) ; // public static functions static void remove(const char *, const char *, const char *, const char *) ; static void remove_region(const char *, const char *) ; // private member functions void find_or_construct_databaseinfo(std::list< container_proxy_type * > &, const char *, const char *, const char *, const char *, std::size_t, bool) ; void open_containers(std::list< container_proxy_type * > &) ; std::pair< transaction_id_t, transaction_id_t > load_containers(std::list< container_proxy_type * > &, std::map< container_proxy_type *, transaction_id_t > &) ; void safety_check() ; // private static functions static void remove_region_impl(const char *, const char *) ; };
ManagedRegionType is one of boost::interprocess::managed_shared_memory, managed_mapped_file, managed_heap_memory, managed_external_buffer, or managed_windows_shared_memory. Lock type is the mutex type which the database will use when synchronizing access to the database itself, and its own internal structures.
Database
public
construct/copy/destructDatabase(create_or_recover_t, const char * database_name, const char * database_directory, std::size_t initial_size, void * fixed_mapping_addr, const char * checkpoint_directory, const char * log_directory, std::size_t max_log_file_len, bool synchronous_logging, std::list< container_proxy_type * > containers);
Create or recover a database. If no database exists, it is created. If the database already exists, its shared memory region is deleted and reinitialized via recovery, regardless of whether that region appears valid. Opening a database in this way can recover from some types of errors in which the shared region contains badly corrupt data.
Database(open_create_or_recover_t, const char * database_name, const char * database_directory, std::size_t initial_size, void * fixed_mapping_addr, const char * checkpoint_directory, const char * log_directory, std::size_t max_log_file_len, bool synchronous_logging, std::list< container_proxy_type * > containers);
Open or Create a database, and recover as needed. If no database exists it is created. If the database exists, but its shared region is in need of recovery, then recovery is performed. Otherwise, the existing region is simply opened and reused. This method is fast if there is no need to create or recover the database.
Database(open_or_create_t, const char * database_name, const char * database_directory, std::size_t initial_size, void * fixed_mapping_addr, const char * checkpoint_directory, const char * log_directory, std::size_t max_log_file_len, bool synchronous_logging, std::list< container_proxy_type * > containers);
Open and optionally Create the database. If no database exists this method will create it. If a database already exists, this method will connect to the existing database region, unless recovery is needed. If recovery is needed, this constructor will throw an exception.
Database(open_or_recover_t, const char * database_name, const char * database_directory, std::size_t initial_size, void * fixed_mapping_addr, const char * checkpoint_directory, const char * log_directory, std::size_t max_log_file_len, bool synchronous_logging, std::list< container_proxy_type * > containers);
Open an existing database. If the database region does not already exist, an exception is thrown. If the region exists but appears to need recovery, then recovery is done and the region is recreated with the same size and configuration as the existing region.
Database(open_only_t, const char * database_name, const char * database_directory, void * fixed_mapping_addr, std::list< container_proxy_type * > containers);
Open an existing database. If the database region does not already exist, an exception is thrown. If the region exists but appears to need recovery, then an exception is thrown. This constructor is guaranteed to be fast if it succeeds.
~Database();
The destructor closes the database. If there are outstanding transactions in effect when the destructor is called, then the destructor will block until those transactions are either resolved, or time out (i.e. gating behavior.)
Database
public member functionsTransaction * beginTransaction(Transaction * reusable_t = NULL) ;
Begins a (normal) transaction. The new transaction is returned. Note that to improve efficiency, an existing completed (previously committed or aborted) transaction object may be passed as reusable_t. It is an error to pass an in-progress transaction in this way. Note that this method can block if there is currently an exclusive transaction in progress, or a thread waiting to start an exclusive transaction.
exclusive_transaction * begin_exclusive_transaction(exclusive_transaction * reusable_t = NULL) ;
Begins an exclusive transaction. Exclusive transactions are required for some operations on containers, as a matter of container design. Examples will be methods like swap() and clear(). An exclusive transaction is guaranteed to be the only transaction which is running against the database when it exists. Calling this method will most likely block the caller, as all existing regular transactions must be completed before the exclusive transaction is begun.
int commit(Transaction * t, bool diskless = false) ;
Commit the transaction (or exclusive transaction) passed.
int rollback(Transaction * t) ;
Rollback the transaction (or exclusive transaction) passed.
transaction_id_t checkpoint() ;
Writes out a checkpoint for each container which is registered with this database. Returns the highest txn_id which the checkpoint has guaranteed to be durable. This value can be used to eliminate log files which are no longer needed. NOTE: This works by calling a checkpoint method on each container in turn.
std::vector< boost::filesystem::path > get_archivable_logs() ;
Get archivable logs. This returns the set of log files which are no longer needed for recovery processing. This static version takes an explicit checkpoint_directory and logging_directory, and does not require an open database.
std::vector< checkpoint_file_info > get_archivable_checkpoints() ;
Get the set of obsolete checkpoint files in the directory passed.
Returns: |
a vector of checkpoint files no longer needed for recovery processing. |
std::vector< boost::filesystem::path > get_current_logs() ;
Get the set of log files which contain transactions that are not yet reflected in any checkpoints. This returns the set of log files which are needed for recovery processing.
Returns: |
a vector of log file names. |
std::map< std::string, checkpoint_file_info > get_current_checkpoints() ;
Get the set of current checkpoint files, containing the most recent completed checkpoint for each container in the database.
Returns: |
a map of container names to current checkpoint filename. |
ManagedRegionType & getRegion() ;
Return a reference to the Managed Shared Memory Region that this database uses for its in-memory containers.
template<typename ContainerType> ContainerType * getContainer(const char * name) ;
Gets a Container by Name. The Container must be one mentioned in the list given to the Database constructor.
void * add_container(container_proxy_type * proxy) ;
Adds the container, specified by the proxy, to the database. This works by invoking the openContainer() API of the proxy. No effort is made to load the container from prior checkpoints. The container is returned (as a void*). This is intended for adding new containers to an already open database.
template<typename ContainerType> bool remove_container(const char * name) ;
delete a container from the database.
container_proxy_type * getContainerProxy(std::string container_name) ;
Gets a container_proxy by id. The Container must be one mentioned in the list given to the Database constructor.
const char * get_database_name() const;Returns the name of the database, as given to the constructor.
const char * get_database_directory() const;Returns the name of the database, as given to the constructor.
const char * get_checkpoint_directory() const;Returns the checkpoint directory, as given to the constructor.
const char * get_logging_directory() const;Returns the directory being used for logging.
std::size_t get_max_logfile_size() const;Returns the maximum log file size, as specified to the constructor.
bool get_synchronous_logging() const;Returns whether syncronous logging is configured.
database_stats get_stats(bool reset = false) ;
Returns a stats object about the region, and the behavior of the associated logging subsystem.
bool check_integrity() ;
Check to see if the Database integrity is assured. This performs the registry check function to see if any process have disconnected.
Returns: |
true if the registry check passes, false if it fails. if returns false, the panic bit in the registry of the existing region is also set to prevent any further transactions from being started. |
void close(bool final_checkpoint = false) ;
Closes the database.
void set_invalid(bool value) ;
sets or clears the internal 'valid' flag on the Database. set_invalid(true), is identical to a Database failing a call to check_integrity(). All processes will start seeing recovery_needed exceptions until they close and Reconstruct the database, permitting recovery to occur. set_invalid(false) clears this flag, permitting processes to carry on using the current shared memory region, even though it may be corrupt.
Database
public static functionsstatic void remove(const char * database_name, const char * database_directory, const char * checkpoint_directory, const char * log_directory) ;
Removes the indicated database, and all of its containers, including backing checkpoints and logs. This method is only safe on a closed database, and an exception is thrown if any process has the database open.
static void remove_region(const char * database_name, const char * database_directory) ;
Remove the shared memory region for the indicated database. On some systems, this can only be done while no processes are attached to the database. The removal of the region forces it to be reloaded during a subsequent open request.
Database
private member functionsvoid find_or_construct_databaseinfo(std::list< container_proxy_type * > & containers, const char * database_name, const char * database_dir, const char * checkpoint_dir, const char * log_directory, std::size_t max_log_file_size, bool synchronous_logging) ;
void open_containers(std::list< container_proxy_type * > & containers) ;
std::pair< transaction_id_t, transaction_id_t > load_containers(std::list< container_proxy_type * > & containers, std::map< container_proxy_type *, transaction_id_t > & container_lsn) ;
void safety_check() ;
Check to see if the _registry->_database_invalid flag has been set, and if it throw a recovery_needed exception. This is used internally by other methods of Database to protect the app from using a region which may have hung locks or other forms of corruption.