1 #define _XOPEN_SOURCE 500 /* srandom(), random() */
7 int main(int argc,char * argv[])
15 MDB_cursor *cursor, *cur2;
22 count = (random()%384) + 64;
23 values = (int *)malloc(count*sizeof(int));
25 for(i = 0;i<count;i++) {
26 values[i] = random()%1024;
29 rc = mdbenv_create(&env);
30 rc = mdbenv_set_mapsize(env, 10485760);
31 rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
32 rc = mdb_txn_begin(env, 0, &txn);
33 rc = mdb_open(txn, NULL, 0, &dbi);
35 key.mv_size = sizeof(int);
37 data.mv_size = sizeof(sval);
40 printf("Adding %d values\n", count);
41 for (i=0;i<count;i++) {
42 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
43 rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
46 if (j) printf("%d duplicates skipped\n", j);
47 rc = mdb_txn_commit(txn);
48 rc = mdbenv_stat(env, &mst);
50 rc = mdb_txn_begin(env, 1, &txn);
51 rc = mdb_cursor_open(txn, dbi, &cursor);
52 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
53 printf("key: %p %.*s, data: %p %.*s\n",
54 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
55 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
57 mdb_cursor_close(cursor);
62 for (i= count - 1; i > -1; i-= (random()%5)) {
65 rc = mdb_txn_begin(env, 0, &txn);
66 sprintf(sval, "%03x ", values[i]);
67 rc = mdb_del(txn, dbi, &key, NULL);
72 rc = mdb_txn_commit(txn);
76 printf("Deleted %d values\n", j);
78 rc = mdbenv_stat(env, &mst);
79 rc = mdb_txn_begin(env, 1, &txn);
80 rc = mdb_cursor_open(txn, dbi, &cursor);
81 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
82 printf("key: %.*s, data: %.*s\n",
83 (int) key.mv_size, (char *) key.mv_data,
84 (int) data.mv_size, (char *) data.mv_data);
87 /* write ops aren't coordinated with cursors,
88 * this stuff all breaks
90 printf("Deleting with cursor\n");
91 rc = mdb_txn_begin(env, 0, &txn);
92 rc = mdb_cursor_open(db, txn, &cur2);
93 for (i=0; i<50; i++) {
94 rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
95 printf("key: %p %.*s, data: %p %.*s\n",
96 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
97 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
98 rc = mdb_del(db, txn, &key, NULL);
101 printf("Restarting cursor in txn\n");
102 rc = mdb_cursor_get(cur2, &key, &data, MDB_FIRST);
103 printf("key: %p %.*s, data: %p %.*s\n",
104 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
105 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
106 for (i=0; i<32; i++) {
107 rc = mdb_cursor_get(cur2, &key, &data, MDB_NEXT);
108 printf("key: %p %.*s, data: %p %.*s\n",
109 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
110 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
112 mdb_cursor_close(cur2);
113 rc = mdb_txn_commit(txn);
114 mdb_cursor_close(cursor);
115 printf("Restarting cursor outside txn\n");
116 rc = mdb_cursor_open(db, NULL, &cursor);
117 rc = mdb_cursor_get(cursor, &key, &data, MDB_FIRST);
118 printf("key: %p %.*s, data: %p %.*s\n",
119 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
120 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
121 for (i=0; i<32; i++) {
122 rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT);
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);
128 mdb_cursor_close(cursor);