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

@ -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);