@@ -93,11 +93,11 @@ The code to access keys, values and metadata all involve pointer arithmetic usin
93
93
94
94
``` diff
95
95
+ uint32_t* leaf_node_num_cells(void* node) {
96
- + return (char *) node + LEAF_NODE_NUM_CELLS_OFFSET;
96
+ + return node + LEAF_NODE_NUM_CELLS_OFFSET;
97
97
+ }
98
98
+
99
99
+ void* leaf_node_cell(void* node, uint32_t cell_num) {
100
- + return (char *) node + LEAF_NODE_HEADER_SIZE + cell_num * LEAF_NODE_CELL_SIZE;
100
+ + return node + LEAF_NODE_HEADER_SIZE + cell_num * LEAF_NODE_CELL_SIZE;
101
101
+ }
102
102
+
103
103
+ uint32_t* leaf_node_key(void* node, uint32_t cell_num) {
@@ -503,8 +503,10 @@ Next time, we'll implement finding a record by primary key, and start storing ro
503
503
## Complete Diff
504
504
505
505
``` diff
506
+ @@ -62,29 +62,101 @@ const uint32_t ROW_SIZE = ID_SIZE + USERNAME_SIZE + EMAIL_SIZE;
507
+
506
508
const uint32_t PAGE_SIZE = 4096;
507
- const uint32_t TABLE_MAX_PAGES = 100;
509
+ #define TABLE_MAX_PAGES 100
508
510
- const uint32_t ROWS_PER_PAGE = PAGE_SIZE / ROW_SIZE;
509
511
- const uint32_t TABLE_MAX_ROWS = ROWS_PER_PAGE * TABLE_MAX_PAGES;
510
512
@@ -531,10 +533,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
531
533
bool end_of_table; // Indicates a position one past the last element
532
534
};
533
535
typedef struct Cursor_t Cursor;
534
- @@ -88,6 +88,77 @@ void print_row(Row* row) {
535
- printf("(%d, %s, %s)\n", row->id, row->username, row->email);
536
- }
537
-
536
+
538
537
+ enum NodeType_t { NODE_INTERNAL, NODE_LEAF };
539
538
+ typedef enum NodeType_t NodeType;
540
539
+
@@ -605,19 +604,19 @@ Next time, we'll implement finding a record by primary key, and start storing ro
605
604
+ }
606
605
+ }
607
606
+
608
- void serialize_row (Row* source, void* destination ) {
609
- memcpy(destination + ID_OFFSET, &(source ->id), ID_SIZE );
610
- memcpy(destination + USERNAME_OFFSET, &(source->username), USERNAME_SIZE);
611
- @@ -100 ,6 +171 ,8 @@ void deserialize_row(void* source, Row* destination) {
612
- memcpy(&(destination->email), source + EMAIL_OFFSET, EMAIL_SIZE);
607
+ void print_row (Row* row ) {
608
+ printf("(%d, %s, %s)\n", row ->id, row->username, row->email );
609
+ }
610
+ @@ -101 ,6 +173 ,8 @@ void deserialize_row(void * source, Row* destination) {
611
+ memcpy(&(destination->email), source + EMAIL_OFFSET, EMAIL_SIZE);
613
612
}
614
613
615
614
+ void initialize_leaf_node(void* node) { *leaf_node_num_cells(node) = 0; }
616
615
+
617
616
void* get_page(Pager* pager, uint32_t page_num) {
618
617
if (page_num > TABLE_MAX_PAGES) {
619
618
printf("Tried to fetch page number out of bounds. %d > %d\n", page_num,
620
- @@ -127 ,6 +200 ,10 @@ void* get_page(Pager* pager, uint32_t page_num) {
619
+ @@ -128 ,6 +202 ,10 @@ void* get_page(Pager* pager, uint32_t page_num) {
621
620
}
622
621
623
622
pager->pages[page_num] = page;
@@ -628,7 +627,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
628
627
}
629
628
630
629
return pager->pages[page_num];
631
- @@ -135 ,8 +212 ,12 @@ void* get_page(Pager* pager, uint32_t page_num) {
630
+ @@ -136 ,8 +214 ,12 @@ void* get_page(Pager* pager, uint32_t page_num) {
632
631
Cursor* table_start(Table* table) {
633
632
Cursor* cursor = malloc(sizeof(Cursor));
634
633
cursor->table = table;
@@ -643,7 +642,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
643
642
644
643
return cursor;
645
644
}
646
- @@ -144 ,24 +225 ,28 @@ Cursor* table_start(Table* table) {
645
+ @@ -145 ,24 +227 ,28 @@ Cursor* table_start(Table* table) {
647
646
Cursor* table_end(Table* table) {
648
647
Cursor* cursor = malloc(sizeof(Cursor));
649
648
cursor->table = table;
@@ -680,7 +679,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
680
679
cursor->end_of_table = true;
681
680
}
682
681
}
683
- @@ -184 ,6 +269 ,12 @@ Pager* pager_open(const char* filename) {
682
+ @@ -185 ,6 +271 ,12 @@ Pager* pager_open(const char* filename) {
684
683
Pager* pager = malloc(sizeof(Pager));
685
684
pager->file_descriptor = fd;
686
685
pager->file_length = file_length;
@@ -694,6 +693,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
694
693
for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) {
695
694
pager->pages[i] = NULL;
696
695
@@ -194,11 +285,15 @@ Pager* pager_open(const char* filename) {
696
+ @@ -195,11 +287,16 @@ Pager* pager_open(const char* filename) {
697
697
698
698
Table* db_open(const char* filename) {
699
699
Pager* pager = pager_open(filename);
@@ -712,8 +712,8 @@ Next time, we'll implement finding a record by primary key, and start storing ro
712
712
713
713
return table;
714
714
}
715
- @@ -228 ,7 +323 ,7 @@ void read_input (InputBuffer* input_buffer) {
716
- input_buffer->buffer[bytes_read - 1] = 0 ;
715
+ @@ -234 ,7 +331 ,7 @@ void close_input_buffer (InputBuffer* input_buffer) {
716
+ free(input_buffer) ;
717
717
}
718
718
719
719
- void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
@@ -722,6 +722,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
722
722
printf("Tried to flush null page\n");
723
723
exit(EXIT_FAILURE);
724
724
@@ -242,7 +337,7 @@ void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
725
+ @@ -249,7 +346,7 @@ void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
725
726
}
726
727
727
728
ssize_t bytes_written =
@@ -731,6 +732,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
731
732
if (bytes_written == -1) {
732
733
printf("Error writing: %d\n", errno);
733
734
@@ -252,29 +347,16 @@ void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
735
+ @@ -260,29 +357,16 @@ void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
734
736
735
737
void db_close(Table* table) {
736
738
Pager* pager = table->pager;
@@ -762,7 +764,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
762
764
int result = close(pager->file_descriptor);
763
765
if (result == -1) {
764
766
printf("Error closing db file.\n");
765
- @@ -294 ,6 +376 ,14 @@ MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table* table) {
767
+ @@ -305 ,6 +389 ,14 @@ MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table * table) {
766
768
if (strcmp(input_buffer->buffer, ".exit") == 0) {
767
769
db_close(table);
768
770
exit(EXIT_SUCCESS);
@@ -777,7 +779,7 @@ Next time, we'll implement finding a record by primary key, and start storing ro
777
779
} else {
778
780
return META_COMMAND_UNRECOGNIZED_COMMAND;
779
781
}
780
- @@ -342 ,16 +432 ,39 @@ PrepareResult prepare_statement(InputBuffer* input_buffer,
782
+ @@ -354 ,16 +446 ,39 @@ PrepareResult prepare_statement(InputBuffer* input_buffer,
781
783
return PREPARE_UNRECOGNIZED_STATEMENT;
782
784
}
783
785
0 commit comments