]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/mtest2.c
Rename libmdb to liblmdb
[openldap] / libraries / liblmdb / mtest2.c
1 /* mtest2.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
15 /* Just like mtest.c, but using a subDB instead of the main DB */
16
17 #define _XOPEN_SOURCE 500               /* srandom(), random() */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <time.h>
21 #include "lmdb.h"
22
23 int main(int argc,char * argv[])
24 {
25         int i = 0, j = 0, rc;
26         MDB_env *env;
27         MDB_dbi dbi;
28         MDB_val key, data;
29         MDB_txn *txn;
30         MDB_stat mst;
31         MDB_cursor *cursor;
32         int count;
33         int *values;
34         char sval[32];
35
36         srandom(time(NULL));
37
38         count = (random()%384) + 64;
39         values = (int *)malloc(count*sizeof(int));
40
41         for(i = 0;i<count;i++) {
42                 values[i] = random()%1024;
43         }
44
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);
51    
52         key.mv_size = sizeof(int);
53         key.mv_data = sval;
54         data.mv_size = sizeof(sval);
55         data.mv_data = sval;
56
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);
61                 if (rc) j++;
62         }
63         if (j) printf("%d duplicates skipped\n", j);
64         rc = mdb_txn_commit(txn);
65         rc = mdb_env_stat(env, &mst);
66
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);
73         }
74         mdb_cursor_close(cursor);
75         mdb_txn_abort(txn);
76
77         j=0;
78         key.mv_data = sval;
79         for (i= count - 1; i > -1; i-= (random()%5)) {  
80                 j++;
81                 txn=NULL;
82                 rc = mdb_txn_begin(env, NULL, 0, &txn);
83                 sprintf(sval, "%03x ", values[i]);
84                 rc = mdb_del(txn, dbi, &key, NULL);
85                 if (rc) {
86                         j--;
87                         mdb_txn_abort(txn);
88                 } else {
89                         rc = mdb_txn_commit(txn);
90                 }
91         }
92         free(values);
93         printf("Deleted %d values\n", j);
94
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);
103         }
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);
109         }
110         mdb_cursor_close(cursor);
111         mdb_close(env, dbi);
112
113         mdb_txn_abort(txn);
114         mdb_env_close(env);
115
116         return 0;
117 }