This commit is contained in:
Timothy Warren 2019-05-03 15:39:56 -04:00
parent 47d569e3fc
commit 9b9fea0984

33
db.c
View File

@ -130,7 +130,9 @@ const uint32_t INTERNAL_NODE_CELL_SIZE = INTERNAL_NODE_CHILD_SIZE + INTERNAL_NOD
*/ */
const uint32_t LEAF_NODE_NUM_CELLS_SIZE = sizeof(uint32_t); const uint32_t LEAF_NODE_NUM_CELLS_SIZE = sizeof(uint32_t);
const uint32_t LEAF_NODE_NUM_CELLS_OFFSET = COMMON_NODE_HEADER_SIZE; const uint32_t LEAF_NODE_NUM_CELLS_OFFSET = COMMON_NODE_HEADER_SIZE;
const uint32_t LEAF_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + LEAF_NODE_NUM_CELLS_SIZE; const uint32_t LEAF_NODE_NEXT_LEAF_SIZE = sizeof(uint32_t);
const uint32_t LEAF_NODE_NEXT_LEAF_OFFSET = LEAF_NODE_NUM_CELLS_OFFSET + LEAF_NODE_NUM_CELLS_SIZE;
const uint32_t LEAF_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + LEAF_NODE_NUM_CELLS_SIZE + LEAF_NODE_NEXT_LEAF_SIZE;
/* /*
* Leaf Node Body Layout * Leaf Node Body Layout
@ -197,6 +199,10 @@ uint32_t* leaf_node_num_cells(void* node) {
return (char *)node + LEAF_NODE_NUM_CELLS_OFFSET; return (char *)node + LEAF_NODE_NUM_CELLS_OFFSET;
} }
uint32_t* leaf_node_next_leaf(void* node) {
return node + LEAF_NODE_NEXT_LEAF_OFFSET;
}
void* leaf_node_cell(void* node, uint32_t cell_num) { void* leaf_node_cell(void* node, uint32_t cell_num) {
return (char *)node + LEAF_NODE_HEADER_SIZE + cell_num; return (char *)node + LEAF_NODE_HEADER_SIZE + cell_num;
} }
@ -315,6 +321,7 @@ void initialize_leaf_node(void* node) {
set_node_type(node, NODE_LEAF); set_node_type(node, NODE_LEAF);
set_node_root(node, false); set_node_root(node, false);
*leaf_node_num_cells(node) = 0; *leaf_node_num_cells(node) = 0;
*leaf_node_next_leaf(node) = 0;
} }
void initialize_internal_node(void* node) { void initialize_internal_node(void* node) {
@ -397,13 +404,10 @@ Cursor* table_find(Table* table, uint32_t key) {
} }
Cursor* table_start(Table* table) { Cursor* table_start(Table* table) {
Cursor* cursor = malloc(sizeof(Cursor)); Cursor* cursor = table_find(table, 0);
cursor->table = table;
cursor->page_num = table->root_page_num;
cursor->cell_num = 0;
void* root_node = get_page(table->pager, table->root_page_num); void* node = get_page(table->pager, cursor->page_num);
uint32_t num_cells = *leaf_node_num_cells(root_node); uint32_t num_cells = *leaf_node_num_cells(node);
cursor->end_of_table = (num_cells == 0); cursor->end_of_table = (num_cells == 0);
return cursor; return cursor;
@ -421,7 +425,15 @@ void cursor_advance(Cursor* cursor) {
cursor->cell_num += 1; cursor->cell_num += 1;
if (cursor->cell_num >= (*leaf_node_num_cells(node))) { if (cursor->cell_num >= (*leaf_node_num_cells(node))) {
cursor->end_of_table = true; /* Advance to next leaf node */
uint32_t next_page_num = *leaf_node_next_leaf(node);
if (next_page_num == 0) {
/* This was rightmost leaf */
cursor->end_of_table = true;
} else {
cursor->page_num = next_page_num;
cursor->cell_num = 0;
}
} }
} }
@ -654,6 +666,8 @@ void leaf_node_split_and_insert(Cursor* cursor, uint32_t key, Row* value) {
uint32_t new_page_num = get_unused_page_num(cursor->table->pager); uint32_t new_page_num = get_unused_page_num(cursor->table->pager);
void* new_node = get_page(cursor->table->pager, new_page_num); void* new_node = get_page(cursor->table->pager, new_page_num);
initialize_leaf_node(new_node); initialize_leaf_node(new_node);
*leaf_node_next_leaf(new_node) = *leaf_node_next_leaf(old_node);
*leaf_node_next_leaf(old_node) = new_page_num;
/* /*
* All existing keys plus new key should be divided * All existing keys plus new key should be divided
@ -671,7 +685,8 @@ void leaf_node_split_and_insert(Cursor* cursor, uint32_t key, Row* value) {
void* destination = leaf_node_cell(destination_node, index_within_node); void* destination = leaf_node_cell(destination_node, index_within_node);
if (i == cursor->cell_num) { if (i == cursor->cell_num) {
serialize_row(value, destination); serialize_row(value, leaf_node_value(destination_node, index_within_node));
*leaf_node_key(destination_node, index_within_node) = key;
} else if (i > cursor->cell_num) { } else if (i > cursor->cell_num) {
memcpy(destination, leaf_node_cell(old_node, i - 1), LEAF_NODE_CELL_SIZE); memcpy(destination, leaf_node_cell(old_node, i - 1), LEAF_NODE_CELL_SIZE);
} else { } else {