-
Notifications
You must be signed in to change notification settings - Fork 23
/
multi-example.cc
60 lines (45 loc) · 1.54 KB
/
multi-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
53
54
55
56
57
58
59
60
#include "lmdb-safe.hh"
using namespace std;
#include <unistd.h>
int main()
{
unlink("./multi");
auto env = getMDBEnv("multi", MDB_NOSUBDIR, 0600);
auto dbi = env->openDB("qnames", MDB_DUPSORT | MDB_CREATE);
auto txn = env->getRWTransaction();
txn->clear(dbi);
txn->put(dbi, "bdb", "old");
txn->put(dbi, "lmdb", "hot");
txn->put(dbi, "lmdb", "fast");
txn->put(dbi, "lmdb", "zooms");
txn->put(dbi, "lmdb", "c");
txn->put(dbi, "mdb", "old name");
string_view v1;
if(!txn->get(dbi, "mdb", v1)) {
cout<<v1<<endl;
}
else {
cout << "found nothing" << endl;
}
txn->commit();
txn = env->getRWTransaction();
auto cursor = txn->getRWCursor(dbi);
MDBOutVal key, data;
for(int rc = cursor.find("lmdb", key, data); !rc; rc = cursor.get(key, data, MDB_NEXT_DUP)) {
cout << key.get<string_view>() << " = " << data.get<string_view>() <<endl;
}
cout<<"Dump of complete database: "<<endl;
for(int rc = cursor.first(key, data); !rc; rc = cursor.next(key, data)) {
cout << key.get<string_view>() << " = " << data.get<string_view>() <<endl;
}
cout << "Done!" <<endl;
cout << "Now going to delete 'lmdb' keys" << endl;
for(int rc = cursor.first(key, data); !rc; rc = cursor.next(key, data)) {
if(key.get<string_view>() == "lmdb")
cursor.del();
}
cout <<"Complete database after deleting 'lmdb' keys: " << endl;
for(int rc = cursor.first(key, data); !rc; rc = cursor.next(key, data)) {
cout << key.get<string_view>() << " = " << data.get<string_view>() <<endl;
}
}