forked from ahupowerdns/lmdb-safe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-example.cc
42 lines (34 loc) · 989 Bytes
/
basic-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
#include "lmdb-safe.hh"
using namespace std;
void checkLMDB(MDBEnv* env, MDBDbi dbi)
{
auto rotxn = env->getROTransaction();
MDBOutVal data;
if(!rotxn->get(dbi, "lmdb", data)) {
cout<< "Outside RW transaction, found that lmdb = " << data.get<string_view>() <<endl;
}
else
cout<<"Outside RW transaction, found nothing" << endl;
}
int main()
{
auto env = getMDBEnv("./database", 0, 0600);
auto dbi = env->openDB("example", MDB_CREATE);
auto txn = env->getRWTransaction();
mdb_drop(*txn, dbi, 0);
txn->put(dbi, "lmdb", "great");
MDBOutVal data;
if(!txn->get(dbi, "lmdb", data)) {
cout<< "Within RW transaction, found that lmdb = " << data.get<string_view>() <<endl;
}
else
cout<<"Found nothing" << endl;
std::thread elsewhere(checkLMDB, env.get(), dbi);
elsewhere.join();
txn->commit();
cout<<"Committed data"<<endl;
checkLMDB(env.get(), dbi);
txn = env->getRWTransaction();
mdb_drop(*txn, dbi, 0);
txn->commit();
}