MDBX Containers
Loading...
Searching...
No Matches
BaseTable.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _MDBX_CONTAINERS_BASE_DB_HPP_INCLUDED
3#define _MDBX_CONTAINERS_BASE_DB_HPP_INCLUDED
4
7
8namespace mdbxc {
9
16 class BaseTable {
17 public:
22 explicit BaseTable(std::shared_ptr<Connection> connection,
23 std::string name,
24 MDBX_db_flags_t flags)
25 : m_connection(std::move(connection)) {
26 auto txn = m_connection->transaction();
28 mdbx_dbi_open(txn.handle(), name.c_str(), flags, &m_dbi),
29 "Failed to open table"
30 );
31 txn.commit();
32 }
33
34 virtual ~BaseTable() = default;
35
37 bool is_connected() const {
38 return m_connection->is_connected();
39 }
40
42 void connect() {
43 m_connection->connect();
44 }
45
47 void disconnect() {
48 m_connection->disconnect();
49 }
50
55 m_connection->begin(mode);
56 }
57
60 void commit() {
61 m_connection->commit();
62 }
63
66 void rollback() {
67 m_connection->rollback();
68 }
69
74 template<typename Func>
76 auto txn = m_connection->transaction(mode);
77 try {
78 operation();
79 txn.commit();
80 } catch(...) {
81 try {
82 txn.rollback();
83 } catch(...) {}
84 throw;
85 }
86 }
87
88 protected:
89 std::shared_ptr<Connection> m_connection;
90 MDBX_dbi m_dbi{};
91
94 MDBX_txn* thread_txn() const {
95 return m_connection->thread_txn();
96 }
97
99 MDBX_dbi handle() const { return m_dbi; }
100 };
101
102}; // namespace mdbxc
103
104#endif // _MDBX_CONTAINERS_BASE_DB_HPP_INCLUDED
105
void begin(TransactionMode mode=TransactionMode::WRITABLE)
Begins a manual transaction (must be committed or rolled back later).
Definition BaseTable.hpp:54
MDBX_txn * thread_txn() const
Returns the transaction bound to the current thread, if any.
Definition BaseTable.hpp:94
MDBX_dbi m_dbi
DBI handle for the opened table.
Definition BaseTable.hpp:90
MDBX_dbi handle() const
Gets the raw DBI handle.
Definition BaseTable.hpp:99
void connect()
Connects to the MDBX environment if not already connected.
Definition BaseTable.hpp:42
void execute_in_transaction(Func operation, TransactionMode mode=TransactionMode::WRITABLE)
Executes an operation inside an automatic transaction.
Definition BaseTable.hpp:75
void commit()
Commits the current manual transaction.
Definition BaseTable.hpp:60
void rollback()
Rolls back the current manual transaction.
Definition BaseTable.hpp:66
void disconnect()
Disconnects the MDBX environment.
Definition BaseTable.hpp:47
BaseTable(std::shared_ptr< Connection > connection, std::string name, MDBX_db_flags_t flags)
Construct the database table accessor.
Definition BaseTable.hpp:22
bool is_connected() const
Checks if the connection is currently active.
Definition BaseTable.hpp:37
virtual ~BaseTable()=default
std::shared_ptr< Connection > m_connection
Shared connection to MDBX environment.
Definition BaseTable.hpp:89
void check_mdbx(int rc, const std::string &context)
Throws an MdbxException if MDBX return code indicates an error.
Definition utils.hpp:18
TransactionMode
Specifies the access mode of a transaction.
@ WRITABLE
Writable transaction (allows inserts, updates, deletes).