1 | /* mtest2.c - memory-mapped database tester/toy */ |
2 | /* |
3 | * Copyright 2011-2018 Howard Chu, Symas Corp. |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted only as authorized by the OpenLDAP |
8 | * Public License. |
9 | * |
10 | * A copy of this license is available in the file LICENSE in the |
11 | * top-level directory of the distribution or, alternatively, at |
12 | * <http://www.OpenLDAP.org/license.html>. |
13 | */ |
14 | |
15 | /* Just like mtest.c, but using a subDB instead of the main DB */ |
16 | |
17 | #include <stdio.h> |
18 | #include <stdlib.h> |
19 | #include <time.h> |
20 | #include "lmdb.h" |
21 | |
22 | #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) |
23 | #define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0)) |
24 | #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ |
25 | "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) |
26 | |
27 | int main(int argc,char * argv[]) |
28 | { |
29 | int i = 0, j = 0, rc; |
30 | MDB_env *env; |
31 | MDB_dbi dbi; |
32 | MDB_val key, data; |
33 | MDB_txn *txn; |
34 | MDB_stat mst; |
35 | MDB_cursor *cursor; |
36 | int count; |
37 | int *values; |
38 | char sval[32] = "" ; |
39 | |
40 | srand(time(NULL)); |
41 | |
42 | count = (rand()%384) + 64; |
43 | values = (int *)malloc(count*sizeof(int)); |
44 | |
45 | for(i = 0;i<count;i++) { |
46 | values[i] = rand()%1024; |
47 | } |
48 | |
49 | E(mdb_env_create(&env)); |
50 | E(mdb_env_set_maxreaders(env, 1)); |
51 | E(mdb_env_set_mapsize(env, 10485760)); |
52 | E(mdb_env_set_maxdbs(env, 4)); |
53 | E(mdb_env_open(env, "./testdb" , MDB_FIXEDMAP|MDB_NOSYNC, 0664)); |
54 | |
55 | E(mdb_txn_begin(env, NULL, 0, &txn)); |
56 | E(mdb_dbi_open(txn, "id1" , MDB_CREATE, &dbi)); |
57 | |
58 | key.mv_size = sizeof(int); |
59 | key.mv_data = sval; |
60 | |
61 | printf("Adding %d values\n" , count); |
62 | for (i=0;i<count;i++) { |
63 | sprintf(sval, "%03x %d foo bar" , values[i], values[i]); |
64 | data.mv_size = sizeof(sval); |
65 | data.mv_data = sval; |
66 | if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE))) |
67 | j++; |
68 | } |
69 | if (j) printf("%d duplicates skipped\n" , j); |
70 | E(mdb_txn_commit(txn)); |
71 | E(mdb_env_stat(env, &mst)); |
72 | |
73 | E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)); |
74 | E(mdb_cursor_open(txn, dbi, &cursor)); |
75 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
76 | printf("key: %p %.*s, data: %p %.*s\n" , |
77 | key.mv_data, (int) key.mv_size, (char *) key.mv_data, |
78 | data.mv_data, (int) data.mv_size, (char *) data.mv_data); |
79 | } |
80 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
81 | mdb_cursor_close(cursor); |
82 | mdb_txn_abort(txn); |
83 | |
84 | j=0; |
85 | key.mv_data = sval; |
86 | for (i= count - 1; i > -1; i-= (rand()%5)) { |
87 | j++; |
88 | txn=NULL; |
89 | E(mdb_txn_begin(env, NULL, 0, &txn)); |
90 | sprintf(sval, "%03x " , values[i]); |
91 | if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) { |
92 | j--; |
93 | mdb_txn_abort(txn); |
94 | } else { |
95 | E(mdb_txn_commit(txn)); |
96 | } |
97 | } |
98 | free(values); |
99 | printf("Deleted %d values\n" , j); |
100 | |
101 | E(mdb_env_stat(env, &mst)); |
102 | E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)); |
103 | E(mdb_cursor_open(txn, dbi, &cursor)); |
104 | printf("Cursor next\n" ); |
105 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
106 | printf("key: %.*s, data: %.*s\n" , |
107 | (int) key.mv_size, (char *) key.mv_data, |
108 | (int) data.mv_size, (char *) data.mv_data); |
109 | } |
110 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
111 | printf("Cursor prev\n" ); |
112 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { |
113 | printf("key: %.*s, data: %.*s\n" , |
114 | (int) key.mv_size, (char *) key.mv_data, |
115 | (int) data.mv_size, (char *) data.mv_data); |
116 | } |
117 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
118 | mdb_cursor_close(cursor); |
119 | mdb_txn_abort(txn); |
120 | |
121 | mdb_dbi_close(env, dbi); |
122 | mdb_env_close(env); |
123 | return 0; |
124 | } |
125 | |