@@ -124,24 +124,30 @@ Alright, that's it! Like I said, this was a shorter refactor that should help us
124
124
125
125
Here's the complete diff to this part:
126
126
``` diff
127
+ @@ -78,6 +78,13 @@ struct Table_t {
127
128
};
128
129
typedef struct Table_t Table;
129
-
130
+
130
131
+ struct Cursor_t {
131
132
+ Table* table;
132
133
+ 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
134
135
+ };
135
136
+ typedef struct Cursor_t Cursor;
136
137
+
137
138
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);
139
140
}
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];
142
143
}
143
-
144
+
144
145
- 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;
145
151
+ Cursor* table_start(Table* table) {
146
152
+ Cursor* cursor = malloc(sizeof(Cursor));
147
153
+ cursor->table = table;
@@ -162,55 +168,50 @@ Here's the complete diff to this part:
162
168
+
163
169
+ void* cursor_value(Cursor* cursor) {
164
170
+ 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
+ +
173
178
+ void cursor_advance(Cursor* cursor) {
174
179
+ cursor->row_num += 1;
175
180
+ if (cursor->row_num >= cursor->table->num_rows) {
176
181
+ cursor->end_of_table = true;
177
182
+ }
178
- + }
179
- +
183
+ }
184
+
180
185
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
+
186
189
Row* row_to_insert = &(statement->row_to_insert);
187
190
+ Cursor* cursor = table_end(table);
188
-
191
+
189
192
- serialize_row(row_to_insert, row_slot(table, table->num_rows));
190
193
+ serialize_row(row_to_insert, cursor_value(cursor));
191
194
table->num_rows += 1;
192
-
195
+
193
196
+ free(cursor);
194
197
+
195
198
return EXECUTE_SUCCESS;
196
199
}
197
-
200
+
198
201
ExecuteResult execute_select(Statement* statement, Table* table) {
199
202
+ Cursor* cursor = table_start(table);
200
203
+
201
204
Row row;
202
205
- 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);
204
207
+ 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);
208
211
}
209
212
+
210
213
+ free(cursor);
211
214
+
212
215
return EXECUTE_SUCCESS;
213
216
}
214
-
215
-
216
- ```
217
+ ```
0 commit comments