Skip to content

Commit

Permalink
feat: support delete-where
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Feb 2, 2022
1 parent 4d4ee52 commit 4625007
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
7 changes: 6 additions & 1 deletion btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,21 +431,26 @@ cursor_t *leaf_node_find(table_t *table, uint32_t page_num, uint32_t key)
* it still occupy disk space.
*
* 4) If the `num_cells` of leaf node is ZERO, then just return and delete nothing.
* The returned value denote whether if we deleted a cell from B+Tree.
**/
void leaf_node_fake_delete(cursor_t *cursor, uint32_t key_to_delete)
bool leaf_node_fake_delete(cursor_t *cursor, uint32_t key_to_delete)
{
uint32_t cell_num = cursor->cell_num;
void *node = get_page(cursor->table->pager, cursor->page_num);

uint32_t num_cells = *leaf_node_num_cells(node);

if (num_cells == 0)
return false;

assert(cell_num < num_cells && *leaf_node_key(node, cell_num) == key_to_delete);

for (uint32_t i = cell_num; i + 1 < num_cells; i++)
{
memcpy(leaf_node_cell(node, i), leaf_node_cell(node, i + 1), LEAF_NODE_CELL_SIZE);
}
*leaf_node_num_cells(node) = num_cells - 1;
return true;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ cursor_t *table_start(table_t *table)
cursor_t *cursor = table_find(table, 0);
void *node = get_page(cursor->table->pager, cursor->page_num);
uint32_t num_cells = *leaf_node_num_cells(node);
cursor->end_of_table = (num_cells == 0);
cursor->end_of_table = (num_cells == 0 && *leaf_node_next_leaf(node) == 0);
return cursor;
}

/* Whether if the node(page) that the cursor points to is empty. */
bool cursor_empty(cursor_t *cursor)
{
void *page = get_page(cursor->table->pager, cursor->page_num);
return *leaf_node_num_cells(page) == 0;
}

// get row address of the argument cursor
void *cursor_value(cursor_t *cursor)
{
Expand Down
10 changes: 5 additions & 5 deletions sql-statement/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ typedef enum

typedef struct
{
statement_type_t type; /* SELECT, UPDATE, DELETE, etc. */
row_t row_value; /* The row value of INSERT SQL */
const char *table_name; /* table name */
schema_node_t *schemas; /* SELECT column items list */
condition_t *conditions; /* WHERE conditions */
statement_type_t type; // SELECT, UPDATE, DELETE, etc.
row_t row_value; // The row value of INSERT SQL
const char *table_name; // table name
schema_node_t *schemas; // SELECT/UPDATE column items list
condition_t *conditions; // WHERE conditions
} statement_t;

static inline void statement_set(
Expand Down
32 changes: 25 additions & 7 deletions sql-vm/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ execute_result_t execute_select(statement_t *statement, table_t *table)
uint32_t i = 0;
while (!(cursor->end_of_table))
{
deserialize_row(cursor_value(cursor), &row);
i++;
print_row(&row);
if (!cursor_empty(cursor))
{
deserialize_row(cursor_value(cursor), &row);
i++;
print_row(&row);
}
cursor_advance(cursor);
}
printf("total %u rows\n", i);
Expand All @@ -76,8 +79,7 @@ execute_result_t execute_select_where(statement_t *statement, table_t *table)
while (!(cursor->end_of_table))
{
deserialize_row(cursor_value(cursor), &row);

if (test_condition(statement->conditions, &row))
if (!cursor_empty(cursor) && test_condition(statement->conditions, &row))
{
cnt++;
print_fields(&row, flags);
Expand Down Expand Up @@ -161,8 +163,24 @@ execute_result_t execute_delete(statement_t *statement, table_t *table)
execute_result_t execute_delete_where(statement_t *statement, table_t *table)
{
/* There is bugs while scanning B+Tree and deleting at the same time. */
TODO(__FILE__, __LINE__);
return EXECUTE_UNKNOWN_STATEMENT;
// TODO(__FILE__, __LINE__);
cursor_t *cursor = table_start(table);
row_t row;
uint32_t cnt = 0;
while (!(cursor->end_of_table))
{
deserialize_row(cursor_value(cursor), &row);
if (test_condition(statement->conditions, &row) &&
leaf_node_fake_delete(cursor, row.id))
{
cnt += 1;
cursor->cell_num -= 1;
}
cursor_advance(cursor);
}
free(cursor);
printf("total %u rows were deleted. \n", cnt);
return EXECUTE_SUCCESS;
}

execute_result_t execute_commit(statement_t *statement, table_t *table)
Expand Down

0 comments on commit 4625007

Please sign in to comment.