summaryrefslogtreecommitdiffstats
path: root/tests/libknot/test_db.c
blob: 49bd6e4f7547f1c589d3f2b1ad2919e55ac47e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <tap/basic.h>
#include <tap/files.h>

#include "contrib/string.h"
#include "libknot/libknot.h"
#include "contrib/mempattern.h"
#include "contrib/openbsd/strlcpy.h"
#include "contrib/ucw/mempool.h"

/* UCW array sorting defines. */
#define ASORT_PREFIX(X) str_key_##X
#define ASORT_KEY_TYPE char*
#define ASORT_LT(x, y) (strcmp((x), (y)) < 0)
#include "contrib/ucw/array-sort.h"

/* Constants. */
#define KEY_MAXLEN 64
#define KEY_SET(key, str) key.data = (str); key.len = strlen(str) + 1

/*! \brief Generate random key. */
static const char *alphabet = "abcdefghijklmn0123456789";
static char *str_key_rand(size_t len, knot_mm_t *pool)
{
	char *s = mm_alloc(pool, len);
	memset(s, 0, len);
	for (unsigned i = 0; i < len - 1; ++i) {
		s[i] = alphabet[rand() % strlen(alphabet)];
	}
	return s;
}

static void knot_db_test_set(unsigned nkeys, char **keys, void *opts,
                            const knot_db_api_t *api, knot_mm_t *pool)
{
	if (api == NULL) {
		skip("API not compiled in");
		return;
	}

	/* Create database */
	knot_db_t *db = NULL;
	int ret = api->init(&db, pool, opts);
	ok(ret == KNOT_EOK && db != NULL, "%s: create", api->name);

	/* Start WR transaction. */
	knot_db_txn_t txn;
	ret = api->txn_begin(db, &txn, 0);
	is_int(KNOT_EOK, ret, "%s: txn_begin(WR)", api->name);

	/* Insert keys */
	knot_db_val_t key, val;
	bool passed = true;
	for (unsigned i = 0; i < nkeys; ++i) {
		KEY_SET(key, keys[i]);
		val.len = sizeof(void*);
		val.data = &key.data;

		ret = api->insert(&txn, &key, &val, 0);
		if (ret != KNOT_EOK && ret != KNOT_EEXIST) {
			passed = false;
			break;
		}
	}
	ok(passed, "%s: insert", api->name);

	/* Commit WR transaction. */
	ret = api->txn_commit(&txn);
	is_int(KNOT_EOK, ret, "%s: txn_commit(WR)", api->name);

	/* Start RD transaction. */
	ret = api->txn_begin(db, &txn, KNOT_DB_RDONLY);
	is_int(KNOT_EOK, ret, "%s: txn_begin(RD)", api->name);

	/* Lookup all keys */
	passed = true;
	for (unsigned i = 0; i < nkeys; ++i) {
		KEY_SET(key, keys[i]);

		ret = api->find(&txn, &key, &val, 0);
		if (ret != KNOT_EOK) {
			passed = false;
			break;
		}

		const char **stored_key = val.data;
		if (strcmp(*stored_key, keys[i]) != 0) {
			diag("%s: mismatch on element '%u'", api->name, i);
			passed = false;
			break;
		}
	}
	ok(passed, "%s: lookup all keys", api->name);

	/* Fetch dataset size. */
	int db_size = api->count(&txn);
	ok(db_size > 0 && db_size <= nkeys, "%s: count %d", api->name, db_size);

	/* Unsorted iteration */
	int iterated = 0;
	knot_db_iter_t *it = api->iter_begin(&txn, 0);
	while (it != NULL) {
		++iterated;
		it = api->iter_next(it);
	}
	api->iter_finish(it);
	is_int(db_size, iterated, "%s: unsorted iteration", api->name);

	/* Sorted iteration. */
	char first_key[KEY_MAXLEN] = { '\0' };
	char second_key[KEY_MAXLEN] = { '\0' };
	char last_key[KEY_MAXLEN] = { '\0' };
	char key_buf[KEY_MAXLEN] = {'\0'};
	iterated = 0;
	memset(&key, 0, sizeof(key));
	it = api->iter_begin(&txn, KNOT_DB_SORTED);
	while (it != NULL) {
		api->iter_key(it, &key);
		if (iterated > 0) { /* Only if previous exists. */
			if (strcmp(key_buf, key.data) > 0) {
				diag("%s: iter_sort '%s' <= '%s' FAIL\n",
				     api->name, key_buf, (const char *)key.data);
				break;
			}
			if (iterated == 1) {
				memcpy(second_key, key.data, key.len);
			}
		} else {
			memcpy(first_key, key.data, key.len);
		}
		++iterated;
		memcpy(key_buf, key.data, key.len);
		it = api->iter_next(it);
	}
	strlcpy(last_key, key_buf, sizeof(last_key));
	is_int(db_size, iterated, "%s: sorted iteration", api->name);
	api->iter_finish(it);

	/* Interactive iteration. */
	it = api->iter_begin(&txn, KNOT_DB_NOOP);
	if (it != NULL) { /* If supported. */
		ret = 0;
		/* Check if first and last keys are reachable */
		it = api->iter_seek(it, NULL, KNOT_DB_FIRST);
		ret += api->iter_key(it, &key);
		is_string(first_key, key.data, "%s: iter_set(FIRST)", api->name);
		/* Check left/right iteration. */
		it = api->iter_seek(it, &key, KNOT_DB_NEXT);
		ret += api->iter_key(it, &key);
		is_string(second_key, key.data, "%s: iter_set(NEXT)", api->name);
		it = api->iter_seek(it, &key, KNOT_DB_PREV);
		ret += api->iter_key(it, &key);
		is_string(first_key, key.data, "%s: iter_set(PREV)", api->name);
		it = api->iter_seek(it, &key, KNOT_DB_LAST);
		ret += api->iter_key(it, &key);
		is_string(last_key, key.data, "%s: iter_set(LAST)", api->name);
		/* Check if prev(last_key + 1) is the last_key */
		strlcpy(key_buf, last_key, sizeof(key_buf));
		key_buf[0] += 1;
		KEY_SET(key, key_buf);
		it = api->iter_seek(it, &key, KNOT_DB_LEQ);
		ret += api->iter_key(it, &key);
		is_string(last_key, key.data, "%s: iter_set(LEQ)", api->name);
		/* Check if next(first_key - 1) is the first_key */
		strlcpy(key_buf, first_key, sizeof(key_buf));
		key_buf[0] -= 1;
		KEY_SET(key, key_buf);
		it = api->iter_seek(it, &key, KNOT_DB_GEQ);
		ret += api->iter_key(it, &key);
		is_string(first_key, key.data, "%s: iter_set(GEQ)", api->name);
		api->iter_finish(it);
		is_int(ret, 0, "%s: iter_* error codes", api->name);
	}
	api->txn_abort(&txn);

	/* Deleting during iteration. */
	const uint8_t DEL_MAX_CNT = 3;
	api->txn_begin(db, &txn, 0);
	api->clear(&txn);
	for (uint8_t i = 0; i < DEL_MAX_CNT; ++i) {
		key.data = &i;
		key.len = sizeof(i);
		val.data = NULL;
		val.len = 0;

		ret = api->insert(&txn, &key, &val, 0);
		is_int(KNOT_EOK, ret, "%s: add key '%u'", api->name, i);
	}
	it = api->iter_begin(&txn, KNOT_DB_NOOP);
	if (it != NULL) { /* If supported. */
		is_int(DEL_MAX_CNT, api->count(&txn), "%s: key count before", api->name);
		it = api->iter_seek(it, NULL, KNOT_DB_FIRST);
		uint8_t pos = 0;
		while (it != NULL) {
			ret = api->iter_key(it, &key);
			is_int(KNOT_EOK, ret, "%s: iter key before del", api->name);
			is_int(pos, ((uint8_t *)(key.data))[0], "%s: iter compare key '%u'",
			       api->name, pos);

			ret = knot_db_lmdb_iter_del(it);
			is_int(KNOT_EOK, ret, "%s: iter del", api->name);

			it = api->iter_next(it);

			ret = api->iter_key(it, &key);
			if (++pos < DEL_MAX_CNT) {
				is_int(KNOT_EOK, ret, "%s: iter key after del", api->name);
				is_int(pos, ((uint8_t *)key.data)[0], "%s: iter compare key '%u",
				       api->name, pos);
			} else {
				is_int(KNOT_EINVAL, ret, "%s: iter key after del", api->name);
			}
		}
		api->iter_finish(it);
		is_int(0, api->count(&txn), "%s: key count after", api->name);
	}
	api->txn_abort(&txn);

	/* Clear database and recheck. */
	ret =  api->txn_begin(db, &txn, 0);
	ret += api->clear(&txn);
	ret += api->txn_commit(&txn);
	is_int(0, ret, "%s: clear()", api->name);

	/* Check if the database is empty. */
	api->txn_begin(db, &txn, KNOT_DB_RDONLY);
	db_size = api->count(&txn);
	is_int(0, db_size, "%s: count after clear = %d", api->name, db_size);
	api->txn_abort(&txn);

	api->deinit(db);
}

int main(int argc, char *argv[])
{
	plan_lazy();

	knot_mm_t pool;
	mm_ctx_mempool(&pool, MM_DEFAULT_BLKSIZE);

	char *dbid = test_mkdtemp();
	ok(dbid != NULL, "make temporary directory");

	/* Random keys. */
	unsigned nkeys = 10000;
	char **keys = mm_alloc(&pool, sizeof(char*) * nkeys);
	for (unsigned i = 0; i < nkeys; ++i) {
		keys[i] = str_key_rand(KEY_MAXLEN, &pool);
	}

	/* Sort random keys. */
	str_key_sort(keys, nkeys);

	/* Execute test set for all backends. */
	struct knot_db_lmdb_opts lmdb_opts = KNOT_DB_LMDB_OPTS_INITIALIZER;
	lmdb_opts.path = dbid;
	struct knot_db_trie_opts trie_opts = KNOT_DB_TRIE_OPTS_INITIALIZER;
	knot_db_test_set(nkeys, keys, &lmdb_opts, knot_db_lmdb_api(), &pool);
	knot_db_test_set(nkeys, keys, &trie_opts, knot_db_trie_api(), &pool);

	/* Cleanup. */
	mp_delete(pool.ctx);
	test_rm_rf(dbid);
	free(dbid);

	return 0;
}