summaryrefslogtreecommitdiff
path: root/lib/ntdb/test/run-03-coalesce.c
blob: 22a681788116a4a93c051b7b98fe43961066d801 (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
#include "ntdb-source.h"
#include "tap-interface.h"
#include "logging.h"
#include "layout.h"

static ntdb_len_t free_record_length(struct ntdb_context *ntdb, ntdb_off_t off)
{
	struct ntdb_free_record f;
	enum NTDB_ERROR ecode;

	ecode = ntdb_read_convert(ntdb, off, &f, sizeof(f));
	if (ecode != NTDB_SUCCESS)
		return ecode;
	if (frec_magic(&f) != NTDB_FREE_MAGIC)
		return NTDB_ERR_CORRUPT;
	return frec_len(&f);
}

int main(int argc, char *argv[])
{
	ntdb_off_t b_off, test;
	struct ntdb_context *ntdb;
	struct ntdb_layout *layout;
	NTDB_DATA data, key;
	ntdb_len_t len;

	/* FIXME: Test NTDB_CONVERT */
	/* FIXME: Test lock order fail. */

	plan_tests(42);
	data = ntdb_mkdata("world", 5);
	key = ntdb_mkdata("hello", 5);

	/* No coalescing can be done due to EOF */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	len = 56544;
	ntdb_layout_add_free(layout, len, 0);
	ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
	/* NOMMAP is for lockcheck. */
	ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
		       &tap_log_attr);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);

	/* Figure out which bucket free entry is. */
	b_off = bucket_off(ntdb->ftable_off, size_to_bucket(len));
	/* Lock and fail to coalesce. */
	ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
	test = layout->elem[1].base.off;
	ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, len, &test)
	    == 0);
	ntdb_unlock_free_bucket(ntdb, b_off);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == len);
	ok1(test == layout->elem[1].base.off);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);

	/* No coalescing can be done due to used record */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	ntdb_layout_add_free(layout, 56512, 0);
	ntdb_layout_add_used(layout, key, data, 6);
	ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
	/* NOMMAP is for lockcheck. */
	ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
		       &tap_log_attr);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == 56512);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);

	/* Figure out which bucket free entry is. */
	b_off = bucket_off(ntdb->ftable_off, size_to_bucket(56512));
	/* Lock and fail to coalesce. */
	ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
	test = layout->elem[1].base.off;
	ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 56512, &test)
	    == 0);
	ntdb_unlock_free_bucket(ntdb, b_off);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == 56512);
	ok1(test == layout->elem[1].base.off);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);

	/* Coalescing can be done due to two free records, then EOF */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	ntdb_layout_add_free(layout, 1024, 0);
	ntdb_layout_add_free(layout, 55504, 0);
	ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
	/* NOMMAP is for lockcheck. */
	ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
		       &tap_log_attr);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
	ok1(free_record_length(ntdb, layout->elem[2].base.off) == 55504);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);

	/* Figure out which bucket (first) free entry is. */
	b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
	/* Lock and coalesce. */
	ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
	test = layout->elem[2].base.off;
	ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
	    == 1024 + sizeof(struct ntdb_used_record) + 55504);
	/* Should tell us it's erased this one... */
	ok1(test == NTDB_ERR_NOEXIST);
	ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
	ok1(free_record_length(ntdb, layout->elem[1].base.off)
	    == 1024 + sizeof(struct ntdb_used_record) + 55504);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);

	/* Coalescing can be done due to two free records, then data */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	ntdb_layout_add_free(layout, 1024, 0);
	ntdb_layout_add_free(layout, 55472, 0);
	ntdb_layout_add_used(layout, key, data, 6);
	ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
	/* NOMMAP is for lockcheck. */
	ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
		       &tap_log_attr);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
	ok1(free_record_length(ntdb, layout->elem[2].base.off) == 55472);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);

	/* Figure out which bucket free entry is. */
	b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
	/* Lock and coalesce. */
	ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
	test = layout->elem[2].base.off;
	ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
	    == 1024 + sizeof(struct ntdb_used_record) + 55472);
	ok1(ntdb->file->allrecord_lock.count == 0 && ntdb->file->num_lockrecs == 0);
	ok1(free_record_length(ntdb, layout->elem[1].base.off)
	    == 1024 + sizeof(struct ntdb_used_record) + 55472);
	ok1(test == NTDB_ERR_NOEXIST);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);

	/* Coalescing can be done due to three free records, then EOF */
	layout = new_ntdb_layout();
	ntdb_layout_add_freetable(layout);
	ntdb_layout_add_free(layout, 1024, 0);
	ntdb_layout_add_free(layout, 512, 0);
	ntdb_layout_add_free(layout, 54976, 0);
	ntdb_layout_write(layout, free, &tap_log_attr, "run-03-coalesce.ntdb");
	/* NOMMAP is for lockcheck. */
	ntdb = ntdb_open("run-03-coalesce.ntdb", NTDB_NOMMAP, O_RDWR, 0,
		       &tap_log_attr);
	ok1(free_record_length(ntdb, layout->elem[1].base.off) == 1024);
	ok1(free_record_length(ntdb, layout->elem[2].base.off) == 512);
	ok1(free_record_length(ntdb, layout->elem[3].base.off) == 54976);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);

	/* Figure out which bucket free entry is. */
	b_off = bucket_off(ntdb->ftable_off, size_to_bucket(1024));
	/* Lock and coalesce. */
	ok1(ntdb_lock_free_bucket(ntdb, b_off, NTDB_LOCK_WAIT) == 0);
	test = layout->elem[2].base.off;
	ok1(coalesce(ntdb, layout->elem[1].base.off, b_off, 1024, &test)
	    == 1024 + sizeof(struct ntdb_used_record) + 512
	    + sizeof(struct ntdb_used_record) + 54976);
	ok1(ntdb->file->allrecord_lock.count == 0
	    && ntdb->file->num_lockrecs == 0);
	ok1(free_record_length(ntdb, layout->elem[1].base.off)
	    == 1024 + sizeof(struct ntdb_used_record) + 512
	    + sizeof(struct ntdb_used_record) + 54976);
	ok1(ntdb_check(ntdb, NULL, NULL) == 0);
	ntdb_close(ntdb);
	ntdb_layout_free(layout);

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