just some thoughts about serializer
This commit is contained in:
parent
c491793387
commit
d67a3c32eb
@ -30,6 +30,8 @@ namespace Core {
|
||||
class DataBase
|
||||
{
|
||||
class _Table;
|
||||
template<class T>
|
||||
class Serializer;
|
||||
public:
|
||||
template <class K, class V>
|
||||
class Table;
|
||||
|
24
main.cpp
24
main.cpp
@ -3,6 +3,28 @@
|
||||
#include "database.h"
|
||||
#include "table.h"
|
||||
|
||||
|
||||
template<class T>
|
||||
class Serializer {
|
||||
public:
|
||||
Serializer(const T& value):val(value) {}
|
||||
|
||||
void print() {
|
||||
std::cout << val << std::endl;
|
||||
}
|
||||
|
||||
T val;
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
class Serializer<int> {
|
||||
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;
|
||||
}
|
||||
|
52
serializer.h
Normal file
52
serializer.h
Normal file
@ -0,0 +1,52 @@
|
||||
// Squawk messenger.
|
||||
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef CORE_DATABASE_SERIALIZER_H
|
||||
#define CORE_DATABASE_SERIALIZER_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QBuffer>
|
||||
#include <QDataStream>
|
||||
|
||||
#include "database.h"
|
||||
|
||||
namespace Core {
|
||||
|
||||
template<class T>
|
||||
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
|
70
serializer.hpp
Normal file
70
serializer.hpp
Normal file
@ -0,0 +1,70 @@
|
||||
// Squawk messenger.
|
||||
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "serializer.h"
|
||||
|
||||
template<class T>
|
||||
Core::DataBase::Serializer<T>::Serializer() :
|
||||
bytes(),
|
||||
buffer(&bytes),
|
||||
stream(&buffer)
|
||||
{
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Core::DataBase::Serializer<T>::Serializer(const T& value) :
|
||||
bytes(),
|
||||
buffer(&bytes),
|
||||
stream(&buffer)
|
||||
{
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
_setValue(value);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Core::DataBase::Serializer<T>::~Serializer() {
|
||||
buffer.close();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
MDB_val Core::DataBase::Serializer<T>::setData(const T& value) {
|
||||
clear();
|
||||
_setValue(value);
|
||||
return getValue();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Core::DataBase::Serializer<T>::_setData(const T& value) {
|
||||
stream << value;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Core::DataBase::Serializer<T>::clear() {
|
||||
if (buffer.pos() > 0) {
|
||||
buffer.seek(0);
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
MDB_val Core::DataBase::Serializer<T>::getData() {
|
||||
MDB_val val;
|
||||
|
||||
val.mv_size = buffer.pos();
|
||||
val.mv_data = (uint8_t*)bytes.data();
|
||||
|
||||
return val;
|
||||
}
|
11
table.h
11
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<K> keySerializer;
|
||||
Serializer<V> 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);
|
||||
}
|
||||
|
42
table.hpp
42
table.hpp
@ -22,7 +22,9 @@
|
||||
|
||||
template<typename K, typename V>
|
||||
Core::DataBase::Table<K, V>::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<K, V>::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<K, V>::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<K, V>::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<K, V>::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<K, V>::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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user