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
38 bool is_connected() const {
39 return m_connection->is_connected();
40 }
41
44 void connect() {
45 m_connection->connect();
46 }
47
50 void disconnect() {
51 m_connection->disconnect();
52 }
53
58 m_connection->begin(mode);
59 }
60
63 void commit() {
64 m_connection->commit();
65 }
66
69 void rollback() {
70 m_connection->rollback();
71 }
72
77 template<typename Func>
79 auto txn = m_connection->transaction(mode);
80 try {
81 operation();
82 txn.commit();
83 } catch(...) {
84 try {
85 txn.rollback();
86 } catch(...) {}
87 throw;
88 }
89 }
90
91 protected:
92 std::shared_ptr<Connection> m_connection;
93 MDBX_dbi m_dbi{};
94
97 MDBX_txn* thread_txn() const {
98 return m_connection->thread_txn();
99 }
100
103 MDBX_dbi handle() const { return m_dbi; }
104 };
105
106}; // namespace mdbxc
107
108#endif // _MDBX_CONTAINERS_BASE_DB_HPP_INCLUDED
109
void begin(TransactionMode mode=TransactionMode::WRITABLE)
Begins a manual transaction (must be committed or rolled back later).
Definition BaseTable.hpp:57
MDBX_txn * thread_txn() const
Returns the transaction bound to the current thread, if any.
Definition BaseTable.hpp:97
MDBX_dbi m_dbi
DBI handle for the opened table.
Definition BaseTable.hpp:93
MDBX_dbi handle() const
Gets the raw DBI handle.
void connect()
Connects to the MDBX environment if not already connected.
Definition BaseTable.hpp:44
void execute_in_transaction(Func operation, TransactionMode mode=TransactionMode::WRITABLE)
Executes an operation inside an automatic transaction.
Definition BaseTable.hpp:78
void commit()
Commits the current manual transaction.
Definition BaseTable.hpp:63
void rollback()
Rolls back the current manual transaction.
Definition BaseTable.hpp:69
void disconnect()
Disconnects the MDBX environment.
Definition BaseTable.hpp:50
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:38
virtual ~BaseTable()=default
std::shared_ptr< Connection > m_connection
Shared connection to MDBX environment.
Definition BaseTable.hpp:92
void check_mdbx(int rc, const std::string &context)
Throws an MdbxException if MDBX return code indicates an error.
Definition utils.hpp:23
TransactionMode
Specifies the access mode of a transaction.
@ WRITABLE
Writable transaction (allows inserts, updates, deletes).