-
Notifications
You must be signed in to change notification settings - Fork 23
/
scale-example.cc
54 lines (47 loc) · 1.19 KB
/
scale-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
#include "lmdb-safe.hh"
using namespace std;
struct MDBVal
{
MDBVal(unsigned int v) : d_v(v)
{
d_mdbval.mv_size = sizeof(d_v);
d_mdbval.mv_data = &d_v;
}
operator const MDB_val&()
{
return d_mdbval;
}
unsigned int d_v;
MDB_val d_mdbval;
};
int main(int argc, char** argv)
{
auto env = getMDBEnv("./database", 0, 0600);
auto dbi = env->openDB(0, MDB_CREATE | MDB_INTEGERKEY);
auto txn = env->getRWTransaction();
unsigned int limit=20000000;
if(argc > 1)
limit = atoi(argv[1]);
cout<<"Counting records.. "; cout.flush();
auto cursor = txn->getCursor(dbi);
MDBOutVal key, data;
int count=0;
while(!cursor.get(key, data, count ? MDB_NEXT : MDB_FIRST)) {
auto d = data.get<unsigned long>();
if(d==17)
cout <<"Got 17!"<<endl;
count++;
}
cout<<"Have "<<count<<"!"<<endl;
cout<<"Clearing records.. "; cout.flush();
mdb_drop(*txn, dbi, 0); // clear records
cout<<"Done!"<<endl;
cout << "Adding "<<limit<<" values .. "; cout.flush();
for(unsigned long n = 0 ; n < limit; ++n) {
txn->put(dbi, n, n, MDB_APPEND);
}
cout <<"Done!"<<endl;
cout <<"Calling commit.. "; cout.flush();
txn->commit();
cout<<"Done!"<<endl;
}