1
0
forked from blue/lmdbal

project name change

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

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.16)
project(storage VERSION 0.0.1 LANGUAGES CXX)
project(lmdbal VERSION 0.2.0 LANGUAGES CXX)
cmake_policy(SET CMP0076 NEW)
cmake_policy(SET CMP0079 NEW)
@ -28,63 +28,32 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif ()
set(SOURCES
exceptions.cpp
storage.cpp
database.cpp
)
set(HEADERS
database.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
)
if (BUILD_STATIC)
add_library(storage STATIC ${SOURCES})
add_library(lmdbal STATIC ${SOURCES})
else ()
add_library(storage SHARED ${SOURCES})
add_library(lmdbal SHARED ${SOURCES})
endif()
add_subdirectory(src)
if (BUILD_TESTS)
add_subdirectory(test)
endif ()
set_target_properties(storage PROPERTIES PUBLIC_HEADER "${HEADERS}")
target_include_directories(lmdbal PUBLIC ${CMAKE_SOURCE_DIR}/src)
target_include_directories(lmdbal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(lmdbal PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(lmdbal PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_include_directories(storage PUBLIC ${CMAKE_SOURCE_DIR})
target_include_directories(storage PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(storage PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(storage PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_link_libraries(storage
target_link_libraries(lmdbal
PRIVATE
Qt${QT_VERSION_MAJOR}::Core
)
target_link_libraries(storage PRIVATE lmdb)
target_link_libraries(lmdbal PRIVATE lmdb)
install(TARGETS storage
install(TARGETS lmdbal
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/storage
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/storage
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}/lmdbal
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/lmdbal
)

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}")

View File

@ -14,11 +14,11 @@
// 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 "database.h"
#include "base.h"
#include "exceptions.h"
#include "storage.h"
LMDBDataBase::DataBase::DataBase(const QString& p_name, uint16_t mapSize):
LMDBAL::Base::Base(const QString& p_name, uint16_t mapSize):
name(p_name.toStdString()),
opened(false),
size(mapSize),
@ -27,19 +27,19 @@ LMDBDataBase::DataBase::DataBase(const QString& p_name, uint16_t mapSize):
transactions(new Transactions())
{}
LMDBDataBase::DataBase::~DataBase() {
LMDBAL::Base::~Base() {
close();
delete transactions;
for (const std::pair<const std::string, StorageBase*>& pair : tables)
for (const std::pair<const std::string, iStorage*>& pair : tables)
delete pair.second;
}
void LMDBDataBase::DataBase::close() {
void LMDBAL::Base::close() {
if (opened) {
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
StorageBase* table = pair.second;
for (const std::pair<const std::string, iStorage*>& pair : tables) {
iStorage* table = pair.second;
mdb_dbi_close(environment, table->dbi);
}
mdb_env_close(environment);
@ -47,7 +47,7 @@ void LMDBDataBase::DataBase::close() {
}
}
void LMDBDataBase::DataBase::open() {
void LMDBAL::Base::open() {
if (!opened) {
mdb_env_create(&environment);
QString path = createDirectory();
@ -59,8 +59,8 @@ void LMDBDataBase::DataBase::open() {
MDB_txn *txn;
mdb_txn_begin(environment, NULL, 0, &txn);
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
StorageBase* table = pair.second;
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));
@ -70,7 +70,7 @@ void LMDBDataBase::DataBase::open() {
}
}
bool LMDBDataBase::DataBase::removeDirectory() {
bool LMDBAL::Base::removeDirectory() {
if (opened)
throw Opened(name, "remove database directory");
@ -84,7 +84,7 @@ bool LMDBDataBase::DataBase::removeDirectory() {
return true;
}
QString LMDBDataBase::DataBase::createDirectory() {
QString LMDBAL::Base::createDirectory() {
if (opened)
throw Opened(name, "create database directory");
@ -101,13 +101,13 @@ QString LMDBDataBase::DataBase::createDirectory() {
return path;
}
QString LMDBDataBase::DataBase::getName() const {
QString LMDBAL::Base::getName() const {
return QString::fromStdString(name);}
bool LMDBDataBase::DataBase::ready() const {
bool LMDBAL::Base::ready() const {
return opened;}
void LMDBDataBase::DataBase::drop() {
void LMDBAL::Base::drop() {
if (!opened)
throw Closed("drop", name);
@ -116,7 +116,7 @@ void LMDBDataBase::DataBase::drop() {
if (rc)
throw Unknown(name, mdb_strerror(rc));
for (const std::pair<const std::string, StorageBase*>& pair : tables) {
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);
@ -125,20 +125,20 @@ void LMDBDataBase::DataBase::drop() {
mdb_txn_commit(txn);
}
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction() const {
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction() const {
return beginReadOnlyTransaction(emptyName);}
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction() const {
LMDBAL::TransactionID LMDBAL::Base::beginTransaction() const {
return beginTransaction(emptyName);}
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id) const {
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id) const {
return abortTransaction(id, emptyName);}
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id) const {
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) const {
return commitTransaction(id, emptyName);}
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction(const std::string& storageName) const {
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
if (!opened)
throw Closed("beginReadOnlyTransaction", name, storageName);
@ -152,7 +152,7 @@ LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction(con
return txn;
}
LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction(const std::string& storageName) const {
LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const {
if (!opened)
throw Closed("beginTransaction", name, storageName);
@ -166,7 +166,7 @@ LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction(const std::
return txn;
}
void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
if (!opened)
throw Closed("abortTransaction", name, storageName);
@ -178,7 +178,7 @@ void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id, co
transactions->erase(itr);
}
void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const {
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
if (!opened)
throw Closed("abortTransaction", name, storageName);

View File

@ -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 LMDBDATABASE_DATABASE_H
#define LMDBDATABASE_DATABASE_H
#ifndef LMDBAL_BASE_H
#define LMDBAL_BASE_H
#include <map>
#include <set>
@ -31,9 +31,9 @@
#include "exceptions.h"
namespace LMDBDataBase {
namespace LMDBAL {
class StorageBase;
class iStorage;
template<class T>
class Serializer;
@ -46,12 +46,12 @@ class Cache;
typedef MDB_txn* TransactionID;
class DataBase {
friend class StorageBase;
class Base {
friend class iStorage;
public:
DataBase(const QString& name, uint16_t mapSize = 10);
~DataBase();
Base(const QString& name, uint16_t mapSize = 10);
~Base();
void open();
void close();
@ -79,7 +79,7 @@ public:
Cache<K, V>* getCache(const std::string& name);
private:
typedef std::map<std::string, StorageBase*> Tables;
typedef std::map<std::string, iStorage*> Tables;
typedef std::set<TransactionID> Transactions;
TransactionID beginReadOnlyTransaction(const std::string& storageName) const;
@ -102,33 +102,33 @@ private:
#include "operators.hpp"
template <class K, class V>
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::addTable(const std::string& p_name) {
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, (StorageBase*)table));
tables.insert(std::make_pair(p_name, (iStorage*)table));
return table;
}
template<class K, class V>
LMDBDataBase::Cache<K, V> * LMDBDataBase::DataBase::addCache(const std::string& p_name) {
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, (StorageBase*)cache));
tables.insert(std::make_pair(p_name, (iStorage*)cache));
return cache;
}
template <class K, class V>
LMDBDataBase::Storage<K, V>* LMDBDataBase::DataBase::getTable(const std::string& p_name) {
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>
LMDBDataBase::Cache<K, V>* LMDBDataBase::DataBase::getCache(const std::string& p_name) {
LMDBAL::Cache<K, V>* LMDBAL::Base::getCache(const std::string& p_name) {
return static_cast<Cache<K, V>*>(tables.at(p_name));
}
#endif // LMDBDATABASE_DATABASE_H
#endif //LMDBAL_BASE_H

View File

@ -14,26 +14,26 @@
// 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_CACHE_H
#define LMDBDATABASE_CACHE_H
#ifndef LMDBAL_CACHE_H
#define LMDBAL_CACHE_H
#include <map>
#include <set>
#include "storage.h"
namespace LMDBDataBase {
namespace LMDBAL {
template <class K, class V>
class Cache : public Storage<K, V> {
friend class DataBase;
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, DataBase* parent);
Cache(const std::string& name, Base* parent);
~Cache() override;
private:
@ -65,4 +65,4 @@ protected:
#include "cache.hpp"
#endif // LMDBDATABASE_CACHE_H
#endif // LMDBAL_CACHE_H

View File

@ -14,14 +14,14 @@
// 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_CACHE_HPP
#define LMDBDATABASE_CACHE_HPP
#ifndef LMDBAL_CACHE_HPP
#define LMDBAL_CACHE_HPP
#include "cache.h"
#include "exceptions.h"
template<class K, class V>
LMDBDataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
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>()),
@ -33,7 +33,7 @@ LMDBDataBase::Cache<K, V>::Cache(const std::string& p_name, DataBase* parent):
}
template<class K, class V>
LMDBDataBase::Cache<K, V>::~Cache() {
LMDBAL::Cache<K, V>::~Cache() {
delete sizeDifference;
delete mode;
delete cache;
@ -41,11 +41,11 @@ LMDBDataBase::Cache<K, V>::~Cache() {
}
template<class K, class V>
void LMDBDataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::addRecordMethodName);
void LMDBAL::Cache<K, V>::addRecord(const K& key, const V& value) {
iStorage::ensureOpened(iStorage::addRecordMethodName);
if (cache->count(key) > 0)
StorageBase::throwDuplicate(StorageBase::toString(key));
iStorage::throwDuplicate(iStorage::toString(key));
Storage<K, V>::addRecord(key, value);
cache->insert(std::make_pair(key, value));
@ -55,8 +55,8 @@ void LMDBDataBase::Cache<K, V>::addRecord(const K& key, const V& value) {
}
template<class K, class V>
bool LMDBDataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::forceRecordMethodName);
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) {
@ -76,19 +76,19 @@ bool LMDBDataBase::Cache<K, V>::forceRecord(const K& key, const V& value) {
}
template<class K, class V>
void LMDBDataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
StorageBase::ensureOpened(StorageBase::changeRecordMethodName);
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())
StorageBase::throwNotFound(StorageBase::toString(key));
iStorage::throwNotFound(iStorage::toString(key));
Storage<K, V>::changeRecord(key, value);
itr->second = value;
} else {
if (abscent->count(key) > 0)
StorageBase::throwNotFound(StorageBase::toString(key));
iStorage::throwNotFound(iStorage::toString(key));
try {
Storage<K, V>::changeRecord(key, value);
@ -105,15 +105,15 @@ void LMDBDataBase::Cache<K, V>::changeRecord(const K& key, const V& value) {
}
template<class K, class V>
V LMDBDataBase::Cache<K, V>::getRecord(const K& key) const {
StorageBase::ensureOpened(StorageBase::getRecordMethodName);
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)
StorageBase::throwNotFound(StorageBase::toString(key));
iStorage::throwNotFound(iStorage::toString(key));
try {
V value = Storage<K, V>::getRecord(key);
@ -129,8 +129,8 @@ V LMDBDataBase::Cache<K, V>::getRecord(const K& key) const {
}
template<class K, class V>
bool LMDBDataBase::Cache<K, V>::checkRecord(const K& key) const {
StorageBase::ensureOpened(StorageBase::checkRecordMethodName);
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())
@ -153,8 +153,8 @@ bool LMDBDataBase::Cache<K, V>::checkRecord(const K& key) const {
}
template<class K, class V>
std::map<K, V> LMDBDataBase::Cache<K, V>::readAll() const {
StorageBase::ensureOpened(StorageBase::readAllMethodName);
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
@ -167,7 +167,7 @@ std::map<K, V> LMDBDataBase::Cache<K, V>::readAll() const {
}
template<class K, class V>
void LMDBDataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
void LMDBAL::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
Storage<K, V>::replaceAll(data);
*cache = data;
@ -179,7 +179,7 @@ void LMDBDataBase::Cache<K, V>::replaceAll(const std::map<K, V>& data) {
}
template<class K, class V>
uint32_t LMDBDataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
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;
@ -210,12 +210,12 @@ uint32_t LMDBDataBase::Cache<K, V>::addRecords(const std::map<K, V>& data, bool
}
template<class K, class V>
void LMDBDataBase::Cache<K, V>::removeRecord(const K& key) {
StorageBase::ensureOpened(StorageBase::removeRecordMethodName);
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)
StorageBase::throwNotFound(StorageBase::toString(key));
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
@ -226,7 +226,7 @@ void LMDBDataBase::Cache<K, V>::removeRecord(const K& key) {
}
template<class K, class V>
uint32_t LMDBDataBase::Cache<K, V>::count() const {
uint32_t LMDBAL::Cache<K, V>::count() const {
switch (*mode) {
case Mode::nothing:
{
@ -250,7 +250,7 @@ uint32_t LMDBDataBase::Cache<K, V>::count() const {
}
template<class K, class V>
void LMDBDataBase::Cache<K, V>::handleMode() const {
void LMDBAL::Cache<K, V>::handleMode() const {
if (*mode == Mode::size) {
--(*sizeDifference);
if (*sizeDifference == 0) {
@ -261,7 +261,7 @@ void LMDBDataBase::Cache<K, V>::handleMode() const {
}
template<class K, class V>
int LMDBDataBase::Cache<K, V>::drop(MDB_txn * transaction) {
int LMDBAL::Cache<K, V>::drop(MDB_txn * transaction) {
int res = Storage<K, V>::drop(transaction);
cache->clear();
abscent->clear();
@ -270,4 +270,4 @@ int LMDBDataBase::Cache<K, V>::drop(MDB_txn * transaction) {
return res;
}
#endif //LMDBDATABASE_CACHE_HPP
#endif //LMDBAL_CACHE_HPP

View File

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

View File

@ -14,14 +14,14 @@
// 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_EXCEPTIONS_H
#define LMDBDATABASE_EXCEPTIONS_H
#ifndef LMDBAL_EXCEPTIONS_H
#define LMDBAL_EXCEPTIONS_H
#include <stdexcept>
#include <string>
#include <optional>
namespace LMDBDataBase {
namespace LMDBAL {
class Exception : public std::exception {
public:
@ -98,4 +98,4 @@ private:
}
#endif //LMDBDATABASE_EXCEPTIONS_H
#endif //LMDBAL_EXCEPTIONS_H

View File

@ -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_OPERATORS_HPP
#define CORE_OPERATORS_HPP
#ifndef LMDBAL_OPERATORS_HPP
#define LMDBAL_OPERATORS_HPP
#include <map>
#include <set>
@ -206,4 +206,4 @@ QDataStream& operator >> (QDataStream &in, std::list<K>& container) {
return in;
}
#endif //CORE_OPERATORS_HPP
#endif //LMDBAL_OPERATORS_HPP

View File

@ -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 LMDBDATABASE_SERIALIZER_H
#define LMDBDATABASE_SERIALIZER_H
#ifndef LMDBAL_SERIALIZER_H
#define LMDBAL_SERIALIZER_H
#include <cstring>
@ -23,9 +23,9 @@
#include <QBuffer>
#include <QDataStream>
#include "database.h"
#include <lmdb.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<class T>
class Serializer {
@ -66,4 +66,4 @@ private:
#include "serializer_qstring.hpp"
#include "serializer_qbytearray.hpp"
#endif // LMDBDATABASE_SERIALIZER_H
#endif // LMDBAL_SERIALIZER_H

View File

@ -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_HPP
#define LMDBDATABASE_SERIALIZER_HPP
#ifndef LMDBAL_SERIALIZER_HPP
#define LMDBAL_SERIALIZER_HPP
#include "serializer.h"
template<class T>
LMDBDataBase::Serializer<T>::Serializer() :
LMDBAL::Serializer<T>::Serializer() :
bytes(),
buffer(&bytes),
stream(&buffer)
@ -29,7 +29,7 @@ LMDBDataBase::Serializer<T>::Serializer() :
}
template<class T>
LMDBDataBase::Serializer<T>::Serializer(const T& value) :
LMDBAL::Serializer<T>::Serializer(const T& value) :
bytes(),
buffer(&bytes),
stream(&buffer)
@ -39,19 +39,19 @@ LMDBDataBase::Serializer<T>::Serializer(const T& value) :
}
template<class T>
LMDBDataBase::Serializer<T>::~Serializer() {
LMDBAL::Serializer<T>::~Serializer() {
buffer.close();
}
template<class T>
MDB_val LMDBDataBase::Serializer<T>::setData(const T& value) {
MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
clear();
_setData(value);
return getData();
}
template<class T>
T LMDBDataBase::Serializer<T>::deserialize(const MDB_val& value) {
T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
clear();
bytes.setRawData((char*)value.mv_data, value.mv_size);
T result;
@ -61,19 +61,19 @@ T LMDBDataBase::Serializer<T>::deserialize(const MDB_val& value) {
}
template<class T>
void LMDBDataBase::Serializer<T>::_setData(const T& value) {
void LMDBAL::Serializer<T>::_setData(const T& value) {
stream << value;
}
template<class T>
void LMDBDataBase::Serializer<T>::clear() {
void LMDBAL::Serializer<T>::clear() {
if (buffer.pos() > 0) {
buffer.seek(0);
}
}
template<class T>
MDB_val LMDBDataBase::Serializer<T>::getData() {
MDB_val LMDBAL::Serializer<T>::getData() {
MDB_val val;
val.mv_size = buffer.pos();
@ -82,4 +82,4 @@ MDB_val LMDBDataBase::Serializer<T>::getData() {
return val;
}
#endif //LMDBDATABASE_SERIALIZER_HPP
#endif //LMDBAL_SERIALIZER_HPP

View File

@ -14,10 +14,10 @@
// 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 LMDBAL_SERIALIZER_DOUBLE_HPP
#define LMDBAL_SERIALIZER_DOUBLE_HPP
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<double> {
@ -48,7 +48,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_DOUBLE_HPP
#endif //LMDBAL_SERIALIZER_DOUBLE_HPP

View File

@ -14,10 +14,10 @@
// 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 LMDBAL_SERIALIZER_FLOAT_HPP
#define LMDBAL_SERIALIZER_FLOAT_HPP
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<float> {
@ -48,7 +48,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_FLOAT_HPP
#endif //LMDBAL_SERIALIZER_FLOAT_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_INT16_HPP
#define LMDBAL_SERIALIZER_INT16_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<int16_t> {
@ -50,4 +50,4 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_INT16_HPP
#endif //LMDBAL_SERIALIZER_INT16_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_INT32_HPP
#define LMDBAL_SERIALIZER_INT32_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<int32_t> {
@ -50,7 +50,7 @@ private:
}
#endif //CORE_DATABASE_SERIALIZER_INT32_HPP
#endif //LMDBAL_SERIALIZER_INT32_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_INT64_HPP
#define LMDBAL_SERIALIZER_INT64_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<int64_t> {
@ -50,7 +50,7 @@ private:
}
#endif //CORE_DATABASE_SERIALIZER_INT64_HPP
#endif //LMDBAL_SERIALIZER_INT64_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_INT8_HPP
#define LMDBAL_SERIALIZER_INT8_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<int8_t> {
@ -50,7 +50,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_INT8_HPP
#endif //LMDBAL_SERIALIZER_INT8_HPP

View File

@ -14,12 +14,12 @@
// 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_QBYTEARRAY_HPP
#define LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP
#ifndef LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#define LMDBAL_SERIALIZER_QBYTEARRAY_HPP
#include <QByteArray>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<QByteArray> {
@ -52,7 +52,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP
#endif //LMDBAL_SERIALIZER_QBYTEARRAY_HPP

View File

@ -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_QSTRING_HPP
#define LMDBDATABASE_SERIALIZER_QSTRING_HPP
#ifndef LMDBAL_SERIALIZER_QSTRING_HPP
#define LMDBAL_SERIALIZER_QSTRING_HPP
#include <QString>
#include <QByteArray>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<QString> {
@ -51,7 +51,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_QSTRING_HPP
#endif //LMDBAL_SERIALIZER_QSTRING_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_STDSTRING_HPP
#define LMDBAL_SERIALIZER_STDSTRING_HPP
#include <string>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<std::string> {
@ -50,7 +50,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_STDSTRING_HPP
#endif //LMDBAL_SERIALIZER_STDSTRING_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_UINT16_HPP
#define LMDBAL_SERIALIZER_UINT16_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<uint16_t> {
@ -50,6 +50,6 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_UINT16_HPP
#endif //LMDBAL_SERIALIZER_UINT16_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_UINT32_HPP
#define LMDBAL_SERIALIZER_UINT32_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<uint32_t> {
@ -50,4 +50,4 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_UINT32_HPP
#endif //LMDBAL_SERIALIZER_UINT32_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_UINT64_HPP
#define LMDBAL_SERIALIZER_UINT64_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<uint64_t> {
@ -50,5 +50,5 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_UINT64_HPP
#endif //LMDBAL_SERIALIZER_UINT64_HPP

View File

@ -14,12 +14,12 @@
// 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 LMDBAL_SERIALIZER_UINT8_HPP
#define LMDBAL_SERIALIZER_UINT8_HPP
#include <stdint.h>
namespace LMDBDataBase {
namespace LMDBAL {
template<>
class Serializer<uint8_t> {
@ -50,7 +50,7 @@ private:
}
#endif //LMDBDATABASE_SERIALIZER_UINT8_HPP
#endif //LMDBAL_SERIALIZER_UINT8_HPP

View File

@ -16,15 +16,15 @@
#include "storage.h"
LMDBDataBase::StorageBase::StorageBase(const std::string& p_name, DataBase* parent):
LMDBAL::iStorage::iStorage(const std::string& p_name, Base* parent):
dbi(),
db(parent),
name(p_name)
{}
LMDBDataBase::StorageBase::~StorageBase() {}
LMDBAL::iStorage::~iStorage() {}
void LMDBDataBase::StorageBase::drop() {
void LMDBAL::iStorage::drop() {
ensureOpened(dropMethodName);
MDB_txn *txn;
@ -42,22 +42,22 @@ void LMDBDataBase::StorageBase::drop() {
mdb_txn_commit(txn);
}
int LMDBDataBase::StorageBase::drop(MDB_txn* transaction) {
int LMDBAL::iStorage::drop(MDB_txn* transaction) {
return mdb_drop(transaction, dbi, 0);
}
const std::string & LMDBDataBase::StorageBase::dbName() const {
const std::string & LMDBAL::iStorage::dbName() const {
return db->name;}
bool LMDBDataBase::StorageBase::isDBOpened() const {
bool LMDBAL::iStorage::isDBOpened() const {
return db->opened;}
void LMDBDataBase::StorageBase::ensureOpened(const std::string& methodName) const {
void LMDBAL::iStorage::ensureOpened(const std::string& methodName) const {
if (!db->opened)
throw Closed(methodName, db->name, name);
}
uint32_t LMDBDataBase::StorageBase::count() const {
uint32_t LMDBAL::iStorage::count() const {
ensureOpened(countMethodName);
MDB_txn *txn;
@ -78,7 +78,7 @@ uint32_t LMDBDataBase::StorageBase::count() const {
return amount;
}
void LMDBDataBase::StorageBase::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
void LMDBAL::iStorage::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const {
abortTransaction(txn);
if (rc == MDB_KEYEXIST)
throwDuplicate(key);
@ -86,7 +86,7 @@ void LMDBDataBase::StorageBase::throwDuplicateOrUnknown(int rc, TransactionID tx
throwUnknown(rc);
}
void LMDBDataBase::StorageBase::throwNotFoundOrUnknown(int rc, LMDBDataBase::TransactionID txn, const std::string& key) const {
void LMDBAL::iStorage::throwNotFoundOrUnknown(int rc, LMDBAL::TransactionID txn, const std::string& key) const {
abortTransaction(txn);
if (rc == MDB_NOTFOUND)
throwNotFound(key);
@ -94,28 +94,28 @@ void LMDBDataBase::StorageBase::throwNotFoundOrUnknown(int rc, LMDBDataBase::Tra
throwUnknown(rc);
}
void LMDBDataBase::StorageBase::throwUnknown(int rc, LMDBDataBase::TransactionID txn) const {
void LMDBAL::iStorage::throwUnknown(int rc, LMDBAL::TransactionID txn) const {
abortTransaction(txn);
throwUnknown(rc);
}
void LMDBDataBase::StorageBase::throwUnknown(int rc) const {
void LMDBAL::iStorage::throwUnknown(int rc) const {
throw Unknown(db->name, mdb_strerror(rc), name);}
void LMDBDataBase::StorageBase::throwDuplicate(const std::string& key) const {
void LMDBAL::iStorage::throwDuplicate(const std::string& key) const {
throw Exist(key, db->name, name);}
void LMDBDataBase::StorageBase::throwNotFound(const std::string& key) const {
void LMDBAL::iStorage::throwNotFound(const std::string& key) const {
throw NotFound(key, db->name, name);}
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginReadOnlyTransaction() const {
LMDBAL::TransactionID LMDBAL::iStorage::beginReadOnlyTransaction() const {
return db->beginReadOnlyTransaction(name);}
LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginTransaction() const {
LMDBAL::TransactionID LMDBAL::iStorage::beginTransaction() const {
return db->beginTransaction(name);}
void LMDBDataBase::StorageBase::abortTransaction(LMDBDataBase::TransactionID id) const {
void LMDBAL::iStorage::abortTransaction(LMDBAL::TransactionID id) const {
db->abortTransaction(id);}
void LMDBDataBase::StorageBase::commitTransaction(LMDBDataBase::TransactionID id) const {
void LMDBAL::iStorage::commitTransaction(LMDBAL::TransactionID id) const {
db->commitTransaction(id);}

View File

@ -14,19 +14,19 @@
// 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_H
#define LMDBDATABASE_STORAGE_H
#ifndef LMDBAL_STORAGE_H
#define LMDBAL_STORAGE_H
#include "database.h"
#include "base.h"
#include "serializer.h"
namespace LMDBDataBase {
namespace LMDBAL {
class StorageBase {
friend class DataBase;
class iStorage {
friend class Base;
protected:
StorageBase(const std::string& name, DataBase* parent);
virtual ~StorageBase();
iStorage(const std::string& name, Base* parent);
virtual ~iStorage();
virtual int createTable(MDB_txn * transaction) = 0;
virtual int drop(MDB_txn * transaction);
@ -53,7 +53,7 @@ public:
protected:
MDB_dbi dbi;
DataBase* db;
Base* db;
const std::string name;
inline static const std::string dropMethodName = "drop";
@ -78,14 +78,14 @@ protected:
};
template <class K, class V>
class Storage : public StorageBase {
friend class DataBase;
class Storage : public iStorage {
friend class Base;
protected:
Storage(const std::string& name, DataBase* parent);
Storage(const std::string& name, Base* parent);
~Storage() override;
public:
using StorageBase::drop;
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);
@ -107,4 +107,4 @@ protected:
#include "storage.hpp"
#endif // LMDBDATABASE_STORAGE_H
#endif //LMDBAL_STORAGE_H

View File

@ -14,27 +14,27 @@
// 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
#ifndef LMDBAL_STORAGE_HPP
#define LMDBAL_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),
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>
LMDBDataBase::Storage<K, V>::~Storage() {
LMDBAL::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) {
void LMDBAL::Storage<K, V>::addRecord(const K& key, const V& value) {
ensureOpened(addRecordMethodName);
TransactionID txn = beginTransaction();
@ -49,7 +49,7 @@ void LMDBDataBase::Storage<K, V>::addRecord(const K& key, const V& value) {
}
template<class K, class V>
bool LMDBDataBase::Storage<K, V>::forceRecord(const K& key, const V& value) {
bool LMDBAL::Storage<K, V>::forceRecord(const K& key, const V& value) {
ensureOpened(forceRecordMethodName);
bool added;
@ -81,7 +81,7 @@ bool LMDBDataBase::Storage<K, V>::forceRecord(const K& key, const V& value) {
}
template<class K, class V>
void LMDBDataBase::Storage<K, V>::changeRecord(const K& key, const V& value) {
void LMDBAL::Storage<K, V>::changeRecord(const K& key, const V& value) {
ensureOpened(changeRecordMethodName);
TransactionID txn = beginTransaction();
@ -96,7 +96,7 @@ void LMDBDataBase::Storage<K, V>::changeRecord(const K& key, const V& value) {
}
template<class K, class V>
V LMDBDataBase::Storage<K, V>::getRecord(const K& key) const {
V LMDBAL::Storage<K, V>::getRecord(const K& key) const {
ensureOpened(getRecordMethodName);
TransactionID txn = beginReadOnlyTransaction();
@ -114,7 +114,7 @@ V LMDBDataBase::Storage<K, V>::getRecord(const K& key) const {
}
template<class K, class V>
bool LMDBDataBase::Storage<K, V>::checkRecord(const K& key) const {
bool LMDBAL::Storage<K, V>::checkRecord(const K& key) const {
ensureOpened(checkRecordMethodName);
TransactionID txn = beginReadOnlyTransaction();
@ -134,7 +134,7 @@ bool LMDBDataBase::Storage<K, V>::checkRecord(const K& key) const {
}
template<class K, class V>
std::map<K, V> LMDBDataBase::Storage<K, V>::readAll() const {
std::map<K, V> LMDBAL::Storage<K, V>::readAll() const {
ensureOpened(readAllMethodName);
TransactionID txn = beginReadOnlyTransaction();
@ -162,7 +162,7 @@ std::map<K, V> LMDBDataBase::Storage<K, V>::readAll() const {
}
template<class K, class V>
void LMDBDataBase::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
void LMDBAL::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
ensureOpened(replaceAllMethodName);
TransactionID txn = beginTransaction();
@ -183,7 +183,7 @@ void LMDBDataBase::Storage<K, V>::replaceAll(const std::map<K, V>& data) {
}
template<class K, class V>
uint32_t LMDBDataBase::Storage<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
uint32_t LMDBAL::Storage<K, V>::addRecords(const std::map<K, V>& data, bool overwrite) {
ensureOpened(addRecordsMethodName);
TransactionID txn = beginTransaction();
@ -211,7 +211,7 @@ uint32_t LMDBDataBase::Storage<K, V>::addRecords(const std::map<K, V>& data, boo
}
template<class K, class V>
void LMDBDataBase::Storage<K, V>::removeRecord(const K& key) {
void LMDBAL::Storage<K, V>::removeRecord(const K& key) {
ensureOpened(removeRecordMethodName);
TransactionID txn = beginTransaction();
@ -224,68 +224,68 @@ void LMDBDataBase::Storage<K, V>::removeRecord(const K& key) {
}
template<class K, class V>
int LMDBDataBase::Storage<K, V>::createTable(MDB_txn* transaction) {
int LMDBAL::Storage<K, V>::createTable(MDB_txn* transaction) {
return makeTable<K>(transaction);
}
template<class K>
inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) {
inline int LMDBAL::iStorage::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) {
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 LMDBDataBase::StorageBase::makeTable<uint32_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<uint16_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<uint8_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<int64_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<int32_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<int16_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::makeTable<int8_t>(MDB_txn* transaction) {
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 LMDBDataBase::StorageBase::toString(const T& value) {
inline std::string LMDBAL::iStorage::toString(const T& value) {
return std::to_string(value);
}
template<>
inline std::string LMDBDataBase::StorageBase::toString(const QString& value) {
inline std::string LMDBAL::iStorage::toString(const QString& value) {
return value.toStdString();
}
template<>
inline std::string LMDBDataBase::StorageBase::toString(const std::string& value) {
inline std::string LMDBAL::iStorage::toString(const std::string& value) {
return value;
}
#endif //LMDBDATABASE_STORAGE_HPP
#endif //LMDBAL_STORAGE_HPP

View File

@ -8,14 +8,14 @@ add_executable(runUnitTests
target_compile_options(runUnitTests PRIVATE -fPIC)
target_include_directories(runUnitTests PRIVATE ${CMAKE_SOURCE_DIR})
target_include_directories(runUnitTests PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}_INCLUDE_DIRS})
target_include_directories(runUnitTests PRIVATE ${Qt${QT_VERSION_MAJOR}Core_INCLUDE_DIRS})
target_link_libraries(
runUnitTests
GTest::gtest_main
storage
lmdbal
)
include(GoogleTest)

View File

@ -1,24 +1,24 @@
#include <gtest/gtest.h>
#include "database.h"
#include "base.h"
#include "storage.h"
#include "cache.h"
#include <QString>
class DataBaseTest : public ::testing::Test {
class BaseTest : public ::testing::Test {
protected:
DataBaseTest():
BaseTest():
::testing::Test(),
t1(db->getTable<uint32_t, uint32_t>("table1")),
t2(db->getTable<QString, QString>("table2")),
c1(db->getCache<int8_t, std::string>("cache1")) {}
~DataBaseTest() {}
~BaseTest() {}
static void SetUpTestSuite() {
if (db == nullptr) {
db = new LMDBDataBase::DataBase("testBase");
db = new LMDBAL::Base("testBase");
db->addTable<uint32_t, uint32_t>("table1");
db->addTable<QString, QString>("table2");
db->addCache<int8_t, std::string>("cache1");
@ -32,21 +32,21 @@ protected:
db = nullptr;
}
static LMDBDataBase::DataBase* db;
static LMDBAL::Base* db;
LMDBDataBase::Storage<uint32_t, uint32_t>* t1;
LMDBDataBase::Storage<QString, QString>* t2;
LMDBDataBase::Cache<int8_t, std::string>* c1;
LMDBAL::Storage<uint32_t, uint32_t>* t1;
LMDBAL::Storage<QString, QString>* t2;
LMDBAL::Cache<int8_t, std::string>* c1;
};
LMDBDataBase::DataBase* DataBaseTest::db = nullptr;
LMDBAL::Base* BaseTest::db = nullptr;
TEST_F(DataBaseTest, RemovingDirectory) {
TEST_F(BaseTest, RemovingDirectory) {
EXPECT_EQ(db->removeDirectory(), true);
}
TEST_F(DataBaseTest, OpeningClosingDatabase) {
TEST_F(BaseTest, OpeningClosingDatabase) {
EXPECT_EQ(db->ready(), false);
db->open();
EXPECT_EQ(db->ready(), true);
@ -56,7 +56,7 @@ TEST_F(DataBaseTest, OpeningClosingDatabase) {
EXPECT_EQ(db->ready(), true);
}
TEST_F(DataBaseTest, AddingIntegerKey) {
TEST_F(BaseTest, AddingIntegerKey) {
EXPECT_EQ(db->ready(), true);
t1->addRecord(1, 2);
t1->addRecord(2, 2);
@ -64,7 +64,7 @@ TEST_F(DataBaseTest, AddingIntegerKey) {
EXPECT_EQ(t1->getRecord(1), 2);
}
TEST_F(DataBaseTest, AddingQStringKey) {
TEST_F(BaseTest, AddingQStringKey) {
EXPECT_EQ(db->ready(), true);
t2->addRecord("hello", "world");
t2->addRecord("aaa", "gagdfsdf");
@ -73,7 +73,7 @@ TEST_F(DataBaseTest, AddingQStringKey) {
EXPECT_EQ(t2->getRecord("hello"), "world");
}
TEST_F(DataBaseTest, AddingKeysToCache) {
TEST_F(BaseTest, AddingKeysToCache) {
EXPECT_EQ(db->ready(), true);
c1->addRecord(2, "blah balah");
c1->addRecord(-4, "testing goes brrr");
@ -83,48 +83,48 @@ TEST_F(DataBaseTest, AddingKeysToCache) {
EXPECT_EQ(c1->getRecord(-116), "whatever");
}
TEST_F(DataBaseTest, AddingRepeatingIntegerKey) {
TEST_F(BaseTest, AddingRepeatingIntegerKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t1->addRecord(3, 24);
} catch (const LMDBDataBase::Exist e) {
} catch (const LMDBAL::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(t1->getRecord(3), 15);
}
TEST_F(DataBaseTest, AddingRepeatingStringKey) {
TEST_F(BaseTest, AddingRepeatingStringKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
t2->addRecord("sdfhga", "world");
} catch (const LMDBDataBase::Exist e) {
} catch (const LMDBAL::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(t2->getRecord("sdfhga"), "DSFFDG");
}
TEST_F(DataBaseTest, AddingRepeatingCacheKey) {
TEST_F(BaseTest, AddingRepeatingCacheKey) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
c1->addRecord(-4, "world");
} catch (const LMDBDataBase::Exist e) {
} catch (const LMDBAL::Exist e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
EXPECT_EQ(c1->getRecord(-4), "testing goes brrr");
}
TEST_F(DataBaseTest, GettingNotExistingKeys) {
TEST_F(BaseTest, GettingNotExistingKeys) {
EXPECT_EQ(db->ready(), true);
bool thrown = false;
try {
QString wrong = t2->getRecord("almonds");
} catch (const LMDBDataBase::NotFound e) {
} catch (const LMDBAL::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 LMDBDataBase::NotFound e) {
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
@ -140,18 +140,18 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) {
thrown = false;
try {
std::string wrong = c1->getRecord(21);
} catch (const LMDBDataBase::NotFound e) {
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
TEST_F(DataBaseTest, Persistence) {
TEST_F(BaseTest, Persistence) {
EXPECT_EQ(db->ready(), true);
db->close();
delete db;
db = new LMDBDataBase::DataBase("testBase");
db = new LMDBAL::Base("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 LMDBDataBase::NotFound e) {
} catch (const LMDBAL::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 LMDBDataBase::NotFound e) {
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
@ -191,13 +191,13 @@ TEST_F(DataBaseTest, Persistence) {
thrown = false;
try {
std::string wrong = c1->getRecord(89);
} catch (const LMDBDataBase::NotFound e) {
} catch (const LMDBAL::NotFound e) {
thrown = true;
}
ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";
}
TEST_F(DataBaseTest, CountAndDrop) {
TEST_F(BaseTest, CountAndDrop) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 3);
EXPECT_EQ(t2->count(), 4);
@ -219,7 +219,7 @@ TEST_F(DataBaseTest, CountAndDrop) {
EXPECT_EQ(c1->count(), 2);
}
TEST_F(DataBaseTest, Change) {
TEST_F(BaseTest, Change) {
EXPECT_EQ(db->ready(), true);
EXPECT_EQ(t1->count(), 1);
EXPECT_EQ(t2->count(), 1);
@ -253,7 +253,7 @@ TEST_F(DataBaseTest, Change) {
EXPECT_EQ(c1->count(), 3);
}
TEST_F(DataBaseTest, Force) {
TEST_F(BaseTest, Force) {
EXPECT_EQ(db->ready(), true);
t1->forceRecord(58, 35); //changing