MDBX Containers
Loading...
Searching...
No Matches
table_methods_showcase.cpp
Go to the documentation of this file.
1
5
7#include <iostream>
8#include <string>
9#include <limits>
10
11int main() {
12 mdbxc::Config config;
13 config.pathname = "full_methods_db";
14 config.max_dbs = 1;
15
16 auto conn = mdbxc::Connection::create(config);
17 mdbxc::KeyValueTable<int, std::string> table(conn, "full_demo");
18
19 // insert_or_assign
20 table.insert_or_assign(1, "one");
21 table.insert_or_assign(2, "two");
22
23 // insert (no overwrite)
24 bool inserted = table.insert(3, "three");
25 std::cout << "Inserted key 3: " << inserted << std::endl;
26
27 inserted = table.insert(2, "TWO");
28 std::cout << "Inserted key 2 again: " << inserted << " (should be false)" << std::endl;
29
30 // contains
31 std::cout << "Contains key 1: " << table.contains(1) << std::endl;
32 std::cout << "Contains key 4: " << table.contains(4) << std::endl;
33
34 // operator[]
35 table[4] = "four";
36 std::cout << "table[4]: " << static_cast<std::string>(table[4]) << std::endl;
37
38 // operator()
39 std::map<int, std::string> snapshot = table();
40 std::cout << "Snapshot:\n";
41 for (std::map<int, std::string>::const_iterator it = snapshot.begin(); it != snapshot.end(); ++it)
42 std::cout << it->first << ": " << it->second << std::endl;
43
44 // operator=
45 std::unordered_map<int, std::string> temp_data = {
46 {100, "hundred"},
47 {200, "two hundred"}
48 };
49 table = temp_data;
50
51 // retrieve_all()
52 std::map<int, std::string> snapshot_2 = table.retrieve_all();
53
54# if __cplusplus >= 201703L
55 // find
56 auto result = table.find(100);
57 if (result)
58 std::cout << "Found key 100: " << *result << std::endl;
59 else
60 std::cout << "Key 100 not found\n";
61# else
62 auto result = table.find_compat(100);
63 if (result.first)
64 std::cout << "Found key 100: " << result.second << std::endl;
65 else
66 std::cout << "Key 100 not found\n";
67# endif
68
69 // erase
70 bool erased = table.erase(200);
71 std::cout << "Erased key 200: " << erased << std::endl;
72
73 // load
74 std::cout << "All key-value pairs:\n";
75 std::vector<std::pair<int, std::string>> all;
76 table.load(all);
77 for (size_t i = 0; i < all.size(); ++i)
78 std::cout << all[i].first << ": " << all[i].second << std::endl;
79
80 // clear
81 table.clear();
82 std::cout << "After clear, size = " << table.count() << std::endl;
83
84 std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
85 std::cin.get();
86 return 0;
87}
Declaration of the KeyValueTable class for managing key-value pairs in an MDBX database.
int main()
Entry point demonstrating AnyValueTable.
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
int64_t max_dbs
Maximum number of named databases (DBI) in the environment.
Definition Config.hpp:27
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.