1 | /* mtest5.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 | /* Tests for sorted duplicate DBs using cursor_put */ |
16 | #include <stdio.h> |
17 | #include <stdlib.h> |
18 | #include <string.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 | char kval[sizeof(int)]; |
40 | |
41 | srand(time(NULL)); |
42 | |
43 | memset(sval, 0, sizeof(sval)); |
44 | |
45 | count = (rand()%384) + 64; |
46 | values = (int *)malloc(count*sizeof(int)); |
47 | |
48 | for(i = 0;i<count;i++) { |
49 | values[i] = rand()%1024; |
50 | } |
51 | |
52 | E(mdb_env_create(&env)); |
53 | E(mdb_env_set_mapsize(env, 10485760)); |
54 | E(mdb_env_set_maxdbs(env, 4)); |
55 | E(mdb_env_open(env, "./testdb" , MDB_FIXEDMAP|MDB_NOSYNC, 0664)); |
56 | |
57 | E(mdb_txn_begin(env, NULL, 0, &txn)); |
58 | E(mdb_dbi_open(txn, "id2" , MDB_CREATE|MDB_DUPSORT, &dbi)); |
59 | E(mdb_cursor_open(txn, dbi, &cursor)); |
60 | |
61 | key.mv_size = sizeof(int); |
62 | key.mv_data = kval; |
63 | data.mv_size = sizeof(sval); |
64 | data.mv_data = sval; |
65 | |
66 | printf("Adding %d values\n" , count); |
67 | for (i=0;i<count;i++) { |
68 | if (!(i & 0x0f)) |
69 | sprintf(kval, "%03x" , values[i]); |
70 | sprintf(sval, "%03x %d foo bar" , values[i], values[i]); |
71 | if (RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA))) |
72 | j++; |
73 | } |
74 | if (j) printf("%d duplicates skipped\n" , j); |
75 | mdb_cursor_close(cursor); |
76 | E(mdb_txn_commit(txn)); |
77 | E(mdb_env_stat(env, &mst)); |
78 | |
79 | E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)); |
80 | E(mdb_cursor_open(txn, dbi, &cursor)); |
81 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
82 | printf("key: %p %.*s, data: %p %.*s\n" , |
83 | key.mv_data, (int) key.mv_size, (char *) key.mv_data, |
84 | data.mv_data, (int) data.mv_size, (char *) data.mv_data); |
85 | } |
86 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
87 | mdb_cursor_close(cursor); |
88 | mdb_txn_abort(txn); |
89 | |
90 | j=0; |
91 | |
92 | for (i= count - 1; i > -1; i-= (rand()%5)) { |
93 | j++; |
94 | txn=NULL; |
95 | E(mdb_txn_begin(env, NULL, 0, &txn)); |
96 | sprintf(kval, "%03x" , values[i & ~0x0f]); |
97 | sprintf(sval, "%03x %d foo bar" , values[i], values[i]); |
98 | key.mv_size = sizeof(int); |
99 | key.mv_data = kval; |
100 | data.mv_size = sizeof(sval); |
101 | data.mv_data = sval; |
102 | if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) { |
103 | j--; |
104 | mdb_txn_abort(txn); |
105 | } else { |
106 | E(mdb_txn_commit(txn)); |
107 | } |
108 | } |
109 | free(values); |
110 | printf("Deleted %d values\n" , j); |
111 | |
112 | E(mdb_env_stat(env, &mst)); |
113 | E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn)); |
114 | E(mdb_cursor_open(txn, dbi, &cursor)); |
115 | printf("Cursor next\n" ); |
116 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) { |
117 | printf("key: %.*s, data: %.*s\n" , |
118 | (int) key.mv_size, (char *) key.mv_data, |
119 | (int) data.mv_size, (char *) data.mv_data); |
120 | } |
121 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
122 | printf("Cursor prev\n" ); |
123 | while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) { |
124 | printf("key: %.*s, data: %.*s\n" , |
125 | (int) key.mv_size, (char *) key.mv_data, |
126 | (int) data.mv_size, (char *) data.mv_data); |
127 | } |
128 | CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get" ); |
129 | mdb_cursor_close(cursor); |
130 | mdb_txn_abort(txn); |
131 | |
132 | mdb_dbi_close(env, dbi); |
133 | mdb_env_close(env); |
134 | return 0; |
135 | } |
136 | |