serializer for QByteArray, method to read all records

This commit is contained in:
Blue 2022-10-15 13:54:34 +03:00
parent 24e1dca5ea
commit 928558539c
Signed by: blue
GPG Key ID: 9B203B252A63EE38
7 changed files with 111 additions and 0 deletions

View File

@ -55,6 +55,7 @@ set(HEADERS
serializer_double.hpp serializer_double.hpp
serializer_stdstring.hpp serializer_stdstring.hpp
serializer_qstring.hpp serializer_qstring.hpp
serializer_qbytearray.hpp
) )
if (BUILD_STATIC) if (BUILD_STATIC)

View File

@ -44,6 +44,7 @@ public:
virtual V getRecord(const K& key) const override; virtual V getRecord(const K& key) const override;
virtual uint32_t count() const override; virtual uint32_t count() const override;
virtual int drop(MDB_txn * transaction) override; virtual int drop(MDB_txn * transaction) override;
virtual std::map<K, V> readAll() const override;
protected: protected:
Mode* mode; Mode* mode;

View File

@ -115,6 +115,22 @@ V DataBase::Cache<K, V>::getRecord(const K& key) const {
} }
} }
template<class K, class V>
std::map<K, V> DataBase::Cache<K, V>::readAll() const {
if (!DataBase::Table<K, V>::db->opened) {
throw Closed("readAll", DataBase::Table<K, V>::db->name, DataBase::Table<K, V>::name);
}
if (*mode != Mode::full) { //there is a room for optimization
*mode = Mode::full; //I can read and deserialize only those values
*cache = DataBase::Table<K, V>::readAll(); //that are missing in the cache
abscent->clear();
*sizeDifference = 0;
}
return *cache;
}
template<class K, class V> template<class K, class V>
void DataBase::Cache<K, V>::removeRecord(const K& key) { void DataBase::Cache<K, V>::removeRecord(const K& key) {
if (!DataBase::Table<K, V>::db->opened) { if (!DataBase::Table<K, V>::db->opened) {

View File

@ -61,5 +61,6 @@ private:
#include "serializer_double.hpp" #include "serializer_double.hpp"
#include "serializer_stdstring.hpp" #include "serializer_stdstring.hpp"
#include "serializer_qstring.hpp" #include "serializer_qstring.hpp"
#include "serializer_qbytearray.hpp"
#endif // CORE_DATABASE_SERIALIZER_H #endif // CORE_DATABASE_SERIALIZER_H

58
serializer_qbytearray.hpp Normal file
View File

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

View File

@ -58,6 +58,7 @@ public:
virtual void changeRecord(const K& key, const V& value); virtual void changeRecord(const K& key, const V& value);
virtual void removeRecord(const K& key); virtual void removeRecord(const K& key);
virtual V getRecord(const K& key) const; virtual V getRecord(const K& key) const;
virtual std::map<K, V> readAll() const;
protected: protected:
Serializer<K>* keySerializer; Serializer<K>* keySerializer;

View File

@ -110,6 +110,39 @@ V DataBase::Table<K, V>::getRecord(const K& key) const {
} }
} }
template<class K, class V>
std::map<K, V> DataBase::Table<K, V>::readAll() const {
if (!db->opened) {
throw Closed("readAll", db->name, name);
}
std::map<K, V> 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) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
rc = mdb_cursor_open(txn, dbi, &cursor);
if (rc != MDB_SUCCESS) {
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);
}
if (rc != MDB_NOTFOUND) {
throw Unknown(db->name, mdb_strerror(rc), name);
}
return result;
}
template<class K, class V> template<class K, class V>
void DataBase::Table<K, V>::removeRecord(const K& key) { void DataBase::Table<K, V>::removeRecord(const K& key) {
if (!db->opened) { if (!db->opened) {