one more test for adding multiple entries, fix of leaking cursor, Storage::change method rework, beginning of documentation
This commit is contained in:
parent
064277fa6e
commit
6d21ecc155
3 changed files with 334 additions and 81 deletions
189
src/storage.hpp
189
src/storage.hpp
|
@ -22,6 +22,20 @@
|
|||
#include "storage.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
/**
|
||||
* \class LMDBAL::Storage
|
||||
* \brief This is a basic key value storage.
|
||||
*
|
||||
* \tparam K type of the keys of the storage
|
||||
* \tparam V type of the values of the storage
|
||||
*
|
||||
* You can receive an instance of this class calling LMDBAL::Base::addStorage(const std::string&)
|
||||
* if the database is yet closed and you're defining the storages you're going to need.
|
||||
* Or you can call LMDBAL::Base::getStorage(const std::string&) if the database is opened and you didn't save a pointer to the storage
|
||||
*
|
||||
* You are not supposed to instantiate or destory instances of this class yourself!
|
||||
*/
|
||||
|
||||
template<class K, class V>
|
||||
LMDBAL::Storage<K, V>::Storage(const std::string& p_name, Base* parent):
|
||||
iStorage(p_name, parent),
|
||||
|
@ -35,6 +49,18 @@ LMDBAL::Storage<K, V>::~Storage() {
|
|||
delete keySerializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage
|
||||
*
|
||||
* Take a note that if the storage already had a record you want to add LMDBAL::Exist is thrown
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
*
|
||||
* \exception LMDBAL::Exist thrown if the storage already has a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
|
||||
ensureOpened(addRecordMethodName);
|
||||
|
@ -49,6 +75,22 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
|
|||
commitTransaction(txn);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage (transaction variant)
|
||||
*
|
||||
* This function schedules an addition of a key-value record, but doesn't immidiately adds it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage already had a record you want to add LMDBAL::Exist is thrown
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
*
|
||||
* \exception LMDBAL::Exist thrown if the storage already has a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(addRecordMethodName);
|
||||
|
@ -61,6 +103,15 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionI
|
|||
throwDuplicateOrUnknown(rc, toString(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
* \returns true if the record was added, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
|
||||
ensureOpened(forceRecordMethodName);
|
||||
|
@ -78,6 +129,21 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
|
|||
return added;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Adds a key-value record to the storage, overwrites if it already exists (transaction variant)
|
||||
*
|
||||
* This function schedules an addition of a key-value record, but doesn't immidiately adds it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
* If the record did already exist in the database the actual overwrite will be done only after calling LMDBAL::Base::commitTransaction().
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value value of the record
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
* \returns true if the record was added, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(forceRecordMethodName);
|
||||
|
@ -107,6 +173,18 @@ bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, Transactio
|
|||
return added;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Changes key-value record to the storage.
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value new value of the record
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
|
||||
ensureOpened(changeRecordMethodName);
|
||||
|
@ -122,18 +200,55 @@ void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
|
|||
commitTransaction(txn);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Changes key-value record to the storage (transaction variant)
|
||||
*
|
||||
* This function schedules a modification of a key-value record, but doesn't immidiately changes it.
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record
|
||||
* \param[in] value new value of the record
|
||||
* \param[in] txn transaction ID, needs to be a writable transaction!
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
|
||||
ensureOpened(changeRecordMethodName);
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData = valueSerializer->setData(value);
|
||||
MDB_cursor* cursor;
|
||||
int rc = mdb_cursor_open(txn, dbi, &cursor);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
|
||||
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, nullptr, MDB_SET);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwNotFoundOrUnknown(rc, toString(key));
|
||||
|
||||
MDB_val lmdbData = valueSerializer->setData(value);
|
||||
rc = mdb_cursor_put(cursor, &lmdbKey, &lmdbData, MDB_CURRENT);
|
||||
mdb_cursor_close(cursor);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database
|
||||
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \returns the value from the storage
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
V LMDBAL::Storage<K, V>::getRecord(const K& key) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
@ -143,6 +258,18 @@ V LMDBAL::Storage<K, V>::getRecord(const K& key) const {
|
|||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (reference variant)
|
||||
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[out] value the value from the storage
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
@ -158,6 +285,22 @@ void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value) const {
|
|||
abortTransaction(txn);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (transaction variant)
|
||||
*
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
* \returns the value from the storage
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
V LMDBAL::Storage<K, V>::getRecord(const K& key, TransactionID txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
@ -167,6 +310,22 @@ V LMDBAL::Storage<K, V>::getRecord(const K& key, TransactionID txn) const {
|
|||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Gets the record from the database (transaction, reference variant)
|
||||
*
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* Take a note that if the storage didn't have a record you want to change LMDBAL::NotFound is thrown
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[out] value the value from the storage
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
*
|
||||
* \exception LMDBAL::NotFound thrown if the storage doesn't have a record with the given key
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn) const {
|
||||
ensureOpened(getRecordMethodName);
|
||||
|
@ -181,6 +340,15 @@ void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn)
|
|||
valueSerializer->deserialize(lmdbData, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Chechs if storage has value
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \returns true if there was a record with given key, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::checkRecord(const K& key) const {
|
||||
ensureOpened(checkRecordMethodName);
|
||||
|
@ -198,6 +366,19 @@ bool LMDBAL::Storage<K, V>::checkRecord(const K& key) const {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Chechs if storage has value (transaction variant)
|
||||
*
|
||||
* You can obtain LMDBAL::TransactionID calling LMDBAL::Base::beginReadOnlyTransaction() or LMDBAL::Base::beginTransaction().
|
||||
* If you just want to read data you should prefer LMDBAL::Base::beginReadOnlyTransaction().
|
||||
*
|
||||
* \param[in] key key of the record you look for
|
||||
* \param[in] txn transaction ID, can be read only transaction
|
||||
* \returns true if there was a record with given key, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was not opened
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happend within lmdb
|
||||
*/
|
||||
template<class K, class V>
|
||||
bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
|
||||
ensureOpened(checkRecordMethodName);
|
||||
|
@ -267,7 +448,7 @@ void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) c
|
|||
valueSerializer->deserialize(lmdbData, value);
|
||||
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
|
||||
}
|
||||
|
||||
mdb_cursor_close(cursor);
|
||||
if (rc != MDB_NOTFOUND)
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue