mtest6.c 3.86 KB
Newer Older
1
2
/* mtest6.c - memory-mapped database tester/toy */
/*
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
3
 * Copyright 2011-2020 Howard Chu, Symas Corp.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 * 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>.
 */

/* Tests for DB splits and merges */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Howard Chu's avatar
Howard Chu committed
20
#include "lmdb.h"
21

22
23
24
25
26
#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()))

27
28
29
30
31
32
33
char dkbuf[1024];

int main(int argc,char * argv[])
{
	int i = 0, j = 0, rc;
	MDB_env *env;
	MDB_dbi dbi;
34
	MDB_val key, data, sdata;
35
36
37
38
39
40
41
42
	MDB_txn *txn;
	MDB_stat mst;
	MDB_cursor *cursor;
	int count;
	int *values;
	long kval;
	char *sval;

43
	srand(time(NULL));
44

45
46
47
48
	E(mdb_env_create(&env));
	E(mdb_env_set_mapsize(env, 10485760));
	E(mdb_env_set_maxdbs(env, 4));
	E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664));
49

50
	E(mdb_txn_begin(env, NULL, 0, &txn));
51
	E(mdb_dbi_open(txn, "id6", MDB_CREATE|MDB_INTEGERKEY, &dbi));
52
53
	E(mdb_cursor_open(txn, dbi, &cursor));
	E(mdb_stat(txn, dbi, &mst));
54
55
56
57

	sval = calloc(1, mst.ms_psize / 4);
	key.mv_size = sizeof(long);
	key.mv_data = &kval;
58
59
	sdata.mv_size = mst.ms_psize / 4 - 30;
	sdata.mv_data = sval;
60
61
62
63
64

	printf("Adding 12 values, should yield 3 splits\n");
	for (i=0;i<12;i++) {
		kval = i*5;
		sprintf(sval, "%08x", kval);
65
		data = sdata;
66
		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
67
68
69
70
71
	}
	printf("Adding 12 more values, should yield 3 splits\n");
	for (i=0;i<12;i++) {
		kval = i*5+4;
		sprintf(sval, "%08x", kval);
72
		data = sdata;
73
		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
74
75
76
77
78
	}
	printf("Adding 12 more values, should yield 3 splits\n");
	for (i=0;i<12;i++) {
		kval = i*5+1;
		sprintf(sval, "%08x", kval);
79
		data = sdata;
80
		(void)RES(MDB_KEYEXIST, mdb_cursor_put(cursor, &key, &data, MDB_NOOVERWRITE));
81
	}
82
	E(mdb_cursor_get(cursor, &key, &data, MDB_FIRST));
83
84
85
86
87
88

	do {
		printf("key: %p %s, data: %p %.*s\n",
			key.mv_data,  mdb_dkey(&key, dkbuf),
			data.mv_data, (int) data.mv_size, (char *) data.mv_data);
	} while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0);
89
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
90
91
92
93
94
95
	mdb_cursor_close(cursor);
	mdb_txn_commit(txn);

#if 0
	j=0;

96
	for (i= count - 1; i > -1; i-= (rand()%5)) {
97
98
		j++;
		txn=NULL;
99
		E(mdb_txn_begin(env, NULL, 0, &txn));
100
101
102
103
104
105
		sprintf(kval, "%03x", values[i & ~0x0f]);
		sprintf(sval, "%03x %d foo bar", values[i], values[i]);
		key.mv_size = sizeof(int);
		key.mv_data = kval;
		data.mv_size = sizeof(sval);
		data.mv_data = sval;
106
		if (RES(MDB_NOTFOUND, mdb_del(txn, dbi, &key, &data))) {
107
108
109
			j--;
			mdb_txn_abort(txn);
		} else {
110
			E(mdb_txn_commit(txn));
111
112
113
114
115
		}
	}
	free(values);
	printf("Deleted %d values\n", j);

116
	E(mdb_env_stat(env, &mst));
117
	E(mdb_txn_begin(env, NULL, MDB_RDONLY, &txn));
118
	E(mdb_cursor_open(txn, dbi, &cursor));
119
120
121
122
123
124
	printf("Cursor next\n");
	while ((rc = mdb_cursor_get(cursor, &key, &data, MDB_NEXT)) == 0) {
		printf("key: %.*s, data: %.*s\n",
			(int) key.mv_size,  (char *) key.mv_data,
			(int) data.mv_size, (char *) data.mv_data);
	}
125
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
126
127
128
129
130
131
	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);
	}
132
	CHECK(rc == MDB_NOTFOUND, "mdb_cursor_get");
133
134
	mdb_cursor_close(cursor);
	mdb_txn_abort(txn);
135
136

	mdb_dbi_close(env, dbi);
137
138
139
140
141
#endif
	mdb_env_close(env);

	return 0;
}