1 /* mtest3.c - memory-mapped database tester/toy */
3 * Copyright 2011 Howard Chu, Symas Corp.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted only as authorized by the OpenLDAP
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>.
15 /* Tests for sorted duplicate DBs */
16 #define _XOPEN_SOURCE 500 /* srandom(), random() */
23 int main(int argc,char * argv[])
35 char kval[sizeof(int)];
39 memset(sval, 0, sizeof(sval));
41 count = (random()%384) + 64;
42 values = (int *)malloc(count*sizeof(int));
44 for(i = 0;i<count;i++) {
45 values[i] = random()%1024;
48 rc = mdb_env_create(&env);
49 rc = mdb_env_set_mapsize(env, 10485760);
50 rc = mdb_env_set_maxdbs(env, 4);
51 rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
52 rc = mdb_txn_begin(env, 0, &txn);
53 rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi);
55 key.mv_size = sizeof(int);
57 data.mv_size = sizeof(sval);
60 printf("Adding %d values\n", count);
61 for (i=0;i<count;i++) {
63 sprintf(kval, "%03x", values[i]);
64 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
65 rc = mdb_put(txn, dbi, &key, &data, MDB_NODUPDATA);
68 if (j) printf("%d duplicates skipped\n", j);
69 rc = mdb_txn_commit(txn);
70 rc = mdb_env_stat(env, &mst);
72 rc = mdb_txn_begin(env, 1, &txn);
73 rc = mdb_cursor_open(txn, dbi, &cursor);
74 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
75 printf("key: %p %.*s, data: %p %.*s\n",
76 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
77 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
79 mdb_cursor_close(cursor);
84 for (i= count - 1; i > -1; i-= (random()%5)) {
87 rc = mdb_txn_begin(env, 0, &txn);
88 sprintf(kval, "%03x", values[i & ~0x0f]);
89 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
90 key.mv_size = sizeof(int);
92 data.mv_size = sizeof(sval);
94 rc = mdb_del(txn, dbi, &key, &data);
99 rc = mdb_txn_commit(txn);
103 printf("Deleted %d values\n", j);
105 rc = mdb_env_stat(env, &mst);
106 rc = mdb_txn_begin(env, 1, &txn);
107 rc = mdb_cursor_open(txn, dbi, &cursor);
108 printf("Cursor next\n");
109 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
110 printf("key: %.*s, data: %.*s\n",
111 (int) key.mv_size, (char *) key.mv_data,
112 (int) data.mv_size, (char *) data.mv_data);
114 printf("Cursor prev\n");
115 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
116 printf("key: %.*s, data: %.*s\n",
117 (int) key.mv_size, (char *) key.mv_data,
118 (int) data.mv_size, (char *) data.mv_data);
120 mdb_cursor_close(cursor);