working optimization=
This commit is contained in:
		
							parent
							
								
									66d4591ebb
								
							
						
					
					
						commit
						534db5d43f
					
				
							
								
								
									
										2
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Makefile
									
									
									
									
									
								
							| @ -1,5 +1,5 @@ | |||||||
| CC=gcc | CC=gcc | ||||||
| CFLAGS=-Wall -O0 -g | CFLAGS=-Wall -O2 -g | ||||||
| LDFLAGS=-lm -lreadline | LDFLAGS=-lm -lreadline | ||||||
| 
 | 
 | ||||||
| all: makeidx openidx readidx | all: makeidx openidx readidx | ||||||
|  | |||||||
							
								
								
									
										33
									
								
								index.c
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								index.c
									
									
									
									
									
								
							| @ -6,12 +6,7 @@ | |||||||
| #include "bitmap.h" | #include "bitmap.h" | ||||||
| #include <libgen.h> | #include <libgen.h> | ||||||
| #include <linux/limits.h> | #include <linux/limits.h> | ||||||
| 
 | #include <unistd.h> | ||||||
| #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 |  | ||||||
| 
 | 
 | ||||||
| #define SWAP(type, x, y) do { type __tmp__; __tmp__ = y; y = x; x = __tmp__; } while (0) | #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.d         = d; | ||||||
|     tree->header.page_size = PAGE_SIZE; |     tree->header.page_size = PAGE_SIZE; | ||||||
|     tree->header.root      = _btree_alloc(tree); |     tree->header.root      = _btree_alloc(tree); | ||||||
|  |     tree->header.records   = 0; | ||||||
|  |     tree->header.changes   = 0; | ||||||
| 
 | 
 | ||||||
|     _btree_save_header(tree); |     _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) { |     if (siblings.left) { | ||||||
|         _btree_load_node(&other, tree, siblings.left); |         _btree_load_node(&other, tree, siblings.left); | ||||||
|         if (other.header.entries < 2*tree->header.d) { |         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); |             _btree_rebalance(tree, &other, old, siblings.left_entry); | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
| @ -321,7 +318,7 @@ bool _btree_compensate_insert(btree_t *tree, btree_node_t *old, btree_entry_t *e | |||||||
|     if (siblings.right) { |     if (siblings.right) { | ||||||
|         _btree_load_node(&other, tree, siblings.right); |         _btree_load_node(&other, tree, siblings.right); | ||||||
|         if (other.header.entries < 2*tree->header.d) { |         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); |             _btree_rebalance(tree, old, &other, siblings.right_entry); | ||||||
|             return true; |             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) | void btree_optimize(btree_t *tree) | ||||||
| { | { | ||||||
| 
 |     char tmp[PATH_MAX], path[PATH_MAX - 10], backup[PATH_MAX]; | ||||||
|     char tmp[PATH_MAX + 4], old[PATH_MAX]; |     strcpy(path, tree->main->file->filename); | ||||||
|     strcpy(old, tree->main->file->filename); |     sprintf(tmp, "%s.tmp", path); | ||||||
|     sprintf(tmp, "%s.tmp", old); |     sprintf(backup, "%s.bckp", path); | ||||||
| 
 | 
 | ||||||
|     printfv(VERBOSITY_DEBUG, "[btree] Optimizing btree using temp file %s.\n", tmp); |     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); |     _btree_optimize_page(tree, temp, tree->header.root); | ||||||
|     tape_close(temp); |     tape_close(temp); | ||||||
|     tape_close(tree->main); |     tape_close(tree->main); | ||||||
| 
 | 
 | ||||||
|     remove(old); |     printfv(VERBOSITY_DEBUG, "[btree] Renaming %s to %s.\n", path, backup); | ||||||
|     rename(tmp, old); |     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; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										6
									
								
								index.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								index.h
									
									
									
									
									
								
							| @ -18,6 +18,12 @@ | |||||||
| #define NODE_IS_ROOT 1 | #define NODE_IS_ROOT 1 | ||||||
| #define NODE_IS_LEAF 2 | #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 { | typedef struct { | ||||||
|     size_t d;         /* 8 bytes long */ |     size_t d;         /* 8 bytes long */ | ||||||
|     size_t page_size; /* 8 bytes long */ |     size_t page_size; /* 8 bytes long */ | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								io.c
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								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); |     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)); |     file_t* result = malloc(sizeof(file_t)); | ||||||
|  |     memset(result, 0, sizeof(file_t)); | ||||||
| 
 | 
 | ||||||
|     result->file     = handle; |     result->file     = handle; | ||||||
|     result->filename = malloc(strlen(filename) + 1);  |     result->filename = malloc(strlen(filename) + 1);  | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user