Skip to content

Commit 6c61e66

Browse files
committed
Update total diff in part6.md
1 parent 0e2de32 commit 6c61e66

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

_parts/part6.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,30 @@ Alright, that's it! Like I said, this was a shorter refactor that should help us
124124

125125
Here's the complete diff to this part:
126126
```diff
127+
@@ -78,6 +78,13 @@ struct Table_t {
127128
};
128129
typedef struct Table_t Table;
129-
130+
130131
+struct Cursor_t {
131132
+ Table* table;
132133
+ uint32_t row_num;
133-
+ bool end_of_table; // Indicates a position one past the last element
134+
+ bool end_of_table; // Indicates a position one past the last element
134135
+};
135136
+typedef struct Cursor_t Cursor;
136137
+
137138
void print_row(Row* row) {
138-
printf("(%d, %s, %s)\n", row->id, row->username, row->email);
139+
printf("(%d, %s, %s)\n", row->id, row->username, row->email);
139140
}
140-
@@ -125,14 +132,40 @@ void* get_page(Pager* pager, uint32_t page_num) {
141-
return pager->pages[page_num];
141+
@@ -126,12 +133,38 @@ void* get_page(Pager* pager, uint32_t page_num) {
142+
return pager->pages[page_num];
142143
}
143-
144+
144145
-void* row_slot(Table* table, uint32_t row_num) {
146+
- uint32_t page_num = row_num / ROWS_PER_PAGE;
147+
- void *page = get_page(table->pager, page_num);
148+
- uint32_t row_offset = row_num % ROWS_PER_PAGE;
149+
- uint32_t byte_offset = row_offset * ROW_SIZE;
150+
- return page + byte_offset;
145151
+Cursor* table_start(Table* table) {
146152
+ Cursor* cursor = malloc(sizeof(Cursor));
147153
+ cursor->table = table;
@@ -162,55 +168,50 @@ Here's the complete diff to this part:
162168
+
163169
+void* cursor_value(Cursor* cursor) {
164170
+ uint32_t row_num = cursor->row_num;
165-
uint32_t page_num = row_num / ROWS_PER_PAGE;
166-
- void* page = get_page(table->pager, page_num);
167-
+ void* page = get_page(cursor->table->pager, page_num);
168-
uint32_t row_offset = row_num % ROWS_PER_PAGE;
169-
uint32_t byte_offset = row_offset * ROW_SIZE;
170-
return page + byte_offset;
171-
}
172-
171+
+ uint32_t page_num = row_num / ROWS_PER_PAGE;
172+
+ void *page = get_page(cursor->table->pager, page_num);
173+
+ uint32_t row_offset = row_num % ROWS_PER_PAGE;
174+
+ uint32_t byte_offset = row_offset * ROW_SIZE;
175+
+ return page + byte_offset;
176+
+}
177+
+
173178
+void cursor_advance(Cursor* cursor) {
174179
+ cursor->row_num += 1;
175180
+ if (cursor->row_num >= cursor->table->num_rows) {
176181
+ cursor->end_of_table = true;
177182
+ }
178-
+}
179-
+
183+
}
184+
180185
Pager* pager_open(const char* filename) {
181-
int fd = open(filename,
182-
O_RDWR | // Read/Write mode
183-
@@ -315,19 +348,28 @@ ExecuteResult execute_insert(Statement* statement, Table* table) {
184-
}
185-
186+
@@ -327,19 +360,28 @@ ExecuteResult execute_insert(Statement* statement, Table* table) {
187+
}
188+
186189
Row* row_to_insert = &(statement->row_to_insert);
187190
+ Cursor* cursor = table_end(table);
188-
191+
189192
- serialize_row(row_to_insert, row_slot(table, table->num_rows));
190193
+ serialize_row(row_to_insert, cursor_value(cursor));
191194
table->num_rows += 1;
192-
195+
193196
+ free(cursor);
194197
+
195198
return EXECUTE_SUCCESS;
196199
}
197-
200+
198201
ExecuteResult execute_select(Statement* statement, Table* table) {
199202
+ Cursor* cursor = table_start(table);
200203
+
201204
Row row;
202205
- for (uint32_t i = 0; i < table->num_rows; i++) {
203-
- deserialize_row(row_slot(table, i), &row);
206+
- deserialize_row(row_slot(table, i), &row);
204207
+ while (!(cursor->end_of_table)) {
205-
+ deserialize_row(cursor_value(cursor), &row);
206-
print_row(&row);
207-
+ cursor_advance(cursor);
208+
+ deserialize_row(cursor_value(cursor), &row);
209+
print_row(&row);
210+
+ cursor_advance(cursor);
208211
}
209212
+
210213
+ free(cursor);
211214
+
212215
return EXECUTE_SUCCESS;
213216
}
214-
215-
216-
```
217+
```

0 commit comments

Comments
 (0)