diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a400b8..1eb24f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,15 +30,15 @@ endif () set(SOURCES exceptions.cpp - table.cpp + storage.cpp database.cpp ) set(HEADERS database.h exceptions.h - table.h - table.hpp + storage.h + storage.hpp cache.h cache.hpp serializer.h diff --git a/cache.h b/cache.h index 6f7042c..ba72f46 100644 --- a/cache.h +++ b/cache.h @@ -14,16 +14,18 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef DATABASE_CACHE_H -#define DATABASE_CACHE_H +#ifndef LMDBDATABASE_CACHE_H +#define LMDBDATABASE_CACHE_H -#include "database.h" -#include "table.h" #include #include +#include "storage.h" + +namespace LMDBDataBase { + template -class DataBase::Cache : public DataBase::Table { +class Cache : public Storage { friend class DataBase; enum class Mode { //it's a cache state when we: nothing, // - know nothing about records in database on disk @@ -46,7 +48,7 @@ public: virtual V getRecord(const K& key) const override; virtual uint32_t count() const override; - using DataBase::Table::drop; + using Storage::drop; virtual int drop(MDB_txn * transaction) override; virtual std::map readAll() const override; virtual void replaceAll(const std::map& data) override; @@ -59,6 +61,8 @@ protected: uint32_t* sizeDifference; }; +} + #include "cache.hpp" -#endif // DATABASE_CACHE_H +#endif // LMDBDATABASE_CACHE_H diff --git a/cache.hpp b/cache.hpp index 811b161..1212f4e 100644 --- a/cache.hpp +++ b/cache.hpp @@ -14,15 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef DATABASE_CACHE_HPP -#define DATABASE_CACHE_HPP +#ifndef LMDBDATABASE_CACHE_HPP +#define LMDBDATABASE_CACHE_HPP #include "cache.h" #include "exceptions.h" template -DataBase::Cache::Cache(const std::string& p_name, DataBase* parent): - DataBase::Table(p_name, parent), +LMDBDataBase::Cache::Cache(const std::string& p_name, DataBase* parent): + Storage(p_name, parent), mode(new Mode), cache(new std::map()), abscent(new std::set()), @@ -33,7 +33,7 @@ DataBase::Cache::Cache(const std::string& p_name, DataBase* parent): } template -DataBase::Cache::~Cache() { +LMDBDataBase::Cache::~Cache() { delete sizeDifference; delete mode; delete cache; @@ -41,73 +41,62 @@ DataBase::Cache::~Cache() { } template -void DataBase::Cache::addRecord(const K& key, const V& value) { - if (!DataBase::Table::db->opened) { - throw Closed("addRecord", DataBase::Table::db->name, DataBase::Table::name); - } +void LMDBDataBase::Cache::addRecord(const K& key, const V& value) { + StorageBase::ensureOpened(StorageBase::addRecordMethodName); - if (cache->count(key) > 0) { - throw Exist(DataBase::_Table::toString(key), DataBase::Table::db->name, DataBase::Table::name); - } + if (cache->count(key) > 0) + StorageBase::throwDuplicate(StorageBase::toString(key)); - Table::addRecord(key, value); + Storage::addRecord(key, value); cache->insert(std::make_pair(key, value)); - if (*mode != Mode::full) { + if (*mode != Mode::full) abscent->erase(key); - } } template -bool DataBase::Cache::forceRecord(const K& key, const V& value) { - if (!DataBase::Table::db->opened) { - throw Closed("forceRecord", DataBase::Table::db->name, DataBase::Table::name); - } +bool LMDBDataBase::Cache::forceRecord(const K& key, const V& value) { + StorageBase::ensureOpened(StorageBase::forceRecordMethodName); - bool added = Table::forceRecord(key, value); + bool added = Storage::forceRecord(key, value); if (*mode == Mode::full) { (*cache)[key] = value; } else { - if (added) { + if (added) abscent->erase(key); - } + std::pair::iterator, bool> result = cache->insert(std::make_pair(key, value)); - if (!result.second) { + if (!result.second) result.first->second = value; - } else if (!added) { //this way database had value but cache didn't, so, need to decrease sizeDifference + else if (!added) //this way database had value but cache didn't, so, need to decrease sizeDifference handleMode(); - } } return added; } template -void DataBase::Cache::changeRecord(const K& key, const V& value) { - if (!DataBase::Table::db->opened) { - throw Closed("changeRecord", DataBase::Table::db->name, DataBase::Table::name); - } +void LMDBDataBase::Cache::changeRecord(const K& key, const V& value) { + StorageBase::ensureOpened(StorageBase::changeRecordMethodName); if (*mode == Mode::full) { typename std::map::iterator itr = cache->find(key); - if (itr == cache->end()) { - throw NotFound(DataBase::_Table::toString(key), DataBase::Table::db->name, DataBase::Table::name); - } - Table::changeRecord(key, value); + if (itr == cache->end()) + StorageBase::throwNotFound(StorageBase::toString(key)); + + Storage::changeRecord(key, value); itr->second = value; } else { - if (abscent->count(key) > 0) { - throw NotFound(DataBase::_Table::toString(key), DataBase::Table::db->name, DataBase::Table::name); - } + if (abscent->count(key) > 0) + StorageBase::throwNotFound(StorageBase::toString(key)); try { - Table::changeRecord(key, value); + Storage::changeRecord(key, value); typename std::pair::iterator, bool> res = cache->insert(std::make_pair(key, value)); - if (!res.second) { + if (!res.second) res.first->second = value; - } else { + else handleMode(); - } } catch (const NotFound& error) { abscent->insert(key); throw error; @@ -116,70 +105,60 @@ void DataBase::Cache::changeRecord(const K& key, const V& value) { } template -V DataBase::Cache::getRecord(const K& key) const { - if (!DataBase::Table::db->opened) { - throw Closed("getRecord", DataBase::Table::db->name, DataBase::Table::name); - } +V LMDBDataBase::Cache::getRecord(const K& key) const { + StorageBase::ensureOpened(StorageBase::getRecordMethodName); typename std::map::const_iterator itr = cache->find(key); - if (itr != cache->end()) { + if (itr != cache->end()) return itr->second; - } - if (*mode == Mode::full || abscent->count(key) != 0) { - throw NotFound(DataBase::_Table::toString(key), DataBase::Table::db->name, DataBase::Table::name); - } + if (*mode == Mode::full || abscent->count(key) != 0) + StorageBase::throwNotFound(StorageBase::toString(key)); try { - V value = Table::getRecord(key); + V value = Storage::getRecord(key); cache->insert(std::make_pair(key, value)); handleMode(); return value; } catch (const NotFound& error) { - if (*mode != Mode::full) { + if (*mode != Mode::full) abscent->insert(key); - } + throw error; } } template -bool DataBase::Cache::checkRecord(const K& key) const { - if (!DataBase::Table::db->opened) { - throw Closed("checkRecord", DataBase::Table::db->name, DataBase::Table::name); - } +bool LMDBDataBase::Cache::checkRecord(const K& key) const { + StorageBase::ensureOpened(StorageBase::checkRecordMethodName); typename std::map::const_iterator itr = cache->find(key); - if (itr != cache->end()) { + if (itr != cache->end()) return true; - } - if (*mode == Mode::full || abscent->count(key) != 0) { + if (*mode == Mode::full || abscent->count(key) != 0) return false; - } try { - V value = Table::getRecord(key); + V value = Storage::getRecord(key); cache->insert(std::make_pair(key, value)); handleMode(); return true; } catch (const NotFound& error) { - if (*mode != Mode::full) { + if (*mode != Mode::full) abscent->insert(key); - } + return false; } } template -std::map DataBase::Cache::readAll() const { - if (!DataBase::Table::db->opened) { - throw Closed("readAll", DataBase::Table::db->name, DataBase::Table::name); - } +std::map LMDBDataBase::Cache::readAll() const { + StorageBase::ensureOpened(StorageBase::readAllMethodName); - if (*mode != Mode::full) { //there is a room for optimization - *mode = Mode::full; //I can read and deserialize only those values - *cache = DataBase::Table::readAll(); //that are missing in the cache + if (*mode != Mode::full) { //there is a room for optimization + *mode = Mode::full; //I can read and deserialize only those values + *cache = Storage::readAll(); //that are missing in the cache abscent->clear(); *sizeDifference = 0; } @@ -188,27 +167,32 @@ std::map DataBase::Cache::readAll() const { } template -void DataBase::Cache::replaceAll(const std::map& data) { - DataBase::Table::replaceAll(data); +void LMDBDataBase::Cache::replaceAll(const std::map& data) { + Storage::replaceAll(data); *cache = data; + + if (*mode != Mode::full) { + *mode = Mode::full; + abscent->clear(); + *sizeDifference = 0; + } } template -uint32_t DataBase::Cache::addRecords(const std::map& data, bool overwrite) { - uint32_t newSize = DataBase::Table::addRecords(data, overwrite); +uint32_t LMDBDataBase::Cache::addRecords(const std::map& data, bool overwrite) { + uint32_t newSize = Storage::addRecords(data, overwrite); Mode& m = *mode; - if (m == Mode::nothing) { + if (m == Mode::nothing) m = Mode::size; - } + std::map& c = *cache; std::set& a = *abscent; for (const std::pair& pair : data) { std::pair::iterator, bool> res = c.insert(pair); if (!res.second) { - if (overwrite) { + if (overwrite) res.first->second = pair.second; - } } else if (m != Mode::full) { a.erase(pair.first); } @@ -226,31 +210,27 @@ uint32_t DataBase::Cache::addRecords(const std::map& data, bool over } template -void DataBase::Cache::removeRecord(const K& key) { - if (!DataBase::Table::db->opened) { - throw Closed("removeRecord", DataBase::Table::db->name, DataBase::Table::name); - } +void LMDBDataBase::Cache::removeRecord(const K& key) { + StorageBase::ensureOpened(StorageBase::removeRecordMethodName); typename std::pair::const_iterator, bool> pair = abscent->insert(key); - if (!pair.second) { - throw NotFound(DataBase::_Table::toString(key), DataBase::Table::db->name, DataBase::Table::name); - } + if (!pair.second) + StorageBase::throwNotFound(StorageBase::toString(key)); - Table::removeRecord(key); - if (cache->erase(key) == 0) { //if it was not cached and we are now in size mode then the sizeDifference would decrease + Storage::removeRecord(key); + if (cache->erase(key) == 0) //if it was not cached and we are now in size mode then the sizeDifference would decrease handleMode(); - } - if (*mode != Mode::full) { + + if (*mode != Mode::full) abscent->insert(key); - } } template -uint32_t DataBase::Cache::count() const { +uint32_t LMDBDataBase::Cache::count() const { switch (*mode) { case Mode::nothing: { - uint32_t sz = DataBase::Table::count(); + uint32_t sz = Storage::count(); *sizeDifference = sz - cache->size(); if (sz == 0) { *mode = Mode::full; @@ -264,11 +244,13 @@ uint32_t DataBase::Cache::count() const { return cache->size() + *sizeDifference; case Mode::full: return cache->size(); + default: + return 0; //unreachable, no such state, just to suppress the waring } } template -void DataBase::Cache::handleMode() const { +void LMDBDataBase::Cache::handleMode() const { if (*mode == Mode::size) { --(*sizeDifference); if (*sizeDifference == 0) { @@ -279,8 +261,8 @@ void DataBase::Cache::handleMode() const { } template -int DataBase::Cache::drop(MDB_txn * transaction) { - int res = DataBase::Table::drop(transaction); +int LMDBDataBase::Cache::drop(MDB_txn * transaction) { + int res = Storage::drop(transaction); cache->clear(); abscent->clear(); *mode = Mode::full; @@ -288,4 +270,4 @@ int DataBase::Cache::drop(MDB_txn * transaction) { return res; } -#endif //DATABASE_CACHE_HPP +#endif //LMDBDATABASE_CACHE_HPP diff --git a/database.cpp b/database.cpp index fc114dc..87f4419 100644 --- a/database.cpp +++ b/database.cpp @@ -16,30 +16,30 @@ #include "database.h" #include "exceptions.h" -#include "table.h" +#include "storage.h" -DataBase::DataBase(const QString& p_name, uint16_t mapSize): +LMDBDataBase::DataBase::DataBase(const QString& p_name, uint16_t mapSize): name(p_name.toStdString()), opened(false), size(mapSize), environment(), - tables() -{ -} + tables(), + transactions(new Transactions()) +{} -DataBase::~DataBase() -{ +LMDBDataBase::DataBase::~DataBase() { close(); - for (const std::pair& pair : tables) { + + delete transactions; + + for (const std::pair& pair : tables) delete pair.second; - } } -void DataBase::close() -{ +void LMDBDataBase::DataBase::close() { if (opened) { - for (const std::pair& pair : tables) { - _Table* table = pair.second; + for (const std::pair& pair : tables) { + StorageBase* table = pair.second; mdb_dbi_close(environment, table->dbi); } mdb_env_close(environment); @@ -47,8 +47,7 @@ void DataBase::close() } } -void DataBase::open() -{ +void LMDBDataBase::DataBase::open() { if (!opened) { mdb_env_create(&environment); QString path = createDirectory(); @@ -60,40 +59,34 @@ void DataBase::open() MDB_txn *txn; mdb_txn_begin(environment, NULL, 0, &txn); - for (const std::pair& pair : tables) { - _Table* table = pair.second; + for (const std::pair& pair : tables) { + StorageBase* table = pair.second; int rc = table->createTable(txn); - if (rc) { + if (rc) throw Unknown(name, mdb_strerror(rc)); - } - } mdb_txn_commit(txn); opened = true; } } -bool DataBase::removeDirectory() -{ - if (opened) { +bool LMDBDataBase::DataBase::removeDirectory() { + if (opened) throw Opened(name, "remove database directory"); - } + QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); path += "/" + getName(); QDir cache(path); - if (cache.exists()) { + if (cache.exists()) return cache.removeRecursively(); - } else { + else return true; - } } -QString DataBase::createDirectory() -{ - if (opened) { +QString LMDBDataBase::DataBase::createDirectory() { + if (opened) throw Opened(name, "create database directory"); - } QString path(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); path += "/" + getName(); @@ -101,44 +94,100 @@ QString DataBase::createDirectory() if (!cache.exists()) { bool res = cache.mkpath(path); - if (!res) { + if (!res) throw Directory(path.toStdString()); - } } return path; } +QString LMDBDataBase::DataBase::getName() const { + return QString::fromStdString(name);} -QString DataBase::getName() const -{ - return QString::fromStdString(name); -} +bool LMDBDataBase::DataBase::ready() const { + return opened;} -bool DataBase::ready() const -{ - return opened; -} - -void DataBase::drop() -{ - if (!opened) { +void LMDBDataBase::DataBase::drop() { + if (!opened) throw Closed("drop", name); - } MDB_txn *txn; int rc = mdb_txn_begin(environment, NULL, 0, &txn); - if (rc) { + if (rc) throw Unknown(name, mdb_strerror(rc)); - } - for (const std::pair& pair : tables) { + + for (const std::pair& pair : tables) { rc = pair.second->drop(txn); - if (rc) { + if (rc) throw Unknown(name, mdb_strerror(rc), pair.first); - } } mdb_txn_commit(txn); } +LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction() const { + return beginReadOnlyTransaction(emptyName);} +LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction() const { + return beginTransaction(emptyName);} + +void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id) const { + return abortTransaction(id, emptyName);} + +void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id) const { + return commitTransaction(id, emptyName);} + + +LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginReadOnlyTransaction(const std::string& storageName) const { + if (!opened) + throw Closed("beginReadOnlyTransaction", name, storageName); + + MDB_txn* txn; + int rc = mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(name, mdb_strerror(rc), storageName); + } + transactions->emplace(txn); + return txn; +} + +LMDBDataBase::TransactionID LMDBDataBase::DataBase::beginTransaction(const std::string& storageName) const { + if (!opened) + throw Closed("beginTransaction", name, storageName); + + MDB_txn* txn; + int rc = mdb_txn_begin(environment, NULL, 0, &txn); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(name, mdb_strerror(rc), storageName); + } + transactions->emplace(txn); + return txn; +} + +void LMDBDataBase::DataBase::abortTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const { + if (!opened) + throw Closed("abortTransaction", name, storageName); + + Transactions::iterator itr = transactions->find(id); + if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this + throw Unknown(name, "unable to abort transaction: transaction was not found", storageName); + + mdb_txn_abort(id); + transactions->erase(itr); +} + +void LMDBDataBase::DataBase::commitTransaction(LMDBDataBase::TransactionID id, const std::string& storageName) const { + if (!opened) + throw Closed("abortTransaction", name, storageName); + + Transactions::iterator itr = transactions->find(id); + if (itr == transactions->end()) //TODO may be it's a good idea to make an exception class for this + throw Unknown(name, "unable to commit transaction: transaction was not found", storageName); + + int rc = mdb_txn_commit(id); + transactions->erase(itr); + if (rc != MDB_SUCCESS) + throw Unknown(name, mdb_strerror(rc), storageName); +} diff --git a/database.h b/database.h index 005c1d9..27f318e 100644 --- a/database.h +++ b/database.h @@ -14,28 +14,41 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef CORE_DATABASE_H -#define CORE_DATABASE_H +#ifndef LMDBDATABASE_DATABASE_H +#define LMDBDATABASE_DATABASE_H #include +#include #include #include +#include #include #include #include + #include -class DataBase -{ - class _Table; - template - class Serializer; +#include "exceptions.h" + +namespace LMDBDataBase { + +class StorageBase; + +template +class Serializer; + +template +class Storage; + +template +class Cache; + +typedef MDB_txn* TransactionID; + +class DataBase { + friend class StorageBase; public: - template - class Table; - template - class Cache; DataBase(const QString& name, uint16_t mapSize = 10); ~DataBase(); @@ -48,69 +61,74 @@ public: QString getName() const; void drop(); + TransactionID beginReadOnlyTransaction() const; + TransactionID beginTransaction() const; + void commitTransaction(TransactionID id) const; + void abortTransaction(TransactionID id) const; template - Table* addTable(const std::string& name); + Storage* addTable(const std::string& name); template Cache* addCache(const std::string& name); template - Table* getTable(const std::string& name); + Storage* getTable(const std::string& name); template Cache* getCache(const std::string& name); -public: - //exceptions - class Exception; - class Directory; - class Closed; - class Opened; - class NotFound; - class Exist; - class Unknown; +private: + typedef std::map Tables; + typedef std::set Transactions; + + TransactionID beginReadOnlyTransaction(const std::string& storageName) const; + TransactionID beginTransaction(const std::string& storageName) const; + void commitTransaction(TransactionID id, const std::string& storageName) const; + void abortTransaction(TransactionID id, const std::string& storageName) const; private: std::string name; bool opened; uint16_t size; MDB_env* environment; - std::map tables; + Tables tables; + Transactions* transactions; + + inline static const std::string emptyName = ""; }; -#include "exceptions.h" +} #include "operators.hpp" template -DataBase::Table* DataBase::addTable(const std::string& p_name) { +LMDBDataBase::Storage* LMDBDataBase::DataBase::addTable(const std::string& p_name) { if (opened) { throw Opened(name, "add table " + p_name); } - DataBase::Table* table = new DataBase::Table(p_name, this); - tables.insert(std::make_pair(p_name, (_Table*)table)); + Storage* table = new Storage(p_name, this); + tables.insert(std::make_pair(p_name, (StorageBase*)table)); return table; } template -DataBase::Cache * DataBase::addCache(const std::string& p_name) { +LMDBDataBase::Cache * LMDBDataBase::DataBase::addCache(const std::string& p_name) { if (opened) { throw Opened(name, "add cache " + p_name); } - DataBase::Cache* cache = new DataBase::Cache(p_name, this); - tables.insert(std::make_pair(p_name, (_Table*)cache)); + Cache* cache = new Cache(p_name, this); + tables.insert(std::make_pair(p_name, (StorageBase*)cache)); return cache; } template -DataBase::Table* DataBase::getTable(const std::string& p_name) { - return static_cast*>(tables.at(p_name)); +LMDBDataBase::Storage* LMDBDataBase::DataBase::getTable(const std::string& p_name) { + return static_cast*>(tables.at(p_name)); } template -DataBase::Cache* DataBase::getCache(const std::string& p_name) { - return static_cast*>(tables.at(p_name)); +LMDBDataBase::Cache* LMDBDataBase::DataBase::getCache(const std::string& p_name) { + return static_cast*>(tables.at(p_name)); } - -#endif // CORE_DATABASE_H +#endif // LMDBDATABASE_DATABASE_H diff --git a/exceptions.cpp b/exceptions.cpp index 086649e..16a8010 100644 --- a/exceptions.cpp +++ b/exceptions.cpp @@ -16,26 +16,25 @@ #include "exceptions.h" -DataBase::Exception::Exception(): -std::exception() +LMDBDataBase::Exception::Exception(): + std::exception() {} -DataBase::Exception::~Exception() {} +LMDBDataBase::Exception::~Exception() {} -const char* DataBase::Exception::what() const noexcept( true ) -{ +const char* LMDBDataBase::Exception::what() const noexcept( true ) { std::string* msg = new std::string(getMessage()); return msg->c_str(); } -DataBase::Directory::Directory(const std::string& p_path): +LMDBDataBase::Directory::Directory(const std::string& p_path): Exception(), path(p_path) {} -std::string DataBase::Directory::getMessage() const { +std::string LMDBDataBase::Directory::getMessage() const { return "Can't create directory for database at " + path;} -DataBase::Closed::Closed( +LMDBDataBase::Closed::Closed( const std::string& p_operation, const std::string& p_dbName, const std::optional& p_tableName @@ -45,29 +44,28 @@ DataBase::Closed::Closed( dbName(p_dbName), tableName(p_tableName) {} -std::string DataBase::Closed::getMessage() const { +std::string LMDBDataBase::Closed::getMessage() const { std::string msg = "An attempt to perform operation " + operation + " on closed database " + dbName; - if (tableName.has_value()) { + if (tableName.has_value()) msg += + " on table " + tableName.value(); - } - return msg; + return msg; } -DataBase::Opened::Opened(const std::string& p_dbName, const std::string& p_action): +LMDBDataBase::Opened::Opened(const std::string& p_dbName, const std::string& p_action): Exception(), dbName(p_dbName), action(p_action) {} -std::string DataBase::Opened::getMessage() const { +std::string LMDBDataBase::Opened::getMessage() const { return "An attempt to " + action + " (the database " + dbName + ") but it's can't be done because the DataBase is already opened"; } -DataBase::NotFound::NotFound( +LMDBDataBase::NotFound::NotFound( const std::string& p_key, const std::string& p_dbName, const std::string& p_tableName @@ -77,12 +75,12 @@ DataBase::NotFound::NotFound( dbName(p_dbName), tableName(p_tableName) {} -std::string DataBase::NotFound::getMessage() const { +std::string LMDBDataBase::NotFound::getMessage() const { return "Element for id " + key + " wasn't found " + " in database " + dbName + " in table " + tableName;} -DataBase::Exist::Exist( +LMDBDataBase::Exist::Exist( const std::string& p_key, const std::string& p_dbName, const std::string& p_tableName @@ -92,14 +90,14 @@ DataBase::Exist::Exist( dbName(p_dbName), tableName(p_tableName) {} -std::string DataBase::Exist::getMessage() const { +std::string LMDBDataBase::Exist::getMessage() const { return "An attempt to insert element with key " + key + " to database " + dbName + " to table " + tableName + " but it already has an element with given id"; } -DataBase::Unknown::Unknown( +LMDBDataBase::Unknown::Unknown( const std::string& p_dbName, const std::string& message, const std::optional& p_tableName @@ -109,12 +107,11 @@ DataBase::Unknown::Unknown( tableName(p_tableName), msg(message) {} -std::string DataBase::Unknown::getMessage() const -{ +std::string LMDBDataBase::Unknown::getMessage() const { std::string result = "Unknown error in database " + dbName; - if (tableName.has_value()) { + if (tableName.has_value()) result += " in table " + tableName.value(); - } + result += ": " + msg; return result; } diff --git a/exceptions.h b/exceptions.h index 88c2e6a..87dc438 100644 --- a/exceptions.h +++ b/exceptions.h @@ -14,17 +14,16 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef CORE_DATABASE_EXCEPTIONS_H -#define CORE_DATABASE_EXCEPTIONS_H +#ifndef LMDBDATABASE_EXCEPTIONS_H +#define LMDBDATABASE_EXCEPTIONS_H #include #include #include -#include "database.h" +namespace LMDBDataBase { -class DataBase::Exception : public std::exception -{ +class Exception : public std::exception { public: Exception(); virtual ~Exception(); @@ -34,7 +33,7 @@ public: const char* what() const noexcept( true ); }; -class DataBase::Directory: public DataBase::Exception { +class Directory: public Exception { public: Directory(const std::string& path); @@ -43,7 +42,7 @@ private: std::string path; }; -class DataBase::Closed : public DataBase::Exception { +class Closed : public Exception { public: Closed(const std::string& p_operation, const std::string& dbName, const std::optional& tableName = std::nullopt); @@ -54,7 +53,7 @@ private: std::optional tableName; }; -class DataBase::Opened : public DataBase::Exception { +class Opened : Exception { public: Opened(const std::string& dbName, const std::string& action); @@ -64,7 +63,7 @@ private: std::string action; }; -class DataBase::NotFound : public DataBase::Exception { +class NotFound : public Exception { public: NotFound(const std::string& key, const std::string& dbName, const std::string& tableName); @@ -75,7 +74,7 @@ private: std::string tableName; }; -class DataBase::Exist : public DataBase::Exception { +class Exist : public Exception { public: Exist(const std::string& key, const std::string& dbName, const std::string& tableName); @@ -86,7 +85,7 @@ private: std::string tableName; }; -class DataBase::Unknown : public DataBase::Exception { +class Unknown : public Exception { public: Unknown(const std::string& dbName, const std::string& message, const std::optional& tableName = std::nullopt); @@ -97,4 +96,6 @@ private: std::string msg; }; -#endif //CORE_DATABASE_EXCEPTIONS_H +} + +#endif //LMDBDATABASE_EXCEPTIONS_H diff --git a/serializer.h b/serializer.h index 9ff3c18..8f00db9 100644 --- a/serializer.h +++ b/serializer.h @@ -14,8 +14,8 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef CORE_DATABASE_SERIALIZER_H -#define CORE_DATABASE_SERIALIZER_H +#ifndef LMDBDATABASE_SERIALIZER_H +#define LMDBDATABASE_SERIALIZER_H #include @@ -25,9 +25,10 @@ #include "database.h" +namespace LMDBDataBase { + template -class DataBase::Serializer -{ +class Serializer { public: Serializer(); Serializer(const T& value); @@ -48,6 +49,8 @@ private: QDataStream stream; }; +} + #include "serializer.hpp" #include "serializer_uint64.hpp" #include "serializer_uint32.hpp" @@ -63,4 +66,4 @@ private: #include "serializer_qstring.hpp" #include "serializer_qbytearray.hpp" -#endif // CORE_DATABASE_SERIALIZER_H +#endif // LMDBDATABASE_SERIALIZER_H diff --git a/serializer.hpp b/serializer.hpp index 59305c0..a580a88 100644 --- a/serializer.hpp +++ b/serializer.hpp @@ -14,14 +14,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . - -#ifndef CORE_DATABASE_SERIALIZER_HPP -#define CORE_DATABASE_SERIALIZER_HPP +#ifndef LMDBDATABASE_SERIALIZER_HPP +#define LMDBDATABASE_SERIALIZER_HPP #include "serializer.h" template -DataBase::Serializer::Serializer() : +LMDBDataBase::Serializer::Serializer() : bytes(), buffer(&bytes), stream(&buffer) @@ -30,7 +29,7 @@ DataBase::Serializer::Serializer() : } template -DataBase::Serializer::Serializer(const T& value) : +LMDBDataBase::Serializer::Serializer(const T& value) : bytes(), buffer(&bytes), stream(&buffer) @@ -40,19 +39,19 @@ DataBase::Serializer::Serializer(const T& value) : } template -DataBase::Serializer::~Serializer() { +LMDBDataBase::Serializer::~Serializer() { buffer.close(); } template -MDB_val DataBase::Serializer::setData(const T& value) { +MDB_val LMDBDataBase::Serializer::setData(const T& value) { clear(); _setData(value); return getData(); } template -T DataBase::Serializer::deserialize(const MDB_val& value) { +T LMDBDataBase::Serializer::deserialize(const MDB_val& value) { clear(); bytes.setRawData((char*)value.mv_data, value.mv_size); T result; @@ -62,19 +61,19 @@ T DataBase::Serializer::deserialize(const MDB_val& value) { } template -void DataBase::Serializer::_setData(const T& value) { +void LMDBDataBase::Serializer::_setData(const T& value) { stream << value; } template -void DataBase::Serializer::clear() { +void LMDBDataBase::Serializer::clear() { if (buffer.pos() > 0) { buffer.seek(0); } } template -MDB_val DataBase::Serializer::getData() { +MDB_val LMDBDataBase::Serializer::getData() { MDB_val val; val.mv_size = buffer.pos(); @@ -83,4 +82,4 @@ MDB_val DataBase::Serializer::getData() { return val; } -#endif //CORE_DATABASE_SERIALIZER_HPP +#endif //LMDBDATABASE_SERIALIZER_HPP diff --git a/serializer_double.hpp b/serializer_double.hpp index aaab3b1..6cdbf01 100644 --- a/serializer_double.hpp +++ b/serializer_double.hpp @@ -14,13 +14,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_DOUBLE_HPP +#define LMDBDATABASE_SERIALIZER_DOUBLE_HPP -#ifndef CORE_DATABASE_SERIALIZER_DOUBLE_HPP -#define CORE_DATABASE_SERIALIZER_DOUBLE_HPP +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const double& p_value):value(p_value) {}; @@ -46,8 +46,9 @@ private: double value; }; +} -#endif //CORE_DATABASE_SERIALIZER_DOUBLE_HPP +#endif //LMDBDATABASE_SERIALIZER_DOUBLE_HPP diff --git a/serializer_float.hpp b/serializer_float.hpp index 6a79946..29ce0e0 100644 --- a/serializer_float.hpp +++ b/serializer_float.hpp @@ -14,13 +14,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_FLOAT_HPP +#define LMDBDATABASE_SERIALIZER_FLOAT_HPP -#ifndef CORE_DATABASE_SERIALIZER_FLOAT_HPP -#define CORE_DATABASE_SERIALIZER_FLOAT_HPP +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const float& p_value):value(p_value) {}; @@ -46,8 +46,9 @@ private: float value; }; +} -#endif //CORE_DATABASE_SERIALIZER_FLOAT_HPP +#endif //LMDBDATABASE_SERIALIZER_FLOAT_HPP diff --git a/serializer_int16.hpp b/serializer_int16.hpp index 380b184..ff07821 100644 --- a/serializer_int16.hpp +++ b/serializer_int16.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_INT16_HPP +#define LMDBDATABASE_SERIALIZER_INT16_HPP -#ifndef CORE_DATABASE_SERIALIZER_INT16_HPP -#define CORE_DATABASE_SERIALIZER_INT16_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const int16_t& p_value):value(p_value) {}; @@ -46,5 +48,6 @@ private: int16_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_INT16_HPP +#endif //LMDBDATABASE_SERIALIZER_INT16_HPP diff --git a/serializer_int32.hpp b/serializer_int32.hpp index da89324..28da663 100644 --- a/serializer_int32.hpp +++ b/serializer_int32.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_INT32_HPP +#define LMDBDATABASE_SERIALIZER_INT32_HPP -#ifndef CORE_DATABASE_SERIALIZER_INT32_HPP -#define CORE_DATABASE_SERIALIZER_INT32_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const int32_t& p_value):value(p_value) {}; @@ -46,6 +48,7 @@ private: int32_t value; }; +} #endif //CORE_DATABASE_SERIALIZER_INT32_HPP diff --git a/serializer_int64.hpp b/serializer_int64.hpp index d39a368..14c7426 100644 --- a/serializer_int64.hpp +++ b/serializer_int64.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_INT64_HPP +#define LMDBDATABASE_SERIALIZER_INT64_HPP -#ifndef CORE_DATABASE_SERIALIZER_INT64_HPP -#define CORE_DATABASE_SERIALIZER_INT64_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const int64_t& p_value):value(p_value) {}; @@ -46,6 +48,7 @@ private: int64_t value; }; +} #endif //CORE_DATABASE_SERIALIZER_INT64_HPP diff --git a/serializer_int8.hpp b/serializer_int8.hpp index a1e0404..d6df4c1 100644 --- a/serializer_int8.hpp +++ b/serializer_int8.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_INT8_HPP +#define LMDBDATABASE_SERIALIZER_INT8_HPP -#ifndef CORE_DATABASE_SERIALIZER_INT8_HPP -#define CORE_DATABASE_SERIALIZER_INT8_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const int8_t& p_value):value(p_value) {}; @@ -46,8 +48,9 @@ private: int8_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_INT8_HPP +#endif //LMDBDATABASE_SERIALIZER_INT8_HPP diff --git a/serializer_qbytearray.hpp b/serializer_qbytearray.hpp index 741bf34..bbb8050 100644 --- a/serializer_qbytearray.hpp +++ b/serializer_qbytearray.hpp @@ -14,15 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . - -#ifndef CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP -#define CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP +#ifndef LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP +#define LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP #include +namespace LMDBDataBase { + template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value() {}; Serializer(const QByteArray& p_value):value(p_value) {}; @@ -50,8 +50,9 @@ private: QByteArray value; }; +} -#endif //CORE_DATABASE_SERIALIZER_QBYTEARRAY_HPP +#endif //LMDBDATABASE_SERIALIZER_QBYTEARRAY_HPP diff --git a/serializer_qstring.hpp b/serializer_qstring.hpp index 72c33dd..6073435 100644 --- a/serializer_qstring.hpp +++ b/serializer_qstring.hpp @@ -14,16 +14,16 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . - -#ifndef CORE_DATABASE_SERIALIZER_QSTRING_HPP -#define CORE_DATABASE_SERIALIZER_QSTRING_HPP +#ifndef LMDBDATABASE_SERIALIZER_QSTRING_HPP +#define LMDBDATABASE_SERIALIZER_QSTRING_HPP #include #include +namespace LMDBDataBase { + template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value() {}; Serializer(const QString& p_value):value(p_value.toUtf8()) {}; @@ -49,8 +49,9 @@ private: QByteArray value; }; +} -#endif //CORE_DATABASE_SERIALIZER_QSTRING_HPP +#endif //LMDBDATABASE_SERIALIZER_QSTRING_HPP diff --git a/serializer_stdstring.hpp b/serializer_stdstring.hpp index a17a6f1..9fdc152 100644 --- a/serializer_stdstring.hpp +++ b/serializer_stdstring.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_STDSTRING_HPP +#define LMDBDATABASE_SERIALIZER_STDSTRING_HPP -#ifndef CORE_DATABASE_SERIALIZER_STDSTRING_HPP -#define CORE_DATABASE_SERIALIZER_STDSTRING_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value() {}; Serializer(const std::string& p_value):value(p_value) {}; @@ -46,8 +48,9 @@ private: std::string value; }; +} -#endif //CORE_DATABASE_SERIALIZER_STDSTRING_HPP +#endif //LMDBDATABASE_SERIALIZER_STDSTRING_HPP diff --git a/serializer_uint16.hpp b/serializer_uint16.hpp index b5df796..ea3324c 100644 --- a/serializer_uint16.hpp +++ b/serializer_uint16.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_UINT16_HPP +#define LMDBDATABASE_SERIALIZER_UINT16_HPP -#ifndef CORE_DATABASE_SERIALIZER_UINT16_HPP -#define CORE_DATABASE_SERIALIZER_UINT16_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const uint16_t& p_value):value(p_value) {}; @@ -46,7 +48,8 @@ private: uint16_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_UINT16_HPP +#endif //LMDBDATABASE_SERIALIZER_UINT16_HPP diff --git a/serializer_uint32.hpp b/serializer_uint32.hpp index 590ecb2..bd07534 100644 --- a/serializer_uint32.hpp +++ b/serializer_uint32.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_UINT32_HPP +#define LMDBDATABASE_SERIALIZER_UINT32_HPP -#ifndef CORE_DATABASE_SERIALIZER_UINT32_HPP -#define CORE_DATABASE_SERIALIZER_UINT32_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const uint32_t& p_value):value(p_value) {}; @@ -46,5 +48,6 @@ private: uint32_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_UINT32_HPP +#endif //LMDBDATABASE_SERIALIZER_UINT32_HPP diff --git a/serializer_uint64.hpp b/serializer_uint64.hpp index 6edbbb5..9015378 100644 --- a/serializer_uint64.hpp +++ b/serializer_uint64.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_UINT64_HPP +#define LMDBDATABASE_SERIALIZER_UINT64_HPP -#ifndef CORE_DATABASE_SERIALIZER_UINT64_HPP -#define CORE_DATABASE_SERIALIZER_UINT64_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const uint64_t& p_value):value(p_value) {}; @@ -46,6 +48,7 @@ private: uint64_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_UINT64_HPP +#endif //LMDBDATABASE_SERIALIZER_UINT64_HPP diff --git a/serializer_uint8.hpp b/serializer_uint8.hpp index bb1fc7e..ba2526c 100644 --- a/serializer_uint8.hpp +++ b/serializer_uint8.hpp @@ -14,13 +14,15 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +#ifndef LMDBDATABASE_SERIALIZER_UINT8_HPP +#define LMDBDATABASE_SERIALIZER_UINT8_HPP -#ifndef CORE_DATABASE_SERIALIZER_UINT8_HPP -#define CORE_DATABASE_SERIALIZER_UINT8_HPP +#include + +namespace LMDBDataBase { template<> -class DataBase::Serializer -{ +class Serializer { public: Serializer():value(0) {}; Serializer(const uint8_t& p_value):value(p_value) {}; @@ -46,8 +48,9 @@ private: uint8_t value; }; +} -#endif //CORE_DATABASE_SERIALIZER_UINT8_HPP +#endif //LMDBDATABASE_SERIALIZER_UINT8_HPP diff --git a/storage.cpp b/storage.cpp new file mode 100644 index 0000000..213a0f2 --- /dev/null +++ b/storage.cpp @@ -0,0 +1,121 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// 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 . + +#include "storage.h" + +LMDBDataBase::StorageBase::StorageBase(const std::string& p_name, DataBase* parent): + dbi(), + db(parent), + name(p_name) +{} + +LMDBDataBase::StorageBase::~StorageBase() {} + +void LMDBDataBase::StorageBase::drop() { + ensureOpened(dropMethodName); + + MDB_txn *txn; + int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(db->name, mdb_strerror(rc), name); + } + rc = drop(txn); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(db->name, mdb_strerror(rc), name); + } + + mdb_txn_commit(txn); +} + +int LMDBDataBase::StorageBase::drop(MDB_txn* transaction) { + return mdb_drop(transaction, dbi, 0); +} + +const std::string & LMDBDataBase::StorageBase::dbName() const { + return db->name;} + +bool LMDBDataBase::StorageBase::isDBOpened() const { + return db->opened;} + +void LMDBDataBase::StorageBase::ensureOpened(const std::string& methodName) const { + if (!db->opened) + throw Closed(methodName, db->name, name); +} + +uint32_t LMDBDataBase::StorageBase::count() const { + ensureOpened(countMethodName); + + MDB_txn *txn; + MDB_stat stat; + int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(db->name, mdb_strerror(rc), name); + } + + rc = mdb_stat(txn, dbi, &stat); + if (rc) { + mdb_txn_abort(txn); + throw Unknown(db->name, mdb_strerror(rc), name); + } + uint32_t amount = stat.ms_entries; + mdb_txn_abort(txn); + return amount; +} + +void LMDBDataBase::StorageBase::throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const { + abortTransaction(txn); + if (rc == MDB_KEYEXIST) + throwDuplicate(key); + else + throwUnknown(rc); +} + +void LMDBDataBase::StorageBase::throwNotFoundOrUnknown(int rc, LMDBDataBase::TransactionID txn, const std::string& key) const { + abortTransaction(txn); + if (rc == MDB_NOTFOUND) + throwNotFound(key); + else + throwUnknown(rc); +} + +void LMDBDataBase::StorageBase::throwUnknown(int rc, LMDBDataBase::TransactionID txn) const { + abortTransaction(txn); + throwUnknown(rc); +} + +void LMDBDataBase::StorageBase::throwUnknown(int rc) const { + throw Unknown(db->name, mdb_strerror(rc), name);} + +void LMDBDataBase::StorageBase::throwDuplicate(const std::string& key) const { + throw Exist(key, db->name, name);} + +void LMDBDataBase::StorageBase::throwNotFound(const std::string& key) const { + throw NotFound(key, db->name, name);} + +LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginReadOnlyTransaction() const { + return db->beginReadOnlyTransaction(name);} + +LMDBDataBase::TransactionID LMDBDataBase::StorageBase::beginTransaction() const { + return db->beginTransaction(name);} + +void LMDBDataBase::StorageBase::abortTransaction(LMDBDataBase::TransactionID id) const { + db->abortTransaction(id);} + +void LMDBDataBase::StorageBase::commitTransaction(LMDBDataBase::TransactionID id) const { + db->commitTransaction(id);} diff --git a/table.h b/storage.h similarity index 51% rename from table.h rename to storage.h index 8279237..19990c1 100644 --- a/table.h +++ b/storage.h @@ -14,30 +14,61 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef CORE_TABLE_H -#define CORE_TABLE_H +#ifndef LMDBDATABASE_STORAGE_H +#define LMDBDATABASE_STORAGE_H #include "database.h" #include "serializer.h" -class DataBase::_Table { +namespace LMDBDataBase { + +class StorageBase { friend class DataBase; protected: - _Table(const std::string& name, DataBase* parent); - virtual ~_Table(); + StorageBase(const std::string& name, DataBase* parent); + virtual ~StorageBase(); virtual int createTable(MDB_txn * transaction) = 0; virtual int drop(MDB_txn * transaction); + bool isDBOpened() const; + const std::string& dbName() const; + + void ensureOpened(const std::string& methodName) const; + void throwDuplicateOrUnknown(int rc, TransactionID txn, const std::string& key) const; + void throwNotFoundOrUnknown(int rc, TransactionID txn, const std::string& key) const; + void throwUnknown(int rc, TransactionID txn) const; + void throwUnknown(int rc) const; + void throwDuplicate(const std::string& key) const; + void throwNotFound(const std::string& key)const ; + public: virtual void drop(); virtual uint32_t count() const; + TransactionID beginReadOnlyTransaction() const; + TransactionID beginTransaction() const; + void commitTransaction(TransactionID id) const; + void abortTransaction(TransactionID id) const; + protected: MDB_dbi dbi; DataBase* db; const std::string name; + inline static const std::string dropMethodName = "drop"; + inline static const std::string countMethodName = "count"; + + inline static const std::string addRecordMethodName = "addRecord"; + inline static const std::string forceRecordMethodName = "forceRecord"; + inline static const std::string changeRecordMethodName = "changeRecord"; + inline static const std::string removeRecordMethodName = "removeRecord"; + inline static const std::string checkRecordMethodName = "checkRecord"; + inline static const std::string getRecordMethodName = "getRecord"; + inline static const std::string readAllMethodName = "readAllRecord"; + inline static const std::string replaceAllMethodName = "replaceAll"; + inline static const std::string addRecordsMethodName = "addRecords"; + protected: template int makeTable(MDB_txn* transaction); @@ -47,14 +78,14 @@ protected: }; template -class DataBase::Table : public DataBase::_Table { +class Storage : public StorageBase { friend class DataBase; protected: - Table(const std::string& name, DataBase* parent); - ~Table() override; + Storage(const std::string& name, DataBase* parent); + ~Storage() override; public: - using DataBase::_Table::drop; + using StorageBase::drop; virtual void addRecord(const K& key, const V& value); virtual bool forceRecord(const K& key, const V& value); //returns true if there was addition, false if change virtual void changeRecord(const K& key, const V& value); @@ -72,6 +103,8 @@ protected: int createTable(MDB_txn* transaction) override; }; -#include "table.hpp" +} -#endif // CORE_TABLE_H +#include "storage.hpp" + +#endif // LMDBDATABASE_STORAGE_H diff --git a/storage.hpp b/storage.hpp new file mode 100644 index 0000000..198fcf1 --- /dev/null +++ b/storage.hpp @@ -0,0 +1,291 @@ +// Squawk messenger. +// Copyright (C) 2019 Yury Gubich +// +// 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 . + +#ifndef LMDBDATABASE_STORAGE_HPP +#define LMDBDATABASE_STORAGE_HPP + +#include "storage.h" +#include "exceptions.h" + +template +LMDBDataBase::Storage::Storage(const std::string& p_name, DataBase* parent): + StorageBase(p_name, parent), + keySerializer(new Serializer()), + valueSerializer(new Serializer()) +{} + +template +LMDBDataBase::Storage::~Storage() { + delete valueSerializer; + delete keySerializer; +} + +template +void LMDBDataBase::Storage::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 +bool LMDBDataBase::Storage::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 +void LMDBDataBase::Storage::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 +V LMDBDataBase::Storage::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 +bool LMDBDataBase::Storage::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 +std::map LMDBDataBase::Storage::readAll() const { + ensureOpened(readAllMethodName); + + TransactionID txn = beginReadOnlyTransaction(); + std::map 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 +void LMDBDataBase::Storage::replaceAll(const std::map& 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& 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 +uint32_t LMDBDataBase::Storage::addRecords(const std::map& data, bool overwrite) { + ensureOpened(addRecordsMethodName); + + TransactionID txn = beginTransaction(); + MDB_val lmdbKey, lmdbData; + int rc; + + for (const std::pair& 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 +void LMDBDataBase::Storage::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 +int LMDBDataBase::Storage::createTable(MDB_txn* transaction) { + return makeTable(transaction); +} + +template +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template<> +inline int LMDBDataBase::StorageBase::makeTable(MDB_txn* transaction) { + return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); +} + +template +inline std::string LMDBDataBase::StorageBase::toString(const T& value) { + return std::to_string(value); +} + +template<> +inline std::string LMDBDataBase::StorageBase::toString(const QString& value) { + return value.toStdString(); +} + +template<> +inline std::string LMDBDataBase::StorageBase::toString(const std::string& value) { + return value; +} + +#endif //LMDBDATABASE_STORAGE_HPP diff --git a/table.cpp b/table.cpp deleted file mode 100644 index 80ac7bc..0000000 --- a/table.cpp +++ /dev/null @@ -1,72 +0,0 @@ -// Squawk messenger. -// Copyright (C) 2019 Yury Gubich -// -// 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 . - -#include "table.h" - -DataBase::_Table::_Table(const std::string& p_name, DataBase* parent): - dbi(), - db(parent), - name(p_name) -{ -} - -DataBase::_Table::~_Table() {} - - -void DataBase::_Table::drop() -{ - if (!db->opened) { - throw Closed("drop", db->name, name); - } - - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - rc = drop(txn); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - mdb_txn_commit(txn); -} - -int DataBase::_Table::drop(MDB_txn* transaction) -{ - return mdb_drop(transaction, dbi, 0); -} - -uint32_t DataBase::_Table::count() const -{ - if (!db->opened) { - throw Closed("count", db->name, name); - } - - MDB_txn *txn; - MDB_stat stat; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - rc = mdb_stat(txn, dbi, &stat); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - uint32_t amount = stat.ms_entries; - mdb_txn_abort(txn); - return amount; -} diff --git a/table.hpp b/table.hpp deleted file mode 100644 index 49cb38e..0000000 --- a/table.hpp +++ /dev/null @@ -1,385 +0,0 @@ -// Squawk messenger. -// Copyright (C) 2019 Yury Gubich -// -// 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 . - -#ifndef CORE_TABLE_HPP -#define CORE_TABLE_HPP - -#include "table.h" -#include "exceptions.h" - -template -DataBase::Table::Table(const std::string& p_name, DataBase* parent): - _Table(p_name, parent), - keySerializer(new Serializer()), - valueSerializer(new Serializer()) -{ -} - -template -DataBase::Table::~Table() { - delete valueSerializer; - delete keySerializer; -} - -template -void DataBase::Table::addRecord(const K& key, const V& value) { - if (!db->opened) { - throw Closed("addRecord", db->name, name); - } - - MDB_val lmdbKey = keySerializer->setData(key); - MDB_val lmdbData = valueSerializer->setData(value); - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); - if (rc != 0) { - mdb_txn_abort(txn); - if (rc == MDB_KEYEXIST) { - throw Exist(toString(key), db->name, name); - } else { - throw Unknown(db->name, mdb_strerror(rc), name); - } - } else { - mdb_txn_commit(txn); - } -} - -template -bool DataBase::Table::forceRecord(const K& key, const V& value) { - if (!db->opened) { - throw Closed("forceRecord", db->name, name); - } - - bool added; - MDB_val lmdbKey = keySerializer->setData(key); - MDB_val lmdbData; - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); - if (rc == 0) { - added = false; - } else if (rc == MDB_NOTFOUND) { - added = true; - } else { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - lmdbData = valueSerializer->setData(value); - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0); - if (rc != 0) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } else { - mdb_txn_commit(txn); - } - - return added; -} - -template -void DataBase::Table::changeRecord(const K& key, const V& value) { - if (!db->opened) { - throw Closed("changeRecord", db->name, name); - } - - MDB_val lmdbKey = keySerializer->setData(key); - MDB_val lmdbData = valueSerializer->setData(value); - MDB_txn *txn; - - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, 0); - if (rc != 0) { - mdb_txn_abort(txn); - if (rc) { - throw Unknown(db->name, mdb_strerror(rc), name); - } - } else { - mdb_txn_commit(txn); - } -} - -template -V DataBase::Table::getRecord(const K& key) const { - if (!db->opened) { - throw Closed("getRecord", db->name, name); - } - - MDB_val lmdbKey = keySerializer->setData(key); - MDB_val lmdbData; - - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); - if (rc) { - mdb_txn_abort(txn); - if (rc == MDB_NOTFOUND) { - throw NotFound(toString(key), db->name, name); - } else { - throw Unknown(db->name, mdb_strerror(rc), name); - } - } else { - V value = valueSerializer->deserialize(lmdbData); - mdb_txn_abort(txn); - - return value; - } -} - -template -bool DataBase::Table::checkRecord(const K& key) const { - if (!db->opened) { - throw Closed("checkRecord", db->name, name); - } - - MDB_val lmdbKey = keySerializer->setData(key); - MDB_val lmdbData; - - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - rc = mdb_get(txn, dbi, &lmdbKey, &lmdbData); - mdb_txn_abort(txn); - - if (rc) { - if (rc == MDB_NOTFOUND) { - return false; - } else { - throw Unknown(db->name, mdb_strerror(rc), name); - } - } else { - return true; - } -} - -template -std::map DataBase::Table::readAll() const { - if (!db->opened) { - throw Closed("readAll", db->name, name); - } - - std::map result; - MDB_txn *txn; - MDB_cursor* cursor; - MDB_val lmdbKey, lmdbData; - int rc = mdb_txn_begin(db->environment, NULL, MDB_RDONLY, &txn); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - rc = mdb_cursor_open(txn, dbi, &cursor); - if (rc != MDB_SUCCESS) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_FIRST); - while (rc == MDB_SUCCESS) { - K key = keySerializer->deserialize(lmdbKey); - V value = valueSerializer->deserialize(lmdbData); - result.insert(std::make_pair(key, value)); - rc = mdb_cursor_get(cursor, &lmdbKey, &lmdbData, MDB_NEXT); - } - - mdb_txn_abort(txn); - if (rc != MDB_NOTFOUND) { - throw Unknown(db->name, mdb_strerror(rc), name); - } - - return result; -} - -template -void DataBase::Table::replaceAll(const std::map& data) { - if (!db->opened) { - throw Closed("replaceAll", db->name, name); - } - - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - rc = drop(txn); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - MDB_val lmdbKey, lmdbData; - for (const std::pair& pair : data) { - lmdbKey = keySerializer->setData(pair.first); - lmdbData = valueSerializer->setData(pair.second); - - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, MDB_NOOVERWRITE); - if (rc != 0) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - } - mdb_txn_commit(txn); -} - -template -uint32_t DataBase::Table::addRecords(const std::map& data, bool overwrite) { - if (!db->opened) { - throw Closed("addRecords", db->name, name); - } - - MDB_txn *txn; - int rc = mdb_txn_begin(db->environment, NULL, 0, &txn); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - - MDB_val lmdbKey, lmdbData; - for (const std::pair& pair : data) { - lmdbKey = keySerializer->setData(pair.first); - lmdbData = valueSerializer->setData(pair.second); - - rc = mdb_put(txn, dbi, &lmdbKey, &lmdbData, overwrite ? 0 : MDB_NOOVERWRITE); - if (rc != 0) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - } - - MDB_stat stat; - rc = mdb_stat(txn, dbi, &stat); - if (rc) { - mdb_txn_abort(txn); - throw Unknown(db->name, mdb_strerror(rc), name); - } - uint32_t amount = stat.ms_entries; - - mdb_txn_commit(txn); - return amount; -} - -template -void DataBase::Table::removeRecord(const K& key) { - if (!db->opened) { - throw Closed("removeRecord", db->name, name); - } - - MDB_val lmdbKey = keySerializer->setData(key); - - MDB_txn *txn; - int rc; - mdb_txn_begin(db->environment, NULL, 0, &txn); - rc = mdb_del(txn, dbi, &lmdbKey, NULL); - if (rc) { - mdb_txn_abort(txn); - if (rc == MDB_NOTFOUND) { - throw NotFound(toString(key), db->name, name); - } else { - throw Unknown(db->name, mdb_strerror(rc), name); - } - } else { - mdb_txn_commit(txn); - } -} - -template -int DataBase::Table::createTable(MDB_txn* transaction) { - return makeTable(transaction); -} - -template -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template<> -inline int DataBase::_Table::makeTable(MDB_txn* transaction) { - return mdb_dbi_open(transaction, name.c_str(), MDB_CREATE | MDB_INTEGERKEY, &dbi); -} - -template -inline std::string DataBase::_Table::toString(const T& value) { - return std::to_string(value); -} - -template<> -inline std::string DataBase::_Table::toString(const QString& value) { - return value.toStdString(); -} - -template<> -inline std::string DataBase::_Table::toString(const std::string& value) { - return value; -} - -#endif //CORE_TABLE_HPP diff --git a/test/basic.cpp b/test/basic.cpp index 2ee0262..cc92a5f 100644 --- a/test/basic.cpp +++ b/test/basic.cpp @@ -1,7 +1,7 @@ #include #include "database.h" -#include "table.h" +#include "storage.h" #include "cache.h" #include @@ -18,7 +18,7 @@ protected: static void SetUpTestSuite() { if (db == nullptr) { - db = new DataBase("testBase"); + db = new LMDBDataBase::DataBase("testBase"); db->addTable("table1"); db->addTable("table2"); db->addCache("cache1"); @@ -32,15 +32,15 @@ protected: db = nullptr; } - static DataBase* db; + static LMDBDataBase::DataBase* db; - DataBase::Table* t1; - DataBase::Table* t2; - DataBase::Cache* c1; + LMDBDataBase::Storage* t1; + LMDBDataBase::Storage* t2; + LMDBDataBase::Cache* c1; }; -DataBase* DataBaseTest::db = nullptr; +LMDBDataBase::DataBase* DataBaseTest::db = nullptr; TEST_F(DataBaseTest, RemovingDirectory) { EXPECT_EQ(db->removeDirectory(), true); @@ -88,7 +88,7 @@ TEST_F(DataBaseTest, AddingRepeatingIntegerKey) { bool thrown = false; try { t1->addRecord(3, 24); - } catch (const DataBase::Exist e) { + } catch (const LMDBDataBase::Exist e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -100,7 +100,7 @@ TEST_F(DataBaseTest, AddingRepeatingStringKey) { bool thrown = false; try { t2->addRecord("sdfhga", "world"); - } catch (const DataBase::Exist e) { + } catch (const LMDBDataBase::Exist e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -112,7 +112,7 @@ TEST_F(DataBaseTest, AddingRepeatingCacheKey) { bool thrown = false; try { c1->addRecord(-4, "world"); - } catch (const DataBase::Exist e) { + } catch (const LMDBDataBase::Exist e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -124,7 +124,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) { bool thrown = false; try { QString wrong = t2->getRecord("almonds"); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -132,7 +132,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) { thrown = false; try { uint32_t wrong = t1->getRecord(64); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -140,7 +140,7 @@ TEST_F(DataBaseTest, GettingNotExistingKeys) { thrown = false; try { std::string wrong = c1->getRecord(21); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -151,7 +151,7 @@ TEST_F(DataBaseTest, Persistence) { db->close(); delete db; - db = new DataBase("testBase"); + db = new LMDBDataBase::DataBase("testBase"); t1 = db->addTable("table1"); t2 = db->addTable("table2"); c1 = db->addCache("cache1"); @@ -175,7 +175,7 @@ TEST_F(DataBaseTest, Persistence) { bool thrown = false; try { QString wrong = t2->getRecord("cats"); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -183,7 +183,7 @@ TEST_F(DataBaseTest, Persistence) { thrown = false; try { uint32_t wrong = t1->getRecord(7893); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened"; @@ -191,7 +191,7 @@ TEST_F(DataBaseTest, Persistence) { thrown = false; try { std::string wrong = c1->getRecord(89); - } catch (const DataBase::NotFound e) { + } catch (const LMDBDataBase::NotFound e) { thrown = true; } ASSERT_EQ(thrown, true) << "The expected behaviour is to throw exception on duplicate, but it didn't happened";