/* * LMDB Abstraction Layer. * Copyright (C) 2023 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 . */ #pragma once #include "storagecommon.h" /** * \brief A functiion to actually open MDB_dbi storage * * \tparam K type of keys in opening storage * * \param[in] transaction - lmdb transaction to call mdb_dbi_open, 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_ -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 inline int LMDBAL::StorageCommon::makeStorage(MDB_txn* transaction, bool duplicates) { unsigned int flags = MDB_CREATE; if constexpr (std::is_integral::value) flags |= MDB_INTEGERKEY; if (duplicates) { flags |= MDB_DUPSORT; if constexpr (std::is_scalar::value) flags |= MDB_DUPFIXED; if constexpr ( std::is_same::value || std::is_same::value || std::is_same::value || std::is_same::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 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; }