summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/run-04-basichash.c
blob: 9888f6e551db6511955b97cb6cc564afab73aa98 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "ntdb-source.h"
#include "tap-interface.h"
#include "logging.h"

/* We rig the hash so all records clash. */
static uint32_t clash(const void *key, size_t len, uint32_t seed, void *priv)
{
	return *((const unsigned int *)key) << 20;
}

int main(int argc, char *argv[])
{
	unsigned int i;
	struct ntdb_context *ntdb;
	unsigned int v;
	struct ntdb_used_record rec;
	NTDB_DATA key = { (unsigned char *)&v, sizeof(v) };
	NTDB_DATA dbuf = { (unsigned char *)&v, sizeof(v) };
	union ntdb_attribute hattr = { .hash = { .base = { NTDB_ATTRIBUTE_HASH },
						.fn = clash } };
	int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
			NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
			NTDB_NOMMAP|NTDB_CONVERT,
	};

	hattr.base.next = &tap_log_attr;

	plan_tests(sizeof(flags) / sizeof(flags[0]) * 137 + 1);
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		struct hash_info h;
		ntdb_off_t new_off, new_off2, off;

		ntdb = ntdb_open("run-04-basichash.ntdb", flags[i]|MAYBE_NOSYNC,
				 O_RDWR|O_CREAT|O_TRUNC, 0600, &hattr);
		ok1(ntdb);
		if (!ntdb)
			continue;

		v = 0;
		/* Should not find it. */
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in top table, bucket 0. */
		ok1(h.table == NTDB_HASH_OFFSET);
		ok1(h.table_size == (1 << ntdb->hash_bits));
		ok1(h.bucket == 0);
		ok1(h.old_val == 0);

		/* Should have lock on bucket 0 */
		ok1(h.h == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		/* FIXME: Check lock length */

		/* Allocate a new record. */
		new_off = alloc(ntdb, key.dsize, dbuf.dsize,
				NTDB_USED_MAGIC, false);
		ok1(!NTDB_OFF_IS_ERR(new_off));

		/* We should be able to add it now. */
		ok1(add_to_hash(ntdb, &h, new_off) == 0);

		/* Make sure we fill it in for later finding. */
		off = new_off + sizeof(struct ntdb_used_record);
		ok1(!ntdb->io->twrite(ntdb, off, key.dptr, key.dsize));
		off += key.dsize;
		ok1(!ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize));

		/* We should be able to unlock that OK. */
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Database should be consistent. */
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Now, this should give a successful lookup. */
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located it in top table, bucket 0. */
		ok1(h.table == NTDB_HASH_OFFSET);
		ok1(h.table_size == (1 << ntdb->hash_bits));
		ok1(h.bucket == 0);

		/* Should have lock on bucket 0 */
		ok1(h.h == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		/* FIXME: Check lock length */

		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Database should be consistent. */
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Test expansion. */
		v = 1;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located clash in toplevel bucket 0. */
		ok1(h.table == NTDB_HASH_OFFSET);
		ok1(h.table_size == (1 << ntdb->hash_bits));
		ok1(h.bucket == 0);
		ok1((h.old_val & NTDB_OFF_MASK) == new_off);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		/* FIXME: Check lock length */

		new_off2 = alloc(ntdb, key.dsize, dbuf.dsize,
				 NTDB_USED_MAGIC, false);
		ok1(!NTDB_OFF_IS_ERR(new_off2));

		off = new_off2 + sizeof(struct ntdb_used_record);
		ok1(!ntdb->io->twrite(ntdb, off, key.dptr, key.dsize));
		off += key.dsize;
		ok1(!ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize));

		/* We should be able to add it now. */
		ok1(add_to_hash(ntdb, &h, new_off2) == 0);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Should be happy with expansion. */
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Should be able to find both. */
		v = 1;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off2);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in chain. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 1);
		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		v = 0;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in chain. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 0);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		/* FIXME: Check lock length */

		/* Simple delete should work. */
		ok1(delete_from_hash(ntdb, &h) == 0);
		ok1(add_free_record(ntdb, new_off,
				    sizeof(struct ntdb_used_record)
				    + rec_key_length(&rec)
				    + rec_data_length(&rec)
				    + rec_extra_padding(&rec),
				    NTDB_LOCK_NOWAIT, false) == 0);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);
		ok1(ntdb_check(ntdb, NULL, NULL) == 0);

		/* Should still be able to find other record. */
		v = 1;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off2);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in chain. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 1);
		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Now should find empty space. */
		v = 0;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in chain, bucket 0. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 0);
		ok1(h.old_val == 0);

		/* Adding another record should work. */
		v = 2;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have located space in chain, bucket 0. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 0);
		ok1(h.old_val == 0);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);

		new_off = alloc(ntdb, key.dsize, dbuf.dsize,
				NTDB_USED_MAGIC, false);
		ok1(!NTDB_OFF_IS_ERR(new_off2));
		ok1(add_to_hash(ntdb, &h, new_off) == 0);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		off = new_off + sizeof(struct ntdb_used_record);
		ok1(!ntdb->io->twrite(ntdb, off, key.dptr, key.dsize));
		off += key.dsize;
		ok1(!ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize));

		/* Adding another record should cause expansion. */
		v = 3;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should not have located space in chain. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 2);
		ok1(h.bucket == 2);
		ok1(h.old_val != 0);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);

		new_off = alloc(ntdb, key.dsize, dbuf.dsize,
				NTDB_USED_MAGIC, false);
		ok1(!NTDB_OFF_IS_ERR(new_off2));
		off = new_off + sizeof(struct ntdb_used_record);
		ok1(!ntdb->io->twrite(ntdb, off, key.dptr, key.dsize));
		off += key.dsize;
		ok1(!ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize));
		ok1(add_to_hash(ntdb, &h, new_off) == 0);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Retrieve it and check. */
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have appended to chain, bucket 2. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 3);
		ok1(h.bucket == 2);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* YA record: relocation. */
		v = 4;
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == 0);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should not have located space in chain. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 3);
		ok1(h.bucket == 3);
		ok1(h.old_val != 0);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);

		new_off = alloc(ntdb, key.dsize, dbuf.dsize,
				NTDB_USED_MAGIC, false);
		ok1(!NTDB_OFF_IS_ERR(new_off2));
		off = new_off + sizeof(struct ntdb_used_record);
		ok1(!ntdb->io->twrite(ntdb, off, key.dptr, key.dsize));
		off += key.dsize;
		ok1(!ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize));
		ok1(add_to_hash(ntdb, &h, new_off) == 0);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		/* Retrieve it and check. */
		ok1(find_and_lock(ntdb, key, F_WRLCK, &h, &rec, NULL) == new_off);
		/* Should have created correct hash. */
		ok1(h.h == ntdb_hash(ntdb, key.dptr, key.dsize));
		/* Should have appended to chain, bucket 2. */
		ok1(h.table > NTDB_HASH_OFFSET);
		ok1(h.table_size == 4);
		ok1(h.bucket == 3);

		/* Should have lock on bucket 0 */
		ok1((h.h & ((1 << ntdb->hash_bits)-1)) == 0);
		ok1((ntdb->flags & NTDB_NOLOCK) || ntdb->file->num_lockrecs == 1);
		ok1((ntdb->flags & NTDB_NOLOCK)
		    || ntdb->file->lockrecs[0].off == NTDB_HASH_LOCK_START);
		ok1(ntdb_unlock_hash(ntdb, h.h, F_WRLCK) == 0);

		ntdb_close(ntdb);
	}

	ok1(tap_log_messages == 0);
	return exit_status();
}