diff --git a/Makefile b/Makefile index 8380f1e..52eb2d1 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=gcc -CFLAGS=-Wall -O0 -g +CFLAGS=-Wall -O2 -g LDFLAGS=-lm -lreadline all: makeidx openidx readidx diff --git a/index.c b/index.c index 5dad358..3b2f762 100644 --- a/index.c +++ b/index.c @@ -6,12 +6,7 @@ #include "bitmap.h" #include #include - -#define BTREE_ERR_CANNOT_OPEN_FILE -1 -#define BTREE_ERR_PAGE_SIZE_DIFFERENT -2 - -#define BTREE_OPTIMIZE_RECORDS_MIN 10 -#define BTREE_OPTIMIZE_THRESHOLD 0.1 +#include #define SWAP(type, x, y) do { type __tmp__; __tmp__ = y; y = x; x = __tmp__; } while (0) @@ -193,6 +188,8 @@ int btree_init(btree_t *tree, char* filename, size_t d) tree->header.d = d; tree->header.page_size = PAGE_SIZE; tree->header.root = _btree_alloc(tree); + tree->header.records = 0; + tree->header.changes = 0; _btree_save_header(tree); @@ -312,7 +309,7 @@ bool _btree_compensate_insert(btree_t *tree, btree_node_t *old, btree_entry_t *e if (siblings.left) { _btree_load_node(&other, tree, siblings.left); if (other.header.entries < 2*tree->header.d) { - _btree_node_insert_entry(&other, *entry); + _btree_node_insert_entry(old, *entry); _btree_rebalance(tree, &other, old, siblings.left_entry); return true; } @@ -321,7 +318,7 @@ bool _btree_compensate_insert(btree_t *tree, btree_node_t *old, btree_entry_t *e if (siblings.right) { _btree_load_node(&other, tree, siblings.right); if (other.header.entries < 2*tree->header.d) { - _btree_node_insert_entry(&other, *entry); + _btree_node_insert_entry(old, *entry); _btree_rebalance(tree, old, &other, siblings.right_entry); return true; } @@ -722,20 +719,24 @@ void _btree_optimize_page(btree_t* tree, tape_t* tmp, page_t page) void btree_optimize(btree_t *tree) { - - char tmp[PATH_MAX + 4], old[PATH_MAX]; - strcpy(old, tree->main->file->filename); - sprintf(tmp, "%s.tmp", old); + char tmp[PATH_MAX], path[PATH_MAX - 10], backup[PATH_MAX]; + strcpy(path, tree->main->file->filename); + sprintf(tmp, "%s.tmp", path); + sprintf(backup, "%s.bckp", path); printfv(VERBOSITY_DEBUG, "[btree] Optimizing btree using temp file %s.\n", tmp); - tape_t *temp = tape_open(tmp, "wb+"); + tape_t *temp = tape_open(tmp, "wb"); _btree_optimize_page(tree, temp, tree->header.root); tape_close(temp); tape_close(tree->main); - remove(old); - rename(tmp, old); + printfv(VERBOSITY_DEBUG, "[btree] Renaming %s to %s.\n", path, backup); + rename(path, backup); - tree->main = tape_open(old, "rb+"); + printfv(VERBOSITY_DEBUG, "[btree] Renaming %s to %s.\n", tmp, path); + if (rename(tmp, path) == 0) { + tree->main = tape_open(path, "rb+"); + tree->header.changes = 0; + } } diff --git a/index.h b/index.h index 2a8dfbc..e2154df 100644 --- a/index.h +++ b/index.h @@ -18,6 +18,12 @@ #define NODE_IS_ROOT 1 #define NODE_IS_LEAF 2 +#define BTREE_ERR_CANNOT_OPEN_FILE -1 +#define BTREE_ERR_PAGE_SIZE_DIFFERENT -2 + +#define BTREE_OPTIMIZE_RECORDS_MIN 1000 +#define BTREE_OPTIMIZE_THRESHOLD 0.1 + typedef struct { size_t d; /* 8 bytes long */ size_t page_size; /* 8 bytes long */ diff --git a/io.c b/io.c index 08e6c23..ec8af3b 100644 --- a/io.c +++ b/io.c @@ -21,6 +21,7 @@ file_t* file_open(const char* filename, const char* mode) printfv(VERBOSITY_VERY_VERBOSE, "[io] File %s opened in %s with page size %zu.\n", filename, mode, PAGE_SIZE); file_t* result = malloc(sizeof(file_t)); + memset(result, 0, sizeof(file_t)); result->file = handle; result->filename = malloc(strlen(filename) + 1);