new example, 'scale-example'

This commit is contained in:
bert hubert 2018-12-08 20:57:28 +01:00
parent c40719f671
commit 35c5365125
2 changed files with 55 additions and 1 deletions

View File

@ -7,7 +7,7 @@ CXXFLAGS:=-std=gnu++17 -Wall -O2 -MMD -MP -ggdb -pthread $(INCLUDES) # -fsaniti
CFLAGS:= -Wall -O2 -MMD -MP -ggdb
PROGRAMS = lmdb-test basic-example
PROGRAMS = lmdb-test basic-example scale-example
all: $(PROGRAMS)
@ -22,3 +22,6 @@ lmdb-test: lmdb-test.o lmdb-safe.o
basic-example: basic-example.o lmdb-safe.o
g++ -std=gnu++17 $^ -o $@ -pthread $(LIBS)
scale-example: scale-example.o lmdb-safe.o
g++ -std=gnu++17 $^ -o $@ -pthread $(LIBS)

51
scale-example.cc Normal file
View File

@ -0,0 +1,51 @@
#include "lmdb-safe.hh"
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("huge", 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);
MDB_val key, data;
int count=0;
while(!cursor.get(key, data, count ? MDB_NEXT : MDB_FIRST)) {
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 int n = 0 ; n < limit; ++n) {
txn.put(dbi, MDBVal(n), MDBVal(n), MDB_APPEND);
}
cout <<"Done!"<<endl;
cout <<"Calling commit.. "; cout.flush();
txn.commit();
cout<<"Done!"<<endl;
}