int test_sky_data_file_load_empty() { cleantmp(); int rc; sky_data_file *data_file = sky_data_file_create(); data_file->block_size = 128; data_file->path = bfromcstr("tmp/data"); data_file->header_path = bfromcstr("tmp/header"); rc = sky_data_file_load(data_file); mu_assert_int_equals(rc, 0); mu_assert_bool(data_file->data != NULL); mu_assert_bool(data_file->data_fd != 0); mu_assert_long_equals(data_file->data_length, 128L); mu_assert_bool(data_file->blocks != NULL); mu_assert_int_equals(data_file->block_count, 1); mu_assert_file("tmp/data", "tests/fixtures/data_files/0/data"); mu_assert_file("tmp/header", "tests/fixtures/data_files/0/header"); rc = sky_data_file_unload(data_file); mu_assert_int_equals(rc, 0); mu_assert_bool(data_file->data == NULL); mu_assert_bool(data_file->data_fd == 0); mu_assert_long_equals(data_file->data_length, 0L); mu_assert_bool(data_file->blocks == NULL); mu_assert_int_equals(data_file->block_count, 0); sky_data_file_free(data_file); return 0; }
int test_sky_block_get_span_count() { int rc; uint32_t count; sky_data_file *data_file = sky_data_file_create(); data_file->block_count = 5; data_file->blocks = malloc(sizeof(sky_block*) * data_file->block_count); data_file->blocks[0] = create_block(data_file, 0, 10LL, 11LL, false); data_file->blocks[1] = create_block(data_file, 1, 20LL, 20LL, true); data_file->blocks[2] = create_block(data_file, 2, 20LL, 20LL, true); data_file->blocks[3] = create_block(data_file, 3, 30LL, 30LL, true); data_file->blocks[4] = create_block(data_file, 4, 30LL, 30LL, true); // Unspanned blocks should return 1. rc = sky_block_get_span_count(data_file->blocks[0], &count); mu_assert_int_equals(rc, 0); mu_assert_int_equals(count, 1); // Test that spanned blocks return correct number. rc = sky_block_get_span_count(data_file->blocks[1], &count); mu_assert_int_equals(rc, 0); mu_assert_int_equals(count, 2); // Test that blocks at the end are returned correctly. rc = sky_block_get_span_count(data_file->blocks[3], &count); mu_assert_int_equals(rc, 0); mu_assert_int_equals(count, 2); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_large_event_to_new_ending_path_causing_block_split() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/2/e", 0); ADD_EVENT_WITH_DATA(15LL, 12LL, 20, 30, "1234567890123456789012345678901234567890123456789012345678901234"); ASSERT_DATA_FILE("tests/fixtures/data_files/2/k"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_small_event_to_new_ending_path_causing_block_split() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/2/e", 0); ADD_EVENT(15LL, 12LL, 20); ASSERT_DATA_FILE("tests/fixtures/data_files/2/i"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_event_to_new_starting_path_causing_block_split() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/2/a", 0); ADD_EVENT(2LL, 12LL, 20); ASSERT_DATA_FILE("tests/fixtures/data_files/2/d"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_event_with_prepending_path() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/1/b", 0); ADD_EVENT(2LL, 11LL, 22); ASSERT_DATA_FILE("tests/fixtures/data_files/1/e"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_append_event_to_existing_path() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/1/b", 0); ADD_EVENT(3LL, 11LL, 22); ASSERT_DATA_FILE("tests/fixtures/data_files/1/c"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_event_to_new_block() { sky_data_file *data_file; INIT_DATA_FILE("", 64); ADD_EVENT(3LL, 10LL, 20); ASSERT_DATA_FILE("tests/fixtures/data_files/1/a"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_set_header_path() { int rc; struct tagbstring path = bsStatic("/dev/null"); sky_data_file *data_file = sky_data_file_create(); rc = sky_data_file_set_path(data_file, &path); mu_assert_int_equals(rc, 0); mu_assert_bstring(data_file->path, "/dev/null"); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_event_to_end_of_ending_path_causing_block_span() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/spanning/a", 0); ADD_EVENT_WITH_DATA(10, 12LL, 20, 30, "12345678901234567890123456789"); ASSERT_DATA_FILE("tests/fixtures/data_files/spanning/j"); mu_assert_int_equals(data_file->block_count, 3); ASSERT_BLOCK(data_file, 0, 0, false); ASSERT_BLOCK(data_file, 1, 1, true); ASSERT_BLOCK(data_file, 2, 2, true); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_add_small_event_to_middle_of_starting_path_causing_block_span() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/spanning/a", 0); ADD_EVENT_WITH_DATA(3, 9LL, 20, 30, "1234567890"); ASSERT_DATA_FILE("tests/fixtures/data_files/spanning/c"); mu_assert_int_equals(data_file->block_count, 3); ASSERT_BLOCK(data_file, 0, 0, true); ASSERT_BLOCK(data_file, 1, 1, true); ASSERT_BLOCK(data_file, 2, 2, false); sky_data_file_free(data_file); return 0; }
// Initializes and opens the data file on the table. // // table - The table to initialize the data file for. // // Returns 0 if successful, otherwise returns -1. int sky_table_unload_data_file(sky_table *table) { check(table != NULL, "Table required"); if(table->data_file) { sky_data_file_free(table->data_file); table->data_file = NULL; } return 0; error: return -1; }
int test_sky_block_get_path_stats_with_no_event() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/blocks/path_stats/a"); sky_block_path_stat *paths = NULL; uint32_t path_count = 0; int rc = sky_block_get_path_stats(data_file->blocks[0], NULL, &paths, &path_count); mu_assert_int_equals(rc, 0); mu_assert_int_equals(path_count, 2); ASSERT_PATH_STAT(paths[0], 3, 0L, 41L, 41L); ASSERT_PATH_STAT(paths[1], 10, 41L, 60L, 19L); sky_data_file_free(data_file); return 0; }
int test_sky_path_iterator_single_block_next() { loadtmp("tests/fixtures/path_iterator/0"); int rc; void *block_ptr, *ptr; sky_data_file *data_file = sky_data_file_create(); data_file->path = bfromcstr("tmp/data"); data_file->header_path = bfromcstr("tmp/header"); sky_data_file_load(data_file); sky_block_get_ptr(data_file->blocks[0], &block_ptr); sky_path_iterator *iterator = sky_path_iterator_create(); sky_path_iterator_set_block(iterator, data_file->blocks[0]); mu_assert(iterator->block == data_file->blocks[0], ""); mu_assert(iterator->data_file == NULL, ""); mu_assert_int_equals(iterator->block_index, 0); // Path 1 rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-block_ptr, 0L); mu_assert_bool(!iterator->eof); // Path 2 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 19); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-block_ptr, 19L); // Path 3 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 47); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-block_ptr, 47L); // EOF rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_bool(iterator->eof); sky_path_iterator_free(iterator); sky_data_file_free(data_file); return 0; }
int test_sky_data_file_deduplicate_insertion_event_data() { struct tagbstring STR1 = bsStatic("foo"); struct tagbstring STR2 = bsStatic("bar"); struct tagbstring STR3 = bsStatic("zzz"); int rc; sky_event *event; sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/data_files/dedup/0/a", 0); // Add initial event. event = sky_event_create(1, 10LL, 1); event->data_count = 8; event->data = calloc(event->data_count, sizeof(*event->data)); event->data[0] = sky_event_data_create_string(-1, &STR2); event->data[1] = sky_event_data_create_int(-2, 200); event->data[2] = sky_event_data_create_double(-3, 1.0); event->data[3] = sky_event_data_create_boolean(-4, false); event->data[4] = sky_event_data_create_string(1, &STR1); event->data[5] = sky_event_data_create_int(2, 100); event->data[6] = sky_event_data_create_double(3, 100.2); event->data[7] = sky_event_data_create_boolean(4, true); rc = sky_data_file_add_event(data_file, event); mu_assert_int_equals(rc, 0); ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/b"); // Add the same event data, different timestamp. event->timestamp = 11LL; rc = sky_data_file_add_event(data_file, event); mu_assert_int_equals(rc, 0); sky_event_free(event); ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/c"); // Add somewhat different object state event. event = sky_event_create(1, 12LL, 0); event->data_count = 4; event->data = calloc(event->data_count, sizeof(*event->data)); event->data[0] = sky_event_data_create_string(1, &STR3); event->data[1] = sky_event_data_create_int(2, 20); event->data[2] = sky_event_data_create_double(3, 100.2); event->data[3] = sky_event_data_create_boolean(4, false); rc = sky_data_file_add_event(data_file, event); mu_assert_int_equals(rc, 0); sky_event_free(event); ASSERT_DATA_FILE("tests/fixtures/data_files/dedup/0/d"); sky_data_file_free(data_file); return 0; }
int test_sky_block_get_offset() { sky_data_file *data_file = sky_data_file_create(); data_file->block_size = 128; sky_block *block = sky_block_create(data_file); block->index = 3; size_t offset; int rc = sky_block_get_offset(block, &offset); mu_assert_int_equals(rc, 0); mu_assert_long_equals(offset, 384L); sky_data_file_free(data_file); sky_block_free(block); return 0; }
int test_sky_block_get_path_stats_with_event_in_new_ending_path() { sky_data_file *data_file; INIT_DATA_FILE("tests/fixtures/blocks/path_stats/a"); sky_block_path_stat *paths = NULL; uint32_t path_count = 0; sky_event *event = sky_event_create(11, 7LL, 20); int rc = sky_block_get_path_stats(data_file->blocks[0], event, &paths, &path_count); mu_assert_int_equals(rc, 0); mu_assert_int_equals(path_count, 3); ASSERT_PATH_STAT(paths[0], 3, 0L, 41L, 41L); ASSERT_PATH_STAT(paths[1], 10, 41L, 60L, 19L); ASSERT_PATH_STAT(paths[2], 11, 60L, 60L, 19L); sky_event_free(event); sky_data_file_free(data_file); return 0; }
int test_sky_block_get_ptr() { uint8_t x; sky_data_file *data_file = sky_data_file_create(); data_file->block_size = 128; data_file->data = (void*)&x; sky_block *block = sky_block_create(data_file); block->index = 3; void *ptr; int rc = sky_block_get_ptr(block, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-((void*)&x), 384L); sky_data_file_free(data_file); sky_block_free(block); return 0; }
int test_sky_path_iterator_data_file_next() { loadtmp("tests/fixtures/path_iterator/1"); int rc; void *ptr; sky_data_file *data_file = sky_data_file_create(); data_file->path = bfromcstr("tmp/data"); data_file->header_path = bfromcstr("tmp/header"); sky_data_file_load(data_file); sky_path_iterator *iterator = sky_path_iterator_create(); sky_path_iterator_set_data_file(iterator, data_file); mu_assert(iterator->block == NULL, ""); mu_assert(iterator->data_file == data_file, ""); mu_assert_int_equals(iterator->block_index, 0); // Path 1 rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 0L); mu_assert_int_equals(iterator->current_object_id, 2); mu_assert_bool(!iterator->eof); // Path 2 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 19); mu_assert_int_equals(iterator->current_object_id, 3); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 19L); // Path 3 (Spanned) rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_int_equals(iterator->current_object_id, 4); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 64L); // Path 4 rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_int_equals(iterator->current_object_id, 5); mu_assert_bool(!iterator->eof); rc = sky_path_iterator_get_ptr(iterator, &ptr); mu_assert_int_equals(rc, 0); mu_assert_long_equals(ptr-data_file->data, 128L); // EOF rc = sky_path_iterator_next(iterator); mu_assert_int_equals(rc, 0); mu_assert_int_equals(iterator->byte_index, 0); mu_assert_bool(iterator->eof); sky_path_iterator_free(iterator); sky_data_file_free(data_file); return 0; }