Skip to content

Commit

Permalink
refact: refact directory structure
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Feb 6, 2022
1 parent 0fa9b9d commit fe58710
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 11 deletions.
14 changes: 7 additions & 7 deletions btree.h → db-kernel/btree.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ const uint32_t INTERNAL_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + INTERNAL_NO
* - the child pointer (includes the right child pointer) is actually page number of a disk file
* - layout is like (child0, key0, child1, key1, ..., child[n-1], key[n-1], right_child)
* - there are n keys and n+1 child in internal nodes
* Notice that our INTERNAL_NODE_HEADER_SIZE = 14,
* Notice that our INTERNAL_NODE_HEADER_SIZE = 14,
* and total size of (child pointer, key) is just INTERNAL_NODE_CELL_SIZE = 8
* which implies:
* which implies:
* - each internal node can store (4096 - 14) / 8 = 510 pairs (child pointer, key),
* - plus the most right child, we can totally 510 keys, and 511 child pointer.
*
*
* What does this means? Let's do a simple arithmetic.
*
*
* +----------------------+-------------------+------------------------+
* | interval node layers | max leaf nodes | size of all leaf nodes |
* +----------------------+-------------------+------------------------+
Expand All @@ -90,7 +90,7 @@ const uint32_t INTERNAL_NODE_HEADER_SIZE = COMMON_NODE_HEADER_SIZE + INTERNAL_NO
* | 2 | 511 ^ 2 = 261,121 | 2MB * 511 < 1GB |
* | 3 | 511 ^ 3 | 1GB * 511 < 512GB |
* +----------------------+-------------------+------------------------+
*
*
* This is why B-Tree is a useful data structure for index.
* - given a key, we can find the corresponding leaf node in log(n) time
* - in each node(internal/leaf), we can do a binary search, which is also log(n) time
Expand Down Expand Up @@ -182,7 +182,7 @@ uint32_t get_unused_page_num(pager_t *pager)
void create_new_root(table_t *table, uint32_t right_child_page_num)
{
/*
- This function is called (called by `leaf_node_split_and_insert`) when we insert a new key
- This function is called (called by `leaf_node_split_and_insert`) when we insert a new key
into the initialized root node, but the initialized root node is full.
- The address of right child can be got by argument `right_child_page_num`.
- The old root contains the data of left child after insertion, then we should:
Expand Down Expand Up @@ -618,7 +618,7 @@ void internal_node_insert(table_t *table, uint32_t parent_page_num, uint32_t chi
// optimize: 或许改成 child_min_key > original_right_child_max_key 比较好
if (child_max_key > original_right_child_max_key)
{
/**
/**
* if the `child` will become the rightmost one of parent
* replace the right child
**/
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions table.h → db-kernel/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ cursor_t *table_exists(table_t *table, uint32_t key_to_find)
// printf("[table exists] page = %d\n", cursor->page_num);
// printf("[table exists] cell = %d\n", cursor->cell_num);
// printf("[table exists] num_cells = %d\n", *leaf_node_num_cells(node));
// In this case, the row address `row = cursor_value(cursor)` is invalid,

// In this case, the row address `row = cursor_value(cursor)` is invalid,
// we should not access memory
// even if we fill zeros when initialize the leaf node
if (cursor->cell_num >= *leaf_node_num_cells(node))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion list.h → libs/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static inline bool list_empty(const list_node_t *head)
* - 同时判断头指针的 next 和 prev, 仅当两者都指向自己时才返回真
* - 这主要是为了应付另一个 CPU 正在处理同一个链表而造成 next, prev 不一致的情况
* - 这一安全保障能力有限:除非其他 CPU 的链表操作只有 list_del_init(), 否则仍然不能保证安全
* - 还是得加锁机制
* - 还是得加锁机制
**/
static inline bool list_empty_careful(const list_node_t *head)
{
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ CC=clang -Wall -Wno-pointer-to-int-cast -Wno-unused-function
PROJECT=$(shell pwd)
SQLPARSER=$(PROJECT)/sql-parser
SQLVM=$(PROJECT)/sql-vm
LIBS=$(PROJECT)/libs
DBKERNEL=$(PROJECT)/db-kernel

INCLUDE=-I$(PROJECT) -I$(SQLPARSER) -I$(SQLVM)
INCLUDE=-I$(PROJECT) -I$(SQLPARSER) -I$(SQLVM) -I$(LIBS) -I$(DBKERNEL)

build: sqlparser
$(CC) $(INCLUDE) sqlparser.tab.c lex.yy.c main2.c -o tinydb
Expand Down

0 comments on commit fe58710

Please sign in to comment.