-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathlmdb-various.cc
239 lines (201 loc) · 5.13 KB
/
lmdb-various.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <arpa/inet.h>
#include <atomic>
#include <string.h>
#include "lmdb-safe.hh"
#include <unistd.h>
#include <thread>
#include <vector>
using namespace std;
static void closeTest()
{
auto env = getMDBEnv("./database", 0, 0600);
int c = MDB_CREATE;
MDBDbi dbi = env->openDB("ahu", c);
MDBDbi main = env->openDB(0, c);
MDBDbi hyc = env->openDB("hyc", c);
auto txn = env->getROTransaction();
for(auto& d : {&main, &dbi, &hyc}) {
auto rocursor = txn->getCursor(*d);
MDBOutVal key, data;
if(rocursor.get(key, data, MDB_FIRST))
continue;
int count=0;
do {
count++;
}while(!rocursor.get(key, data, MDB_NEXT));
cout<<"Have "<<count<<" entries"<<endl;
}
return;
}
void doPuts(int tid)
try
{
auto env = getMDBEnv("./database", 0, 0600);
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
for(int n=0; n < 15; ++n) {
auto txn = env->getRWTransaction();
int val = n + 1000*tid;
txn->put(dbi, val, val);
txn->commit();
cout << "Done with transaction "<<n<<" in thread " << tid<<endl;
}
cout<<"Done with thread "<<tid<<endl;
}
catch(std::exception& e)
{
cout<<"in thread "<<tid<<": "<<e.what()<<endl;
throw;
}
void doGets(int tid)
try
{
auto env = getMDBEnv("./database", 0, 0600);
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
for(int n=0; n < 15; ++n) {
auto txn = env->getROTransaction();
int val = n + 1000*tid;
MDBOutVal res;
if(txn->get(dbi, val, res)) {
throw std::runtime_error("no record");
}
cout << "Done with readtransaction "<<n<<" in thread " << tid<<endl;
}
cout<<"Done with read thread "<<tid<<endl;
}
catch(std::exception& e)
{
cout<<"in thread "<<tid<<": "<<e.what()<<endl;
throw;
}
void doFill()
{
auto env = getMDBEnv("./database", 0, 0600);
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
for(int n = 0; n < 20; ++n) {
auto txn = env->getRWTransaction();
for(int j=0; j < 1000000; ++j) {
MDBInVal mv(n*1000000+j);
txn->put(dbi, mv, mv, 0);
}
txn->commit();
}
cout<<"Done filling"<<endl;
}
void doMeasure()
{
auto env = getMDBEnv("./database", 0, 0600);
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
for(;;) {
for(int n = 0; n < 20; ++n) {
auto txn = env->getROTransaction();
unsigned int count=0;
for(int j=0; j < 1000000; ++j) {
MDBInVal mv(n*1000000+j);
MDBOutVal res;
if(!txn->get(dbi, mv, res))
++count;
}
cout<<count<<" ";
cout.flush();
if(!count)
break;
}
cout<<endl;
}
}
int main(int argc, char** argv)
{
std::thread t1(doMeasure);
std::thread t2(doFill);
t1.join();
t2.join();
}
/*
auto env = getMDBEnv("./database", 0, 0600);
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
vector<std::thread> threads;
for(int n=0; n < 100; ++n) {
std::thread t(doPuts, n);
threads.emplace_back(std::move(t));
}
for(auto& t: threads) {
t.join();
}
threads.clear();
for(int n=0; n < 100; ++n) {
std::thread t(doGets, n);
threads.emplace_back(std::move(t));
}
for(auto& t: threads) {
t.join();
}
return 0;
}
closeTest();
auto env = getMDBEnv("./database", 0, 0600);
MDB_stat stat;
mdb_env_stat(*env.get(), &stat);
cout << stat.ms_entries<< " entries in database"<<endl;
MDBDbi dbi = env->openDB("ahu", MDB_CREATE);
{
MDBROTransaction rotxn = env->getROTransaction();
{
auto rocursor = rotxn.getCursor(dbi);
MDB_val key{0,0}, data{0,0};
rocursor.get(key, data, MDB_FIRST);
int count=0;
do {
count++;
}while(!rocursor.get(key, data, MDB_NEXT));
cout<<"Counted "<<count<<" entries"<<endl;
}
int found{0}, notfound{0};
for(unsigned n=0; n < 20000000; ++n) {
unsigned int store = htonl(n);
MDB_val data;
int rc = rotxn.get(dbi, {sizeof(store), (char*)&store},
data);
if(!rc)
found++;
else if(rc == MDB_NOTFOUND)
notfound++;
else
throw std::runtime_error("error");
rotxn.reset();
rotxn.renew();
if(!(n % 1024000))
cout << n << " " <<found<< " " << notfound <<endl;
}
cout<<"Found "<<found<<", notfound: "<<notfound<<endl;
}
auto txn = env->getRWTransaction();
auto cursor = txn.getCursor(dbi);
time_t start=time(0);
ofstream delplot("plot");
for(unsigned n=0; n < 20000000*8; ++n) {
unsigned int store = htonl(n);
txn.del(dbi, {sizeof(store), (char*)&store});
if(!(n % (1024*1024))) {
cout << time(0)- start << '\t' << n << endl;
delplot << time(0)- start << '\t' << n << endl;
}
}
cout<<"Done deleting, committing"<<endl;
txn.commit();
cout<<"Done with commit"<<endl;
txn = env->getRWTransaction();
start=time(0);
ofstream plot("plot");
for(unsigned n=0; n < 20000000*8; ++n) {
int res = n*n;
unsigned int store = htonl(n);
txn.put(dbi, {sizeof(store), (char*)&store},
{sizeof(res), (char*)&res}, MDB_APPEND);
if(!(n % (1024*1024))) {
cout << time(0)- start << '\t' << n << endl;
plot << time(0)- start << '\t' << n << endl;
}
}
txn.commit();
}
*/