Debian Codegraph @

lmdbxx/check.cc

raw symbols SCIP
1 /* This is free and unencumbered software released into the public domain. */
2
3 #include "lmdb++.h"
4
5 #include <iostream>
6 #include <stdexcept>
7
8
9 int main() {
10 std::string longLivedValue;
11
12 try {
13 auto env = lmdb::env::create();
14 env.set_max_dbs(64);
15 env.open("testdb/");
16 lmdb::dbi mydb;
17 lmdb::dbi mydbdups;
18
19 // Put some values in and read them back out
20
21 {
22 auto txn = lmdb::txn::begin(env);
23 mydb = lmdb::dbi::open(txn, "mydb", MDB_CREATE);
24
25 mydb.put(txn, "hello", "world");
26 mydb.put(txn, "abc", std::string("Q\0X", 3));
27
28 txn.commit();
29 }
30
31 {
32 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
33
34 std::string_view v;
35 mydb.get(txn, "hello", v);
36 if (v != "world") throw std::runtime_error("bad read");
37
38 longLivedValue = v;
39 }
40
41
42 // Update one of the values
43
44 {
45 auto txn = lmdb::txn::begin(env);
46
47 mydb.put(txn, "hello", "WORLD!");
48
49 txn.commit();
50 }
51
52 {
53 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
54
55 std::string_view v;
56 mydb.get(txn, "hello", v);
57 if (v != "WORLD!") throw std::runtime_error("bad read 2");
58 }
59
60
61 // Iterate over the values with a cursor
62
63 {
64 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
65
66 auto cursor = lmdb::cursor::open(txn, mydb);
67 std::string_view key, val;
68
69 if (!cursor.get(key, val, MDB_FIRST)) throw std::runtime_error("cursor err 1");
70 if (key != "abc" || val != std::string("Q\0X", 3)) throw std::runtime_error("cursor err 2");
71
72 if (!cursor.get(key, val, MDB_NEXT)) throw std::runtime_error("cursor err 3");
73 if (key != "hello" || val != "WORLD!") throw std::runtime_error("cursor err 4");
74
75 if (cursor.get(key, val, MDB_NEXT)) throw std::runtime_error("cursor err 5");
76
77 cursor.close();
78 }
79
80
81 // Delete a value
82
83 {
84 auto txn = lmdb::txn::begin(env);
85
86 mydb.del(txn, "hello");
87
88 std::string_view v;
89 bool found = mydb.get(txn, "hello", v);
90 if (found) throw std::runtime_error("wasn't deleted");
91
92 txn.commit();
93 }
94
95
96
97 // Sorted dups
98
99 {
100 auto txn = lmdb::txn::begin(env);
101 mydbdups = lmdb::dbi::open(txn, "mydbdups", MDB_CREATE | MDB_DUPSORT);
102
103 mydbdups.put(txn, "aaaa", "junk");
104
105 mydbdups.put(txn, "blah", "abc2");
106 mydbdups.put(txn, "blah", "abc1");
107 mydbdups.put(txn, "blah", "abc3");
108
109 mydbdups.put(txn, "cccc", "junk");
110
111 txn.commit();
112 }
113
114 {
115 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
116
117 auto cursor = lmdb::cursor::open(txn, mydbdups);
118 std::string_view key("blah"), val;
119
120 if (!cursor.get(key, val, MDB_SET_KEY)) throw std::runtime_error("cursor err 1");
121 if (!cursor.get(key, val, MDB_FIRST_DUP)) throw std::runtime_error("FIRST_DUP err");
122
123 if (cursor.count() != 3) throw std::runtime_error("cursor.count error");
124
125 if (key != "blah" || val != "abc1") throw std::runtime_error("cursor err 2");
126
127 if (!cursor.get(key, val, MDB_NEXT_DUP)) throw std::runtime_error("cursor err 3");
128 if (key != "blah" || val != "abc2") throw std::runtime_error("cursor err 4");
129
130 if (cursor.count() != 3) throw std::runtime_error("cursor.count error");
131
132 if (!cursor.get(key, val, MDB_NEXT_DUP)) throw std::runtime_error("cursor err 5");
133 if (key != "blah" || val != "abc3") throw std::runtime_error("cursor err 6");
134
135 if (cursor.get(key, val, MDB_NEXT_DUP)) throw std::runtime_error("cursor err 7");
136
137 cursor.close();
138 txn.commit();
139 }
140
141
142 // Deleting a dup
143
144 {
145 auto txn = lmdb::txn::begin(env);
146
147 mydbdups.del(txn, "blah", "abc2");
148
149 txn.commit();
150 }
151
152 {
153 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
154
155 auto cursor = lmdb::cursor::open(txn, mydbdups);
156 std::string_view key("blah"), val;
157
158 if (!cursor.get(key, val, MDB_SET_KEY)) throw std::runtime_error("cursor err 1");
159 if (key != "blah" || val != "abc1") throw std::runtime_error("cursor err 2");
160
161 if (cursor.count() != 2) throw std::runtime_error("cursor.count error");
162
163 if (!cursor.get(key, val, MDB_NEXT_DUP)) throw std::runtime_error("cursor err 5");
164 if (key != "blah" || val != "abc3") throw std::runtime_error("cursor err 6");
165
166 if (cursor.get(key, val, MDB_NEXT_DUP)) throw std::runtime_error("cursor err 7");
167
168 cursor.close();
169 txn.commit();
170 }
171
172
173
174 // to_sv / from_sv
175
176 {
177 auto txn = lmdb::txn::begin(env);
178
179 // DON'T DO THIS: the r-value is destroyed at the end of the full expression
180 //auto my_sv = lmdb::to_sv<uint64_t>(0x1122334455667788);
181 //mydb.put(txn, "to_sv_key", my_sv);
182
183 // OK: The rvalue will stay alive until the end of the "full-expression" (mydb.put(...))
184 mydb.put(txn, "to_sv_key", lmdb::to_sv<uint64_t>(0x1122334455667788));
185
186 // OK: temp stays alive until the end of its scope
187 uint64_t temp = 0x8877665544332211;
188 auto my_sv = lmdb::to_sv(temp);
189 mydb.put(txn, "to_sv_key2", my_sv);
190
191 // OK: v stays alive until the end of its scope
192 int16_t v = -19288;
193 mydb.put(txn, "to_sv_key3", lmdb::ptr_to_sv(&v));
194
195 txn.commit();
196 }
197
198 {
199 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
200
201 std::string_view v;
202 if (!mydb.get(txn, "to_sv_key", v)) throw std::runtime_error("bad read to_sv 1");
203 if (lmdb::from_sv<uint64_t>(v) != 0x1122334455667788) throw std::runtime_error("bad read to_sv 2");
204
205 if (!mydb.get(txn, "to_sv_key2", v)) throw std::runtime_error("bad read to_sv 2");
206 if (lmdb::from_sv<uint64_t>(v) != 0x8877665544332211) throw std::runtime_error("bad read to_sv 3");
207
208 if (!mydb.get(txn, "to_sv_key3", v)) throw std::runtime_error("bad read to_sv 4");
209 if (lmdb::from_sv<int16_t>(v) != -19288) throw std::runtime_error("bad read to_sv 5");
210
211 int16_t *ptr = lmdb::ptr_from_sv<int16_t>(v);
212 if (*ptr != -19288) throw std::runtime_error("bad read to_sv 6");
213 }
214
215
216
217 // Nested transactions
218
219 {
220 auto txn = lmdb::txn::begin(env);
221
222 {
223 auto txn2 = lmdb::txn::begin(env, txn);
224
225 mydb.put(txn2, "junk1", "blah");
226
227 {
228 // Can't use the parent txn when child active
229 bool caught = false;
230
231 try {
232 std::string_view v;
233 mydb.get(txn, "junk1", v);
234 } catch (std::exception &e) {
235 caught = true;
236 }
237
238 if (!caught) throw std::runtime_error("bad nested tx 0");
239 }
240
241 txn2.abort();
242 }
243
244 {
245 std::string_view v;
246 if (mydb.get(txn, "junk1", v)) throw std::runtime_error("bad nested tx 1");
247 }
248
249 {
250 auto txn2 = lmdb::txn::begin(env, txn);
251
252 mydb.put(txn2, "junk2", "bleh");
253
254 txn2.commit();
255 }
256
257 {
258 std::string_view v;
259 if (!mydb.get(txn, "junk2", v)) throw std::runtime_error("bad nested tx 2");
260 }
261
262 {
263 // Creating a read-only sub-transaction inside a read-write transaction
264 bool caught = false;
265
266 try {
267 auto txn2 = lmdb::txn::begin(env, txn, MDB_RDONLY);
268 } catch (std::exception &e) {
269 caught = true;
270 }
271
272 if (!caught) throw std::runtime_error("bad nested tx 2.1");
273 }
274
275 txn.commit();
276 }
277
278 {
279 auto txn = lmdb::txn::begin(env, nullptr, MDB_RDONLY);
280
281 std::string_view v;
282
283 if (mydb.get(txn, "junk1", v)) throw std::runtime_error("bad nested tx 3");
284
285 if (!mydb.get(txn, "junk2", v)) throw std::runtime_error("bad nested tx 4");
286 if (v != "bleh") throw std::runtime_error("bad nested tx 3");
287
288
289 // trying to create a non-readonly sub-tx
290
291 bool caught = false;
292
293 try {
294 auto txn2 = lmdb::txn::begin(env, txn);
295 } catch (std::exception &e) {
296 caught = true;
297 }
298
299 if (!caught) throw std::runtime_error("bad nested tx 5");
300 }
301
302
303
304
305 if (0) {
306 // This test case is not enabled by default because it causes the process
307 // to crash. See the "Cursor double-free issue" section in README.md
308 std::cout << "Running optional test #1" << std::endl;
309
310 auto txn = lmdb::txn::begin(env);
311
312 auto cursor = lmdb::cursor::open(txn, mydb);
313 std::string_view key, val;
314 cursor.get(key, val, MDB_FIRST);
315
316 //cursor.close(); // <-- UNCOMMENT TO FIX CRASH (https://github.com/drycpp/lmdbxx/issues/22)
317 txn.commit();
318 }
319 }
320 catch (const lmdb::error& error) {
321 std::cerr << "Failed with error: " << error.what() << std::endl;
322 return 1;
323 }
324
325 if (longLivedValue != "world") throw std::runtime_error("bad longLivedValue");
326
327
328
329 if (0) {
330 // Enable this to verify the fix for https://github.com/hoytech/lmdbxx/pull/3
331 // This test is not run in the normal test-suite because the sizes chosen depend
332 // on internal LMDB layout, which could change.
333 std::cout << "Running optional test #2" << std::endl;
334
335 if (system("rm testdb/data.mdb testdb/lock.mdb")) {
336 std::cerr << "Unable to delete DB files during test" << std::endl;
337 return 1;
338 }
339
340 auto env = lmdb::env::create();
341 env.set_max_dbs(64);
342 env.set_mapsize(30000);
343 env.open("testdb/");
344
345 lmdb::dbi mydb;
346
347 {
348 auto txn = lmdb::txn::begin(env);
349 mydb = lmdb::dbi::open(txn, "mydb", MDB_CREATE);
350 txn.commit();
351 }
352
353 {
354 auto txn = lmdb::txn::begin(env);
355
356 mydb.put(txn, "k", std::string(4000, '\x01'));
357
358 bool got_map_full_error = false;
359
360 try {
361 txn.commit(); // <-- This throws MDB_MAP_FULL, which leaves the transaction "partially committed"
362 } catch (const lmdb::map_full_error& error) {
363 got_map_full_error = true;
364 }
365
366 if (!got_map_full_error) throw std::runtime_error("didn't get expected lmdb::map_full_error");
367
368 // <-- The transaction is aborted here, which results in double-free
369 }
370 }
371
372 return 0;
373 }