MDBX Containers
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _MDBX_CONTAINERS_CONNECTION_HPP_INCLUDED
3#define _MDBX_CONTAINERS_CONNECTION_HPP_INCLUDED
4
7
8namespace mdbxc {
9
14 friend class BaseTable;
15 public:
16
18 Connection() = default;
19
22 explicit Connection(const Config& config);
23
26
30 static std::shared_ptr<Connection> create(const Config& config);
31
34 void configure(const Config& config);
35
38 void connect();
39
42 void connect(const Config& config);
43
46 void disconnect();
47
50 bool is_connected() const;
51
57
63
66 void commit();
67
70 void rollback();
71
74 std::shared_ptr<Transaction> current_txn() const;
75
78 MDBX_env* env_handle() noexcept;
79
80 private:
81 friend class Transaction;
82
83 MDBX_env *m_env = nullptr;
84 mutable std::mutex m_mdbx_mutex;
85 std::unordered_map<std::thread::id, std::shared_ptr<Transaction>> m_transactions;
86# if __cplusplus >= 201703L
87 using config_t = std::optional<Config>;
88# else
89 using config_t = std::unique_ptr<Config>;
90# endif
92
94 void initialize();
95
98 void cleanup(bool use_throw = true);
99
101 void db_init();
102
103 }; // Connection
104
105} // namespace mdbxc
106
107#ifdef MDBX_CONTAINERS_HEADER_ONLY
108#include "Connection.ipp"
109#endif
110
111#endif // _MDBX_CONTAINERS_CONNECTION_HPP_INCLUDED
Parameters used by Connection to create the MDBX environment.
Definition Config.hpp:17
std::shared_ptr< Transaction > current_txn() const
Returns the transaction associated with the current thread.
bool is_connected() const
Checks whether the environment is currently open.
MDBX_env * m_env
Pointer to the MDBX environment handle.
Transaction transaction(TransactionMode mode=TransactionMode::WRITABLE)
Creates a RAII transaction object.
MDBX_env * env_handle() noexcept
Returns the environment handle.
void begin(TransactionMode mode=TransactionMode::WRITABLE)
Begins a manual transaction (must be committed or rolled back later).
friend class Transaction
void disconnect()
Disconnects from the MDBX environment and releases resources.
std::unique_ptr< Config > config_t
void db_init()
Initializes MDBX environment and opens a read-only transaction.
void rollback()
Rolls back the current manual transaction.
config_t m_config
Database configuration object.
Connection()=default
Default constructor.
void cleanup(bool use_throw=true)
Safely closes the environment and aborts transaction if needed.
void connect()
Connects to the database using the current configuration.
void configure(const Config &config)
Sets the MDBX configuration (must be called before connect()).
~Connection()
Destructor. Closes the MDBX environment and aborts any open transactions.
std::mutex m_mdbx_mutex
Mutex for thread-safe access.
void commit()
Commits the current manual transaction.
static std::shared_ptr< Connection > create(const Config &config)
Creates and connects a new shared Connection instance.
friend class BaseTable
void initialize()
Initializes the environment and sets up read-only transaction.
std::unordered_map< std::thread::id, std::shared_ptr< Transaction > > m_transactions
Associates MDBX transactions with threads.
TransactionMode
Specifies the access mode of a transaction.
@ WRITABLE
Writable transaction (allows inserts, updates, deletes).