forked from blue/lmdbal
292 lines
8.4 KiB
C++
292 lines
8.4 KiB
C++
// Squawk messenger.
|
|
// Copyright (C) 2019 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/>.
|
|
|
|
#ifndef LMDBDATABASE_STORAGE_HPP
|
|
#define LMDBDATABASE_STORAGE_HPP
|
|
|
|
#include "storage.h"
|
|
#include "exceptions.h"
|
|
|
|
template<class K, class V>
|
|
LMDBDataBase::Storage<K, V>::Storage(const std::string& p_name, DataBase* parent):
|
|
StorageBase(p_name, parent),
|
|
keySerializer(new Serializer<K>()),
|
|
valueSerializer(new Serializer<V>())
|
|
{}
|
|
|
|
template<class K, class V>
|
|
LMDBDataBase::Storage<K, V>::~Storage() {
|
|
delete valueSerializer;
|
|
delete keySerializer;
|
|
}
|
|
|
|
template<class K, class V>
|
|
void LMDBDataBase::Storage<K, V>::addRecord(const K& key, const V& value) {
|
|
ensureOpened(addRecordMethodName);
|
|
|
|
TransactionID txn = beginTransaction();
|
|
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, txn, toString(key));
|
|
|
|
commitTransaction(txn);
|
|
}
|
|
|
|
template<class K, class V>
|
|
bool LMDBDataBase::Storage<K, V>::forceRecord(const K& key, const V& value) {
|
|
ensureOpened(forceRecordMethodName);
|
|
|
|
bool added;
|
|
TransactionID txn = beginTransaction();
|
|
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, txn);
|
|
}
|
|
|
|
lmdbData = valueSerializer->setData(value);
|
|
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
|
if (rc != MDB_SUCCESS)
|
|
throwUnknown(rc, txn);
|
|
|
|
commitTransaction(txn);
|
|
|
|
return added;
|
|
}
|
|
|
|
template<class K, class V>
|
|
void LMDBDataBase::Storage<K, V>::changeRecord(const K& key, const V& value) {
|
|
ensureOpened(changeRecordMethodName);
|
|
|
|
TransactionID txn = beginTransaction();
|
|
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, txn);
|
|
|
|
commitTransaction(txn);
|
|
}
|
|
|
|
template<class K, class V>
|
|
V LMDBDataBase::Storage<K, V>::getRecord(const K& key) const {
|
|
ensureOpened(getRecordMethodName);
|
|
|
|
TransactionID txn = beginReadOnlyTransaction();
|
|
MDB_val lmdbKey = keySerializer->setData(key);
|
|
MDB_val lmdbData;
|
|
|
|
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
|
if (rc != MDB_SUCCESS)
|
|
throwNotFoundOrUnknown(rc, txn, toString(key));
|
|
|
|
V value = valueSerializer->deserialize(lmdbData);
|
|
abortTransaction(txn);
|
|
|
|
return value;
|
|
}
|
|
|
|
template<class K, class V>
|
|
bool LMDBDataBase::Storage<K, V>::checkRecord(const K& key) const {
|
|
ensureOpened(checkRecordMethodName);
|
|
|
|
TransactionID txn = beginReadOnlyTransaction();
|
|
MDB_val lmdbKey = keySerializer->setData(key);
|
|
MDB_val lmdbData;
|
|
|
|
int rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
|
abortTransaction(txn);
|
|
|
|
if (rc == MDB_SUCCESS)
|
|
return true;
|
|
|
|
if (rc != MDB_NOTFOUND)
|
|
throwUnknown(rc);
|
|
|
|
return false;
|
|
}
|
|
|
|
template<class K, class V>
|
|
std::map<K, V> LMDBDataBase::Storage<K, V>::readAll() const {
|
|
ensureOpened(readAllMethodName);
|
|
|
|
TransactionID txn = beginReadOnlyTransaction();
|
|
std::map<K, V> result;
|
|
MDB_cursor* cursor;
|
|
MDB_val lmdbKey, lmdbData;
|
|
|
|
int rc = mdb_cursor_open(txn, dbi, &cursor);
|
|
if (rc != MDB_SUCCESS)
|
|
throwUnknown(rc, txn);
|
|
|
|
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST);
|
|
while (rc == MDB_SUCCESS) {
|
|
K key = keySerializer->deserialize(lmdbKey);
|
|
V value = valueSerializer->deserialize(lmdbData);
|
|
result.insert(std::make_pair(key, value));
|
|
rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT);
|
|
}
|
|
|
|
abortTransaction(txn);
|
|
if (rc != MDB_NOTFOUND)
|
|
throwUnknown(rc);
|
|
|
|
return result;
|
|
}
|
|
|
|
template<class K, class V>
|
|
void LMDBDataBase::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
|
|
ensureOpened(replaceAllMethodName);
|
|
|
|
TransactionID txn = beginTransaction();
|
|
int rc = drop(txn);
|
|
if (rc != MDB_SUCCESS)
|
|
throwUnknown(rc, txn);
|
|
|
|
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, txn);
|
|
}
|
|
commitTransaction(txn);
|
|
}
|
|
|
|
template<class K, class V>
|
|
uint32_t LMDBDataBase::Storage<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
|
|
ensureOpened(addRecordsMethodName);
|
|
|
|
TransactionID txn = beginTransaction();
|
|
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, txn);
|
|
}
|
|
|
|
MDB_stat stat;
|
|
rc = mdb_stat(txn, dbi, &stat);
|
|
if (rc != MDB_SUCCESS)
|
|
throwUnknown(rc, txn);
|
|
|
|
uint32_t amount = stat.ms_entries;
|
|
commitTransaction(txn);
|
|
|
|
return amount;
|
|
}
|
|
|
|
template<class K, class V>
|
|
void LMDBDataBase::Storage<K, V>::removeRecord(const K& key) {
|
|
ensureOpened(removeRecordMethodName);
|
|
|
|
TransactionID txn = beginTransaction();
|
|
MDB_val lmdbKey = keySerializer->setData(key);
|
|
int rc = mdb_del(txn, dbi, &lmdbKey, NULL);
|
|
if (rc != MDB_SUCCESS)
|
|
throwNotFoundOrUnknown(rc, txn, toString(key));
|
|
|
|
commitTransaction(txn);
|
|
}
|
|
|
|
template<class K, class V>
|
|
int LMDBDataBase::Storage<K, V>::createTable(MDB_txn* transaction) {
|
|
return makeTable<K>(transaction);
|
|
}
|
|
|
|
template<class K>
|
|
inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<uint64_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<uint32_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<uint16_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<uint8_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<int64_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<int32_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<int16_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<>
|
|
inline int LMDBDataBase::StorageBase::makeTable<int8_t>(MDB_txn* transaction) {
|
|
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
|
}
|
|
|
|
template<class T>
|
|
inline std::string LMDBDataBase::StorageBase::toString(const T& value) {
|
|
return std::to_string(value);
|
|
}
|
|
|
|
template<>
|
|
inline std::string LMDBDataBase::StorageBase::toString(const QString& value) {
|
|
return value.toStdString();
|
|
}
|
|
|
|
template<>
|
|
inline std::string LMDBDataBase::StorageBase::toString(const std::string& value) {
|
|
return value;
|
|
}
|
|
|
|
#endif //LMDBDATABASE_STORAGE_HPP
|