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