forked from blue/lmdbal
Fix typos, fix some warnings, added more compile options, moved to forgejo CI
This commit is contained in:
parent
3ae1fd15c0
commit
1585b8e4f5
15 changed files with 204 additions and 120 deletions
23
src/base.cpp
23
src/base.cpp
|
@ -28,13 +28,13 @@
|
|||
* \brief Database abstraction
|
||||
*
|
||||
* This is a basic class that represents the database as a collection of storages.
|
||||
* Storages is something key-value database has instead of tables in classic SQL databases.
|
||||
* Storages are something that key-value databases have instead of tables in classic SQL databases.
|
||||
*/
|
||||
|
||||
/**
|
||||
* \brief Creates the database
|
||||
*
|
||||
* \param[in] _name - name of the database, it is going to affect folder name that is created to store data
|
||||
* \param[in] _name - name of the database, it is going to affect the folder name that is created to store data
|
||||
* \param[in] _mapSize - LMDB map size (MiB), multiplied by 1024^2 and passed to <a class="el" href="http://www.lmdb.tech/doc/group__mdb.html#gaa2506ec8dab3d969b0e609cd82e619e5">mdb_env_set_mapsize</a> during the call of LMDBAL::Base::open()
|
||||
*/
|
||||
LMDBAL::Base::Base(const QString& _name, uint16_t _mapSize):
|
||||
|
@ -60,13 +60,13 @@ LMDBAL::Base::~Base() {
|
|||
* \brief Closes the database
|
||||
*
|
||||
* Closes all lmdb handles, aborts all public transactions.
|
||||
* This function will do nothing on closed database
|
||||
* This function will do nothing on a closed database
|
||||
*
|
||||
* \exception LMDBAL::Unknown - thrown if something went wrong aborting transactions
|
||||
*/
|
||||
void LMDBAL::Base::close() {
|
||||
if (opened) {
|
||||
for (const std::pair<LMDBAL::TransactionID, LMDBAL::Transaction*> pair : transactions) {
|
||||
for (const std::pair<TransactionID, Transaction*> pair : transactions) {
|
||||
abortTransaction(pair.first, emptyName);
|
||||
pair.second->reset();
|
||||
}
|
||||
|
@ -84,8 +84,8 @@ void LMDBAL::Base::close() {
|
|||
* \brief Opens the database
|
||||
*
|
||||
* Almost every LMDBAL::Base require it to be opened, this function does it.
|
||||
* It laso creates the directory for the database if it was an initial launch.
|
||||
* This function will do nothing on opened database
|
||||
* It also creates the directory for the database if it was an initial launch.
|
||||
* This function will do nothing on an opened database
|
||||
*
|
||||
* \exception LMDBAL::Unknown - thrown if something went wrong opening storages and caches
|
||||
*/
|
||||
|
@ -94,15 +94,14 @@ void LMDBAL::Base::open() {
|
|||
mdb_env_create(&environment);
|
||||
QString path = createDirectory();
|
||||
|
||||
mdb_env_set_maxdbs(environment, storages.size());
|
||||
mdb_env_set_maxdbs(environment, static_cast<MDB_dbi>(storages.size()));
|
||||
mdb_env_set_mapsize(environment, size * 1024UL * 1024UL);
|
||||
mdb_env_open(environment, path.toStdString().c_str(), 0, 0664);
|
||||
|
||||
TransactionID txn = beginPrivateTransaction(emptyName);
|
||||
for (const std::pair<const std::string, StorageCommon*>& pair : storages) {
|
||||
StorageCommon* storage = pair.second;
|
||||
int rc = storage->open(txn);
|
||||
if (rc)
|
||||
if (const int rc = storage->open(txn))
|
||||
throw Unknown(name, mdb_strerror(rc));
|
||||
}
|
||||
commitPrivateTransaction(txn, emptyName);
|
||||
|
@ -113,9 +112,9 @@ void LMDBAL::Base::open() {
|
|||
/**
|
||||
* \brief Removes database directory
|
||||
*
|
||||
* \returns true if removal was successfull of if no directory was created where it's expected to be, false otherwise
|
||||
* \returns true if removal was successful of if no directory was created where it's expected to be, false otherwise
|
||||
*
|
||||
* \exception LMDBAL::Opened - thrown if this function was called on opened database
|
||||
* \exception LMDBAL::Opened - thrown if this function was called on an opened database
|
||||
*/
|
||||
bool LMDBAL::Base::removeDirectory() {
|
||||
if (opened)
|
||||
|
@ -141,7 +140,7 @@ bool LMDBAL::Base::removeDirectory() {
|
|||
*
|
||||
* \returns the path of the created directory
|
||||
*
|
||||
* \exception LMDBAL::Opened - thrown if called on opened database
|
||||
* \exception LMDBAL::Opened - thrown if called on an opened database
|
||||
* \exception LMDBAL::Directory - if the database couldn't create the folder
|
||||
*/
|
||||
QString LMDBAL::Base::createDirectory() {
|
||||
|
|
10
src/base.h
10
src/base.h
|
@ -48,7 +48,7 @@ template <class K, class V>
|
|||
class Cache;
|
||||
|
||||
typedef MDB_txn* TransactionID; /**<\brief I'm going to use transaction pointers as transaction IDs*/
|
||||
typedef uint32_t SizeType; /**<\brief All LMDBAL sizes are uint32_t*/
|
||||
typedef uint32_t SizeType; /**<\brief All LMDBAL sizes are uint32*/
|
||||
|
||||
class Base {
|
||||
friend class StorageCommon;
|
||||
|
@ -135,8 +135,8 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName,
|
|||
if (opened)
|
||||
throw Opened(name, "add storage " + storageName);
|
||||
|
||||
Storage<K, V>* storage = new Storage<K, V>(this, storageName, duplicates);
|
||||
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (StorageCommon *)storage));
|
||||
auto storage = new Storage<K, V>(this, storageName, duplicates);
|
||||
std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(storage));
|
||||
if (!pair.second)
|
||||
throw StorageDuplicate(name, storageName);
|
||||
|
||||
|
@ -163,8 +163,8 @@ LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
|
|||
if (opened)
|
||||
throw Opened(name, "add cache " + storageName);
|
||||
|
||||
Cache<K, V>* cache = new Cache<K, V>(this, storageName, false);
|
||||
std::pair<Storages::const_iterator, bool> pair = storages.insert(std::make_pair(storageName, (StorageCommon *)cache));
|
||||
auto cache = new Cache<K, V>(this, storageName, false);
|
||||
std::pair<Storages::const_iterator, bool> pair = storages.emplace(storageName, static_cast<StorageCommon *>(cache));
|
||||
if (!pair.second)
|
||||
throw StorageDuplicate(name, storageName);
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ T LMDBAL::Serializer<T>::deserialize(const MDB_val& value) {
|
|||
template<class T>
|
||||
void LMDBAL::Serializer<T>::deserialize(const MDB_val& value, T& result) {
|
||||
clear();
|
||||
bytes.setRawData((char*)value.mv_data, value.mv_size);
|
||||
bytes.setRawData(static_cast<char *>(value.mv_data), value.mv_size);
|
||||
stream >> result;
|
||||
}
|
||||
|
||||
|
@ -154,7 +154,7 @@ MDB_val LMDBAL::Serializer<T>::getData() {
|
|||
MDB_val val;
|
||||
|
||||
val.mv_size = buffer.pos();
|
||||
val.mv_data = (char*)bytes.data();
|
||||
val.mv_data = bytes.data();
|
||||
|
||||
return val;
|
||||
}
|
||||
|
|
|
@ -34,7 +34,17 @@ public:
|
|||
return value;
|
||||
};
|
||||
void deserialize(const MDB_val& data, QByteArray& result) {
|
||||
result.setRawData((char*)data.mv_data, data.mv_size);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
result.setRawData(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
|
||||
#else
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<uint>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
result.setRawData(static_cast<char *>(data.mv_data), static_cast<uint>(data.mv_size));
|
||||
#endif
|
||||
}
|
||||
MDB_val setData(const QByteArray& data) {
|
||||
value = data;
|
||||
|
@ -43,7 +53,7 @@ public:
|
|||
MDB_val getData() {
|
||||
MDB_val result;
|
||||
result.mv_data = value.data();
|
||||
result.mv_size = value.size();
|
||||
result.mv_size = static_cast<size_t>(value.size());
|
||||
return result;
|
||||
};
|
||||
void clear() {
|
||||
|
|
|
@ -31,11 +31,33 @@ public:
|
|||
~Serializer() {};
|
||||
|
||||
QString deserialize(const MDB_val& data) {
|
||||
value = QByteArray((char*)data.mv_data, data.mv_size);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
value = QByteArray(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
|
||||
#else
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
value = QByteArray(static_cast<char *>(data.mv_data), static_cast<int>(data.mv_size));
|
||||
#endif
|
||||
|
||||
return QString::fromUtf8(value);
|
||||
};
|
||||
void deserialize(const MDB_val& data, QString& result) {
|
||||
value = QByteArray((char*)data.mv_data, data.mv_size);
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<qsizetype>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
value = QByteArray(static_cast<char *>(data.mv_data), static_cast<qsizetype>(data.mv_size));
|
||||
#else
|
||||
if (data.mv_size > static_cast<size_t>(std::numeric_limits<int>::max()))
|
||||
throw std::runtime_error("Data size exceeds QByteArray capacity");
|
||||
|
||||
value = QByteArray(static_cast<char *>(data.mv_data), static_cast<int>(data.mv_size));
|
||||
#endif
|
||||
|
||||
result = QString::fromUtf8(value);
|
||||
}
|
||||
MDB_val setData(const QString& data) {
|
||||
|
@ -45,7 +67,7 @@ public:
|
|||
MDB_val getData() {
|
||||
MDB_val result;
|
||||
result.mv_data = value.data();
|
||||
result.mv_size = value.size();
|
||||
result.mv_size = static_cast<size_t>(value.size());
|
||||
return result;
|
||||
};
|
||||
void clear() {}; //not possible;
|
||||
|
@ -54,4 +76,4 @@ private:
|
|||
QByteArray value;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
|
@ -34,7 +34,7 @@ public:
|
|||
return value;
|
||||
};
|
||||
void deserialize(const MDB_val& data, std::string& result) {
|
||||
result.assign((char*)data.mv_data, data.mv_size);
|
||||
result.assign(static_cast<char *>(data.mv_data), data.mv_size);
|
||||
}
|
||||
MDB_val setData(const std::string& data) {
|
||||
value = data;
|
||||
|
@ -42,7 +42,7 @@ public:
|
|||
};
|
||||
MDB_val getData() {
|
||||
MDB_val result;
|
||||
result.mv_data = (char*)value.c_str();
|
||||
result.mv_data = const_cast<char *>(value.c_str());
|
||||
result.mv_size = value.size();
|
||||
return result;
|
||||
};
|
||||
|
|
|
@ -23,13 +23,13 @@
|
|||
#define UNUSED(x) (void)(x)
|
||||
|
||||
/**
|
||||
* \class LMDBAL::iStorage
|
||||
* \class LMDBAL::StorageCommon
|
||||
*
|
||||
* \brief Storage interface
|
||||
*
|
||||
* This is a interface-like class, it's designed to be an inner database interface to
|
||||
* be used as a polymorphic entity, and provide protected interaction with the database
|
||||
* from the heirs code
|
||||
* This is an interface-like class, it's designed to be an inner database interface to
|
||||
* be used as a polymorphic entity and provide protected interaction with the database
|
||||
* from the heir code
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -49,7 +49,7 @@ LMDBAL::StorageCommon::StorageCommon(Base* parent, const std::string& name, bool
|
|||
/**
|
||||
* \brief Destroys a storage interface
|
||||
*/
|
||||
LMDBAL::StorageCommon::~StorageCommon () {}
|
||||
LMDBAL::StorageCommon::~StorageCommon () = default;
|
||||
|
||||
/**
|
||||
* \brief A private virtual function to close each storage in the database
|
||||
|
@ -103,7 +103,7 @@ void LMDBAL::StorageCommon::drop() {
|
|||
*
|
||||
* Just performs content drop
|
||||
*
|
||||
* \param[in] transaction - transaction ID, must be writable transaction!
|
||||
* \param[in] transaction - transaction ID; must be writable transaction!
|
||||
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
|
||||
*/
|
||||
int LMDBAL::StorageCommon::drop(TransactionID transaction) {
|
||||
|
@ -115,7 +115,7 @@ int LMDBAL::StorageCommon::drop(TransactionID transaction) {
|
|||
*
|
||||
* Just performs content drop
|
||||
*
|
||||
* \param[in] txn - transaction ID, must be writable transaction!
|
||||
* \param[in] txn - transaction ID; must be writable transaction!
|
||||
* \returns MDB_SUCCESS if everything went fine, MDB_<error> code otherwise
|
||||
*
|
||||
* \exception LMDBAL::TransactionTerminated thrown if the transaction was not active
|
||||
|
@ -126,7 +126,7 @@ int LMDBAL::StorageCommon::drop(const WriteTransaction& txn) {
|
|||
}
|
||||
|
||||
/**
|
||||
* \brief Helper function, thows exception if the database is not opened
|
||||
* \brief Helper function; throws an exception if the database is not opened
|
||||
*
|
||||
* \param[in] methodName - name of the method this function is called from, just for display in std::exception::what() message
|
||||
*
|
||||
|
@ -140,7 +140,7 @@ void LMDBAL::StorageCommon::ensureOpened(const std::string& methodName) const {
|
|||
/**
|
||||
* \brief Storage size
|
||||
*
|
||||
* \returns amount of records in the storage
|
||||
* \returns number of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was closed
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happened
|
||||
|
@ -162,7 +162,7 @@ LMDBAL::SizeType LMDBAL::StorageCommon::count() const {
|
|||
}
|
||||
|
||||
/**
|
||||
* \brief Retrieves public transaction object for a cursor handle
|
||||
* \brief Retrieves a public transaction object for a cursor handle
|
||||
*
|
||||
* Cursor must be still opened by a public transaction
|
||||
*
|
||||
|
@ -222,25 +222,27 @@ void LMDBAL::StorageCommon::closeCursorTransaction (MDB_cursor* cursor, bool clo
|
|||
/**
|
||||
* \brief Storage size (private transaction variant)
|
||||
*
|
||||
* \param[in] txn - transaction ID, can be read-only transaction
|
||||
* \returns amount of records in the storage
|
||||
* \param[in] txn - transaction ID; can be read-only transaction
|
||||
* \returns number of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happened
|
||||
*/
|
||||
LMDBAL::SizeType LMDBAL::StorageCommon::count(TransactionID txn) const {
|
||||
MDB_stat stat;
|
||||
int rc = mdb_stat(txn, dbi, &stat);
|
||||
if (rc != MDB_SUCCESS)
|
||||
if (const int rc = mdb_stat(txn, dbi, &stat); rc != MDB_SUCCESS)
|
||||
throw Unknown(db->name, mdb_strerror(rc), name);
|
||||
|
||||
return stat.ms_entries;
|
||||
if (stat.ms_entries > std::numeric_limits<SizeType>::max())
|
||||
throw Unknown(db->name, "Storage size exceeds it's limits", name);
|
||||
|
||||
return static_cast<SizeType>(stat.ms_entries);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Storage size (public transaction variant)
|
||||
*
|
||||
* \param[in] txn - transaction, can be read-only transaction
|
||||
* \returns amount of records in the storage
|
||||
* \param[in] txn - transaction; can be read-only transaction
|
||||
* \returns number of records in the storage
|
||||
*
|
||||
* \exception LMDBAL::Closed thrown if the database was closed
|
||||
* \exception LMDBAL::Unknown thrown if something unexpected happened
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <stdint.h>
|
||||
#include <cstdint>
|
||||
|
||||
#include <lmdb.h>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue