big refactoring part 1
This commit is contained in:
parent
6a8f67ac34
commit
19229f6c26
@ -30,15 +30,15 @@ endif ()
|
||||
|
||||
set(SOURCES
|
||||
exceptions.cpp
|
||||
table.cpp
|
||||
storage.cpp
|
||||
database.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
database.h
|
||||
exceptions.h
|
||||
table.h
|
||||
table.hpp
|
||||
storage.h
|
||||
storage.hpp
|
||||
cache.h
|
||||
cache.hpp
|
||||
serializer.h
|
||||
|
18
cache.h
18
cache.h
@ -14,16 +14,18 @@
|
||||
// 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 DATABASE_CACHE_H
|
||||
#define DATABASE_CACHE_H
|
||||
#ifndef LMDBDATABASE_CACHE_H
|
||||
#define LMDBDATABASE_CACHE_H
|
||||
|
||||
#include "database.h"
|
||||
#include "table.h"
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
#include "storage.h"
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template <class K, class V>
|
||||
class DataBase::Cache : public DataBase::Table<K, V> {
|
||||
class Cache : public Storage<K, V> {
|
||||
friend class DataBase;
|
||||
enum class Mode { //it's a cache state when we:
|
||||
nothing, // - know nothing about records in database on disk
|
||||
@ -46,7 +48,7 @@ public:
|
||||
virtual V getRecord(const K& key) const override;
|
||||
virtual uint32_t count() const override;
|
||||
|
||||
using DataBase::Table<K, V>::drop;
|
||||
using Storage<K, V>::drop;
|
||||
virtual int drop(MDB_txn * transaction) override;
|
||||
virtual std::map<K, V> readAll() const override;
|
||||
virtual void replaceAll(const std::map<K, V>& data) override;
|
||||
@ -59,6 +61,8 @@ protected:
|
||||
uint32_t* sizeDifference;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "cache.hpp"
|
||||
|
||||
#endif // DATABASE_CACHE_H
|
||||
#endif // LMDBDATABASE_CACHE_H
|
||||
|
170
cache.hpp
170
cache.hpp
@ -14,15 +14,15 @@
|
||||
// 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 DATABASE_CACHE_HPP
|
||||
#define DATABASE_CACHE_HPP
|
||||
#ifndef LMDBDATABASE_CACHE_HPP
|
||||
#define LMDBDATABASE_CACHE_HPP
|
||||
|
||||
#include "cache.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
template<class K, class V>
|
||||
DataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
|
||||
DataBase::Table<K, V>(p_name, parent),
|
||||
LMDBDataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
|
||||
Storage<K, V>(p_name, parent),
|
||||
mode(new Mode),
|
||||
cache(new std::map<K, V>()),
|
||||
abscent(new std::set<K>()),
|
||||
@ -33,7 +33,7 @@ DataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
DataBase::Cache<K, V>::~Cache() {
|
||||
LMDBDataBase::Cache<K, V>::~Cache() {
|
||||
delete sizeDifference;
|
||||
delete mode;
|
||||
delete cache;
|
||||
@ -41,73 +41,62 @@ DataBase::Cache<K, V>::~Cache() {
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("addRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
void LMDBDataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
|
||||
StorageBase::ensureOpened(StorageBase::addRecordMethodName);
|
||||
|
||||
if (cache->count(key) > 0) {
|
||||
throw Exist(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
if (cache->count(key) > 0)
|
||||
StorageBase::throwDuplicate(StorageBase::toString(key));
|
||||
|
||||
Table<K, V>::addRecord(key, value);
|
||||
Storage<K, V>::addRecord(key, value);
|
||||
cache->insert(std::make_pair(key, value));
|
||||
|
||||
if (*mode != Mode::full) {
|
||||
if (*mode != Mode::full)
|
||||
abscent->erase(key);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("forceRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
bool LMDBDataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
|
||||
StorageBase::ensureOpened(StorageBase::forceRecordMethodName);
|
||||
|
||||
bool added = Table<K, V>::forceRecord(key, value);
|
||||
bool added = Storage<K, V>::forceRecord(key, value);
|
||||
if (*mode == Mode::full) {
|
||||
(*cache)[key] = value;
|
||||
} else {
|
||||
if (added) {
|
||||
if (added)
|
||||
abscent->erase(key);
|
||||
}
|
||||
|
||||
std::pair<typename std::map<K, V>::iterator, bool> result = cache->insert(std::make_pair(key, value));
|
||||
if (!result.second) {
|
||||
if (!result.second)
|
||||
result.first->second = value;
|
||||
} else if (!added) { //this way database had value but cache didn't, so, need to decrease sizeDifference
|
||||
else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference
|
||||
handleMode();
|
||||
}
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("changeRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
void LMDBDataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
||||
StorageBase::ensureOpened(StorageBase::changeRecordMethodName);
|
||||
|
||||
if (*mode == Mode::full) {
|
||||
typename std::map<K, V>::iterator itr = cache->find(key);
|
||||
if (itr == cache->end()) {
|
||||
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
Table<K, V>::changeRecord(key, value);
|
||||
if (itr == cache->end())
|
||||
StorageBase::throwNotFound(StorageBase::toString(key));
|
||||
|
||||
Storage<K, V>::changeRecord(key, value);
|
||||
itr->second = value;
|
||||
} else {
|
||||
if (abscent->count(key) > 0) {
|
||||
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
if (abscent->count(key) > 0)
|
||||
StorageBase::throwNotFound(StorageBase::toString(key));
|
||||
|
||||
try {
|
||||
Table<K, V>::changeRecord(key, value);
|
||||
Storage<K, V>::changeRecord(key, value);
|
||||
typename std::pair<typename std::map<K, V>::iterator, bool> res = cache->insert(std::make_pair(key, value));
|
||||
if (!res.second) {
|
||||
if (!res.second)
|
||||
res.first->second = value;
|
||||
} else {
|
||||
else
|
||||
handleMode();
|
||||
}
|
||||
} catch (const NotFound& error) {
|
||||
abscent->insert(key);
|
||||
throw error;
|
||||
@ -116,70 +105,60 @@ void DataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
V DataBase::Cache<K, V>::getRecord(const K& key) const {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("getRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
V LMDBDataBase::Cache<K, V>::getRecord(const K& key) const {
|
||||
StorageBase::ensureOpened(StorageBase::getRecordMethodName);
|
||||
|
||||
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
||||
if (itr != cache->end()) {
|
||||
if (itr != cache->end())
|
||||
return itr->second;
|
||||
}
|
||||
|
||||
if (*mode == Mode::full || abscent->count(key) != 0) {
|
||||
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
if (*mode == Mode::full || abscent->count(key) != 0)
|
||||
StorageBase::throwNotFound(StorageBase::toString(key));
|
||||
|
||||
try {
|
||||
V value = Table<K, V>::getRecord(key);
|
||||
V value = Storage<K, V>::getRecord(key);
|
||||
cache->insert(std::make_pair(key, value));
|
||||
handleMode();
|
||||
return value;
|
||||
} catch (const NotFound& error) {
|
||||
if (*mode != Mode::full) {
|
||||
if (*mode != Mode::full)
|
||||
abscent->insert(key);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Cache<K, V>::checkRecord(const K& key) const {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("checkRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
bool LMDBDataBase::Cache<K, V>::checkRecord(const K& key) const {
|
||||
StorageBase::ensureOpened(StorageBase::checkRecordMethodName);
|
||||
|
||||
typename std::map<K, V>::const_iterator itr = cache->find(key);
|
||||
if (itr != cache->end()) {
|
||||
if (itr != cache->end())
|
||||
return true;
|
||||
}
|
||||
|
||||
if (*mode == Mode::full || abscent->count(key) != 0) {
|
||||
if (*mode == Mode::full || abscent->count(key) != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
V value = Table<K, V>::getRecord(key);
|
||||
V value = Storage<K, V>::getRecord(key);
|
||||
cache->insert(std::make_pair(key, value));
|
||||
handleMode();
|
||||
return true;
|
||||
} catch (const NotFound& error) {
|
||||
if (*mode != Mode::full) {
|
||||
if (*mode != Mode::full)
|
||||
abscent->insert(key);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
std::map<K, V> DataBase::Cache<K, V>::readAll() const {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("readAll", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
std::map<K, V> LMDBDataBase::Cache<K, V>::readAll() const {
|
||||
StorageBase::ensureOpened(StorageBase::readAllMethodName);
|
||||
|
||||
if (*mode != Mode::full) { //there is a room for optimization
|
||||
*mode = Mode::full; //I can read and deserialize only those values
|
||||
*cache = DataBase::Table<K, V>::readAll(); //that are missing in the cache
|
||||
*cache = Storage<K, V>::readAll(); //that are missing in the cache
|
||||
abscent->clear();
|
||||
*sizeDifference = 0;
|
||||
}
|
||||
@ -188,27 +167,32 @@ std::map<K, V> DataBase::Cache<K, V>::readAll() const {
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
|
||||
DataBase::Table<K, V>::replaceAll(data);
|
||||
void LMDBDataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
|
||||
Storage<K, V>::replaceAll(data);
|
||||
*cache = data;
|
||||
|
||||
if (*mode != Mode::full) {
|
||||
*mode = Mode::full;
|
||||
abscent->clear();
|
||||
*sizeDifference = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
uint32_t DataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
|
||||
uint32_t newSize = DataBase::Table<K, V>::addRecords(data, overwrite);
|
||||
uint32_t LMDBDataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
|
||||
uint32_t newSize = Storage<K, V>::addRecords(data, overwrite);
|
||||
|
||||
Mode& m = *mode;
|
||||
if (m == Mode::nothing) {
|
||||
if (m == Mode::nothing)
|
||||
m = Mode::size;
|
||||
}
|
||||
|
||||
std::map<K, V>& c = *cache;
|
||||
std::set<K>& a = *abscent;
|
||||
for (const std::pair<const K, V>& pair : data) {
|
||||
std::pair<typename std::map<K, V>::iterator, bool> res = c.insert(pair);
|
||||
if (!res.second) {
|
||||
if (overwrite) {
|
||||
if (overwrite)
|
||||
res.first->second = pair.second;
|
||||
}
|
||||
} else if (m != Mode::full) {
|
||||
a.erase(pair.first);
|
||||
}
|
||||
@ -226,31 +210,27 @@ uint32_t DataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool over
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::removeRecord(const K& key) {
|
||||
if (!DataBase::Table<K, V>::db->opened) {
|
||||
throw Closed("removeRecord", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
void LMDBDataBase::Cache<K, V>::removeRecord(const K& key) {
|
||||
StorageBase::ensureOpened(StorageBase::removeRecordMethodName);
|
||||
|
||||
typename std::pair<typename std::set<K>::const_iterator, bool> pair = abscent->insert(key);
|
||||
if (!pair.second) {
|
||||
throw NotFound(DataBase::_Table::toString(key), DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
|
||||
}
|
||||
if (!pair.second)
|
||||
StorageBase::throwNotFound(StorageBase::toString(key));
|
||||
|
||||
Table<K, V>::removeRecord(key);
|
||||
if (cache->erase(key) == 0) { //if it was not cached and we are now in size mode then the sizeDifference would decrease
|
||||
Storage<K, V>::removeRecord(key);
|
||||
if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease
|
||||
handleMode();
|
||||
}
|
||||
if (*mode != Mode::full) {
|
||||
|
||||
if (*mode != Mode::full)
|
||||
abscent->insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
uint32_t DataBase::Cache<K, V>::count() const {
|
||||
uint32_t LMDBDataBase::Cache<K, V>::count() const {
|
||||
switch (*mode) {
|
||||
case Mode::nothing:
|
||||
{
|
||||
uint32_t sz = DataBase::Table<K, V>::count();
|
||||
uint32_t sz = Storage<K, V>::count();
|
||||
*sizeDifference = sz - cache->size();
|
||||
if (sz == 0) {
|
||||
*mode = Mode::full;
|
||||
@ -264,11 +244,13 @@ uint32_t DataBase::Cache<K, V>::count() const {
|
||||
return cache->size() + *sizeDifference;
|
||||
case Mode::full:
|
||||
return cache->size();
|
||||
default:
|
||||
return 0; //unreachable, no such state, just to suppress the waring
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Cache<K, V>::handleMode() const {
|
||||
void LMDBDataBase::Cache<K, V>::handleMode() const {
|
||||
if (*mode == Mode::size) {
|
||||
--(*sizeDifference);
|
||||
if (*sizeDifference == 0) {
|
||||
@ -279,8 +261,8 @@ void DataBase::Cache<K, V>::handleMode() const {
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
|
||||
int res = DataBase::Table<K, V>::drop(transaction);
|
||||
int LMDBDataBase::Cache<K, V>::drop(MDB_txn * transaction) {
|
||||
int res = Storage<K, V>::drop(transaction);
|
||||
cache->clear();
|
||||
abscent->clear();
|
||||
*mode = Mode::full;
|
||||
@ -288,4 +270,4 @@ int DataBase::Cache<K, V>::drop(MDB_txn * transaction) {
|
||||
return res;
|
||||
}
|
||||
|
||||
#endif //DATABASE_CACHE_HPP
|
||||
#endif //LMDBDATABASE_CACHE_HPP
|
||||
|
151
database.cpp
151
database.cpp
@ -16,30 +16,30 @@
|
||||
|
||||
#include "database.h"
|
||||
#include "exceptions.h"
|
||||
#include "table.h"
|
||||
#include "storage.h"
|
||||
|
||||
DataBase::DataBase(const QString& p_name, uint16_t mapSize):
|
||||
LMDBDataBase::DataBase::DataBase(const QString& p_name, uint16_t mapSize):
|
||||
name(p_name.toStdString()),
|
||||
opened(false),
|
||||
size(mapSize),
|
||||
environment(),
|
||||
tables()
|
||||
{
|
||||
}
|
||||
tables(),
|
||||
transactions(new Transactions())
|
||||
{}
|
||||
|
||||
DataBase::~DataBase()
|
||||
{
|
||||
LMDBDataBase::DataBase::~DataBase() {
|
||||
close();
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
|
||||
delete transactions;
|
||||
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables)
|
||||
delete pair.second;
|
||||
}
|
||||
}
|
||||
|
||||
void DataBase::close()
|
||||
{
|
||||
void LMDBDataBase::DataBase::close() {
|
||||
if (opened) {
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
_Table* table = pair.second;
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
StorageBase* table = pair.second;
|
||||
mdb_dbi_close(environment, table->dbi);
|
||||
}
|
||||
mdb_env_close(environment);
|
||||
@ -47,8 +47,7 @@ void DataBase::close()
|
||||
}
|
||||
}
|
||||
|
||||
void DataBase::open()
|
||||
{
|
||||
void LMDBDataBase::DataBase::open() {
|
||||
if (!opened) {
|
||||
mdb_env_create(&environment);
|
||||
QString path = createDirectory();
|
||||
@ -60,40 +59,34 @@ void DataBase::open()
|
||||
MDB_txn *txn;
|
||||
mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
_Table* table = pair.second;
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
StorageBase* table = pair.second;
|
||||
int rc = table->createTable(txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
|
||||
}
|
||||
mdb_txn_commit(txn);
|
||||
opened = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool DataBase::removeDirectory()
|
||||
{
|
||||
if (opened) {
|
||||
bool LMDBDataBase::DataBase::removeDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "remove database directory");
|
||||
}
|
||||
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
QDir cache(path);
|
||||
|
||||
if (cache.exists()) {
|
||||
if (cache.exists())
|
||||
return cache.removeRecursively();
|
||||
} else {
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
QString DataBase::createDirectory()
|
||||
{
|
||||
if (opened) {
|
||||
QString LMDBDataBase::DataBase::createDirectory() {
|
||||
if (opened)
|
||||
throw Opened(name, "create database directory");
|
||||
}
|
||||
|
||||
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
|
||||
path += "/" + getName();
|
||||
@ -101,44 +94,100 @@ QString DataBase::createDirectory()
|
||||
|
||||
if (!cache.exists()) {
|
||||
bool res = cache.mkpath(path);
|
||||
if (!res) {
|
||||
if (!res)
|
||||
throw Directory(path.toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
QString LMDBDataBase::DataBase::getName() const {
|
||||
return QString::fromStdString(name);}
|
||||
|
||||
QString DataBase::getName() const
|
||||
{
|
||||
return QString::fromStdString(name);
|
||||
}
|
||||
bool LMDBDataBase::DataBase::ready() const {
|
||||
return opened;}
|
||||
|
||||
bool DataBase::ready() const
|
||||
{
|
||||
return opened;
|
||||
}
|
||||
|
||||
void DataBase::drop()
|
||||
{
|
||||
if (!opened) {
|
||||
void LMDBDataBase::DataBase::drop() {
|
||||
if (!opened)
|
||||
throw Closed("drop", name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
for (const std::pair<const std::string, _Table*>& pair : tables) {
|
||||
|
||||
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
|
||||
rc = pair.second->drop(txn);
|
||||
if (rc) {
|
||||
if (rc)
|
||||
throw Unknown(name, mdb_strerror(rc), pair.first);
|
||||
}
|
||||
}
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction() const {
|
||||
return beginReadOnlyTransaction(emptyName);}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction() const {
|
||||
return beginTransaction(emptyName);}
|
||||
|
||||
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id) const {
|
||||
return abortTransaction(id, emptyName);}
|
||||
|
||||
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id) const {
|
||||
return commitTransaction(id, emptyName);}
|
||||
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction(const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("beginReadOnlyTransaction", name, storageName);
|
||||
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
transactions->emplace(txn);
|
||||
return txn;
|
||||
}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction(const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("beginTransaction", name, storageName);
|
||||
|
||||
MDB_txn* txn;
|
||||
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
transactions->emplace(txn);
|
||||
return txn;
|
||||
}
|
||||
|
||||
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("abortTransaction", name, storageName);
|
||||
|
||||
Transactions::iterator itr = transactions->find(id);
|
||||
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
|
||||
throw Unknown(name, "unable to abort transaction: transaction was not found", storageName);
|
||||
|
||||
mdb_txn_abort(id);
|
||||
transactions->erase(itr);
|
||||
}
|
||||
|
||||
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
|
||||
if (!opened)
|
||||
throw Closed("abortTransaction", name, storageName);
|
||||
|
||||
Transactions::iterator itr = transactions->find(id);
|
||||
if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this
|
||||
throw Unknown(name, "unable to commit transaction: transaction was not found", storageName);
|
||||
|
||||
int rc = mdb_txn_commit(id);
|
||||
transactions->erase(itr);
|
||||
if (rc != MDB_SUCCESS)
|
||||
throw Unknown(name, mdb_strerror(rc), storageName);
|
||||
}
|
||||
|
90
database.h
90
database.h
@ -14,28 +14,41 @@
|
||||
// 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 CORE_DATABASE_H
|
||||
#define CORE_DATABASE_H
|
||||
#ifndef LMDBDATABASE_DATABASE_H
|
||||
#define LMDBDATABASE_DATABASE_H
|
||||
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
#include <limits>
|
||||
|
||||
#include <QString>
|
||||
#include <QStandardPaths>
|
||||
#include <QDir>
|
||||
|
||||
#include <lmdb.h>
|
||||
|
||||
class DataBase
|
||||
{
|
||||
class _Table;
|
||||
template<class T>
|
||||
class Serializer;
|
||||
#include "exceptions.h"
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
class StorageBase;
|
||||
|
||||
template<class T>
|
||||
class Serializer;
|
||||
|
||||
template <class K, class V>
|
||||
class Storage;
|
||||
|
||||
template <class K, class V>
|
||||
class Cache;
|
||||
|
||||
typedef MDB_txn* TransactionID;
|
||||
|
||||
class DataBase {
|
||||
friend class StorageBase;
|
||||
public:
|
||||
template <class K, class V>
|
||||
class Table;
|
||||
template <class K, class V>
|
||||
class Cache;
|
||||
|
||||
DataBase(const QString& name, uint16_t mapSize = 10);
|
||||
~DataBase();
|
||||
@ -48,69 +61,74 @@ public:
|
||||
QString getName() const;
|
||||
void drop();
|
||||
|
||||
TransactionID beginReadOnlyTransaction() const;
|
||||
TransactionID beginTransaction() const;
|
||||
void commitTransaction(TransactionID id) const;
|
||||
void abortTransaction(TransactionID id) const;
|
||||
|
||||
template <class K, class V>
|
||||
Table<K, V>* addTable(const std::string& name);
|
||||
Storage<K, V>* addTable(const std::string& name);
|
||||
|
||||
template <class K, class V>
|
||||
Cache<K, V>* addCache(const std::string& name);
|
||||
|
||||
template <class K, class V>
|
||||
Table<K, V>* getTable(const std::string& name);
|
||||
Storage<K, V>* getTable(const std::string& name);
|
||||
|
||||
template <class K, class V>
|
||||
Cache<K, V>* getCache(const std::string& name);
|
||||
|
||||
public:
|
||||
//exceptions
|
||||
class Exception;
|
||||
class Directory;
|
||||
class Closed;
|
||||
class Opened;
|
||||
class NotFound;
|
||||
class Exist;
|
||||
class Unknown;
|
||||
private:
|
||||
typedef std::map<std::string, StorageBase*> Tables;
|
||||
typedef std::set<TransactionID> Transactions;
|
||||
|
||||
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
|
||||
TransactionID beginTransaction(const std::string& storageName) const;
|
||||
void commitTransaction(TransactionID id, const std::string& storageName) const;
|
||||
void abortTransaction(TransactionID id, const std::string& storageName) const;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
bool opened;
|
||||
uint16_t size;
|
||||
MDB_env* environment;
|
||||
std::map<std::string, _Table*> tables;
|
||||
Tables tables;
|
||||
Transactions* transactions;
|
||||
|
||||
inline static const std::string emptyName = "";
|
||||
};
|
||||
|
||||
#include "exceptions.h"
|
||||
}
|
||||
#include "operators.hpp"
|
||||
|
||||
template <class K, class V>
|
||||
DataBase::Table<K, V>* DataBase::addTable(const std::string& p_name) {
|
||||
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::addTable(const std::string& p_name) {
|
||||
if (opened) {
|
||||
throw Opened(name, "add table " + p_name);
|
||||
}
|
||||
DataBase::Table<K, V>* table = new DataBase::Table<K, V>(p_name, this);
|
||||
tables.insert(std::make_pair(p_name, (_Table*)table));
|
||||
Storage<K, V>* table = new Storage<K, V>(p_name, this);
|
||||
tables.insert(std::make_pair(p_name, (StorageBase*)table));
|
||||
return table;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
DataBase::Cache<K, V> * DataBase::addCache(const std::string& p_name) {
|
||||
LMDBDataBase::Cache<K, V> * LMDBDataBase::DataBase::addCache(const std::string& p_name) {
|
||||
if (opened) {
|
||||
throw Opened(name, "add cache " + p_name);
|
||||
}
|
||||
DataBase::Cache<K, V>* cache = new DataBase::Cache<K, V>(p_name, this);
|
||||
tables.insert(std::make_pair(p_name, (_Table*)cache));
|
||||
Cache<K, V>* cache = new Cache<K, V>(p_name, this);
|
||||
tables.insert(std::make_pair(p_name, (StorageBase*)cache));
|
||||
return cache;
|
||||
}
|
||||
|
||||
template <class K, class V>
|
||||
DataBase::Table<K, V>* DataBase::getTable(const std::string& p_name) {
|
||||
return static_cast<DataBase::Table<K, V>*>(tables.at(p_name));
|
||||
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::getTable(const std::string& p_name) {
|
||||
return static_cast<Storage<K, V>*>(tables.at(p_name));
|
||||
}
|
||||
|
||||
template <class K, class V>
|
||||
DataBase::Cache<K, V>* DataBase::getCache(const std::string& p_name) {
|
||||
return static_cast<DataBase::Cache<K, V>*>(tables.at(p_name));
|
||||
LMDBDataBase::Cache<K, V>* LMDBDataBase::DataBase::getCache(const std::string& p_name) {
|
||||
return static_cast<Cache<K, V>*>(tables.at(p_name));
|
||||
}
|
||||
|
||||
|
||||
#endif // CORE_DATABASE_H
|
||||
#endif // LMDBDATABASE_DATABASE_H
|
||||
|
@ -16,26 +16,25 @@
|
||||
|
||||
#include "exceptions.h"
|
||||
|
||||
DataBase::Exception::Exception():
|
||||
std::exception()
|
||||
LMDBDataBase::Exception::Exception():
|
||||
std::exception()
|
||||
{}
|
||||
|
||||
DataBase::Exception::~Exception() {}
|
||||
LMDBDataBase::Exception::~Exception() {}
|
||||
|
||||
const char* DataBase::Exception::what() const noexcept( true )
|
||||
{
|
||||
const char* LMDBDataBase::Exception::what() const noexcept( true ) {
|
||||
std::string* msg = new std::string(getMessage());
|
||||
return msg->c_str();
|
||||
}
|
||||
|
||||
DataBase::Directory::Directory(const std::string& p_path):
|
||||
LMDBDataBase::Directory::Directory(const std::string& p_path):
|
||||
Exception(),
|
||||
path(p_path) {}
|
||||
|
||||
std::string DataBase::Directory::getMessage() const {
|
||||
std::string LMDBDataBase::Directory::getMessage() const {
|
||||
return "Can't create directory for database at " + path;}
|
||||
|
||||
DataBase::Closed::Closed(
|
||||
LMDBDataBase::Closed::Closed(
|
||||
const std::string& p_operation,
|
||||
const std::string& p_dbName,
|
||||
const std::optional<std::string>& p_tableName
|
||||
@ -45,29 +44,28 @@ DataBase::Closed::Closed(
|
||||
dbName(p_dbName),
|
||||
tableName(p_tableName) {}
|
||||
|
||||
std::string DataBase::Closed::getMessage() const {
|
||||
std::string LMDBDataBase::Closed::getMessage() const {
|
||||
std::string msg = "An attempt to perform operation " + operation
|
||||
+ " on closed database " + dbName;
|
||||
if (tableName.has_value()) {
|
||||
if (tableName.has_value())
|
||||
msg += + " on table " + tableName.value();
|
||||
}
|
||||
return msg;
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
DataBase::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
|
||||
LMDBDataBase::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
|
||||
Exception(),
|
||||
dbName(p_dbName),
|
||||
action(p_action) {}
|
||||
|
||||
|
||||
std::string DataBase::Opened::getMessage() const {
|
||||
std::string LMDBDataBase::Opened::getMessage() const {
|
||||
return "An attempt to " + action
|
||||
+ " (the database " + dbName
|
||||
+ ") but it's can't be done because the DataBase is already opened";
|
||||
}
|
||||
|
||||
DataBase::NotFound::NotFound(
|
||||
LMDBDataBase::NotFound::NotFound(
|
||||
const std::string& p_key,
|
||||
const std::string& p_dbName,
|
||||
const std::string& p_tableName
|
||||
@ -77,12 +75,12 @@ DataBase::NotFound::NotFound(
|
||||
dbName(p_dbName),
|
||||
tableName(p_tableName) {}
|
||||
|
||||
std::string DataBase::NotFound::getMessage() const {
|
||||
std::string LMDBDataBase::NotFound::getMessage() const {
|
||||
return "Element for id " + key + " wasn't found "
|
||||
+ " in database " + dbName
|
||||
+ " in table " + tableName;}
|
||||
|
||||
DataBase::Exist::Exist(
|
||||
LMDBDataBase::Exist::Exist(
|
||||
const std::string& p_key,
|
||||
const std::string& p_dbName,
|
||||
const std::string& p_tableName
|
||||
@ -92,14 +90,14 @@ DataBase::Exist::Exist(
|
||||
dbName(p_dbName),
|
||||
tableName(p_tableName) {}
|
||||
|
||||
std::string DataBase::Exist::getMessage() const {
|
||||
std::string LMDBDataBase::Exist::getMessage() const {
|
||||
return "An attempt to insert element with key " + key
|
||||
+ " to database " + dbName
|
||||
+ " to table " + tableName
|
||||
+ " but it already has an element with given id";
|
||||
}
|
||||
|
||||
DataBase::Unknown::Unknown(
|
||||
LMDBDataBase::Unknown::Unknown(
|
||||
const std::string& p_dbName,
|
||||
const std::string& message,
|
||||
const std::optional<std::string>& p_tableName
|
||||
@ -109,12 +107,11 @@ DataBase::Unknown::Unknown(
|
||||
tableName(p_tableName),
|
||||
msg(message) {}
|
||||
|
||||
std::string DataBase::Unknown::getMessage() const
|
||||
{
|
||||
std::string LMDBDataBase::Unknown::getMessage() const {
|
||||
std::string result = "Unknown error in database " + dbName;
|
||||
if (tableName.has_value()) {
|
||||
if (tableName.has_value())
|
||||
result += " in table " + tableName.value();
|
||||
}
|
||||
|
||||
result += ": " + msg;
|
||||
return result;
|
||||
}
|
||||
|
25
exceptions.h
25
exceptions.h
@ -14,17 +14,16 @@
|
||||
// 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 CORE_DATABASE_EXCEPTIONS_H
|
||||
#define CORE_DATABASE_EXCEPTIONS_H
|
||||
#ifndef LMDBDATABASE_EXCEPTIONS_H
|
||||
#define LMDBDATABASE_EXCEPTIONS_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
#include "database.h"
|
||||
namespace LMDBDataBase {
|
||||
|
||||
class DataBase::Exception : public std::exception
|
||||
{
|
||||
class Exception : public std::exception {
|
||||
public:
|
||||
Exception();
|
||||
virtual ~Exception();
|
||||
@ -34,7 +33,7 @@ public:
|
||||
const char* what() const noexcept( true );
|
||||
};
|
||||
|
||||
class DataBase::Directory: public DataBase::Exception {
|
||||
class Directory: public Exception {
|
||||
public:
|
||||
Directory(const std::string& path);
|
||||
|
||||
@ -43,7 +42,7 @@ private:
|
||||
std::string path;
|
||||
};
|
||||
|
||||
class DataBase::Closed : public DataBase::Exception {
|
||||
class Closed : public Exception {
|
||||
public:
|
||||
Closed(const std::string& p_operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt);
|
||||
|
||||
@ -54,7 +53,7 @@ private:
|
||||
std::optional<std::string> tableName;
|
||||
};
|
||||
|
||||
class DataBase::Opened : public DataBase::Exception {
|
||||
class Opened : Exception {
|
||||
public:
|
||||
Opened(const std::string& dbName, const std::string& action);
|
||||
|
||||
@ -64,7 +63,7 @@ private:
|
||||
std::string action;
|
||||
};
|
||||
|
||||
class DataBase::NotFound : public DataBase::Exception {
|
||||
class NotFound : public Exception {
|
||||
public:
|
||||
NotFound(const std::string& key, const std::string& dbName, const std::string& tableName);
|
||||
|
||||
@ -75,7 +74,7 @@ private:
|
||||
std::string tableName;
|
||||
};
|
||||
|
||||
class DataBase::Exist : public DataBase::Exception {
|
||||
class Exist : public Exception {
|
||||
public:
|
||||
Exist(const std::string& key, const std::string& dbName, const std::string& tableName);
|
||||
|
||||
@ -86,7 +85,7 @@ private:
|
||||
std::string tableName;
|
||||
};
|
||||
|
||||
class DataBase::Unknown : public DataBase::Exception {
|
||||
class Unknown : public Exception {
|
||||
public:
|
||||
Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt);
|
||||
|
||||
@ -97,4 +96,6 @@ private:
|
||||
std::string msg;
|
||||
};
|
||||
|
||||
#endif //CORE_DATABASE_EXCEPTIONS_H
|
||||
}
|
||||
|
||||
#endif //LMDBDATABASE_EXCEPTIONS_H
|
||||
|
13
serializer.h
13
serializer.h
@ -14,8 +14,8 @@
|
||||
// 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 CORE_DATABASE_SERIALIZER_H
|
||||
#define CORE_DATABASE_SERIALIZER_H
|
||||
#ifndef LMDBDATABASE_SERIALIZER_H
|
||||
#define LMDBDATABASE_SERIALIZER_H
|
||||
|
||||
#include <cstring>
|
||||
|
||||
@ -25,9 +25,10 @@
|
||||
|
||||
#include "database.h"
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<class T>
|
||||
class DataBase::Serializer
|
||||
{
|
||||
class Serializer {
|
||||
public:
|
||||
Serializer();
|
||||
Serializer(const T& value);
|
||||
@ -48,6 +49,8 @@ private:
|
||||
QDataStream stream;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#include "serializer.hpp"
|
||||
#include "serializer_uint64.hpp"
|
||||
#include "serializer_uint32.hpp"
|
||||
@ -63,4 +66,4 @@ private:
|
||||
#include "serializer_qstring.hpp"
|
||||
#include "serializer_qbytearray.hpp"
|
||||
|
||||
#endif // CORE_DATABASE_SERIALIZER_H
|
||||
#endif // LMDBDATABASE_SERIALIZER_H
|
||||
|
@ -14,14 +14,13 @@
|
||||
// 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 CORE_DATABASE_SERIALIZER_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_HPP
|
||||
#ifndef LMDBDATABASE_SERIALIZER_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_HPP
|
||||
|
||||
#include "serializer.h"
|
||||
|
||||
template<class T>
|
||||
DataBase::Serializer<T>::Serializer() :
|
||||
LMDBDataBase::Serializer<T>::Serializer() :
|
||||
bytes(),
|
||||
buffer(&bytes),
|
||||
stream(&buffer)
|
||||
@ -30,7 +29,7 @@ DataBase::Serializer<T>::Serializer() :
|
||||
}
|
||||
|
||||
template<class T>
|
||||
DataBase::Serializer<T>::Serializer(const T& value) :
|
||||
LMDBDataBase::Serializer<T>::Serializer(const T& value) :
|
||||
bytes(),
|
||||
buffer(&bytes),
|
||||
stream(&buffer)
|
||||
@ -40,19 +39,19 @@ DataBase::Serializer<T>::Serializer(const T& value) :
|
||||
}
|
||||
|
||||
template<class T>
|
||||
DataBase::Serializer<T>::~Serializer() {
|
||||
LMDBDataBase::Serializer<T>::~Serializer() {
|
||||
buffer.close();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
MDB_val DataBase::Serializer<T>::setData(const T& value) {
|
||||
MDB_val LMDBDataBase::Serializer<T>::setData(const T& value) {
|
||||
clear();
|
||||
_setData(value);
|
||||
return getData();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T DataBase::Serializer<T>::deserialize(const MDB_val& value) {
|
||||
T LMDBDataBase::Serializer<T>::deserialize(const MDB_val& value) {
|
||||
clear();
|
||||
bytes.setRawData((char*)value.mv_data, value.mv_size);
|
||||
T result;
|
||||
@ -62,19 +61,19 @@ T DataBase::Serializer<T>::deserialize(const MDB_val& value) {
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void DataBase::Serializer<T>::_setData(const T& value) {
|
||||
void LMDBDataBase::Serializer<T>::_setData(const T& value) {
|
||||
stream << value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void DataBase::Serializer<T>::clear() {
|
||||
void LMDBDataBase::Serializer<T>::clear() {
|
||||
if (buffer.pos() > 0) {
|
||||
buffer.seek(0);
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
MDB_val DataBase::Serializer<T>::getData() {
|
||||
MDB_val LMDBDataBase::Serializer<T>::getData() {
|
||||
MDB_val val;
|
||||
|
||||
val.mv_size = buffer.pos();
|
||||
@ -83,4 +82,4 @@ MDB_val DataBase::Serializer<T>::getData() {
|
||||
return val;
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_HPP
|
||||
|
@ -14,13 +14,13 @@
|
||||
// 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_SERIALIZER_DOUBLE_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_DOUBLE_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_DOUBLE_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_DOUBLE_HPP
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<double>
|
||||
{
|
||||
class Serializer<double> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const double& p_value):value(p_value) {};
|
||||
@ -46,8 +46,9 @@ private:
|
||||
double value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_DOUBLE_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_DOUBLE_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,13 +14,13 @@
|
||||
// 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_SERIALIZER_FLOAT_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_FLOAT_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_FLOAT_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_FLOAT_HPP
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<float>
|
||||
{
|
||||
class Serializer<float> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const float& p_value):value(p_value) {};
|
||||
@ -46,8 +46,9 @@ private:
|
||||
float value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_FLOAT_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_FLOAT_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_INT16_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_INT16_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_INT16_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_INT16_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<int16_t>
|
||||
{
|
||||
class Serializer<int16_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const int16_t& p_value):value(p_value) {};
|
||||
@ -46,5 +48,6 @@ private:
|
||||
int16_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_INT16_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_INT16_HPP
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_INT32_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_INT32_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_INT32_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_INT32_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<int32_t>
|
||||
{
|
||||
class Serializer<int32_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const int32_t& p_value):value(p_value) {};
|
||||
@ -46,6 +48,7 @@ private:
|
||||
int32_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_INT32_HPP
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_INT64_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_INT64_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_INT64_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_INT64_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<int64_t>
|
||||
{
|
||||
class Serializer<int64_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const int64_t& p_value):value(p_value) {};
|
||||
@ -46,6 +48,7 @@ private:
|
||||
int64_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_INT64_HPP
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_INT8_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_INT8_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_INT8_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_INT8_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<int8_t>
|
||||
{
|
||||
class Serializer<int8_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const int8_t& p_value):value(p_value) {};
|
||||
@ -46,8 +48,9 @@ private:
|
||||
int8_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_INT8_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_INT8_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,15 +14,15 @@
|
||||
// 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 CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
#ifndef LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<QByteArray>
|
||||
{
|
||||
class Serializer<QByteArray> {
|
||||
public:
|
||||
Serializer():value() {};
|
||||
Serializer(const QByteArray& p_value):value(p_value) {};
|
||||
@ -50,8 +50,9 @@ private:
|
||||
QByteArray value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,16 +14,16 @@
|
||||
// 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 CORE_DATABASE_SERIALIZER_QSTRING_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_QSTRING_HPP
|
||||
#ifndef LMDBDATABASE_SERIALIZER_QSTRING_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_QSTRING_HPP
|
||||
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<QString>
|
||||
{
|
||||
class Serializer<QString> {
|
||||
public:
|
||||
Serializer():value() {};
|
||||
Serializer(const QString& p_value):value(p_value.toUtf8()) {};
|
||||
@ -49,8 +49,9 @@ private:
|
||||
QByteArray value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_QSTRING_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_QSTRING_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_STDSTRING_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_STDSTRING_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_STDSTRING_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_STDSTRING_HPP
|
||||
#include <string>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<std::string>
|
||||
{
|
||||
class Serializer<std::string> {
|
||||
public:
|
||||
Serializer():value() {};
|
||||
Serializer(const std::string& p_value):value(p_value) {};
|
||||
@ -46,8 +48,9 @@ private:
|
||||
std::string value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_STDSTRING_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_STDSTRING_HPP
|
||||
|
||||
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_UINT16_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_UINT16_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_UINT16_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_UINT16_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<uint16_t>
|
||||
{
|
||||
class Serializer<uint16_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const uint16_t& p_value):value(p_value) {};
|
||||
@ -46,7 +48,8 @@ private:
|
||||
uint16_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_UINT16_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_UINT16_HPP
|
||||
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_UINT32_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_UINT32_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_UINT32_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_UINT32_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<uint32_t>
|
||||
{
|
||||
class Serializer<uint32_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const uint32_t& p_value):value(p_value) {};
|
||||
@ -46,5 +48,6 @@ private:
|
||||
uint32_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_UINT32_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_UINT32_HPP
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_UINT64_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_UINT64_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_UINT64_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_UINT64_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<uint64_t>
|
||||
{
|
||||
class Serializer<uint64_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const uint64_t& p_value):value(p_value) {};
|
||||
@ -46,6 +48,7 @@ private:
|
||||
uint64_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_UINT64_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_UINT64_HPP
|
||||
|
||||
|
@ -14,13 +14,15 @@
|
||||
// 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_SERIALIZER_UINT8_HPP
|
||||
#define LMDBDATABASE_SERIALIZER_UINT8_HPP
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_UINT8_HPP
|
||||
#define CORE_DATABASE_SERIALIZER_UINT8_HPP
|
||||
#include <stdint.h>
|
||||
|
||||
namespace LMDBDataBase {
|
||||
|
||||
template<>
|
||||
class DataBase::Serializer<uint8_t>
|
||||
{
|
||||
class Serializer<uint8_t> {
|
||||
public:
|
||||
Serializer():value(0) {};
|
||||
Serializer(const uint8_t& p_value):value(p_value) {};
|
||||
@ -46,8 +48,9 @@ private:
|
||||
uint8_t value;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //CORE_DATABASE_SERIALIZER_UINT8_HPP
|
||||
#endif //LMDBDATABASE_SERIALIZER_UINT8_HPP
|
||||
|
||||
|
||||
|
||||
|
121
storage.cpp
Normal file
121
storage.cpp
Normal file
@ -0,0 +1,121 @@
|
||||
// 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/>.
|
||||
|
||||
#include "storage.h"
|
||||
|
||||
LMDBDataBase::StorageBase::StorageBase(const std::string& p_name, DataBase* parent):
|
||||
dbi(),
|
||||
db(parent),
|
||||
name(p_name)
|
||||
{}
|
||||
|
||||
LMDBDataBase::StorageBase::~StorageBase() {}
|
||||
|
||||
void LMDBDataBase::StorageBase::drop() {
|
||||
ensureOpened(dropMethodName);
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
rc = drop(txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
int LMDBDataBase::StorageBase::drop(MDB_txn* transaction) {
|
||||
return mdb_drop(transaction, dbi, 0);
|
||||
}
|
||||
|
||||
const std::string & LMDBDataBase::StorageBase::dbName() const {
|
||||
return db->name;}
|
||||
|
||||
bool LMDBDataBase::StorageBase::isDBOpened() const {
|
||||
return db->opened;}
|
||||
|
||||
void LMDBDataBase::StorageBase::ensureOpened(const std::string& methodName) const {
|
||||
if (!db->opened)
|
||||
throw Closed(methodName, db->name, name);
|
||||
}
|
||||
|
||||
uint32_t LMDBDataBase::StorageBase::count() const {
|
||||
ensureOpened(countMethodName);
|
||||
|
||||
MDB_txn *txn;
|
||||
MDB_stat stat;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_stat(txn, dbi, &stat);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
uint32_t amount = stat.ms_entries;
|
||||
mdb_txn_abort(txn);
|
||||
return amount;
|
||||
}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
|
||||
abortTransaction(txn);
|
||||
if (rc == MDB_KEYEXIST)
|
||||
throwDuplicate(key);
|
||||
else
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwNotFoundOrUnknown(int rc, LMDBDataBase::TransactionID txn, const std::string& key) const {
|
||||
abortTransaction(txn);
|
||||
if (rc == MDB_NOTFOUND)
|
||||
throwNotFound(key);
|
||||
else
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwUnknown(int rc, LMDBDataBase::TransactionID txn) const {
|
||||
abortTransaction(txn);
|
||||
throwUnknown(rc);
|
||||
}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwUnknown(int rc) const {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwDuplicate(const std::string& key) const {
|
||||
throw Exist(key, db->name, name);}
|
||||
|
||||
void LMDBDataBase::StorageBase::throwNotFound(const std::string& key) const {
|
||||
throw NotFound(key, db->name, name);}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginReadOnlyTransaction() const {
|
||||
return db->beginReadOnlyTransaction(name);}
|
||||
|
||||
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginTransaction() const {
|
||||
return db->beginTransaction(name);}
|
||||
|
||||
void LMDBDataBase::StorageBase::abortTransaction(LMDBDataBase::TransactionID id) const {
|
||||
db->abortTransaction(id);}
|
||||
|
||||
void LMDBDataBase::StorageBase::commitTransaction(LMDBDataBase::TransactionID id) const {
|
||||
db->commitTransaction(id);}
|
@ -14,30 +14,61 @@
|
||||
// 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 CORE_TABLE_H
|
||||
#define CORE_TABLE_H
|
||||
#ifndef LMDBDATABASE_STORAGE_H
|
||||
#define LMDBDATABASE_STORAGE_H
|
||||
|
||||
#include "database.h"
|
||||
#include "serializer.h"
|
||||
|
||||
class DataBase::_Table {
|
||||
namespace LMDBDataBase {
|
||||
|
||||
class StorageBase {
|
||||
friend class DataBase;
|
||||
protected:
|
||||
_Table(const std::string& name, DataBase* parent);
|
||||
virtual ~_Table();
|
||||
StorageBase(const std::string& name, DataBase* parent);
|
||||
virtual ~StorageBase();
|
||||
|
||||
virtual int createTable(MDB_txn * transaction) = 0;
|
||||
virtual int drop(MDB_txn * transaction);
|
||||
|
||||
bool isDBOpened() const;
|
||||
const std::string& dbName() const;
|
||||
|
||||
void ensureOpened(const std::string& methodName) const;
|
||||
void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const;
|
||||
void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const;
|
||||
void throwUnknown(int rc, TransactionID txn) const;
|
||||
void throwUnknown(int rc) const;
|
||||
void throwDuplicate(const std::string& key) const;
|
||||
void throwNotFound(const std::string& key)const ;
|
||||
|
||||
public:
|
||||
virtual void drop();
|
||||
virtual uint32_t count() const;
|
||||
|
||||
TransactionID beginReadOnlyTransaction() const;
|
||||
TransactionID beginTransaction() const;
|
||||
void commitTransaction(TransactionID id) const;
|
||||
void abortTransaction(TransactionID id) const;
|
||||
|
||||
protected:
|
||||
MDB_dbi dbi;
|
||||
DataBase* db;
|
||||
const std::string name;
|
||||
|
||||
inline static const std::string dropMethodName = "drop";
|
||||
inline static const std::string countMethodName = "count";
|
||||
|
||||
inline static const std::string addRecordMethodName = "addRecord";
|
||||
inline static const std::string forceRecordMethodName = "forceRecord";
|
||||
inline static const std::string changeRecordMethodName = "changeRecord";
|
||||
inline static const std::string removeRecordMethodName = "removeRecord";
|
||||
inline static const std::string checkRecordMethodName = "checkRecord";
|
||||
inline static const std::string getRecordMethodName = "getRecord";
|
||||
inline static const std::string readAllMethodName = "readAllRecord";
|
||||
inline static const std::string replaceAllMethodName = "replaceAll";
|
||||
inline static const std::string addRecordsMethodName = "addRecords";
|
||||
|
||||
protected:
|
||||
template <class T>
|
||||
int makeTable(MDB_txn* transaction);
|
||||
@ -47,14 +78,14 @@ protected:
|
||||
};
|
||||
|
||||
template <class K, class V>
|
||||
class DataBase::Table : public DataBase::_Table {
|
||||
class Storage : public StorageBase {
|
||||
friend class DataBase;
|
||||
protected:
|
||||
Table(const std::string& name, DataBase* parent);
|
||||
~Table() override;
|
||||
Storage(const std::string& name, DataBase* parent);
|
||||
~Storage() override;
|
||||
|
||||
public:
|
||||
using DataBase::_Table::drop;
|
||||
using StorageBase::drop;
|
||||
virtual void addRecord(const K& key, const V& value);
|
||||
virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change
|
||||
virtual void changeRecord(const K& key, const V& value);
|
||||
@ -72,6 +103,8 @@ protected:
|
||||
int createTable(MDB_txn* transaction) override;
|
||||
};
|
||||
|
||||
#include "table.hpp"
|
||||
}
|
||||
|
||||
#endif // CORE_TABLE_H
|
||||
#include "storage.hpp"
|
||||
|
||||
#endif // LMDBDATABASE_STORAGE_H
|
291
storage.hpp
Normal file
291
storage.hpp
Normal file
@ -0,0 +1,291 @@
|
||||
// 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
|
72
table.cpp
72
table.cpp
@ -1,72 +0,0 @@
|
||||
// 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/>.
|
||||
|
||||
#include "table.h"
|
||||
|
||||
DataBase::_Table::_Table(const std::string& p_name, DataBase* parent):
|
||||
dbi(),
|
||||
db(parent),
|
||||
name(p_name)
|
||||
{
|
||||
}
|
||||
|
||||
DataBase::_Table::~_Table() {}
|
||||
|
||||
|
||||
void DataBase::_Table::drop()
|
||||
{
|
||||
if (!db->opened) {
|
||||
throw Closed("drop", db->name, name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
rc = drop(txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
int DataBase::_Table::drop(MDB_txn* transaction)
|
||||
{
|
||||
return mdb_drop(transaction, dbi, 0);
|
||||
}
|
||||
|
||||
uint32_t DataBase::_Table::count() const
|
||||
{
|
||||
if (!db->opened) {
|
||||
throw Closed("count", db->name, name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
MDB_stat stat;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
rc = mdb_stat(txn, dbi, &stat);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
uint32_t amount = stat.ms_entries;
|
||||
mdb_txn_abort(txn);
|
||||
return amount;
|
||||
}
|
385
table.hpp
385
table.hpp
@ -1,385 +0,0 @@
|
||||
// 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 CORE_TABLE_HPP
|
||||
#define CORE_TABLE_HPP
|
||||
|
||||
#include "table.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
template<class K, class V>
|
||||
DataBase::Table<K, V>::Table(const std::string& p_name, DataBase* parent):
|
||||
_Table(p_name, parent),
|
||||
keySerializer(new Serializer<K>()),
|
||||
valueSerializer(new Serializer<V>())
|
||||
{
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
DataBase::Table<K, V>::~Table() {
|
||||
delete valueSerializer;
|
||||
delete keySerializer;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Table<K, V>::addRecord(const K& key, const V& value) {
|
||||
if (!db->opened) {
|
||||
throw Closed("addRecord", db->name, name);
|
||||
}
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData = valueSerializer->setData(value);
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE);
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_KEYEXIST) {
|
||||
throw Exist(toString(key), db->name, name);
|
||||
} else {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Table<K, V>::forceRecord(const K& key, const V& value) {
|
||||
if (!db->opened) {
|
||||
throw Closed("forceRecord", db->name, name);
|
||||
}
|
||||
|
||||
bool added;
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData;
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
if (rc == 0) {
|
||||
added = false;
|
||||
} else if (rc == MDB_NOTFOUND) {
|
||||
added = true;
|
||||
} else {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
lmdbData = valueSerializer->setData(value);
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
return added;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Table<K, V>::changeRecord(const K& key, const V& value) {
|
||||
if (!db->opened) {
|
||||
throw Closed("changeRecord", db->name, name);
|
||||
}
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData = valueSerializer->setData(value);
|
||||
MDB_txn *txn;
|
||||
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0);
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc) {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
V DataBase::Table<K, V>::getRecord(const K& key) const {
|
||||
if (!db->opened) {
|
||||
throw Closed("getRecord", db->name, name);
|
||||
}
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData;
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
throw NotFound(toString(key), db->name, name);
|
||||
} else {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
} else {
|
||||
V value = valueSerializer->deserialize(lmdbData);
|
||||
mdb_txn_abort(txn);
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
bool DataBase::Table<K, V>::checkRecord(const K& key) const {
|
||||
if (!db->opened) {
|
||||
throw Closed("checkRecord", db->name, name);
|
||||
}
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
MDB_val lmdbData;
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData);
|
||||
mdb_txn_abort(txn);
|
||||
|
||||
if (rc) {
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
return false;
|
||||
} else {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
std::map<K, V> DataBase::Table<K, V>::readAll() const {
|
||||
if (!db->opened) {
|
||||
throw Closed("readAll", db->name, name);
|
||||
}
|
||||
|
||||
std::map<K, V> result;
|
||||
MDB_txn *txn;
|
||||
MDB_cursor* cursor;
|
||||
MDB_val lmdbKey, lmdbData;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
rc = mdb_cursor_open(txn, dbi, &cursor);
|
||||
if (rc != MDB_SUCCESS) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
mdb_txn_abort(txn);
|
||||
if (rc != MDB_NOTFOUND) {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Table<K, V>::replaceAll(const std::map<K, V>& data) {
|
||||
if (!db->opened) {
|
||||
throw Closed("replaceAll", db->name, name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
rc = drop(txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
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 != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
}
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
uint32_t DataBase::Table<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
|
||||
if (!db->opened) {
|
||||
throw Closed("addRecords", db->name, name);
|
||||
}
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc = mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
|
||||
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, overwrite ? 0 : MDB_NOOVERWRITE);
|
||||
if (rc != 0) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
}
|
||||
|
||||
MDB_stat stat;
|
||||
rc = mdb_stat(txn, dbi, &stat);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
uint32_t amount = stat.ms_entries;
|
||||
|
||||
mdb_txn_commit(txn);
|
||||
return amount;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
void DataBase::Table<K, V>::removeRecord(const K& key) {
|
||||
if (!db->opened) {
|
||||
throw Closed("removeRecord", db->name, name);
|
||||
}
|
||||
|
||||
MDB_val lmdbKey = keySerializer->setData(key);
|
||||
|
||||
MDB_txn *txn;
|
||||
int rc;
|
||||
mdb_txn_begin(db->environment, NULL, 0, &txn);
|
||||
rc = mdb_del(txn, dbi, &lmdbKey, NULL);
|
||||
if (rc) {
|
||||
mdb_txn_abort(txn);
|
||||
if (rc == MDB_NOTFOUND) {
|
||||
throw NotFound(toString(key), db->name, name);
|
||||
} else {
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
}
|
||||
} else {
|
||||
mdb_txn_commit(txn);
|
||||
}
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
int DataBase::Table<K, V>::createTable(MDB_txn* transaction) {
|
||||
return makeTable<K>(transaction);
|
||||
}
|
||||
|
||||
template<class K>
|
||||
inline int DataBase::_Table::makeTable(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<uint64_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<uint32_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<uint16_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<uint8_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<int64_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<int32_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::makeTable<int16_t>(MDB_txn* transaction) {
|
||||
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int DataBase::_Table::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 DataBase::_Table::toString(const T& value) {
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string DataBase::_Table::toString(const QString& value) {
|
||||
return value.toStdString();
|
||||
}
|
||||
|
||||
template<>
|
||||
inline std::string DataBase::_Table::toString(const std::string& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
#endif //CORE_TABLE_HPP
|
@ -1,7 +1,7 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "database.h"
|
||||
#include "table.h"
|
||||
#include "storage.h"
|
||||
#include "cache.h"
|
||||
|
||||
#include <QString>
|
||||
@ -18,7 +18,7 @@ protected:
|
||||
|
||||
static void SetUpTestSuite() {
|
||||
if (db == nullptr) {
|
||||
db = new DataBase("testBase");
|
||||
db = new LMDBDataBase::DataBase("testBase");
|
||||
db->addTable<uint32_t, uint32_t>("table1");
|
||||
db->addTable<QString, QString>("table2");
|
||||
db->addCache<int8_t, std::string>("cache1");
|
||||
@ -32,15 +32,15 @@ protected:
|
||||
db = nullptr;
|
||||
}
|
||||
|
||||
static DataBase* db;
|
||||
static LMDBDataBase::DataBase* db;
|
||||
|
||||
DataBase::Table<uint32_t, uint32_t>* t1;
|
||||
DataBase::Table<QString, QString>* t2;
|
||||
DataBase::Cache<int8_t, std::string>* c1;
|
||||
LMDBDataBase::Storage<uint32_t, uint32_t>* t1;
|
||||
LMDBDataBase::Storage<QString, QString>* t2;
|
||||
LMDBDataBase::Cache<int8_t, std::string>* c1;
|
||||
};
|
||||
|
||||
|
||||
DataBase* DataBaseTest::db = nullptr;
|
||||
LMDBDataBase::DataBase* DataBaseTest::db = nullptr;
|
||||
|
||||
TEST_F(DataBaseTest, RemovingDirectory) {
|
||||
EXPECT_EQ(db->removeDirectory(), true);
|
||||
@ -88,7 +88,7 @@ TEST_F(DataBaseTest, AddingRepeatingIntegerKey) {
|
||||
bool thrown = false;
|
||||
try {
|
||||
t1->addRecord(3, 24);
|
||||
} catch (const DataBase::Exist e) {
|
||||
} catch (const LMDBDataBase::Exist e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -100,7 +100,7 @@ TEST_F(DataBaseTest, AddingRepeatingStringKey) {
|
||||
bool thrown = false;
|
||||
try {
|
||||
t2->addRecord("sdfhga", "world");
|
||||
} catch (const DataBase::Exist e) {
|
||||
} catch (const LMDBDataBase::Exist e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -112,7 +112,7 @@ TEST_F(DataBaseTest, AddingRepeatingCacheKey) {
|
||||
bool thrown = false;
|
||||
try {
|
||||
c1->addRecord(-4, "world");
|
||||
} catch (const DataBase::Exist e) {
|
||||
} catch (const LMDBDataBase::Exist e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -124,7 +124,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) {
|
||||
bool thrown = false;
|
||||
try {
|
||||
QString wrong = t2->getRecord("almonds");
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -132,7 +132,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) {
|
||||
thrown = false;
|
||||
try {
|
||||
uint32_t wrong = t1->getRecord(64);
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -140,7 +140,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) {
|
||||
thrown = false;
|
||||
try {
|
||||
std::string wrong = c1->getRecord(21);
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -151,7 +151,7 @@ TEST_F(DataBaseTest, Persistence) {
|
||||
db->close();
|
||||
delete db;
|
||||
|
||||
db = new DataBase("testBase");
|
||||
db = new LMDBDataBase::DataBase("testBase");
|
||||
t1 = db->addTable<uint32_t, uint32_t>("table1");
|
||||
t2 = db->addTable<QString, QString>("table2");
|
||||
c1 = db->addCache<int8_t, std::string>("cache1");
|
||||
@ -175,7 +175,7 @@ TEST_F(DataBaseTest, Persistence) {
|
||||
bool thrown = false;
|
||||
try {
|
||||
QString wrong = t2->getRecord("cats");
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -183,7 +183,7 @@ TEST_F(DataBaseTest, Persistence) {
|
||||
thrown = false;
|
||||
try {
|
||||
uint32_t wrong = t1->getRecord(7893);
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
@ -191,7 +191,7 @@ TEST_F(DataBaseTest, Persistence) {
|
||||
thrown = false;
|
||||
try {
|
||||
std::string wrong = c1->getRecord(89);
|
||||
} catch (const DataBase::NotFound e) {
|
||||
} catch (const LMDBDataBase::NotFound e) {
|
||||
thrown = true;
|
||||
}
|
||||
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
|
||||
|
Loading…
Reference in New Issue
Block a user