more duplicates test, misinterpreted something about duplicates, had to fallback

This commit is contained in:
Blue 2023-08-18 10:31:30 -03:00
parent 06e1aca45a
commit 180c40370c
Signed by: blue
GPG key ID: 9B203B252A63EE38
8 changed files with 126 additions and 89 deletions

View file

@ -48,12 +48,6 @@ class Cache;
typedef MDB_txn* TransactionID; /**<I'm going to use transaction pointers as transaction IDs*/
enum Duplicates { /**<Duplicates mode:*/
uniqueKey, /** - no duplicate keys allowed*/
uniquePair, /** - no duplicate key <b>AND</b> value pairs allowed*/
full /** - any configuration of duplicates goes*/
};
class Base {
friend class iStorage;
public:
@ -75,7 +69,7 @@ public:
void abortTransaction(TransactionID id) const;
template <class K, class V>
LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, Duplicates duplicates = uniqueKey);
LMDBAL::Storage<K, V>* addStorage(const std::string& storageName, bool duplicates = false);
template <class K, class V>
LMDBAL::Cache<K, V>* addCache(const std::string& storageName);
@ -121,7 +115,7 @@ private:
* The LMDBAL::Base must be closed
*
* \param[in] storageName - storage name
* \param[in] duplicates - LMDBAL::Duplicates duplicates mode (uniqueKey by default)
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*
* \returns storage pointer. LMDBAL::Base keeps the ownership of the added storage, you don't need to destoroy it
*
@ -132,7 +126,7 @@ private:
* \exception LMDBAL::StorageDuplicate thrown if somebody tries to add storage with repeating name
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, Duplicates duplicates) {
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
if (opened)
throw Opened(name, "add storage " + storageName);
@ -164,7 +158,7 @@ LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
if (opened)
throw Opened(name, "add cache " + storageName);
Cache<K, V>* cache = new Cache<K, V>(this, storageName, uniqueKey);
Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (iStorage*)cache));
if (!pair.second)
throw StorageDuplicate(name, storageName);

View file

@ -51,7 +51,7 @@ class Cache : public Storage<K, V> {
typedef std::map<TransactionID, Queue> TransactionCache;
protected:
Cache(Base* parent, const std::string& name, Duplicates duplicates = uniqueKey);
Cache(Base* parent, const std::string& name, bool duplicates = false);
~Cache() override;
virtual void transactionStarted(TransactionID txn, bool readOnly) const override;

View file

@ -41,10 +41,10 @@
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - LMDBAL::Duplicates duplicates mode (uniqueKey by default)
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
template<class K, class V>
LMDBAL::Cache<K, V>::Cache(Base* parent, const std::string& name, Duplicates duplicates):
LMDBAL::Cache<K, V>::Cache(Base* parent, const std::string& name, bool duplicates):
Storage<K, V>(parent, name, duplicates),
mode(Mode::nothing),
cache(new std::map<K, V>()),

View file

@ -33,9 +33,9 @@
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - LMDBAL::Duplicates duplicates mode (uniqueKey by default)
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
LMDBAL::iStorage::iStorage(Base* parent, const std::string& name, Duplicates duplicates):
LMDBAL::iStorage::iStorage(Base* parent, const std::string& name, bool duplicates):
dbi(),
db(parent),
name(name),

View file

@ -36,7 +36,7 @@ class iStorage {
friend class Base;
public:
protected:
iStorage(Base* parent, const std::string& name, Duplicates duplicates = uniqueKey);
iStorage(Base* parent, const std::string& name, bool duplicates = false);
virtual ~iStorage();
/**
@ -81,7 +81,7 @@ protected:
MDB_dbi dbi; /**<\brief lmdb storage handle*/
Base* db; /**<\brief parent database pointer (borrowed)*/
const std::string name; /**<\brief this storage name*/
const Duplicates duplicates; /**<\brief true if storage supports duplicates*/
const bool duplicates; /**<\brief true if storage supports duplicates*/
inline static const std::string dropMethodName = "drop"; /**<\brief member function name, just for exceptions*/
inline static const std::string countMethodName = "count"; /**<\brief member function name, just for exceptions*/
@ -99,7 +99,7 @@ protected:
protected:
template <class K, class V>
int makeStorage(MDB_txn* transaction, Duplicates duplicates = uniqueKey);
int makeStorage(MDB_txn* transaction, bool duplicates = false);
template <class T>
static std::string toString(const T& value);
@ -112,7 +112,7 @@ class Storage : public iStorage {
friend class Base;
friend class Cursor<K, V>;
protected:
Storage(Base* parent, const std::string& name, Duplicates duplicates = uniqueKey);
Storage(Base* parent, const std::string& name, bool duplicates = false);
~Storage() override;
virtual void discoveredRecord(const K& key, const V& value) const;

View file

@ -43,10 +43,10 @@
*
* \param[in] parent - LMDBAL::Base pointer for the owning database (borrowed)
* \param[in] name - the name of the storage
* \param[in] duplicates - LMDBAL::Duplicates duplicates mode (uniqueKey by default)
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*/
template<class K, class V>
LMDBAL::Storage<K, V>::Storage(Base* parent, const std::string& name, Duplicates duplicates):
LMDBAL::Storage<K, V>::Storage(Base* parent, const std::string& name, bool duplicates):
iStorage(parent, name, duplicates),
keySerializer(),
valueSerializer(),
@ -111,18 +111,11 @@ void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionI
MDB_val lmdbKey = keySerializer.setData(key);
MDB_val lmdbData = valueSerializer.setData(value);
unsigned int flags;
switch (duplicates) {
case uniqueKey:
flags = MDB_NOOVERWRITE;
break;
case uniquePair:
flags = MDB_NODUPDATA;
break;
case full:
flags = 0;
break;
}
unsigned int flags = 0;
if (duplicates)
flags |= MDB_NODUPDATA;
else
flags |= MDB_NOOVERWRITE;
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, flags);
if (rc != MDB_SUCCESS)
@ -808,7 +801,7 @@ void LMDBAL::Storage<K, V>::discoveredRecord(const K& key, const V& value, Trans
* \tparam K type of keys in opening storage
*
* \param[in] transaction - lmdb transaction to call <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a">mdb_dbi_open</a>, must be a writable transaction!
* \param[in] duplicates - true if you wish to enable duplicates support for the storage
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*
@ -817,12 +810,12 @@ void LMDBAL::Storage<K, V>::discoveredRecord(const K& key, const V& value, Trans
* This infrastructure also allowes us to customize mdb_dbi_open call in the future
*/
template<class K, class V>
inline int LMDBAL::iStorage::makeStorage(MDB_txn* transaction, Duplicates duplicates) {
inline int LMDBAL::iStorage::makeStorage(MDB_txn* transaction, bool duplicates) {
unsigned int flags = MDB_CREATE;
if constexpr (std::is_integral<K>::value)
flags |= MDB_INTEGERKEY;
if (duplicates != uniqueKey) {
if (duplicates) {
flags |= MDB_DUPSORT;
if constexpr (std::is_scalar<K>::value && std::is_scalar<V>::value)