project name change

This commit is contained in:
Blue 2023-03-21 14:05:54 +03:00
parent 19229f6c26
commit 7377f13534
Signed by: blue
GPG key ID: 9B203B252A63EE38
29 changed files with 316 additions and 310 deletions

37
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,37 @@
set(SOURCES
exceptions.cpp
storage.cpp
base.cpp
)
set(HEADERS
base.h
exceptions.h
storage.h
storage.hpp
cache.h
cache.hpp
serializer.h
serializer.hpp
serializer_uint8.hpp
serializer_uint16.hpp
serializer_uint32.hpp
serializer_uint64.hpp
serializer_int8.hpp
serializer_int16.hpp
serializer_int32.hpp
serializer_int64.hpp
serializer_float.hpp
serializer_double.hpp
serializer_stdstring.hpp
serializer_qstring.hpp
serializer_qbytearray.hpp
operators.hpp
)
target_sources(lmdbal PRIVATE
${SOURCES}
${HEADERS}
)
set_target_properties(lmdbal PROPERTIES PUBLIC_HEADER "${HEADERS}")

193
src/base.cpp Normal file
View file

@ -0,0 +1,193 @@
// 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 "base.h"
#include "exceptions.h"
#include "storage.h"
LMDBAL::Base::Base(const QString& p_name, uint16_t mapSize):
name(p_name.toStdString()),
opened(false),
size(mapSize),
environment(),
tables(),
transactions(new Transactions())
{}
LMDBAL::Base::~Base() {
close();
delete transactions;
for (const std::pair<const std::string, iStorage*>& pair : tables)
delete pair.second;
}
void LMDBAL::Base::close() {
if (opened) {
for (const std::pair<const std::string, iStorage*>& pair : tables) {
iStorage* table = pair.second;
mdb_dbi_close(environment, table->dbi);
}
mdb_env_close(environment);
opened = false;
}
}
void LMDBAL::Base::open() {
if (!opened) {
mdb_env_create(&environment);
QString path = createDirectory();
mdb_env_set_maxdbs(environment, tables.size());
mdb_env_set_mapsize(environment, size * 1024UL * 1024UL);
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
for (const std::pair<const std::string, iStorage*>& pair : tables) {
iStorage* table = pair.second;
int rc = table->createTable(txn);
if (rc)
throw Unknown(name, mdb_strerror(rc));
}
mdb_txn_commit(txn);
opened = true;
}
}
bool LMDBAL::Base::removeDirectory() {
if (opened)
throw Opened(name, "remove database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
QDir cache(path);
if (cache.exists())
return cache.removeRecursively();
else
return true;
}
QString LMDBAL::Base::createDirectory() {
if (opened)
throw Opened(name, "create database directory");
QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
path += "/" + getName();
QDir cache(path);
if (!cache.exists()) {
bool res = cache.mkpath(path);
if (!res)
throw Directory(path.toStdString());
}
return path;
}
QString LMDBAL::Base::getName() const {
return QString::fromStdString(name);}
bool LMDBAL::Base::ready() const {
return opened;}
void LMDBAL::Base::drop() {
if (!opened)
throw Closed("drop", name);
MDB_txn *txn;
int rc = mdb_txn_begin(environment, NULL, 0, &txn);
if (rc)
throw Unknown(name, mdb_strerror(rc));
for (const std::pair<const std::string, iStorage*>& pair : tables) {
rc = pair.second->drop(txn);
if (rc)
throw Unknown(name, mdb_strerror(rc), pair.first);
}
mdb_txn_commit(txn);
}
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
return beginReadOnlyTransaction(emptyName);}
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const {
return beginTransaction(emptyName);}
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
return abortTransaction(id, emptyName);}
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) const {
return commitTransaction(id, emptyName);}
LMDBAL::TransactionID LMDBAL::Base::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;
}
LMDBAL::TransactionID LMDBAL::Base::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 LMDBAL::Base::abortTransaction(LMDBAL::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 LMDBAL::Base::commitTransaction(LMDBAL::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);
}

134
src/base.h Normal file
View file

@ -0,0 +1,134 @@
// 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 LMDBAL_BASE_H
#define LMDBAL_BASE_H
#include <map>
#include <set>
#include <string>
#include <optional>
#include <limits>
#include <QString>
#include <QStandardPaths>
#include <QDir>
#include <lmdb.h>
#include "exceptions.h"
namespace LMDBAL {
class iStorage;
template<class T>
class Serializer;
template <class K, class V>
class Storage;
template <class K, class V>
class Cache;
typedef MDB_txn* TransactionID;
class Base {
friend class iStorage;
public:
Base(const QString& name, uint16_t mapSize = 10);
~Base();
void open();
void close();
bool ready() const;
bool removeDirectory();
QString createDirectory();
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>
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>
Storage<K, V>* getTable(const std::string& name);
template <class K, class V>
Cache<K, V>* getCache(const std::string& name);
private:
typedef std::map<std::string, iStorage*> 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;
Tables tables;
Transactions* transactions;
inline static const std::string emptyName = "";
};
}
#include "operators.hpp"
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addTable(const std::string& p_name) {
if (opened) {
throw Opened(name, "add table " + p_name);
}
Storage<K, V>* table = new Storage<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (iStorage*)table));
return table;
}
template<class K, class V>
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& p_name) {
if (opened) {
throw Opened(name, "add cache " + p_name);
}
Cache<K, V>* cache = new Cache<K, V>(p_name, this);
tables.insert(std::make_pair(p_name, (iStorage*)cache));
return cache;
}
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::getTable(const std::string& p_name) {
return static_cast<Storage<K, V>*>(tables.at(p_name));
}
template <class K, class V>
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& p_name) {
return static_cast<Cache<K, V>*>(tables.at(p_name));
}
#endif //LMDBAL_BASE_H

68
src/cache.h Normal file
View file

@ -0,0 +1,68 @@
// 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 LMDBAL_CACHE_H
#define LMDBAL_CACHE_H
#include <map>
#include <set>
#include "storage.h"
namespace LMDBAL {
template <class K, class V>
class Cache : public Storage<K, V> {
friend class Base;
enum class Mode { //it's a cache state when we:
nothing, // - know nothing about records in database on disk
size, // - know just an amount of records
full // - shure that our cache is equal to the database on disk
};
protected:
Cache(const std::string& name, Base* parent);
~Cache() override;
private:
void handleMode() const;
public:
virtual void addRecord(const K& key, const V& value) override;
virtual bool forceRecord(const K& key, const V& value) override;
virtual void changeRecord(const K& key, const V& value) override;
virtual void removeRecord(const K& key) override;
virtual bool checkRecord(const K& key) const override;
virtual V getRecord(const K& key) const override;
virtual uint32_t count() const override;
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;
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false) override;
protected:
Mode* mode;
std::map<K, V>* cache;
std::set<K>* abscent;
uint32_t* sizeDifference;
};
}
#include "cache.hpp"
#endif // LMDBAL_CACHE_H

273
src/cache.hpp Normal file
View file

@ -0,0 +1,273 @@
// 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 LMDBAL_CACHE_HPP
#define LMDBAL_CACHE_HPP
#include "cache.h"
#include "exceptions.h"
template<class K, class V>
LMDBAL::Cache<K, V>::Cache(const std::string& p_name, Base* parent):
Storage<K, V>(p_name, parent),
mode(new Mode),
cache(new std::map<K, V>()),
abscent(new std::set<K>()),
sizeDifference(new uint32_t)
{
*mode = Mode::nothing;
*sizeDifference = 0;
}
template<class K, class V>
LMDBAL::Cache<K, V>::~Cache() {
delete sizeDifference;
delete mode;
delete cache;
delete abscent;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::addRecordMethodName);
if (cache->count(key) > 0)
iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value);
cache->insert(std::make_pair(key, value));
if (*mode != Mode::full)
abscent->erase(key);
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::forceRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::forceRecordMethodName);
bool added = Storage<K, V>::forceRecord(key, value);
if (*mode == Mode::full) {
(*cache)[key] = value;
} else {
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)
result.first->second = value;
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 LMDBAL::Cache<K, V>::changeRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::changeRecordMethodName);
if (*mode == Mode::full) {
typename std::map<K, V>::iterator itr = cache->find(key);
if (itr == cache->end())
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::changeRecord(key, value);
itr->second = value;
} else {
if (abscent->count(key) > 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
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)
res.first->second = value;
else
handleMode();
} catch (const NotFound& error) {
abscent->insert(key);
throw error;
}
}
}
template<class K, class V>
V LMDBAL::Cache<K, V>::getRecord(const K& key) const {
iStorage::ensureOpened(iStorage::getRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return itr->second;
if (*mode == Mode::full || abscent->count(key) != 0)
iStorage::throwNotFound(iStorage::toString(key));
try {
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)
abscent->insert(key);
throw error;
}
}
template<class K, class V>
bool LMDBAL::Cache<K, V>::checkRecord(const K& key) const {
iStorage::ensureOpened(iStorage::checkRecordMethodName);
typename std::map<K, V>::const_iterator itr = cache->find(key);
if (itr != cache->end())
return true;
if (*mode == Mode::full || abscent->count(key) != 0)
return false;
try {
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)
abscent->insert(key);
return false;
}
}
template<class K, class V>
std::map<K, V> LMDBAL::Cache<K, V>::readAll() const {
iStorage::ensureOpened(iStorage::readAllMethodName);
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
*cache = Storage<K, V>::readAll(); //that are missing in the cache
abscent->clear();
*sizeDifference = 0;
}
return *cache;
}
template<class K, class V>
void LMDBAL::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 LMDBAL::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)
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)
res.first->second = pair.second;
} else if (m != Mode::full) {
a.erase(pair.first);
}
}
if (m != Mode::full) {
*sizeDifference = newSize - c.size();
if (*sizeDifference == 0) {
*mode = Mode::full;
abscent->clear();
}
}
return newSize;
}
template<class K, class V>
void LMDBAL::Cache<K, V>::removeRecord(const K& key) {
iStorage::ensureOpened(iStorage::removeRecordMethodName);
typename std::pair<typename std::set<K>::const_iterator, bool> pair = abscent->insert(key);
if (!pair.second)
iStorage::throwNotFound(iStorage::toString(key));
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)
abscent->insert(key);
}
template<class K, class V>
uint32_t LMDBAL::Cache<K, V>::count() const {
switch (*mode) {
case Mode::nothing:
{
uint32_t sz = Storage<K, V>::count();
*sizeDifference = sz - cache->size();
if (sz == 0) {
*mode = Mode::full;
abscent->clear();
} else {
*mode = Mode::size;
}
return sz;
}
case Mode::size:
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 LMDBAL::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) {
--(*sizeDifference);
if (*sizeDifference == 0) {
*mode = Mode::full;
abscent->clear();
}
}
}
template<class K, class V>
int LMDBAL::Cache<K, V>::drop(MDB_txn * transaction) {
int res = Storage<K, V>::drop(transaction);
cache->clear();
abscent->clear();
*mode = Mode::full;
*sizeDifference = 0;
return res;
}
#endif //LMDBAL_CACHE_HPP

117
src/exceptions.cpp Normal file
View file

@ -0,0 +1,117 @@
// 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 "exceptions.h"
LMDBAL::Exception::Exception():
std::exception()
{}
LMDBAL::Exception::~Exception() {}
const char* LMDBAL::Exception::what() const noexcept( true ) {
std::string* msg = new std::string(getMessage());
return msg->c_str();
}
LMDBAL::Directory::Directory(const std::string& p_path):
Exception(),
path(p_path) {}
std::string LMDBAL::Directory::getMessage() const {
return "Can't create directory for database at " + path;}
LMDBAL::Closed::Closed(
const std::string& p_operation,
const std::string& p_dbName,
const std::optional<std::string>& p_tableName
):
Exception(),
operation(p_operation),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::Closed::getMessage() const {
std::string msg = "An attempt to perform operation " + operation
+ " on closed database " + dbName;
if (tableName.has_value())
msg += + " on table " + tableName.value();
return msg;
}
LMDBAL::Opened::Opened(const std::string& p_dbName, const std::string& p_action):
Exception(),
dbName(p_dbName),
action(p_action) {}
std::string LMDBAL::Opened::getMessage() const {
return "An attempt to " + action
+ " (the database " + dbName
+ ") but it's can't be done because the Base is already opened";
}
LMDBAL::NotFound::NotFound(
const std::string& p_key,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
key(p_key),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::NotFound::getMessage() const {
return "Element for id " + key + " wasn't found "
+ " in database " + dbName
+ " in table " + tableName;}
LMDBAL::Exist::Exist(
const std::string& p_key,
const std::string& p_dbName,
const std::string& p_tableName
):
Exception(),
key(p_key),
dbName(p_dbName),
tableName(p_tableName) {}
std::string LMDBAL::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";
}
LMDBAL::Unknown::Unknown(
const std::string& p_dbName,
const std::string& message,
const std::optional<std::string>& p_tableName
):
Exception(),
dbName(p_dbName),
tableName(p_tableName),
msg(message) {}
std::string LMDBAL::Unknown::getMessage() const {
std::string result = "Unknown error in database " + dbName;
if (tableName.has_value())
result += " in table " + tableName.value();
result += ": " + msg;
return result;
}

101
src/exceptions.h Normal file
View file

@ -0,0 +1,101 @@
// 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 LMDBAL_EXCEPTIONS_H
#define LMDBAL_EXCEPTIONS_H
#include <stdexcept>
#include <string>
#include <optional>
namespace LMDBAL {
class Exception : public std::exception {
public:
Exception();
virtual ~Exception();
virtual std::string getMessage() const = 0;
const char* what() const noexcept( true );
};
class Directory: public Exception {
public:
Directory(const std::string& path);
std::string getMessage() const;
private:
std::string path;
};
class Closed : public Exception {
public:
Closed(const std::string& p_operation, const std::string& dbName, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const;
private:
std::string operation;
std::string dbName;
std::optional<std::string> tableName;
};
class Opened : Exception {
public:
Opened(const std::string& dbName, const std::string& action);
std::string getMessage() const;
private:
std::string dbName;
std::string action;
};
class NotFound : public Exception {
public:
NotFound(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string key;
std::string dbName;
std::string tableName;
};
class Exist : public Exception {
public:
Exist(const std::string& key, const std::string& dbName, const std::string& tableName);
std::string getMessage() const;
private:
std::string key;
std::string dbName;
std::string tableName;
};
class Unknown : public Exception {
public:
Unknown(const std::string& dbName, const std::string& message, const std::optional<std::string>& tableName = std::nullopt);
std::string getMessage() const;
private:
std::string dbName;
std::optional<std::string> tableName;
std::string msg;
};
}
#endif //LMDBAL_EXCEPTIONS_H

209
src/operators.hpp Normal file
View file

@ -0,0 +1,209 @@
// 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 LMDBAL_OPERATORS_HPP
#define LMDBAL_OPERATORS_HPP
#include <map>
#include <set>
#include <vector>
#include <list>
#include <deque>
#include <QDataStream>
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::map<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::map<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::multimap<K, V>& container) {
uint32_t size = container.size();
out << size;
for (const std::pair<const K, V>& pair : container) {
out << pair;
}
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::multimap<K, V>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
std::pair<K, V> pair;
in >> pair;
container.insert(pair);
}
return in;
}
template <class K, class V>
QDataStream& operator << (QDataStream &out, const std::pair<K, V>& pair) {
out << pair.first;
out << pair.second;
return out;
}
template <class K, class V>
QDataStream& operator >> (QDataStream &in, std::pair<K, V>& container) {
in >> container.first;
in >> container.second;
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::set<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::set<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::multiset<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::multiset<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
K value;
in >> value;
container.insert(value);
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::vector<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::vector<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::deque<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::deque<K>& container) {
uint32_t size;
in >> size;
container.resize(size);
for (uint32_t i = 0; i < size; ++i) {
in >> container[i];
}
return in;
}
template <class K>
QDataStream& operator << (QDataStream &out, const std::list<K>& container) {
uint32_t size = container.size();
out << size;
for (const K& value : container) {
out << value;
}
return out;
}
template <class K>
QDataStream& operator >> (QDataStream &in, std::list<K>& container) {
uint32_t size;
in >> size;
for (uint32_t i = 0; i < size; ++i) {
typename std::list<K>::iterator itr = container.emplace(container.end());
in >> *itr;
}
return in;
}
#endif //LMDBAL_OPERATORS_HPP

69
src/serializer.h Normal file
View file

@ -0,0 +1,69 @@
// 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 LMDBAL_SERIALIZER_H
#define LMDBAL_SERIALIZER_H
#include <cstring>
#include <QByteArray>
#include <QBuffer>
#include <QDataStream>
#include <lmdb.h>
namespace LMDBAL {
template<class T>
class Serializer {
public:
Serializer();
Serializer(const T& value);
~Serializer();
T deserialize(const MDB_val& value);
MDB_val setData(const T& value);
MDB_val getData();
void clear();
private:
void _setData(const T& value);
private:
QByteArray bytes;
QBuffer buffer;
QDataStream stream;
};
}
#include "serializer.hpp"
#include "serializer_uint64.hpp"
#include "serializer_uint32.hpp"
#include "serializer_uint16.hpp"
#include "serializer_uint8.hpp"
#include "serializer_int64.hpp"
#include "serializer_int32.hpp"
#include "serializer_int16.hpp"
#include "serializer_int8.hpp"
#include "serializer_float.hpp"
#include "serializer_double.hpp"
#include "serializer_stdstring.hpp"
#include "serializer_qstring.hpp"
#include "serializer_qbytearray.hpp"
#endif // LMDBAL_SERIALIZER_H

85
src/serializer.hpp Normal file
View file

@ -0,0 +1,85 @@
// 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 LMDBAL_SERIALIZER_HPP
#define LMDBAL_SERIALIZER_HPP
#include "serializer.h"
template<class T>
LMDBAL::Serializer<T>::Serializer() :
bytes(),
buffer(&bytes),
stream(&buffer)
{
buffer.open(QIODevice::ReadWrite);
}
template<class T>
LMDBAL::Serializer<T>::Serializer(const T& value) :
bytes(),
buffer(&bytes),
stream(&buffer)
{
buffer.open(QIODevice::ReadWrite);
_setValue(value);
}
template<class T>
LMDBAL::Serializer<T>::~Serializer() {
buffer.close();
}
template<class T>
MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
clear();
_setData(value);
return getData();
}
template<class T>
T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
clear();
bytes.setRawData((char*)value.mv_data, value.mv_size);
T result;
stream >> result;
return result;
}
template<class T>
void LMDBAL::Serializer<T>::_setData(const T& value) {
stream << value;
}
template<class T>
void LMDBAL::Serializer<T>::clear() {
if (buffer.pos() > 0) {
buffer.seek(0);
}
}
template<class T>
MDB_val LMDBAL::Serializer<T>::getData() {
MDB_val val;
val.mv_size = buffer.pos();
val.mv_data = (char*)bytes.data();
return val;
}
#endif //LMDBAL_SERIALIZER_HPP

54
src/serializer_double.hpp Normal file
View file

@ -0,0 +1,54 @@
// 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 LMDBAL_SERIALIZER_DOUBLE_HPP
#define LMDBAL_SERIALIZER_DOUBLE_HPP
namespace LMDBAL {
template<>
class Serializer<double> {
public:
Serializer():value(0) {};
Serializer(const double& p_value):value(p_value) {};
~Serializer() {};
double deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8);
return value;
};
MDB_val setData(const double& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
double value;
};
}
#endif //LMDBAL_SERIALIZER_DOUBLE_HPP

54
src/serializer_float.hpp Normal file
View file

@ -0,0 +1,54 @@
// 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 LMDBAL_SERIALIZER_FLOAT_HPP
#define LMDBAL_SERIALIZER_FLOAT_HPP
namespace LMDBAL {
template<>
class Serializer<float> {
public:
Serializer():value(0) {};
Serializer(const float& p_value):value(p_value) {};
~Serializer() {};
float deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4);
return value;
};
MDB_val setData(const float& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
float value;
};
}
#endif //LMDBAL_SERIALIZER_FLOAT_HPP

53
src/serializer_int16.hpp Normal file
View file

@ -0,0 +1,53 @@
// 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 LMDBAL_SERIALIZER_INT16_HPP
#define LMDBAL_SERIALIZER_INT16_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int16_t> {
public:
Serializer():value(0) {};
Serializer(const int16_t& p_value):value(p_value) {};
~Serializer() {};
int16_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 2);
return value;
};
MDB_val setData(const int16_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 2;
return result;
};
void clear() {}; //not possible;
private:
int16_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT16_HPP

56
src/serializer_int32.hpp Normal file
View file

@ -0,0 +1,56 @@
// 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 LMDBAL_SERIALIZER_INT32_HPP
#define LMDBAL_SERIALIZER_INT32_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int32_t> {
public:
Serializer():value(0) {};
Serializer(const int32_t& p_value):value(p_value) {};
~Serializer() {};
int32_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4);
return value;
};
MDB_val setData(const int32_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
int32_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT32_HPP

56
src/serializer_int64.hpp Normal file
View file

@ -0,0 +1,56 @@
// 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 LMDBAL_SERIALIZER_INT64_HPP
#define LMDBAL_SERIALIZER_INT64_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int64_t> {
public:
Serializer():value(0) {};
Serializer(const int64_t& p_value):value(p_value) {};
~Serializer() {};
int64_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8);
return value;
};
MDB_val setData(const int64_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
int64_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT64_HPP

56
src/serializer_int8.hpp Normal file
View file

@ -0,0 +1,56 @@
// 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 LMDBAL_SERIALIZER_INT8_HPP
#define LMDBAL_SERIALIZER_INT8_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<int8_t> {
public:
Serializer():value(0) {};
Serializer(const int8_t& p_value):value(p_value) {};
~Serializer() {};
int8_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 1);
return value;
};
MDB_val setData(const int8_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 1;
return result;
};
void clear() {}; //not possible;
private:
int8_t value;
};
}
#endif //LMDBAL_SERIALIZER_INT8_HPP

View file

@ -0,0 +1,59 @@
// 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 LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#define LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#include <QByteArray>
namespace LMDBAL {
template<>
class Serializer<QByteArray> {
public:
Serializer():value() {};
Serializer(const QByteArray& p_value):value(p_value) {};
~Serializer() {};
QByteArray deserialize(const MDB_val& data) {
value.setRawData((char*)data.mv_data, data.mv_size);
return value;
};
MDB_val setData(const QByteArray& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = value.data();
result.mv_size = value.size();
return result;
};
void clear() {
value.clear();
};
private:
QByteArray value;
};
}
#endif //LMDBAL_SERIALIZER_QBYTEARRAY_HPP

View file

@ -0,0 +1,59 @@
// 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 LMDBAL_SERIALIZER_QSTRING_HPP
#define LMDBAL_SERIALIZER_QSTRING_HPP
#include <QString>
#include <QByteArray>
namespace LMDBAL {
template<>
class Serializer<QString> {
public:
Serializer():value() {};
Serializer(const QString& p_value):value(p_value.toUtf8()) {};
~Serializer() {};
QString deserialize(const MDB_val& data) {
value = QByteArray((char*)data.mv_data, data.mv_size);
return QString::fromUtf8(value);
};
MDB_val setData(const QString& data) {
value = data.toUtf8();
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = value.data();
result.mv_size = value.size();
return result;
};
void clear() {}; //not possible;
private:
QByteArray value;
};
}
#endif //LMDBAL_SERIALIZER_QSTRING_HPP

View file

@ -0,0 +1,57 @@
// 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 LMDBAL_SERIALIZER_STDSTRING_HPP
#define LMDBAL_SERIALIZER_STDSTRING_HPP
#include <string>
namespace LMDBAL {
template<>
class Serializer<std::string> {
public:
Serializer():value() {};
Serializer(const std::string& p_value):value(p_value) {};
~Serializer() {};
std::string deserialize(const MDB_val& data) {
value = std::string((char*)data.mv_data, data.mv_size);
return value;
};
MDB_val setData(const std::string& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = (char*)value.c_str();
result.mv_size = value.size();
return result;
};
void clear() {}; //not possible;
private:
std::string value;
};
}
#endif //LMDBAL_SERIALIZER_STDSTRING_HPP

55
src/serializer_uint16.hpp Normal file
View file

@ -0,0 +1,55 @@
// 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 LMDBAL_SERIALIZER_UINT16_HPP
#define LMDBAL_SERIALIZER_UINT16_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint16_t> {
public:
Serializer():value(0) {};
Serializer(const uint16_t& p_value):value(p_value) {};
~Serializer() {};
uint16_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 2);
return value;
};
MDB_val setData(const uint16_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 2;
return result;
};
void clear() {}; //not possible;
private:
uint16_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT16_HPP

53
src/serializer_uint32.hpp Normal file
View file

@ -0,0 +1,53 @@
// 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 LMDBAL_SERIALIZER_UINT32_HPP
#define LMDBAL_SERIALIZER_UINT32_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint32_t> {
public:
Serializer():value(0) {};
Serializer(const uint32_t& p_value):value(p_value) {};
~Serializer() {};
uint32_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 4);
return value;
};
MDB_val setData(const uint32_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 4;
return result;
};
void clear() {}; //not possible;
private:
uint32_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT32_HPP

54
src/serializer_uint64.hpp Normal file
View file

@ -0,0 +1,54 @@
// 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 LMDBAL_SERIALIZER_UINT64_HPP
#define LMDBAL_SERIALIZER_UINT64_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint64_t> {
public:
Serializer():value(0) {};
Serializer(const uint64_t& p_value):value(p_value) {};
~Serializer() {};
uint64_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 8);
return value;
};
MDB_val setData(const uint64_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 8;
return result;
};
void clear() {}; //not possible;
private:
uint64_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT64_HPP

56
src/serializer_uint8.hpp Normal file
View file

@ -0,0 +1,56 @@
// 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 LMDBAL_SERIALIZER_UINT8_HPP
#define LMDBAL_SERIALIZER_UINT8_HPP
#include <stdint.h>
namespace LMDBAL {
template<>
class Serializer<uint8_t> {
public:
Serializer():value(0) {};
Serializer(const uint8_t& p_value):value(p_value) {};
~Serializer() {};
uint8_t deserialize(const MDB_val& data) {
std::memcpy(&value, data.mv_data, 1);
return value;
};
MDB_val setData(const uint8_t& data) {
value = data;
return getData();
};
MDB_val getData() {
MDB_val result;
result.mv_data = &value;
result.mv_size = 1;
return result;
};
void clear() {}; //not possible;
private:
uint8_t value;
};
}
#endif //LMDBAL_SERIALIZER_UINT8_HPP

121
src/storage.cpp Normal file
View 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"
LMDBAL::iStorage::iStorage(const std::string& p_name, Base* parent):
dbi(),
db(parent),
name(p_name)
{}
LMDBAL::iStorage::~iStorage() {}
void LMDBAL::iStorage::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 LMDBAL::iStorage::drop(MDB_txn* transaction) {
return mdb_drop(transaction, dbi, 0);
}
const std::string & LMDBAL::iStorage::dbName() const {
return db->name;}
bool LMDBAL::iStorage::isDBOpened() const {
return db->opened;}
void LMDBAL::iStorage::ensureOpened(const std::string& methodName) const {
if (!db->opened)
throw Closed(methodName, db->name, name);
}
uint32_t LMDBAL::iStorage::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 LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
abortTransaction(txn);
if (rc == MDB_KEYEXIST)
throwDuplicate(key);
else
throwUnknown(rc);
}
void LMDBAL::iStorage::throwNotFoundOrUnknown(int rc, LMDBAL::TransactionID txn, const std::string& key) const {
abortTransaction(txn);
if (rc == MDB_NOTFOUND)
throwNotFound(key);
else
throwUnknown(rc);
}
void LMDBAL::iStorage::throwUnknown(int rc, LMDBAL::TransactionID txn) const {
abortTransaction(txn);
throwUnknown(rc);
}
void LMDBAL::iStorage::throwUnknown(int rc) const {
throw Unknown(db->name, mdb_strerror(rc), name);}
void LMDBAL::iStorage::throwDuplicate(const std::string& key) const {
throw Exist(key, db->name, name);}
void LMDBAL::iStorage::throwNotFound(const std::string& key) const {
throw NotFound(key, db->name, name);}
LMDBAL::TransactionID LMDBAL::iStorage::beginReadOnlyTransaction() const {
return db->beginReadOnlyTransaction(name);}
LMDBAL::TransactionID LMDBAL::iStorage::beginTransaction() const {
return db->beginTransaction(name);}
void LMDBAL::iStorage::abortTransaction(LMDBAL::TransactionID id) const {
db->abortTransaction(id);}
void LMDBAL::iStorage::commitTransaction(LMDBAL::TransactionID id) const {
db->commitTransaction(id);}

110
src/storage.h Normal file
View file

@ -0,0 +1,110 @@
// 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 LMDBAL_STORAGE_H
#define LMDBAL_STORAGE_H
#include "base.h"
#include "serializer.h"
namespace LMDBAL {
class iStorage {
friend class Base;
protected:
iStorage(const std::string& name, Base* parent);
virtual ~iStorage();
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;
Base* 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);
template <class T>
static std::string toString(const T& value);
};
template <class K, class V>
class Storage : public iStorage {
friend class Base;
protected:
Storage(const std::string& name, Base* parent);
~Storage() override;
public:
using iStorage::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);
virtual void removeRecord(const K& key);
virtual bool checkRecord(const K& key) const; //checks if there is a record with given key
virtual V getRecord(const K& key) const;
virtual std::map<K, V> readAll() const;
virtual void replaceAll(const std::map<K, V>& data);
virtual uint32_t addRecords(const std::map<K, V>& data, bool overwrite = false);
protected:
Serializer<K>* keySerializer;
Serializer<V>* valueSerializer;
int createTable(MDB_txn* transaction) override;
};
}
#include "storage.hpp"
#endif //LMDBAL_STORAGE_H

291
src/storage.hpp Normal file
View 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 LMDBAL_STORAGE_HPP
#define LMDBAL_STORAGE_HPP
#include "storage.h"
#include "exceptions.h"
template<class K, class V>
LMDBAL::Storage<K, V>::Storage(const std::string& p_name, Base* parent):
iStorage(p_name, parent),
keySerializer(new Serializer<K>()),
valueSerializer(new Serializer<V>())
{}
template<class K, class V>
LMDBAL::Storage<K, V>::~Storage() {
delete valueSerializer;
delete keySerializer;
}
template<class K, class V>
void LMDBAL::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 LMDBAL::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 LMDBAL::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 LMDBAL::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 LMDBAL::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> LMDBAL::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 LMDBAL::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 LMDBAL::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 LMDBAL::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 LMDBAL::Storage<K, V>::createTable(MDB_txn* transaction) {
return makeTable<K>(transaction);
}
template<class K>
inline int LMDBAL::iStorage::makeTable(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<uint64_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<uint32_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<uint16_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<uint8_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<int64_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<int32_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::makeTable<int16_t>(MDB_txn* transaction) {
return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi);
}
template<>
inline int LMDBAL::iStorage::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 LMDBAL::iStorage::toString(const T& value) {
return std::to_string(value);
}
template<>
inline std::string LMDBAL::iStorage::toString(const QString& value) {
return value.toStdString();
}
template<>
inline std::string LMDBAL::iStorage::toString(const std::string& value) {
return value;
}
#endif //LMDBAL_STORAGE_HPP