diff --git a/database.h b/database.h index 6bb25fd..685e3fe 100644 --- a/database.h +++ b/database.h @@ -30,6 +30,8 @@ namespace Core { class DataBase { class _Table; + template + class Serializer; public: template class Table; diff --git a/main.cpp b/main.cpp index 3375807..505c051 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,28 @@ #include "database.h" #include "table.h" + +template +class Serializer { +public: + Serializer(const T& value):val(value) {} + + void print() { + std::cout << val << std::endl; + } + + T val; +}; + + +template<> +class Serializer { +public: + Serializer(int v): value(v) {} + + int value; +}; + int main(int argc, char **argv) { Core::DataBase base("test1"); @@ -13,5 +35,7 @@ int main(int argc, char **argv) { table1->addRecord(1, 2); + + return 0; } diff --git a/serializer.h b/serializer.h new file mode 100644 index 0000000..db29197 --- /dev/null +++ b/serializer.h @@ -0,0 +1,52 @@ +// 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_DATABASE_SERIALIZER_H +#define CORE_DATABASE_SERIALIZER_H + +#include +#include +#include + +#include "database.h" + +namespace Core { + +template +class DataBase::Serializer +{ +public: + Serializer(); + Serializer(const T& value); + ~Serializer(); + + MDB_val setData(const T& value); + MDB_val getData(); + void clear(); + +private: + void _setData(const T& value); + + +private: + QByteArray bytes; + QBuffer buffer; + QDataStream stream; +}; + +} + +#endif // CORE_DATABASE_SERIALIZER_H diff --git a/serializer.hpp b/serializer.hpp new file mode 100644 index 0000000..4a43c66 --- /dev/null +++ b/serializer.hpp @@ -0,0 +1,70 @@ +// 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 "serializer.h" + +template +Core::DataBase::Serializer::Serializer() : + bytes(), + buffer(&bytes), + stream(&buffer) +{ + buffer.open(QIODevice::WriteOnly); +} + +template +Core::DataBase::Serializer::Serializer(const T& value) : + bytes(), + buffer(&bytes), + stream(&buffer) +{ + buffer.open(QIODevice::WriteOnly); + _setValue(value); +} + +template +Core::DataBase::Serializer::~Serializer() { + buffer.close(); +} + +template +MDB_val Core::DataBase::Serializer::setData(const T& value) { + clear(); + _setValue(value); + return getValue(); +} + +template +void Core::DataBase::Serializer::_setData(const T& value) { + stream << value; +} + +template +void Core::DataBase::Serializer::clear() { + if (buffer.pos() > 0) { + buffer.seek(0); + } +} + +template +MDB_val Core::DataBase::Serializer::getData() { + MDB_val val; + + val.mv_size = buffer.pos(); + val.mv_data = (uint8_t*)bytes.data(); + + return val; +} diff --git a/table.h b/table.h index cdde697..1fadee5 100644 --- a/table.h +++ b/table.h @@ -18,6 +18,7 @@ #define CORE_TABLE_H #include "database.h" +#include "serializer.h" namespace Core { @@ -44,16 +45,14 @@ public: void changeRecord(const K& key, const V& value); void removeRecord(const K& key); V getRecord(const K& key) const; + +private: + Serializer keySerializer; + Serializer valueSerializer; }; } -MDB_val& operator << (MDB_val& data, const QString& value); -MDB_val& operator >> (MDB_val& data, QString& value); - -MDB_val& operator << (MDB_val& data, const uint32_t& value); -MDB_val& operator >> (MDB_val& data, uint32_t& value); - namespace std { std::string to_string(const QString& str); } diff --git a/table.hpp b/table.hpp index 8ccbca0..ee6766c 100644 --- a/table.hpp +++ b/table.hpp @@ -22,7 +22,9 @@ template Core::DataBase::Table::Table(const std::string& p_name, Core::DataBase* parent): - _Table(p_name, parent) + _Table(p_name, parent), + keySerializer(), + valueSerializer() { } @@ -36,9 +38,8 @@ void Core::DataBase::Table::addRecord(const K& key, const V& value) { throw Closed("addRecord", db->name, name); } - MDB_val lmdbKey, lmdbData; - lmdbKey << key; - lmdbData << value; + MDB_val lmdbKey = keySerializer.setData(key); + MDB_val lmdbData = valueSerializer.setData(value); MDB_txn *txn; mdb_txn_begin(db->environment, NULL, 0, &txn); int rc; @@ -63,9 +64,8 @@ void Core::DataBase::Table::changeRecord(const K& key, const V& value) { throw Closed("changeRecord", db->name, name); } - MDB_val lmdbKey, lmdbData; - lmdbKey << key; - lmdbData << value; + MDB_val lmdbKey = keySerializer.setData(key); + MDB_val lmdbData = valueSerializer.setData(value); MDB_txn *txn; mdb_txn_begin(db->environment, NULL, 0, &txn); int rc; @@ -86,8 +86,8 @@ V Core::DataBase::Table::getRecord(const K& key) const { throw Closed("getRecord", db->name, name); } - MDB_val lmdbKey, lmdbData; - lmdbKey << key; + MDB_val lmdbKey = keySerializer.setData(key); + MDB_val lmdbData; MDB_txn *txn; int rc; @@ -115,8 +115,7 @@ void Core::DataBase::Table::removeRecord(const K& key) { throw Closed("removeRecord", db->name, name); } - MDB_val lmdbKey; - lmdbKey << key; + MDB_val lmdbKey = keySerializer.setData(key); MDB_txn *txn; int rc; @@ -134,27 +133,6 @@ void Core::DataBase::Table::removeRecord(const K& key) { } } -MDB_val& operator << (MDB_val& data, const QString& value) { - QByteArray ba = value.toUtf8(); - data.mv_size = ba.size(); - data.mv_data = ba.data(); - return data; -} -MDB_val& operator >> (MDB_val& data, QString& value) { - value = QString::fromUtf8((const char*)data.mv_data, data.mv_size); - return data; -} - -MDB_val& operator << (MDB_val& data, const uint32_t& value) { - data.mv_size = 4; - data.mv_data = &value; - return data; -} -MDB_val& operator >> (MDB_val& data, uint32_t& value) { - std::memcpy(&value, data.mv_data, data.mv_size); - return data; -} - std::string std::to_string(const QString& str) { return str.toStdString(); }