mtest5.c revision 1.1 1 1.1 tron /* $NetBSD: mtest5.c,v 1.1 2014/05/28 09:58:42 tron Exp $ */
2 1.1 tron
3 1.1 tron /* mtest5.c - memory-mapped database tester/toy */
4 1.1 tron /*
5 1.1 tron * Copyright 2011 Howard Chu, Symas Corp.
6 1.1 tron * All rights reserved.
7 1.1 tron *
8 1.1 tron * Redistribution and use in source and binary forms, with or without
9 1.1 tron * modification, are permitted only as authorized by the OpenLDAP
10 1.1 tron * Public License.
11 1.1 tron *
12 1.1 tron * A copy of this license is available in the file LICENSE in the
13 1.1 tron * top-level directory of the distribution or, alternatively, at
14 1.1 tron * <http://www.OpenLDAP.org/license.html>.
15 1.1 tron */
16 1.1 tron
17 1.1 tron /* Tests for sorted duplicate DBs using cursor_put */
18 1.1 tron #define _XOPEN_SOURCE 500 /* srandom(), random() */
19 1.1 tron #include <stdio.h>
20 1.1 tron #include <stdlib.h>
21 1.1 tron #include <string.h>
22 1.1 tron #include <time.h>
23 1.1 tron #include "lmdb.h"
24 1.1 tron
25 1.1 tron int main(int argc,char * argv[])
26 1.1 tron {
27 1.1 tron int i = 0, j = 0, rc;
28 1.1 tron MDB_env *env;
29 1.1 tron MDB_dbi dbi;
30 1.1 tron MDB_val key, data;
31 1.1 tron MDB_txn *txn;
32 1.1 tron MDB_stat mst;
33 1.1 tron MDB_cursor *cursor;
34 1.1 tron int count;
35 1.1 tron int *values;
36 1.1 tron char sval[32];
37 1.1 tron char kval[sizeof(int)];
38 1.1 tron
39 1.1 tron srandom(time(NULL));
40 1.1 tron
41 1.1 tron memset(sval, 0, sizeof(sval));
42 1.1 tron
43 1.1 tron count = (random()%384) + 64;
44 1.1 tron values = (int *)malloc(count*sizeof(int));
45 1.1 tron
46 1.1 tron for(i = 0;i<count;i++) {
47 1.1 tron values[i] = random()%1024;
48 1.1 tron }
49 1.1 tron
50 1.1 tron rc = mdb_env_create(&env);
51 1.1 tron rc = mdb_env_set_mapsize(env, 10485760);
52 1.1 tron rc = mdb_env_set_maxdbs(env, 4);
53 1.1 tron rc = mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664);
54 1.1 tron rc = mdb_txn_begin(env, NULL, 0, &txn);
55 1.1 tron rc = mdb_open(txn, "id2", MDB_CREATE|MDB_DUPSORT, &dbi);
56 1.1 tron rc = mdb_cursor_open(txn, dbi, &cursor);
57 1.1 tron
58 1.1 tron key.mv_size = sizeof(int);
59 1.1 tron key.mv_data = kval;
60 1.1 tron data.mv_size = sizeof(sval);
61 1.1 tron data.mv_data = sval;
62 1.1 tron
63 1.1 tron printf("Adding %d values\n", count);
64 1.1 tron for (i=0;i<count;i++) {
65 1.1 tron if (!(i & 0x0f))
66 1.1 tron sprintf(kval, "%03x", values[i]);
67 1.1 tron sprintf(sval, "%03x %d foo bar", values[i], values[i]);
68 1.1 tron rc = mdb_cursor_put(cursor, &key, &data, MDB_NODUPDATA);
69 1.1 tron if (rc) j++;
70 1.1 tron }
71 1.1 tron if (j) printf("%d duplicates skipped\n", j);
72 1.1 tron mdb_cursor_close(cursor);
73 1.1 tron rc = mdb_txn_commit(txn);
74 1.1 tron rc = mdb_env_stat(env, &mst);
75 1.1 tron
76 1.1 tron rc = mdb_txn_begin(env, NULL, 1, &txn);
77 1.1 tron rc = mdb_cursor_open(txn, dbi, &cursor);
78 1.1 tron while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
79 1.1 tron printf("key: %p %.*s, data: %p %.*s\n",
80 1.1 tron key.mv_data, (int) key.mv_size, (char *) key.mv_data,
81 1.1 tron data.mv_data, (int) data.mv_size, (char *) data.mv_data);
82 1.1 tron }
83 1.1 tron mdb_cursor_close(cursor);
84 1.1 tron mdb_txn_abort(txn);
85 1.1 tron
86 1.1 tron j=0;
87 1.1 tron
88 1.1 tron for (i= count - 1; i > -1; i-= (random()%5)) {
89 1.1 tron j++;
90 1.1 tron txn=NULL;
91 1.1 tron rc = mdb_txn_begin(env, NULL, 0, &txn);
92 1.1 tron sprintf(kval, "%03x", values[i & ~0x0f]);
93 1.1 tron sprintf(sval, "%03x %d foo bar", values[i], values[i]);
94 1.1 tron key.mv_size = sizeof(int);
95 1.1 tron key.mv_data = kval;
96 1.1 tron data.mv_size = sizeof(sval);
97 1.1 tron data.mv_data = sval;
98 1.1 tron rc = mdb_del(txn, dbi, &key, &data);
99 1.1 tron if (rc) {
100 1.1 tron j--;
101 1.1 tron mdb_txn_abort(txn);
102 1.1 tron } else {
103 1.1 tron rc = mdb_txn_commit(txn);
104 1.1 tron }
105 1.1 tron }
106 1.1 tron free(values);
107 1.1 tron printf("Deleted %d values\n", j);
108 1.1 tron
109 1.1 tron rc = mdb_env_stat(env, &mst);
110 1.1 tron rc = mdb_txn_begin(env, NULL, 1, &txn);
111 1.1 tron rc = mdb_cursor_open(txn, dbi, &cursor);
112 1.1 tron printf("Cursor next\n");
113 1.1 tron while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
114 1.1 tron printf("key: %.*s, data: %.*s\n",
115 1.1 tron (int) key.mv_size, (char *) key.mv_data,
116 1.1 tron (int) data.mv_size, (char *) data.mv_data);
117 1.1 tron }
118 1.1 tron printf("Cursor prev\n");
119 1.1 tron while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
120 1.1 tron printf("key: %.*s, data: %.*s\n",
121 1.1 tron (int) key.mv_size, (char *) key.mv_data,
122 1.1 tron (int) data.mv_size, (char *) data.mv_data);
123 1.1 tron }
124 1.1 tron mdb_cursor_close(cursor);
125 1.1 tron mdb_close(env, dbi);
126 1.1 tron
127 1.1 tron mdb_txn_abort(txn);
128 1.1 tron mdb_env_close(env);
129 1.1 tron
130 1.1 tron return 0;
131 1.1 tron }
132