mtest.c 5.42 KB
Newer Older
Howard Chu's avatar
Howard Chu committed
1
2
/* mtest.c - memory-mapped database tester/toy */
/*
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
3
 * Copyright 2011-2021 Howard Chu, Symas Corp.
Howard Chu's avatar
Howard Chu committed
4
5
6
7
8
9
10
11
12
13
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */
Howard Chu's avatar
Howard Chu committed
14
15
16
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Howard Chu's avatar
Howard Chu committed
17
#include "lmdb.h"
Howard Chu's avatar
Howard Chu committed
18

19
20
21
22
23
#define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr)
#define RES(err, expr) ((rc = expr) == (err) || (CHECK(!rc, #expr), 0))
#define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \
	"%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort()))

Howard Chu's avatar
Howard Chu committed
24
25
int main(int argc,char * argv[])
{
Howard Chu's avatar
Howard Chu committed
26
	int i = 0, j = 0, rc;
Howard Chu's avatar
Howard Chu committed
27
	MDB_env *env;
Howard Chu's avatar
Howard Chu committed
28
	MDB_dbi dbi;
Howard Chu's avatar
Howard Chu committed
29
30
	MDB_val key, data;
	MDB_txn *txn;
Howard Chu's avatar
Howard Chu committed
31
	MDB_stat mst;
Howard Chu's avatar
Howard Chu committed
32
	MDB_cursor *cursor, *cur2;
33
	MDB_cursor_op op;
Howard Chu's avatar
Howard Chu committed
34
35
	int count;
	int *values;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
36
	char sval[32] = "";
Howard Chu's avatar
Howard Chu committed
37

38
	srand(time(NULL));
Howard Chu's avatar
Howard Chu committed
39

40
	    count = (rand()%384) + 64;
Howard Chu's avatar
Howard Chu committed
41
42
43
	    values = (int *)malloc(count*sizeof(int));

	    for(i = 0;i<count;i++) {
44
			values[i] = rand()%1024;
Howard Chu's avatar
Howard Chu committed
45
46
	    }
    
47
		E(mdb_env_create(&env));
48
		E(mdb_env_set_maxreaders(env, 1));
49
50
		E(mdb_env_set_mapsize(env, 10485760));
		E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664));
51

52
		E(mdb_txn_begin(env, NULL, 0, &txn));
53
		E(mdb_dbi_open(txn, NULL, 0, &dbi));
Howard Chu's avatar
Howard Chu committed
54
55
56
57
   
		key.mv_size = sizeof(int);
		key.mv_data = sval;

Howard Chu's avatar
Howard Chu committed
58
		printf("Adding %d values\n", count);
Howard Chu's avatar
Howard Chu committed
59
60
	    for (i=0;i<count;i++) {	
			sprintf(sval, "%03x %d foo bar", values[i], values[i]);
61
62
63
			/* Set <data> in each iteration, since MDB_NOOVERWRITE may modify it */
			data.mv_size = sizeof(sval);
			data.mv_data = sval;
64
			if (RES(MDB_KEYEXIST, mdb_put(txn, dbi, &key, &data, MDB_NOOVERWRITE))) {
Howard Chu's avatar
Howard Chu committed
65
66
67
68
				j++;
				data.mv_size = sizeof(sval);
				data.mv_data = sval;
			}
Howard Chu's avatar
Howard Chu committed
69
70
	    }
		if (j) printf("%d duplicates skipped\n", j);
71
72
		E(mdb_txn_commit(txn));
		E(mdb_env_stat(env, &mst));
Howard Chu's avatar
Howard Chu committed
73

74
		E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
75
		E(mdb_cursor_open(txn, dbi, &cursor));
76
		while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
Howard Chu's avatar
Howard Chu committed
77
			printf("key: %p %.*s, data: %p %.*s\n",
78
79
				key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
				data.mv_data, (int) data.mv_size, (char *) data.mv_data);
Howard Chu's avatar
Howard Chu committed
80
		}
81
		CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
Howard Chu's avatar
Howard Chu committed
82
		mdb_cursor_close(cursor);
Howard Chu's avatar
Howard Chu committed
83
		mdb_txn_abort(txn);
Howard Chu's avatar
Howard Chu committed
84
85

		j=0;
Howard Chu's avatar
Howard Chu committed
86
		key.mv_data = sval;
87
	    for (i= count - 1; i > -1; i-= (rand()%5)) {
Howard Chu's avatar
Howard Chu committed
88
			j++;
Howard Chu's avatar
Howard Chu committed
89
			txn=NULL;
90
			E(mdb_txn_begin(env, NULL, 0, &txn));
Howard Chu's avatar
Howard Chu committed
91
			sprintf(sval, "%03x ", values[i]);
92
			if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, NULL))) {
Howard Chu's avatar
Howard Chu committed
93
94
95
				j--;
				mdb_txn_abort(txn);
			} else {
96
				E(mdb_txn_commit(txn));
Howard Chu's avatar
Howard Chu committed
97
			}
Howard Chu's avatar
Howard Chu committed
98
99
	    }
	    free(values);
Howard Chu's avatar
Howard Chu committed
100
101
		printf("Deleted %d values\n", j);

102
		E(mdb_env_stat(env, &mst));
103
		E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
104
		E(mdb_cursor_open(txn, dbi, &cursor));
Howard Chu's avatar
Howard Chu committed
105
		printf("Cursor next\n");
106
		while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
Howard Chu's avatar
Howard Chu committed
107
			printf("key: %.*s, data: %.*s\n",
108
109
				(int) key.mv_size,  (char *) key.mv_data,
				(int) data.mv_size, (char *) data.mv_data);
Howard Chu's avatar
Howard Chu committed
110
		}
111
		CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
Howard Chu's avatar
Howard Chu committed
112
		printf("Cursor last\n");
113
		E(mdb_cursor_get(cursor, &key, &data, MDB_LAST));
Howard Chu's avatar
Howard Chu committed
114
115
116
		printf("key: %.*s, data: %.*s\n",
			(int) key.mv_size,  (char *) key.mv_data,
			(int) data.mv_size, (char *) data.mv_data);
Howard Chu's avatar
Howard Chu committed
117
118
119
120
121
122
		printf("Cursor prev\n");
		while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_PREV)) == 0) {
			printf("key: %.*s, data: %.*s\n",
				(int) key.mv_size,  (char *) key.mv_data,
				(int) data.mv_size, (char *) data.mv_data);
		}
123
		CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
Howard Chu's avatar
Howard Chu committed
124
		printf("Cursor last/prev\n");
125
		E(mdb_cursor_get(cursor, &key, &data, MDB_LAST));
Howard Chu's avatar
Howard Chu committed
126
127
128
			printf("key: %.*s, data: %.*s\n",
				(int) key.mv_size,  (char *) key.mv_data,
				(int) data.mv_size, (char *) data.mv_data);
129
		E(mdb_cursor_get(cursor, &key, &data, MDB_PREV));
Howard Chu's avatar
Howard Chu committed
130
131
132
133
			printf("key: %.*s, data: %.*s\n",
				(int) key.mv_size,  (char *) key.mv_data,
				(int) data.mv_size, (char *) data.mv_data);

134
		mdb_cursor_close(cursor);
Howard Chu's avatar
Howard Chu committed
135
136
		mdb_txn_abort(txn);

Howard Chu's avatar
Howard Chu committed
137
		printf("Deleting with cursor\n");
138
139
		E(mdb_txn_begin(env, NULL, 0, &txn));
		E(mdb_cursor_open(txn, dbi, &cur2));
Howard Chu's avatar
Howard Chu committed
140
		for (i=0; i<50; i++) {
141
			if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, MDB_NEXT)))
Howard Chu's avatar
Howard Chu committed
142
				break;
Howard Chu's avatar
Howard Chu committed
143
			printf("key: %p %.*s, data: %p %.*s\n",
144
145
				key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
				data.mv_data, (int) data.mv_size, (char *) data.mv_data);
146
			E(mdb_del(txn, dbi, &key, NULL));
Howard Chu's avatar
Howard Chu committed
147
148
149
		}

		printf("Restarting cursor in txn\n");
150
151
		for (op=MDB_FIRST, i=0; i<=32; op=MDB_NEXT, i++) {
			if (RES(MDB_NOTFOUND, mdb_cursor_get(cur2, &key, &data, op)))
152
				break;
Howard Chu's avatar
Howard Chu committed
153
			printf("key: %p %.*s, data: %p %.*s\n",
154
155
				key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
				data.mv_data, (int) data.mv_size, (char *) data.mv_data);
Howard Chu's avatar
Howard Chu committed
156
157
		}
		mdb_cursor_close(cur2);
158
		E(mdb_txn_commit(txn));
Howard Chu's avatar
Howard Chu committed
159

Howard Chu's avatar
Howard Chu committed
160
		printf("Restarting cursor outside txn\n");
161
162
		E(mdb_txn_begin(env, NULL, 0, &txn));
		E(mdb_cursor_open(txn, dbi, &cursor));
163
164
		for (op=MDB_FIRST, i=0; i<=32; op=MDB_NEXT, i++) {
			if (RES(MDB_NOTFOUND, mdb_cursor_get(cursor, &key, &data, op)))
165
				break;
Howard Chu's avatar
Howard Chu committed
166
			printf("key: %p %.*s, data: %p %.*s\n",
167
168
				key.mv_data,  (int) key.mv_size,  (char *) key.mv_data,
				data.mv_data, (int) data.mv_size, (char *) data.mv_data);
Howard Chu's avatar
Howard Chu committed
169
		}
Howard Chu's avatar
Howard Chu committed
170
		mdb_cursor_close(cursor);
Howard Chu's avatar
Howard Chu committed
171
		mdb_txn_abort(txn);
172
173

		mdb_dbi_close(env, dbi);
Howard Chu's avatar
Howard Chu committed
174
		mdb_env_close(env);
Howard Chu's avatar
Howard Chu committed
175
176
177

	return 0;
}