forked from blue/lmdbal
RAII transactions
This commit is contained in:
parent
a9aa6b549f
commit
de741eda21
22 changed files with 689 additions and 222 deletions
|
@ -56,6 +56,23 @@ void LMDBAL::iStorage::close() {
|
|||
mdb_dbi_close(db->environment, dbi);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Checks if the transaction is still active, returns inner TransactionID
|
||||
*
|
||||
* This method is for internal usage only
|
||||
*
|
||||
* \param[in] txn - a transaction, you want to extract ID from
|
||||
* \param[in] action - a description of what you're going to do, in case of exception the error description will be verbose
|
||||
* \returns - Transaction inner TransactionID
|
||||
*
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
LMDBAL::TransactionID LMDBAL::iStorage::extractTransactionId(const Transaction& txn, const std::string& action) const {
|
||||
if (!txn.isActive())
|
||||
throw TransactionTerminated(db->name, name, action);
|
||||
|
||||
return txn.txn;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Drops content of a storage interface
|
||||
|
@ -68,7 +85,7 @@ void LMDBAL::iStorage::close() {
|
|||
void LMDBAL::iStorage::drop() {
|
||||
ensureOpened(dropMethodName);
|
||||
|
||||
TransactionID txn = db->beginTransaction();
|
||||
TransactionID txn = beginTransaction();
|
||||
int rc = iStorage::drop(txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
abortTransaction(txn);
|
||||
|
@ -76,6 +93,7 @@ void LMDBAL::iStorage::drop() {
|
|||
}
|
||||
|
||||
db->commitTransaction(txn);
|
||||
handleDrop();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,6 +108,21 @@ int LMDBAL::iStorage::drop(TransactionID transaction) {
|
|||
return mdb_drop(transaction, dbi, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Drops content of a storage interface (public transaction variant)
|
||||
*
|
||||
* Just performs content drop
|
||||
*
|
||||
* \param[in] txn - transaction ID, must be writable transaction!
|
||||
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
|
||||
*
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the transaction was not active
|
||||
*/
|
||||
int LMDBAL::iStorage::drop(const WriteTransaction& txn) {
|
||||
ensureOpened(dropMethodName);
|
||||
return drop(extractTransactionId(txn, dropMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Helper function, thows exception if the database is not opened
|
||||
*
|
||||
|
@ -127,7 +160,7 @@ LMDBAL::SizeType LMDBAL::iStorage::count() const {
|
|||
}
|
||||
|
||||
/**
|
||||
* \brief Storage size (transaction variant)
|
||||
* \brief Storage size (private transaction variant)
|
||||
*
|
||||
* \param[in] txn - transaction ID, can be read-only transaction
|
||||
* \returns amount of records in the storage
|
||||
|
@ -143,6 +176,21 @@ LMDBAL::SizeType LMDBAL::iStorage::count(TransactionID txn) const {
|
|||
return stat.ms_entries;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Storage size (public transaction variant)
|
||||
*
|
||||
* \param[in] txn - transaction, can be read-only transaction
|
||||
* \returns amount of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was closed
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happened
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the passed transaction not active, any action with it's inner ID is an error
|
||||
*/
|
||||
LMDBAL::SizeType LMDBAL::iStorage::count(const Transaction& txn) const {
|
||||
ensureOpened(countMethodName);
|
||||
return count(extractTransactionId(txn, countMethodName));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Throws LMDBAL::Exist or LMDBAL::Unknown (transaction vairiant)
|
||||
*
|
||||
|
@ -391,5 +439,4 @@ void LMDBAL::iStorage::transactionCommited(LMDBAL::TransactionID txn) {
|
|||
void LMDBAL::iStorage::transactionAborted(LMDBAL::TransactionID txn) const {
|
||||
UNUSED(txn);}
|
||||
|
||||
|
||||
|
||||
void LMDBAL::iStorage::handleDrop() {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue