1
0
Fork 0
forked from blue/lmdbal
lmdbal/src/storagecommon.hpp

99 lines
3.5 KiB
C++

/*
* LMDB Abstraction Layer.
* Copyright (C) 2023 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/>.
*/
#pragma once
#include "storagecommon.h"
/**
* \brief A functiion to actually open <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gadbe68a06c448dfb62da16443d251a78b">MDB_dbi</a> storage
*
* \tparam K type of keys in opening storage
*
* \param[in] transaction - lmdb transaction to call <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gac08cad5b096925642ca359a6d6f0562a">mdb_dbi_open</a>, must be a writable transaction!
* \param[in] duplicates - true if key duplicates are allowed (false by default)
*
* \returns MDB_SUCCESS if everything went smooth or MDB_<error> -like error code
*
* This is a way to optimise database using MDB_INTEGERKEY flag,
* when the key is actually kind of an integer
* This infrastructure also allowes us to customize mdb_dbi_open call in the future
*/
template<class K, class V>
inline int LMDBAL::StorageCommon::makeStorage(MDB_txn* transaction, bool duplicates) {
unsigned int flags = MDB_CREATE;
if constexpr (std::is_integral<K>::value)
flags |= MDB_INTEGERKEY;
if (duplicates) {
flags |= MDB_DUPSORT;
if constexpr (std::is_scalar<V>::value)
flags |= MDB_DUPFIXED;
if constexpr (
std::is_same<V, uint32_t>::value ||
std::is_same<V, int32_t>::value ||
std::is_same<V, uint64_t>::value ||
std::is_same<V, int64_t>::value
) //for some reason lmdb breaks if it's not one of these types in MDB_DUPFIXED mode
flags |= MDB_INTEGERDUP;
}
return _mdbOpen(transaction, flags);
}
/**
* \brief A method to cast a value (which can be a value or a key) to string.
*
* This function is mainly used in exceptions, to report which key was duplicated or not found.
* You can define your own specializations to this function in case std::to_string doesn't cover your case
*
* \param[in] value a value that should be converted to string
* \returns a string presentation of value
*/
template<class T>
inline std::string LMDBAL::StorageCommon::toString(const T& value) {
return std::to_string(value);
}
/**
* \brief A method to cast a value (which can be a value or a key) to string.
*
* QString spectialization
*
* \param[in] value a value that should be converted to string
* \returns a string presentation of value
*/
template<>
inline std::string LMDBAL::StorageCommon::toString(const QString& value) {
return value.toStdString();
}
/**
* \brief A method to cast a value (which can be a value or a key) to string.
*
* std::string spectialization
*
* \param[in] value a value that should be converted to string
* \returns a string presentation of value
*/
template<>
inline std::string LMDBAL::StorageCommon::toString(const std::string& value) {
return value;
}