LMDBAL 0.6.2
LMDB (Lightning Memory-Mapped Database Manager) Abstraction Layer
Loading...
Searching...
No Matches
serializer.hpp
1/*
2 * LMDB Abstraction Layer.
3 * Copyright (C) 2023 Yury Gubich <blue@macaw.me>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include "serializer.h"
22
23#include <stdexcept>
24#include <limits>
25
26#include "base.h"
27
37 * \tparam K type of the keys of the storage
38 * \tparam V type of the values of the storage
39 */
40
41/**
42 * \brief Creates an empty Serializer
43 */
44template<class T>
46 bytes(),
47 buffer(&bytes),
48 stream(&buffer)
49{
50 LMDBAL::detail::applyQDataStreamVersion(stream);
51 buffer.open(QIODevice::ReadWrite);
52}
53
61template<class T>
63 bytes(),
64 buffer(&bytes),
65 stream(&buffer)
66{
67 LMDBAL::detail::applyQDataStreamVersion(stream);
68 buffer.open(QIODevice::ReadWrite);
69 _setData(value);
70}
71
75namespace LMDBAL {
76 template<class T>
77 Serializer<T>::~Serializer() {
78 buffer.close();
79 }
80}
81
91template<class T>
92MDB_val LMDBAL::Serializer<T>::setData(const T& value) {
93 clear();
94 _setData(value);
95 return getData();
96}
97
107template<class T>
108T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
109 T result;
110 deserialize(value, result);
111
112 return result;
113}
114
123template<class T>
124void LMDBAL::Serializer<T>::deserialize(const MDB_val& value, T& result) {
125 clear();
126
127
128#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
129 if (value.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
130 throw std::runtime_error("Data size exceeds QByteArray capacity");
131
132 QByteArray array(static_cast<const char *>(value.mv_data), static_cast<qsizetype>(value.mv_size));
133#else
134 if (value.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
135 throw std::runtime_error("Data size exceeds QByteArray capacity");
136
137 QByteArray array(static_cast<const char *>(value.mv_data), static_cast<int>(value.mv_size));
138#endif
139 QDataStream reader(array);
140
141 reader >> result;
142}
143
149template<class T>
150void LMDBAL::Serializer<T>::_setData(const T& value) {
151 stream << value;
152}
153
159template<class T>
160void LMDBAL::Serializer<T>::setStreamVersion(QDataStream::Version version) {
161 stream.setVersion(version);
162}
163
169template<class T>
171 if (buffer.pos() > 0)
172 buffer.seek(0);
173}
174
184template<class T>
186 MDB_val val;
187
188 val.mv_size = static_cast<size_t>(bytes.size());
189 val.mv_data = bytes.data();
190
191 return val;
192}
Serializer()
Creates an empty Serializer.
Definition serializer.hpp:45
MDB_val getData()
Returns the data if it already was serialized.
Definition serializer.hpp:185
T deserialize(const MDB_val &value)
Deserializes value.
Definition serializer.hpp:108
void setStreamVersion(QDataStream::Version version)
Sets QDataStream version for serialization.
Definition serializer.hpp:160
void clear()
Clears the state of serializer.
Definition serializer.hpp:170
MDB_val setData(const T &value)
Sets the data to the serializer.
Definition serializer.hpp:92
Destroys a cache.
Definition base.h:36