summaryrefslogtreecommitdiffstats
path: root/src/test-storage-mmap.c
blob: e5ab7367d2e6f563a9eff904f4c85ff12fe2da63 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include "test.h"
#include "storage.h"


#define FILENAME "/tmp/tsnif-storage-mmap.tsnif"

int test_storage_mmap_init(void)
{
	int rc = 0;
	struct stat st;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_header_mmap *header;
	struct tsnif_storage_opts opts = {
		.flags     = TSNIF_STORAGE_OPT_WRITE | TSNIF_STORAGE_OPT_WRITE,
		.size_max  = 1024*1024,
		.term_type = 1,
	};

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	TEST_ASSERT(handle.opts == &opts, out_free);
	TEST_ASSERT(handle.opts->chunk_size == sysconf(_SC_PAGESIZE), out_free);
	TEST_ASSERT(handle.fd != 0, out_free);
	TEST_ASSERT(handle.header != NULL, out_free);

	/* header check before when creating new file */
	header = handle.header;
	TEST_ASSERT(header->common.magic == TSNIF_HEADER_MAGIC, out_free);
	TEST_ASSERT(header->common.storage_type == TSNIF_STORAGE_TYPE_MMAP, out_free);
	TEST_ASSERT(header->common.term_type == 1, out_free);
	TEST_ASSERT(header->common.version == TSNIF_STORAGE_VERSION, out_free);
	TEST_ASSERT(header->size_max == opts.size_max, out_free);

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);
	TEST_ASSERT(0 == stat(FILENAME, &st), out);
	TEST_ASSERT(st.st_size == sysconf(_SC_PAGESIZE), out);

out:
	unlink(FILENAME);
	return rc;
}

int test_storage_mmap_open(void)
{
	int rc = 0;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_header_mmap *header;
	struct tsnif_storage_opts opts = {
		.flags     = TSNIF_STORAGE_OPT_WRITE,
		.size_max  = 1024*1024,
		.term_type = 1,
	};

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

	opts.flags = TSNIF_STORAGE_OPT_READ;
	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	/* header check before when opening file */
	header = handle.header;
	TEST_ASSERT(header->common.magic == TSNIF_HEADER_MAGIC, out_free);
	TEST_ASSERT(header->common.storage_type == TSNIF_STORAGE_TYPE_MMAP, out_free);
	TEST_ASSERT(header->common.term_type == 1, out_free);
	TEST_ASSERT(header->common.version == TSNIF_STORAGE_VERSION, out_free);
	TEST_ASSERT(header->size_max == opts.size_max, out_free);
	TEST_ASSERT(tsnif_storage_term_type(&handle) == 1, out_free);

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

out:
	unlink(FILENAME);
	return rc;
}

static int get_rec(struct tsnif_storage_rec *rec, char *msg, int i)
{
	sprintf(msg, "krava-%i-", i);
	memset(rec, 0x0, sizeof(rec));
	rec->ptr = msg;
	rec->len = strlen(msg);
	return 0;
}

int test_storage_mmap_write_single_chunk(void)
{
	int rc = 0, i;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_opts opts = {
		.flags    = TSNIF_STORAGE_OPT_WRITE | TSNIF_STORAGE_OPT_WRITE,
		.size_max = 1024*1024,
	};

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	for(i = 0; i < 10; i++) {
		struct tsnif_storage_chunk *chunk = &handle.active_chunk;
		struct tsnif_storage_chunk_header *header;
		struct tsnif_storage_rec_mmap *rec_mmap;
		uint32_t *index;
		off_t offset;
		struct tsnif_storage_rec rec;
		char msg[15];
		int free;

		get_rec(&rec, msg, i);

		TEST_ASSERT(0 == tsnif_storage_write(&handle, &rec), out_free);
		TEST_ASSERT(chunk->header, out_free);

		/* check chunk properties */
		header = chunk->header;
		free = opts.chunk_size -
			/* chunk header */
			sizeof(struct tsnif_storage_chunk_header) -
			(i + 1) *
			(
				/* record header */
				sizeof(struct tsnif_storage_rec_mmap) +
				/* record data lenght */
				rec.len +
				/* index item */
				sizeof(uint32_t)
			);

		TEST_ASSERT(header->cnt  == i + 1, out_free);
		TEST_ASSERT(header->free == free, out_free);

		/* find current record and check it */
		index = chunk->current_index;
		offset = *(++index);
		rec_mmap = (void*) chunk->header + offset;

		TEST_ASSERT(rec_mmap->len == rec.len, out_free);
		TEST_ASSERT(!memcmp(rec_mmap->data, rec.ptr, rec.len), out_free);
	}

	TEST_ASSERT(handle.header->offset_start == sysconf(_SC_PAGESIZE), out_free);
	TEST_ASSERT(handle.header->cnt == 10, out_free);

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

out:
	unlink(FILENAME);
	return rc;
}

int test_storage_mmap_write_multi_chunks(void)
{
	int rc = 0, i = 0;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_chunk *chunk = &handle.active_chunk;
	struct tsnif_storage_chunk_header *header;
	struct tsnif_storage_opts opts = {
		.flags    = TSNIF_STORAGE_OPT_WRITE | TSNIF_STORAGE_OPT_WRITE,
		.size_max = 1024*1024,
	};
	struct tsnif_storage_rec rec;
	char msg[15];

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	get_rec(&rec, msg, i++);

	if (tsnif_storage_write(&handle, &rec))
		TEST_ASSERT(1, out_free);

	header = chunk->header;
	TEST_ASSERT(header, out_free);

	do {
		off_t offset_old, offset_new;

		int free_threshold = (
				/* record header */
				sizeof(struct tsnif_storage_rec_mmap) +
				/* record data */
				rec.len +
				/* safety overhead */
				+ (4*sizeof(uint32_t)));

		get_rec(&rec, msg, i++);

		/* get to the chunk end */
		if (header->free > free_threshold) {
			if (tsnif_storage_write(&handle, &rec))
				TEST_ASSERT(1, out_free);
			continue;
		}

		offset_old = chunk->offset;

		TEST_ASSERT(0 == tsnif_storage_write(&handle, &rec), out_free);

		offset_new = chunk->offset;
		TEST_ASSERT(offset_new == (offset_old + opts.chunk_size), out_free);
		break;

	} while(1);

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

out:
	unlink(FILENAME);
	return rc;
}

int test_storage_mmap_read_single_chunk(void)
{
	int rc = 0, i = 0;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_opts opts = {
		.flags    = TSNIF_STORAGE_OPT_WRITE | TSNIF_STORAGE_OPT_WRITE,
		.size_max = 1024*1024,
	};
	struct tsnif_storage_rec rec;
	char msg[15];

	if (tsnif_storage_init(&handle, &opts, FILENAME))
		TEST_ASSERT(1, out);

	for(i = 0; i < 10; i++) {
		get_rec(&rec, msg, i);
		if (tsnif_storage_write(&handle, &rec))
			TEST_ASSERT(1, out_free);
	}

	if (tsnif_storage_close(&handle))
		TEST_ASSERT(1, out);

	/* READ mode */
	opts.flags = TSNIF_STORAGE_OPT_READ | TSNIF_STORAGE_OPT_READ;
	opts.size_max = 0;

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	for(i = 0; i < 10; i++) {
		struct tsnif_storage_rec rec_read;

		get_rec(&rec, msg, i);

		TEST_ASSERT(0 == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_NEXT, &rec_read), out_free);

		TEST_ASSERT(rec.len   == rec_read.len, out_free);
		TEST_ASSERT(rec.flags == rec_read.flags, out_free);
		TEST_ASSERT(!memcmp(rec_read.ptr, rec.ptr, rec.len), out_free);
	}

	/* there should be no other record */
	TEST_ASSERT(TSNIF_STORAGE_READ_EOF == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_NEXT, &rec), out_free);

	for(i = 8; i >= 0; i--) {
		struct tsnif_storage_rec rec_read;

		get_rec(&rec, msg, i);

		TEST_ASSERT(0 == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_PREV, &rec_read), out_free);

#if 0
printf("krava:\n");
fwrite(rec_read.ptr, rec_read.len, 1, stdout);
fflush(NULL);
#endif
		TEST_ASSERT(rec.len   == rec_read.len, out_free);
		TEST_ASSERT(rec.flags == rec_read.flags, out_free);
		TEST_ASSERT(!memcmp(rec_read.ptr, rec.ptr, rec.len), out_free);
	}

	/* there should be no other record */
	TEST_ASSERT(TSNIF_STORAGE_READ_EOF == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_PREV, &rec), out_free);

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

out:
	unlink(FILENAME);
	return rc;
}

int test_storage_mmap_read_multi_chunks(void)
{
	int rc = 0, i = 0;
	struct tsnif_storage_handle handle;
	struct tsnif_storage_chunk *chunk = &handle.active_chunk;
	struct tsnif_storage_opts opts = {
		.flags    = TSNIF_STORAGE_OPT_WRITE | TSNIF_STORAGE_OPT_WRITE,
		.size_max = 1024*1024,
	};
	struct tsnif_storage_rec rec;
	char msg[15];
	int offset = 0;

	if (tsnif_storage_init(&handle, &opts, FILENAME))
		TEST_ASSERT(1, out_free);

	/* write whole chunk of data + one record in 2nd chunk */
	do {
		get_rec(&rec, msg, i++);

		if (tsnif_storage_write(&handle, &rec))
			TEST_ASSERT(1, out_free);

		if (!offset)
			offset = chunk->offset;

	} while(chunk->offset == offset);

	get_rec(&rec, msg, i++);
	if (tsnif_storage_write(&handle, &rec))
		TEST_ASSERT(1, out_free);

	if (tsnif_storage_close(&handle))
		TEST_ASSERT(1, out);

	opts.flags = TSNIF_STORAGE_OPT_READ | TSNIF_STORAGE_OPT_READ;
	opts.size_max = 0;

	TEST_ASSERT(0 == tsnif_storage_init(&handle, &opts, FILENAME), out);

	for(i = 0; i < handle.header->cnt; i++) {
		struct tsnif_storage_rec rec_read;

		get_rec(&rec, msg, i);
		TEST_ASSERT(0 == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_NEXT, &rec_read), out_free);

		TEST_ASSERT(rec.len   == rec_read.len, out_free);
		TEST_ASSERT(rec.flags == rec_read.flags, out_free);
		TEST_ASSERT(!memcmp(rec_read.ptr, rec.ptr, rec.len), out_free);
	}

	TEST_ASSERT(TSNIF_STORAGE_READ_EOF == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_NEXT, &rec), out_free);

	for(i = handle.header->cnt - 2; i >= 0; i--) {
		struct tsnif_storage_rec rec_read;

		get_rec(&rec, msg, i);
		TEST_ASSERT(0 == tsnif_storage_read(&handle, TSNIF_STORAGE_READ_PREV, &rec_read), out_free);

		TEST_ASSERT(rec.len   == rec_read.len, out_free);
		TEST_ASSERT(rec.flags == rec_read.flags, out_free);
		TEST_ASSERT(!memcmp(rec_read.ptr, rec.ptr, rec.len), out_free);
	}

out_free:
	TEST_ASSERT(0 == tsnif_storage_close(&handle), out);

out:
	unlink(FILENAME);
	return rc;
}