Sessions to manage db open state
All checks were successful
Main LMDBAL workflow / Test LMDBAL with qt5 (push) Successful in 1m16s
Main LMDBAL workflow / Test LMDBAL with qt6 (push) Successful in 1m39s
Main LMDBAL workflow / Builds documentation (push) Successful in 46s
Main LMDBAL workflow / Deploys documentation (push) Successful in 7s

This commit is contained in:
Blue 2025-05-06 00:22:03 +03:00
parent 3701fb92a1
commit f9902bc0b1
Signed by: blue
GPG key ID: 9B203B252A63EE38
13 changed files with 342 additions and 124 deletions

View file

@ -4,6 +4,7 @@ set(SOURCES
base.cpp
transaction.cpp
cursorcommon.cpp
session.cpp
)
set(HEADERS
@ -20,6 +21,7 @@ set(HEADERS
cache.hpp
operators.hpp
transaction.h
session.h
)
target_sources(${LMDBAL_NAME} PRIVATE ${SOURCES})

View file

@ -18,8 +18,10 @@
#include "base.h"
#include "exceptions.h"
#include "session.h"
#include "storage.h"
#include "transaction.h"
#include "session.h"
#define UNUSED(x) (void)(x)
@ -39,75 +41,34 @@
*/
LMDBAL::Base::Base(const QString& _name, uint16_t _mapSize):
name(_name.toStdString()),
opened(false),
sessions(),
size(_mapSize),
environment(),
storages(),
transactions()
transactions(),
mutex()
{}
/**
* \brief Destroys the database
*/
LMDBAL::Base::~Base() {
close();
std::lock_guard lock(mutex);
for (Session* session : sessions)
session->terminate();
if (opened())
deactivate();
for (const std::pair<const std::string, StorageCommon*>& pair : storages)
delete pair.second;
}
/**
* \brief Closes the database
*
* Closes all lmdb handles, aborts all public transactions.
* 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<TransactionID, Transaction*> pair : transactions) {
abortTransaction(pair.first, emptyName);
pair.second->reset();
}
for (const std::pair<const std::string, StorageCommon*>& pair : storages)
pair.second->close();
mdb_env_close(environment);
transactions.clear();
opened = false;
}
LMDBAL::Session LMDBAL::Base::open() {
return {this};
}
/**
* \brief Opens the database
*
* Almost every LMDBAL::Base require it to be opened, this function does it.
* 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
*/
void LMDBAL::Base::open() {
if (!opened) {
mdb_env_create(&environment);
QString path = createDirectory();
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;
if (const int rc = storage->open(txn))
throw Unknown(name, mdb_strerror(rc));
}
commitPrivateTransaction(txn, emptyName);
opened = true;
}
}
/**
* \brief Removes database directory
@ -117,7 +78,7 @@ void LMDBAL::Base::open() {
* \exception LMDBAL::Opened - thrown if this function was called on an opened database
*/
bool LMDBAL::Base::removeDirectory() {
if (opened)
if (opened())
throw Opened(name, "remove database directory");
QString path = getPath();
@ -144,7 +105,7 @@ bool LMDBAL::Base::removeDirectory() {
* \exception LMDBAL::Directory - if the database couldn't create the folder
*/
QString LMDBAL::Base::createDirectory() {
if (opened)
if (opened())
throw Opened(name, "create database directory");
QString path = getPath();
@ -183,8 +144,9 @@ QString LMDBAL::Base::getPath() const {
*
* \returns true if the database is opened and ready for work, false otherwise
*/
bool LMDBAL::Base::ready() const {
return opened;}
bool LMDBAL::Base::opened() const {
return !sessions.empty();
}
/**
* \brief Drops the database
@ -195,7 +157,7 @@ bool LMDBAL::Base::ready() const {
* \exception LMDBAL::Unknown - thrown if something unexpected happend
*/
void LMDBAL::Base::drop() {
if (!opened)
if (!opened())
throw Closed("drop", name);
TransactionID txn = beginPrivateTransaction(emptyName);
@ -301,7 +263,7 @@ void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id) {
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string& storageName) const {
if (!opened)
if (!opened())
throw Closed("beginReadOnlyTransaction", name, storageName);
TransactionID txn = beginPrivateReadOnlyTransaction(storageName);
@ -326,7 +288,7 @@ LMDBAL::TransactionID LMDBAL::Base::beginReadOnlyTransaction(const std::string&
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageName) const {
if (!opened)
if (!opened())
throw Closed("beginTransaction", name, storageName);
TransactionID txn = beginPrivateTransaction(storageName);
@ -351,7 +313,7 @@ LMDBAL::TransactionID LMDBAL::Base::beginTransaction(const std::string& storageN
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string& storageName) const {
if (!opened)
if (!opened())
throw Closed("abortTransaction", name, storageName);
abortPrivateTransaction(id, storageName);
@ -374,7 +336,7 @@ void LMDBAL::Base::abortTransaction(LMDBAL::TransactionID id, const std::string&
* \exception LMDBAL::Unknown - thrown if something unexpected happened
*/
void LMDBAL::Base::commitTransaction(LMDBAL::TransactionID id, const std::string& storageName) {
if (!opened)
if (!opened())
throw Closed("abortTransaction", name, storageName);
commitPrivateTransaction(id, storageName);
@ -407,7 +369,7 @@ LMDBAL::TransactionID LMDBAL::Base::beginPrivateReadOnlyTransaction(const std::s
* \brief Begins writable transaction
*
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
* it's not accounted in the transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] storageName - name of the storage/cache that you begin transaction from, needed just to inform if something went wrong
*
@ -428,10 +390,10 @@ LMDBAL::TransactionID LMDBAL::Base::beginPrivateTransaction(const std::string& s
/**
* \brief Aborts transaction
*
* Terminates transaction cancelling changes.
* Terminates transaction, cancelling changes.
* This is an optimal way to terminate read-only transactions.
* This function is intended to be called from subordinate storage or cache,
* it's not accounted in transaction collection, other storages and caches are not notified about this kind of transaction
* it's not accounted in the transaction collection, other storages and caches are not notified about this kind of transaction
*
* \param[in] id - transaction ID you want to abort
* \param[in] storageName - name of the storage/cache that you begin transaction from, unused here
@ -458,3 +420,104 @@ void LMDBAL::Base::commitPrivateTransaction(LMDBAL::TransactionID id, const std:
if (rc != MDB_SUCCESS)
throw Unknown(name, mdb_strerror(rc), storageName);
}
/**
* \brief Registers session
*
* Registers the session in the session collection.
* If it was the first accounted session, it activates the database
*
* \exception LMDBAL::Unknown - thrown if this session was already registered
*/
void LMDBAL::Base::registerSession(Session* session) {
std::lock_guard lock(mutex);
if (sessions.empty())
activate();
if (!sessions.insert(session).second)
throw Unknown(name, "session already registered");
}
/**
* \brief Unregisters session
*
* Unregisters the session from the session collection.
* If it was the last accounted session, it deactivates the database
*
* \exception LMDBAL::Unknown - thrown if this session was not registered
*/
void LMDBAL::Base::unregisterSession(Session* session) {
std::lock_guard lock(mutex);
if (sessions.size() == 1)
deactivate();
if (sessions.erase(session) != 1)
throw Unknown(name, "session was not registered");
}
/**
* \brief Swaps sessions
*
* Replaces one session by another in the session collection.
*
* \exception LMDBAL::Unknown - thrown if there is some unexpected state with sessions, it means the database is in the wrong state
*/
void LMDBAL::Base::replaceSession(Session* closing, Session* opening) {
std::lock_guard lock(mutex);
if (sessions.erase(closing) != 1)
throw Unknown(name, "session was not registered");
if (!sessions.insert(opening).second)
throw Unknown(name, "session already registered");
}
/**
* \brief Deactivates the database,
*
* Closes all lmdb handles, aborts all public transactions.
* This function will emit SIGSEGV on a deactivated database
*
* \exception LMDBAL::Unknown - thrown if something went wrong aborting transactions
*/
void LMDBAL::Base::deactivate() {
for (const std::pair<TransactionID, Transaction*> pair : transactions) {
abortTransaction(pair.first, emptyName);
pair.second->reset();
}
for (const std::pair<const std::string, StorageCommon*>& pair : storages)
pair.second->close();
mdb_env_close(environment);
transactions.clear();
}
/**
* \brief Activates the database
*
* Almost every LMDBAL::Base require it to be opened, this function does it.
* It also creates the directory for the database if it was an initial launch.
* This function will behave unpredictably on an activated database, possible data corruption.
*
* \exception LMDBAL::Unknown - thrown if something went wrong opening storages and caches
*/
void LMDBAL::Base::activate() {
mdb_env_create(&environment);
QString path = createDirectory();
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;
if (const int rc = storage->open(txn))
throw Unknown(name, mdb_strerror(rc));
}
commitPrivateTransaction(txn, emptyName);
}

View file

@ -23,6 +23,8 @@
#include <string>
#include <optional>
#include <limits>
#include <cstdint>
#include <mutex>
#include <QString>
#include <QStandardPaths>
@ -37,6 +39,7 @@ namespace LMDBAL {
class StorageCommon;
class Transaction;
class WriteTransaction;
class Session;
template<class T>
class Serializer;
@ -54,14 +57,14 @@ class Base {
friend class StorageCommon;
friend class Transaction;
friend class WriteTransaction;
public:
friend class Session;
public:
Base(const QString& name, uint16_t mapSize = 10);
~Base();
void open();
void close();
bool ready() const;
Session open();
bool opened() const;
bool removeDirectory();
QString createDirectory();
QString getName() const;
@ -84,8 +87,9 @@ public:
LMDBAL::Cache<K, V>* getCache(const std::string& storageName);
private:
typedef std::map<std::string, LMDBAL::StorageCommon *> Storages; /**<\brief Storage and Cache pointers are saved in the std::map*/
typedef std::map<TransactionID, Transaction*> Transactions; /**<\brief Piblic transaction IDs are saved in the std::set*/
typedef std::map<std::string, LMDBAL::StorageCommon *> Storages; /**<\brief Storage and Cache pointers are saved in the std::map*/
typedef std::map<TransactionID, Transaction*> Transactions; /**<\brief Public transaction IDs are saved in the std::map*/
typedef std::set<Session*> Sessions; /**<\brief Sessions are saved in the std::set*/
void commitTransaction(TransactionID id);
void abortTransaction(TransactionID id) const;
@ -99,13 +103,21 @@ private:
void commitPrivateTransaction(TransactionID id, const std::string& storageName);
void abortPrivateTransaction(TransactionID id, const std::string& storageName) const;
void registerSession(Session* session);
void unregisterSession(Session* session);
void replaceSession(Session* closing, Session* opening);
void activate();
void deactivate();
private:
std::string name; /**<\brief Name of this database*/
bool opened; /**<\brief State of this database*/
Sessions sessions; /**<\brief Opened session pointers*/
uint16_t size; /**<\brief lmdb map size in MiB*/
MDB_env* environment; /**<\brief lmdb environment handle*/
Storages storages; /**<\brief Registered storages and caches*/
mutable Transactions transactions; /**<\brief Active public transactions*/
std::mutex mutex; /**<\brief Mutex for thread safety*/
inline static const std::string emptyName = ""; /**<\brief Empty string for general fallback purposes*/
};
@ -132,7 +144,7 @@ private:
*/
template <class K, class V>
LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName, bool duplicates) {
if (opened)
if (opened())
throw Opened(name, "add storage " + storageName);
auto storage = new Storage<K, V>(this, storageName, duplicates);
@ -160,7 +172,7 @@ LMDBAL::Storage<K, V>* LMDBAL::Base::addStorage(const std::string& storageName,
*/
template<class K, class V>
LMDBAL::Cache<K, V> * LMDBAL::Base::addCache(const std::string& storageName) {
if (opened)
if (opened())
throw Opened(name, "add cache " + storageName);
auto cache = new Cache<K, V>(this, storageName, false);

73
src/session.cpp Normal file
View file

@ -0,0 +1,73 @@
/*
* 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/>.
*/
#include "session.h"
LMDBAL::Session::Session():
parent(nullptr) {}
LMDBAL::Session::Session(Base* parent):
parent(parent)
{
parent->registerSession(this);
}
LMDBAL::Session::Session(Session&& other):
parent(other.parent)
{
if (parent)
parent->replaceSession(&other, this);
}
LMDBAL::Session::~Session() {
if (parent)
parent->unregisterSession(this);
}
LMDBAL::Session& LMDBAL::Session::operator = (Session&& other) {
if (parent)
if (other.parent)
parent->unregisterSession(&other);
else
parent->unregisterSession(this);
else
if (other.parent)
other.parent->replaceSession(&other, this);
parent = other.parent;
other.terminate();
return *this;
}
void LMDBAL::Session::close() {
if (!parent)
return;
parent->unregisterSession(this);
terminate();
}
bool LMDBAL::Session::opened() const {
return parent != nullptr;
}
void LMDBAL::Session::terminate() {
parent = nullptr;
}

47
src/session.h Normal file
View file

@ -0,0 +1,47 @@
/*
* 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 "base.h"
namespace LMDBAL {
class Session {
friend class Base;
public:
explicit Session();
~Session();
Session(const Session&) = delete;
Session(Session&&);
Session& operator = (const Session&) = delete;
Session& operator = (Session&&);
void close();
bool opened() const;
private:
Base* parent;
private:
Session(Base* parent);
void terminate();
};
}

View file

@ -356,7 +356,7 @@ const std::string & LMDBAL::StorageCommon::dbName() const {
* \returns true if database is ipened, false otherwise
*/
bool LMDBAL::StorageCommon::isDBOpened() const {
return db->opened;}
return db->opened();}
/**
* \brief Throws LMDBAL::Unknown