MDBX Containers
Loading...
Searching...
No Matches
manual_transaction_example.cpp
Go to the documentation of this file.
1
5
7#include <iostream>
8
9int main() {
10 mdbxc::Config config;
11 config.pathname = "manual_txn_db";
12
13 auto conn = mdbxc::Connection::create(config);
15 table.clear();
16
17 // Start writable transaction manually
19
20 // Insert key-value pairs
21 table.insert_or_assign(10, "ten");
22 table.insert_or_assign(20, "twenty");
23
24 // Use operator[] to assign a value (modifies in-place)
25 table[30] = "thirty";
26
27 // Use operator[] to read the value
28 std::cout << "Key 20 (operator[]): " << static_cast<std::string>(table[20]) << std::endl;
29
30 // Use at() for bounds-checked access
31 try {
32 std::cout << "Key 30 (at): " << table.at(30) << std::endl;
33 } catch (const std::out_of_range&) {
34 std::cout << "Key 30 not found" << std::endl;
35 }
36
37# if __cplusplus >= 201703L
38 auto result = table.find(10);
39 std::cout << "Key 10 (find): " << result.value_or("not found") << std::endl;
40# else
41 auto result = table.find_compat(10);
42 if (result.first)
43 std::cout << "Key 10 (find): " << result.second << std::endl;
44 else
45 std::cout << "Key 10 (find): not found" << std::endl;
46# endif
47
48 // Commit transaction
49 conn->commit();
50 return 0;
51}
Declaration of the KeyValueTable class for managing key-value pairs in an MDBX database.
Parameters used by Connection to create the MDBX environment.
Definition Config.hpp:17
std::string pathname
Path to the database file or directory containing the database.
Definition Config.hpp:19
static std::shared_ptr< Connection > create(const Config &config)
Creates and connects a new shared Connection instance.
Template class for managing key-value pairs in an MDBX database.
int main()
Basic example using Config to initialize a table.
@ WRITABLE
Writable transaction (allows inserts, updates, deletes).