Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 156 lines (142 sloc) 4.697 kb
738e9887 »
2012-10-08 initial import
1 /* mtest.c - memory-mapped database tester/toy */
2 /*
3 * Copyright 2011 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 #define _XOPEN_SOURCE 500 /* srandom(), random() */
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include "mdb.h"
19
20 int main(int argc,char * argv[])
21 {
22 int i = 0, j = 0, rc;
23 MDB_env *env;
24 MDB_dbi dbi;
25 MDB_val key, data;
26 MDB_txn *txn;
27 MDB_stat mst;
28 MDB_cursor *cursor;
29 int count;
30 int *values;
31 char sval[32];
32
33 srandom(time(NULL));
34
35 count = (random()%384) + 64;
36 values = (int *)malloc(count*sizeof(int));
37
38 for(i = 0;i<count;i++) {
39 values[i] = random()%1024;
40 }
41
42 rc = mdb_env_create(&env);
43 rc = mdb_env_set_mapsize(env, 10485760);
44 rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664);
45 rc = mdb_txn_begin(env, NULL, 0, &txn);
46 rc = mdb_open(txn, NULL, 0, &dbi);
47
48 key.mv_size = sizeof(int);
49 key.mv_data = sval;
50 data.mv_size = sizeof(sval);
51 data.mv_data = sval;
52
53 printf("Adding %d values\n", count);
54 for (i=0;i<count;i++) {
55 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
56 rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
57 if (rc) j++;
58 }
59 if (j) printf("%d duplicates skipped\n", j);
60 rc = mdb_txn_commit(txn);
61 rc = mdb_env_stat(env, &mst);
62
63 rc = mdb_txn_begin(env, NULL, 1, &txn);
64 rc = mdb_cursor_open(txn, dbi, &cursor);
65 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
66 printf("key: %p %.*s, data: %p %.*s\n",
67 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
68 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
69 }
70 mdb_cursor_close(cursor);
71 mdb_txn_abort(txn);
72
73 j=0;
74 key.mv_data = sval;
75 for (i= count - 1; i > -1; i-= (random()%5)) {
76 j++;
77 txn=NULL;
78 rc = mdb_txn_begin(env, NULL, 0, &txn);
79 sprintf(sval, "%03x ", values[i]);
80 rc = mdb_del(txn, dbi, &key, NULL);
81 if (rc) {
82 j--;
83 mdb_txn_abort(txn);
84 } else {
85 rc = mdb_txn_commit(txn);
86 }
87 }
88 free(values);
89 printf("Deleted %d values\n", j);
90
91 rc = mdb_env_stat(env, &mst);
92 rc = mdb_txn_begin(env, NULL, 1, &txn);
93 rc = mdb_cursor_open(txn, dbi, &cursor);
94 printf("Cursor next\n");
95 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
96 printf("key: %.*s, data: %.*s\n",
97 (int) key.mv_size, (char *) key.mv_data,
98 (int) data.mv_size, (char *) data.mv_data);
99 }
100 printf("Cursor prev\n");
101 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
102 printf("key: %.*s, data: %.*s\n",
103 (int) key.mv_size, (char *) key.mv_data,
104 (int) data.mv_size, (char *) data.mv_data);
105 }
106 #if 0
107 /* write ops aren't coordinated with cursors,
108 * this stuff all breaks
109 */
110 printf("Deleting with cursor\n");
111 rc = mdb_txn_begin(env, NULL, 0, &txn);
112 rc = mdb_cursor_open(db, txn, &cur2);
113 for (i=0; i<50; i++) {
114 rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
115 printf("key: %p %.*s, data: %p %.*s\n",
116 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
117 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
118 rc = mdb_del(db, txn, &key, NULL);
119 }
120
121 printf("Restarting cursor in txn\n");
122 rc = mdb_cursor_get(cur2, &key, &data, MDB_FIRST);
123 printf("key: %p %.*s, data: %p %.*s\n",
124 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
125 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
126 for (i=0; i<32; i++) {
127 rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
128 printf("key: %p %.*s, data: %p %.*s\n",
129 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
130 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
131 }
132 mdb_cursor_close(cur2);
133 rc = mdb_txn_commit(txn);
134 mdb_cursor_close(cursor);
135 printf("Restarting cursor outside txn\n");
136 rc = mdb_cursor_open(db, NULL, &cursor);
137 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
138 printf("key: %p %.*s, data: %p %.*s\n",
139 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
140 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
141 for (i=0; i<32; i++) {
142 rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
143 printf("key: %p %.*s, data: %p %.*s\n",
144 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
145 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
146 }
147 #endif
148 mdb_cursor_close(cursor);
149 mdb_close(env, dbi);
150
151 mdb_txn_abort(txn);
152 mdb_env_close(env);
153
154 return 0;
155 }
Something went wrong with that request. Please try again.