-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_multi.c
225 lines (169 loc) · 4.27 KB
/
test_multi.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "trlmdb.h"
#define DB_1 "./databases/trlmdb-1"
#define DB_2 "./databases/trlmdb-2"
#define DB_3 "./databases/trlmdb-3"
#define N 100000
void test(void);
int main (void)
{
test();
printf("All tests passed\n");
return 0;
}
void print_mdb_val(MDB_val *val)
{
printf("size = %zu, data = ", val->mv_size);
for (size_t i = 0; i < val->mv_size; i++) {
printf("%02x", *((uint8_t *)val->mv_data + i));
}
printf("\n");
}
void print_table(trlmdb_txn *txn, char *table)
{
printf("\n%s:\n", table);
trlmdb_cursor *cursor;
int rc = trlmdb_cursor_open(txn, table, &cursor);
if (rc)
return;
rc = trlmdb_cursor_first(cursor);
if (rc)
return;
MDB_val key, val;
do {
trlmdb_cursor_get(cursor, &key, &val);
printf("key: ");
print_mdb_val(&key);
printf("val: ");
print_mdb_val(&val);
} while (!trlmdb_cursor_next(cursor));
}
void print_table_backwards(trlmdb_txn *txn, char *table)
{
printf("\n%s backwards:\n", table);
trlmdb_cursor *cursor;
int rc = trlmdb_cursor_open(txn, table, &cursor);
if (rc)
return;
rc = trlmdb_cursor_last(cursor);
if (rc)
return;
MDB_val key, val;
do {
trlmdb_cursor_get(cursor, &key, &val);
printf("key: ");
print_mdb_val(&key);
printf("val: ");
print_mdb_val(&val);
} while (!trlmdb_cursor_prev(cursor));
}
void print_error(int rc)
{
fprintf(stderr, "%d, %s\n", rc, mdb_strerror(rc));
}
int cmp_mdb_val(MDB_val *val_1, MDB_val *val_2)
{
size_t min_size = val_1->mv_size < val_2->mv_size ? val_1->mv_size : val_2->mv_size;
int res = memcmp(val_1->mv_data, val_2->mv_data, min_size);
if (res)
return res;
if (val_1->mv_size == val_2->mv_size)
return 0;
return val_1->mv_size > val_2->mv_size ? 1 : -1;
}
char *make_key_val(const char *base, int i, char *key_val)
{
sprintf(key_val, "%s-%d", base, i);
return key_val;
}
void test(void)
{
int rc = 0;
char key[20];
char val[20];
trlmdb_env *env_1;
rc = trlmdb_env_create(&env_1);
assert(!rc);
rc = trlmdb_env_set_mapsize(env_1, 4096 * 100000);
assert(!rc);
rc = trlmdb_env_open(env_1, DB_1, 0, 0644);
assert(!rc);
trlmdb_env *env_2;
rc = trlmdb_env_create(&env_2);
assert(!rc);
rc = trlmdb_env_set_mapsize(env_2, 4096 * 100000);
assert(!rc);
rc = trlmdb_env_open(env_2, DB_2, 0, 0644);
assert(!rc);
trlmdb_env *env_3;
rc = trlmdb_env_create(&env_3);
assert(!rc);
rc = trlmdb_env_set_mapsize(env_3, 4096 * 100000);
assert(!rc);
rc = trlmdb_env_open(env_3, DB_3, 0, 0644);
assert(!rc);
trlmdb_txn *txn;
char *table = "table";
for (int i = 0; i < N; i++) {
make_key_val("key", i, key);
make_key_val("val", i, val);
MDB_val mdb_key = {strlen(key), key};
MDB_val mdb_val = {strlen(val), val};
trlmdb_env *env = i % 3 == 0 ? env_1 : (i % 3 == 1 ? env_2 : env_3);
rc = trlmdb_txn_begin(env, 0, &txn);
assert(!rc);
rc = trlmdb_put(txn, table, &mdb_key, &mdb_val);
assert(!rc);
rc = trlmdb_txn_commit(txn);
if (rc)
print_error(rc);
assert(!rc);
}
sleep(5 + N / 20000);
trlmdb_txn_begin(env_1, MDB_RDONLY, &txn);
assert(!rc);
for (int i = 0; i < N; i++) {
make_key_val("key", i, key);
make_key_val("val", i, val);
MDB_val mdb_key = {strlen(key), key};
MDB_val mdb_val = {strlen(val), val};
MDB_val mdb_actual;
rc = trlmdb_get(txn, table, &mdb_key, &mdb_actual);
assert(!rc);
assert(!cmp_mdb_val(&mdb_val, &mdb_actual));
}
rc = trlmdb_txn_commit(txn);
assert(!rc);
trlmdb_txn_begin(env_2, MDB_RDONLY, &txn);
assert(!rc);
for (int i = 0; i < N; i++) {
make_key_val("key", i, key);
make_key_val("val", i, val);
MDB_val mdb_key = {strlen(key), key};
MDB_val mdb_val = {strlen(val), val};
MDB_val mdb_actual;
rc = trlmdb_get(txn, table, &mdb_key, &mdb_actual);
assert(!rc);
assert(!cmp_mdb_val(&mdb_val, &mdb_actual));
}
rc = trlmdb_txn_commit(txn);
assert(!rc);
trlmdb_txn_begin(env_3, MDB_RDONLY, &txn);
assert(!rc);
for (int i = 0; i < N; i++) {
make_key_val("key", i, key);
make_key_val("val", i, val);
MDB_val mdb_key = {strlen(key), key};
MDB_val mdb_val = {strlen(val), val};
MDB_val mdb_actual;
rc = trlmdb_get(txn, table, &mdb_key, &mdb_actual);
assert(!rc);
assert(!cmp_mdb_val(&mdb_val, &mdb_actual));
}
rc = trlmdb_txn_commit(txn);
assert(!rc);
}