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_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_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_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_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; }
// 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_load_data_file(sky_table *table) { int rc; check(table != NULL, "Table required"); check(table->path != NULL, "Table path required"); // Unload any existing data file. sky_table_unload_data_file(table); // Initialize table space (0). bstring tablespace_path = bformat("%s/0", bdata(table->path)); if(!sky_file_exists(tablespace_path)) { rc = mkdir(bdata(tablespace_path), S_IRWXU); check(rc == 0, "Unable to create tablespace directory: %s", bdata(tablespace_path)); } bdestroy(tablespace_path); // Initialize data file. table->data_file = sky_data_file_create(); check_mem(table->data_file); table->data_file->path = bformat("%s/0/data", bdata(table->path)); check_mem(table->data_file->path); table->data_file->header_path = bformat("%s/0/header", bdata(table->path)); check_mem(table->data_file->header_path); // Initialize settings on the block. if(table->default_block_size > 0) { table->data_file->block_size = table->default_block_size; } // Load data rc = sky_data_file_load(table->data_file); check(rc == 0, "Unable to load data file"); return 0; error: bdestroy(tablespace_path); sky_table_unload_data_file(table); return -1; }
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; }