forked from hoytech/lmdbxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cc
52 lines (42 loc) · 1.77 KB
/
example.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <lmdb++.h>
int main() {
/* Create and open the LMDB environment: */
auto env = lmdb::env::create();
env.set_mapsize(1UL * 1024UL * 1024UL * 1024UL); /* 1 GiB */
env.open("./example.mdb/", 0, 0664);
lmdb::dbi dbi;
// Get the dbi handle, and insert some key/value pairs in a write transaction:
{
auto wtxn = lmdb::txn::begin(env);
dbi = lmdb::dbi::open(wtxn, nullptr);
dbi.put(wtxn, "username", "jhacker");
dbi.put(wtxn, "email", std::string("[email protected]"));
dbi.put(wtxn, "fullname", std::string_view("J. Random Hacker"));
wtxn.commit();
}
// In a read-only transaction, get and print one of the values:
{
auto rtxn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
std::string_view email;
if (dbi.get(rtxn, "email", email)) {
std::cout << "The email is: " << email << std::endl;
} else {
std::cout << "email not found!" << std::endl;
}
} // rtxn aborted automatically
// Print out all the values using a cursor:
{
auto rtxn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
{
auto cursor = lmdb::cursor::open(rtxn, dbi);
std::string_view key, value;
if (cursor.get(key, value, MDB_FIRST)) {
do {
std::cout << "key: " << key << " value: " << value << std::endl;
} while (cursor.get(key, value, MDB_NEXT));
}
} // destroying cursor before committing/aborting transaction (see below)
}
return 0;
} // enviroment closed automatically