1 /* mtest2.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 /* Just like mtest.c, but using a subDB instead of the main DB */
17 #define _XOPEN_SOURCE 500 /* srandom(), random() */
23 int main(int argc,char * argv[])
38 count = (random()%384) + 64;
39 values = (int *)malloc(count*sizeof(int));
41 for(i = 0;i<count;i++) {
42 values[i] = random()%1024;
45 rc = mdb_env_create(&env);
46 rc = mdb_env_set_mapsize(env, 10485760);
47 rc = mdb_env_set_maxdbs(env, 4);
48 rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
49 rc = mdb_txn_begin(env, NULL, 0, &txn);
50 rc = mdb_open(txn, "id1", MDB_CREATE, &dbi);
52 key.mv_size = sizeof(int);
54 data.mv_size = sizeof(sval);
57 printf("Adding %d values\n", count);
58 for (i=0;i<count;i++) {
59 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
60 rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
63 if (j) printf("%d duplicates skipped\n", j);
64 rc = mdb_txn_commit(txn);
65 rc = mdb_env_stat(env, &mst);
67 rc = mdb_txn_begin(env, NULL, 1, &txn);
68 rc = mdb_cursor_open(txn, dbi, &cursor);
69 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
70 printf("key: %p %.*s, data: %p %.*s\n",
71 key.mv_data, (int) key.mv_size, (char *) key.mv_data,
72 data.mv_data, (int) data.mv_size, (char *) data.mv_data);
74 mdb_cursor_close(cursor);
79 for (i= count - 1; i > -1; i-= (random()%5)) {
82 rc = mdb_txn_begin(env, NULL, 0, &txn);
83 sprintf(sval, "%03x ", values[i]);
84 rc = mdb_del(txn, dbi, &key, NULL);
89 rc = mdb_txn_commit(txn);
93 printf("Deleted %d values\n", j);
95 rc = mdb_env_stat(env, &mst);
96 rc = mdb_txn_begin(env, NULL, 1, &txn);
97 rc = mdb_cursor_open(txn, dbi, &cursor);
98 printf("Cursor next\n");
99 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
100 printf("key: %.*s, data: %.*s\n",
101 (int) key.mv_size, (char *) key.mv_data,
102 (int) data.mv_size, (char *) data.mv_data);
104 printf("Cursor prev\n");
105 while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 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);
110 mdb_cursor_close(cursor);