-
Notifications
You must be signed in to change notification settings - Fork 1
/
lmdb-bench.c
245 lines (196 loc) · 5.29 KB
/
lmdb-bench.c
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
240
241
242
243
244
245
/*
* Copyright 2022-2023 CrossDB.ORG. All rights reserved.
*
* https://crossdb.org
* https://github.com/crossdb-org/crossdb
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
gcc lmdb-bench.c -Wall -o lmdb-bench -llmdb -O3
*/
#include "benchmark.h"
#include "lmdb.h"
typedef struct {
uint32_t prefix;
uint8_t mask;
} route_key;
#define CHECK(rc,str) if (rc != MDB_SUCCESS) { printf (str": %s\n", mdb_strerror(rc)); exit(-1); }
static MDB_env *env = NULL;
static MDB_dbi dbi;
static MDB_txn *s_txn = NULL;
const char* lmdb_create (char *dbname, dbtype_e type, keytype_e keyType, uint32_t flags)
{
int rc = mdb_env_create(&env);
MDB_txn *txn;
if (!s_quiet && (DB_INMEM == type)) {
printf ("*** Warning: Doesn't support INMEM, use RAMDISK to do test\n");
}
if (!s_quiet && (KEY_DEFAULT != keyType) && (KEY_TREE != keyType)) {
printf ("*** Warning: Doesn't support key type %s, use default\n", keyTypeStr(keyType));
}
CHECK (rc, "mdb_env_create");
rc = mdb_env_set_mapsize(env, (uint64_t)row_count * sizeof(route_t) * 32 / 10 + 1024*1024);
CHECK (rc, "mdb_env_set_mapsize");
unsigned modeflags = MDB_NOSYNC | MDB_NOMETASYNC | MDB_WRITEMAP | MDB_MAPASYNC | MDB_NORDAHEAD;
rc = mdb_env_open(env, dbname, modeflags, 0644);
CHECK (rc, "mdb_env_open");
rc = mdb_txn_begin(env, NULL, 0, &txn);
CHECK (rc, "mdb_txn_begin");
rc = mdb_open(txn, NULL, 0, &dbi);
CHECK (rc, "mdb_open");
rc = mdb_txn_commit(txn);
CHECK (rc, "mdb_txn_commit");
return "LMDB";
}
void lmdb_close ()
{
mdb_env_close(env);
}
void lmdb_insert (route_t* pRoute)
{
MDB_txn *txn;
MDB_val key, val;
int rc;
if (NULL == s_txn) {
rc = mdb_txn_begin(env, NULL, 0, &txn);
CHECK (rc, "mdb_txn_begin");
} else {
txn = s_txn;
}
route_key rtkey;
memset (&rtkey, 0, sizeof(rtkey));
rtkey.prefix = pRoute->prefix;
rtkey.mask = pRoute->mask;
key.mv_data = &rtkey;
key.mv_size = sizeof(rtkey);
val.mv_data = pRoute;
val.mv_size = sizeof (*pRoute);
rc = mdb_put (txn, dbi, &key, &val, 0);
CHECK (rc, "mdb_cursor_put");
if (NULL == s_txn) {
rc = mdb_txn_commit(txn);
CHECK (rc, "mdb_txn_commit");
}
}
bool lmdb_query (route_t* pRoute)
{
MDB_txn *txn;
MDB_val key, val;
int rc;
bool found = false;
if (NULL == s_txn) {
rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
CHECK (rc, "mdb_txn_begin");
} else {
txn = s_txn;
}
route_key rtkey;
memset (&rtkey, 0, sizeof(rtkey));
rtkey.prefix = pRoute->prefix;
rtkey.mask = pRoute->mask;
key.mv_data = &rtkey;
key.mv_size = sizeof(rtkey);
rc = mdb_get (txn, dbi, &key, &val);
if (MDB_SUCCESS == rc) {
found = true;
} else {
printf ("lmdb error: status = %d, no row found\n", rc);
}
memcpy (pRoute, val.mv_data, sizeof(*pRoute));
if (NULL == s_txn) {
rc = mdb_txn_commit(txn);
CHECK (rc, "mdb_txn_commit");
}
return found;
}
void lmdb_delete (route_t* pRoute)
{
MDB_txn *txn;
MDB_val key;
int rc;
if (NULL == s_txn) {
rc = mdb_txn_begin(env, NULL, 0, &txn);
CHECK (rc, "mdb_txn_begin");
} else {
txn = s_txn;
}
route_key rtkey;
memset (&rtkey, 0, sizeof(rtkey));
rtkey.prefix = pRoute->prefix;
rtkey.mask = pRoute->mask;
key.mv_data = &rtkey;
key.mv_size = sizeof(rtkey);
rc = mdb_del (txn, dbi, &key, NULL);
CHECK (rc, "mdb_del");
if (NULL == s_txn) {
rc = mdb_txn_commit(txn);
CHECK (rc, "mdb_txn_commit");
}
}
bool lmdb_update (route_t* pRoute)
{
MDB_txn *txn;
MDB_val key, val;
int rc;
bool found;
if (NULL == s_txn) {
rc = mdb_txn_begin(env, NULL, 0, &txn);
CHECK (rc, "mdb_txn_begin");
} else {
txn = s_txn;
}
route_key rtkey;
memset (&rtkey, 0, sizeof(rtkey));
rtkey.prefix = pRoute->prefix;
rtkey.mask = pRoute->mask;
key.mv_data = &rtkey;
key.mv_size = sizeof(rtkey);
rc = mdb_get (txn, dbi, &key, &val);
if (MDB_SUCCESS == rc) {
found = true;
} else {
printf ("lmdb error: status = %d, no row found\n", rc);
}
route_t route;
memcpy (&route, val.mv_data, sizeof(*pRoute));
route.birth = pRoute->birth;
rc = mdb_put (txn, dbi, &key, &val, 0);
CHECK (rc, "mdb_put");
if (NULL == s_txn) {
rc = mdb_txn_commit(txn);
CHECK (rc, "mdb_txn_commit");
}
return found;
}
void lmdb_begin ()
{
int rc = mdb_txn_begin(env, NULL, 0, &s_txn);
CHECK (rc, "mdb_txn_begin");
}
void lmdb_commit ()
{
int rc = mdb_txn_commit(s_txn);
CHECK (rc, "mdb_txn_commit");
s_txn = NULL;
}
#define db_name "LMDB"
#define db_create lmdb_create
#define db_close lmdb_close
#define db_insert lmdb_insert
#define db_query lmdb_query
#define db_update lmdb_update
#define db_delete lmdb_delete
#define db_begin lmdb_begin
#define db_commit lmdb_commit
#include "benchmark.c"