lmdbal/src/storage.hpp

440 lines
12 KiB
C++
Raw Normal View History

/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 Yury Gubich <blue@macaw.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2023-03-20 15:37:13 +00:00
2023-03-21 11:05:54 +00:00
#ifndef LMDBAL_STORAGE_HPP
#define LMDBAL_STORAGE_HPP
2023-03-20 15:37:13 +00:00
#include "storage.h"
#include "exceptions.h"
template<class K, class V>
2023-03-21 11:05:54 +00:00
LMDBAL::Storage<K, V>::Storage(const std::string& p_name, Base* parent):
iStorage(p_name, parent),
2023-03-20 15:37:13 +00:00
keySerializer(new Serializer<K>()),
valueSerializer(new Serializer<V>())
{}
template<class K, class V>
2023-03-21 11:05:54 +00:00
LMDBAL::Storage<K, V>::~Storage() {
2023-03-20 15:37:13 +00:00
delete valueSerializer;
delete keySerializer;
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
2023-03-20 15:37:13 +00:00
ensureOpened(addRecordMethodName);
TransactionID txn = beginTransaction();
try {
Storage<K, V>::addRecord(key, value, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
commitTransaction(txn);
}
template<class K, class V>
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value, TransactionID txn) {
ensureOpened(addRecordMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData = valueSerializer->setData(value);
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != MDB_SUCCESS)
throwDuplicateOrUnknown(rc, toString(key));
}
template<class K, class V>
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
ensureOpened(forceRecordMethodName);
TransactionID txn = beginTransaction();
bool added;
try {
added = Storage<K, V>::forceRecord(key, value, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
2023-03-20 15:37:13 +00:00
commitTransaction(txn);
return added;
2023-03-20 15:37:13 +00:00
}
template<class K, class V>
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value, TransactionID txn) {
2023-03-20 15:37:13 +00:00
ensureOpened(forceRecordMethodName);
bool added;
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
switch (rc) {
case MDB_SUCCESS:
added = false;
break;
case MDB_NOTFOUND:
added = true;
break;
default:
added = false;
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
}
lmdbData = valueSerializer->setData(value);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
return added;
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
2023-03-20 15:37:13 +00:00
ensureOpened(changeRecordMethodName);
TransactionID txn = beginTransaction();
try {
Storage<K, V>::changeRecord(key, value, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
commitTransaction(txn);
}
template<class K, class V>
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value, TransactionID txn) {
ensureOpened(changeRecordMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData = valueSerializer->setData(value);
int rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
V LMDBAL::Storage<K, V>::getRecord(const K& key) const {
2023-03-20 15:37:13 +00:00
ensureOpened(getRecordMethodName);
V value;
Storage<K, V>::getRecord(key, value);
return value;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value) const {
ensureOpened(getRecordMethodName);
2023-03-20 15:37:13 +00:00
TransactionID txn = beginReadOnlyTransaction();
try {
Storage<K, V>::getRecord(key, value, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
abortTransaction(txn);
}
template<class K, class V>
V LMDBAL::Storage<K, V>::getRecord(const K& key, TransactionID txn) const {
ensureOpened(getRecordMethodName);
V value;
Storage<K, V>::getRecord(key, value, txn);
return value;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::getRecord(const K& key, V& value, TransactionID txn) const {
ensureOpened(getRecordMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key));
2023-03-20 15:37:13 +00:00
valueSerializer->deserialize(lmdbData, value);
2023-03-20 15:37:13 +00:00
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
bool LMDBAL::Storage<K, V>::checkRecord(const K& key) const {
2023-03-20 15:37:13 +00:00
ensureOpened(checkRecordMethodName);
TransactionID txn = beginReadOnlyTransaction();
bool result;
try {
result = Storage<K, V>::checkRecord(key, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
abortTransaction(txn);
return result;
}
template<class K, class V>
bool LMDBAL::Storage<K, V>::checkRecord(const K& key, TransactionID txn) const {
ensureOpened(checkRecordMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey = keySerializer->setData(key);
MDB_val lmdbData;
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
if (rc == MDB_SUCCESS)
return true;
if (rc != MDB_NOTFOUND)
throwUnknown(rc);
return false;
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
std::map<K, V> LMDBAL::Storage<K, V>::readAll() const {
2023-03-20 15:37:13 +00:00
ensureOpened(readAllMethodName);
std::map<K, V> result;
Storage<K, V>::readAll(result);
return result;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result) const {
ensureOpened(readAllMethodName);
2023-03-20 15:37:13 +00:00
TransactionID txn = beginReadOnlyTransaction();
try {
Storage<K, V>::readAll(result, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
abortTransaction(txn);
}
template<class K, class V>
std::map<K, V> LMDBAL::Storage<K, V>::readAll(TransactionID txn) const {
ensureOpened(readAllMethodName);
2023-03-20 15:37:13 +00:00
std::map<K, V> result;
Storage<K, V>::readAll(result, txn);
return result;
}
template<class K, class V>
void LMDBAL::Storage<K, V>::readAll(std::map<K, V>& result, TransactionID txn) const {
ensureOpened(readAllMethodName);
2023-03-20 15:37:13 +00:00
MDB_cursor* cursor;
MDB_val lmdbKey, lmdbData;
int rc = mdb_cursor_open(txn, dbi, &cursor);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
while (rc == MDB_SUCCESS) {
K key;
keySerializer->deserialize(lmdbKey, key);
V& value = result[key];
valueSerializer->deserialize(lmdbData, value);
2023-03-20 15:37:13 +00:00
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
}
if (rc != MDB_NOTFOUND)
throwUnknown(rc);
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
2023-03-20 15:37:13 +00:00
ensureOpened(replaceAllMethodName);
TransactionID txn = beginTransaction();
try {
Storage<K, V>::replaceAll(data, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
commitTransaction(txn);
}
template<class K, class V>
void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data, TransactionID txn) {
ensureOpened(replaceAllMethodName);
2023-03-20 15:37:13 +00:00
int rc = drop(txn);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey, lmdbData;
for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first);
lmdbData = valueSerializer->setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
}
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
2023-03-20 15:37:13 +00:00
ensureOpened(addRecordsMethodName);
TransactionID txn = beginTransaction();
uint32_t amount;
try {
amount = Storage<K, V>::addRecords(data, txn, overwrite);
} catch (...) {
abortTransaction(txn);
throw;
}
commitTransaction(txn);
return amount;
}
template<class K, class V>
uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, TransactionID txn, bool overwrite) {
ensureOpened(addRecordsMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey, lmdbData;
int rc;
for (const std::pair<const K, V>& pair : data) {
lmdbKey = keySerializer->setData(pair.first);
lmdbData = valueSerializer->setData(pair.second);
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
}
MDB_stat stat;
rc = mdb_stat(txn, dbi, &stat);
if (rc != MDB_SUCCESS)
throwUnknown(rc);
2023-03-20 15:37:13 +00:00
return stat.ms_entries;
2023-03-20 15:37:13 +00:00
}
template<class K, class V>
2023-03-21 11:05:54 +00:00
void LMDBAL::Storage<K, V>::removeRecord(const K& key) {
2023-03-20 15:37:13 +00:00
ensureOpened(removeRecordMethodName);
TransactionID txn = beginTransaction();
try {
Storage<K, V>::removeRecord(key, txn);
} catch (...) {
abortTransaction(txn);
throw;
}
commitTransaction(txn);
}
template<class K, class V>
void LMDBAL::Storage<K, V>::removeRecord(const K& key, TransactionID txn) {
ensureOpened(removeRecordMethodName);
2023-03-20 15:37:13 +00:00
MDB_val lmdbKey = keySerializer->setData(key);
int rc = mdb_del(txn, dbi, &lmdbKey, NULL);
if (rc != MDB_SUCCESS)
throwNotFoundOrUnknown(rc, toString(key));
2023-03-20 15:37:13 +00:00
}
template<class K, class V>
2023-04-02 13:00:21 +00:00
int LMDBAL::Storage<K, V>::createStorage(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return makeTable<K>(transaction);
}
template<class K>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<uint64_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<uint32_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<uint16_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<uint8_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<int64_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<int32_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<int16_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
2023-03-21 11:05:54 +00:00
inline int LMDBAL::iStorage::makeTable<int8_t>(MDB_txn* transaction) {
2023-03-20 15:37:13 +00:00
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<class T>
2023-03-21 11:05:54 +00:00
inline std::string LMDBAL::iStorage::toString(const T& value) {
2023-03-20 15:37:13 +00:00
return std::to_string(value);
}
template<>
2023-03-21 11:05:54 +00:00
inline std::string LMDBAL::iStorage::toString(const QString& value) {
2023-03-20 15:37:13 +00:00
return value.toStdString();
}
template<>
2023-03-21 11:05:54 +00:00
inline std::string LMDBAL::iStorage::toString(const std::string& value) {
2023-03-20 15:37:13 +00:00
return value;
}
2023-03-21 11:05:54 +00:00
#endif //LMDBAL_STORAGE_HPP