]> git.sur5r.net Git - openldap/blob - libraries/libmdb/mtest2.c
7576027a987afd9b571f6bf48a03eaf0e890d4b2
[openldap] / libraries / libmdb / mtest2.c
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, *cur2;
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 = mdbenv_create(&env);
43         rc = mdbenv_set_mapsize(env, 10485760);
44         rc = mdbenv_set_maxdbs(env, 4);
45         rc = mdbenv_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
46         rc = mdb_txn_begin(env, 0, &txn);
47         rc = mdb_open(txn, "id1", MDB_CREATE, &dbi);
48    
49         key.mv_size = sizeof(int);
50         key.mv_data = sval;
51         data.mv_size = sizeof(sval);
52         data.mv_data = sval;
53
54         printf("Adding %d values\n", count);
55         for (i=0;i<count;i++) { 
56                 sprintf(sval, "%03x %d foo bar", values[i], values[i]);
57                 rc = mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE);
58                 if (rc) j++;
59         }
60         if (j) printf("%d duplicates skipped\n", j);
61         rc = mdb_txn_commit(txn);
62         rc = mdbenv_stat(env, &mst);
63
64         rc = mdb_txn_begin(env, 1, &txn);
65         rc = mdb_cursor_open(txn, dbi, &cursor);
66         while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
67                 printf("key: %p %.*s, data: %p %.*s\n",
68                         key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
69                         data.mv_data, (int) data.mv_size, (char *) data.mv_data);
70         }
71         mdb_cursor_close(cursor);
72         mdb_txn_abort(txn);
73
74         j=0;
75         key.mv_data = sval;
76         for (i= count - 1; i > -1; i-= (random()%5)) {  
77                 j++;
78                 txn=NULL;
79                 rc = mdb_txn_begin(env, 0, &txn);
80                 sprintf(sval, "%03x ", values[i]);
81                 rc = mdb_del(txn, dbi, &key, NULL, 0);
82                 if (rc) {
83                         j--;
84                         mdb_txn_abort(txn);
85                 } else {
86                         rc = mdb_txn_commit(txn);
87                 }
88         }
89         free(values);
90         printf("Deleted %d values\n", j);
91
92         rc = mdbenv_stat(env, &mst);
93         rc = mdb_txn_begin(env, 1, &txn);
94         rc = mdb_cursor_open(txn, dbi, &cursor);
95         printf("Cursor next\n");
96         while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
97                 printf("key: %.*s, data: %.*s\n",
98                         (int) key.mv_size,  (char *) key.mv_data,
99                         (int) data.mv_size, (char *) data.mv_data);
100         }
101         printf("Cursor prev\n");
102         while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
103                 printf("key: %.*s, data: %.*s\n",
104                         (int) key.mv_size,  (char *) key.mv_data,
105                         (int) data.mv_size, (char *) data.mv_data);
106         }
107         mdb_cursor_close(cursor);
108         mdb_close(txn, dbi);
109
110         mdb_txn_abort(txn);
111         mdbenv_close(env);
112
113         return 0;
114 }