Part 5 - Persistence to Disk

Part 4 - Our First Tests (and Bugs)

Part 6 - The Cursor Abstraction

“Nothing in the world can take the place of persistence.” – Calvin Coolidge

Our database lets you insert records and read them back out, but only as long as you keep the program running. If you kill the program and start it back up, all your records are gone. Here’s a spec for the behavior we want:

  1. it 'keeps data after closing connection' do
  2. result1 = run_script([
  3. "insert 1 user1 person1@example.com",
  4. ".exit",
  5. ])
  6. expect(result1).to match_array([
  7. "db > Executed.",
  8. "db > ",
  9. ])
  10. result2 = run_script([
  11. "select",
  12. ".exit",
  13. ])
  14. expect(result2).to match_array([
  15. "db > (1, user1, person1@example.com)",
  16. "Executed.",
  17. "db > ",
  18. ])
  19. end

Like sqlite, we’re going to persist records by saving the entire database to a file.

We already set ourselves up to do that by serializing rows into page-sized memory blocks. To add persistence, we can simply write those blocks of memory to a file, and read them back into memory the next time the program starts up.

To make this easier, we’re going to make an abstraction called the pager. We ask the pager for page number x, and the pager gives us back a block of memory. It first looks in its cache. On a cache miss, it copies data from disk into memory (by reading the database file).

|How our program matches up with SQLite architecture

The Pager accesses the page cache and the file. The Table object makes requests for pages through the pager:

  1. +struct Pager_t {
  2. + int file_descriptor;
  3. + uint32_t file_length;
  4. + void* pages[TABLE_MAX_PAGES];
  5. +};
  6. +typedef struct Pager_t Pager;
  7. +
  8. struct Table_t {
  9. - void* pages[TABLE_MAX_PAGES];
  10. + Pager* pager;
  11. uint32_t num_rows;
  12. };

I’m renaming new_table() to db_open() because it now has the effect of opening a connection to the database. By opening a connection, I mean:

  • opening the database file
  • initializing a pager data structure
  • initializing a table data structure
  1. -Table* new_table() {
  2. +Table* db_open(const char* filename) {
  3. + Pager* pager = pager_open(filename);
  4. + uint32_t num_rows = pager->file_length / ROW_SIZE;
  5. +
  6. Table* table = malloc(sizeof(Table));
  7. - table->num_rows = 0;
  8. + table->pager = pager;
  9. + table->num_rows = num_rows;
  10. return table;
  11. }

db_open() in turn calls pager_open(), which opens the database file and keeps track of its size. It also initializes the page cache to all NULLs.

  1. +Pager* pager_open(const char* filename) {
  2. + int fd = open(filename,
  3. + O_RDWR | // Read/Write mode
  4. + O_CREAT, // Create file if it does not exist
  5. + S_IWUSR | // User write permission
  6. + S_IRUSR // User read permission
  7. + );
  8. +
  9. + if (fd == -1) {
  10. + printf("Unable to open file\n");
  11. + exit(EXIT_FAILURE);
  12. + }
  13. +
  14. + off_t file_length = lseek(fd, 0, SEEK_END);
  15. +
  16. + Pager* pager = malloc(sizeof(Pager));
  17. + pager->file_descriptor = fd;
  18. + pager->file_length = file_length;
  19. +
  20. + for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) {
  21. + pager->pages[i] = NULL;
  22. + }
  23. +
  24. + return pager;
  25. +}

Following our new abstraction, we move the logic for fetching a page into its own method:

  1. void* row_slot(Table* table, uint32_t row_num) {
  2. uint32_t page_num = row_num / ROWS_PER_PAGE;
  3. - void* page = table->pages[page_num];
  4. - if (!page) {
  5. - // Allocate memory only when we try to access page
  6. - page = table->pages[page_num] = malloc(PAGE_SIZE);
  7. - }
  8. + void* page = get_page(table->pager, page_num);
  9. uint32_t row_offset = row_num % ROWS_PER_PAGE;
  10. uint32_t byte_offset = row_offset * ROW_SIZE;
  11. return page + byte_offset;
  12. }

The get_page() method has the logic for handling a cache miss. We assume pages are saved one after the other in the database file: Page 0 at offset 0, page 1 at offset 4096, page 2 at offset 8192, etc. If the requested page lies outside the bounds of the file, we know it should be blank, so we just allocate some memory and return it. The page will be added to the file when we flush the cache to disk later.

  1. +void* get_page(Pager* pager, uint32_t page_num) {
  2. + if (page_num > TABLE_MAX_PAGES) {
  3. + printf("Tried to fetch page number out of bounds. %d > %d\n", page_num,
  4. + TABLE_MAX_PAGES);
  5. + exit(EXIT_FAILURE);
  6. + }
  7. +
  8. + if (pager->pages[page_num] == NULL) {
  9. + // Cache miss. Allocate memory and load from file.
  10. + void* page = malloc(PAGE_SIZE);
  11. + uint32_t num_pages = pager->file_length / PAGE_SIZE;
  12. +
  13. + // We might save a partial page at the end of the file
  14. + if (pager->file_length % PAGE_SIZE) {
  15. + num_pages += 1;
  16. + }
  17. +
  18. + if (page_num <= num_pages) {
  19. + lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET);
  20. + ssize_t bytes_read = read(pager->file_descriptor, page, PAGE_SIZE);
  21. + if (bytes_read == -1) {
  22. + printf("Error reading file: %d\n", errno);
  23. + exit(EXIT_FAILURE);
  24. + }
  25. + }
  26. +
  27. + pager->pages[page_num] = page;
  28. + }
  29. +
  30. + return pager->pages[page_num];
  31. +}

For now, we’ll wait to flush the cache to disk until the user closes the connection to the database. When the user exits, we’ll call a new method called db_close(), which

  • flushes the page cache to disk
  • closes the database file
  • frees the memory for the Pager and Table data structures
  1. +void db_close(Table* table) {
  2. + Pager* pager = table->pager;
  3. + uint32_t num_full_pages = table->num_rows / ROWS_PER_PAGE;
  4. +
  5. + for (uint32_t i = 0; i < num_full_pages; i++) {
  6. + if (pager->pages[i] == NULL) {
  7. + continue;
  8. + }
  9. + pager_flush(pager, i, PAGE_SIZE);
  10. + free(pager->pages[i]);
  11. + pager->pages[i] = NULL;
  12. + }
  13. +
  14. + // There may be a partial page to write to the end of the file
  15. + // This should not be needed after we switch to a B-tree
  16. + uint32_t num_additional_rows = table->num_rows % ROWS_PER_PAGE;
  17. + if (num_additional_rows > 0) {
  18. + uint32_t page_num = num_full_pages;
  19. + if (pager->pages[page_num] != NULL) {
  20. + pager_flush(pager, page_num, num_additional_rows * ROW_SIZE);
  21. + free(pager->pages[page_num]);
  22. + pager->pages[page_num] = NULL;
  23. + }
  24. + }
  25. +
  26. + int result = close(pager->file_descriptor);
  27. + if (result == -1) {
  28. + printf("Error closing db file.\n");
  29. + exit(EXIT_FAILURE);
  30. + }
  31. + for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) {
  32. + void* page = pager->pages[i];
  33. + if (page) {
  34. + free(page);
  35. + pager->pages[i] = NULL;
  36. + }
  37. + }
  38. + free(pager);
  39. +}
  40. +
  41. -MetaCommandResult do_meta_command(InputBuffer* input_buffer) {
  42. +MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table* table) {
  43. if (strcmp(input_buffer->buffer, ".exit") == 0) {
  44. + db_close(table);
  45. exit(EXIT_SUCCESS);
  46. } else {
  47. return META_COMMAND_UNRECOGNIZED_COMMAND;

In our current design, the length of the file encodes how many rows are in the database, so we need to write a partial page at the end of the file. That’s why pager_flush() takes both a page number and a size. It’s not the greatest design, but it will go away pretty quickly when we start implementing the B-tree.

  1. +void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
  2. + if (pager->pages[page_num] == NULL) {
  3. + printf("Tried to flush null page\n");
  4. + exit(EXIT_FAILURE);
  5. + }
  6. +
  7. + off_t offset = lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET);
  8. +
  9. + if (offset == -1) {
  10. + printf("Error seeking: %d\n", errno);
  11. + exit(EXIT_FAILURE);
  12. + }
  13. +
  14. + ssize_t bytes_written =
  15. + write(pager->file_descriptor, pager->pages[page_num], size);
  16. +
  17. + if (bytes_written == -1) {
  18. + printf("Error writing: %d\n", errno);
  19. + exit(EXIT_FAILURE);
  20. + }
  21. +}

Lastly, we need to accept the filename as a command-line argument. Don’t forget to also add the extra argument to do_meta_command:

  1. int main(int argc, char* argv[]) {
  2. - Table* table = new_table();
  3. + if (argc < 2) {
  4. + printf("Must supply a database filename.\n");
  5. + exit(EXIT_FAILURE);
  6. + }
  7. +
  8. + char* filename = argv[1];
  9. + Table* table = db_open(filename);
  10. +
  11. InputBuffer* input_buffer = new_input_buffer();
  12. while (true) {
  13. print_prompt();
  14. read_input(input_buffer);
  15. if (input_buffer->buffer[0] == '.') {
  16. - switch (do_meta_command(input_buffer)) {
  17. + switch (do_meta_command(input_buffer, table)) {

With these changes, we’re able to close then reopen the database, and our records are still there!

  1. ~ ./db mydb.db
  2. db > insert 1 cstack foo@bar.com
  3. Executed.
  4. db > insert 2 voltorb volty@example.com
  5. Executed.
  6. db > .exit
  7. ~
  8. ~ ./db mydb.db
  9. db > select
  10. (1, cstack, foo@bar.com)
  11. (2, voltorb, volty@example.com)
  12. Executed.
  13. db > .exit
  14. ~

For extra fun, let’s take a look at mydb.db to see how our data is being stored. I’ll use vim as a hex editor to look at the memory layout of the file:

  1. vim mydb.db
  2. :%!xxd

|Current File Format

The first four bytes are the id of the first row (4 bytes because we store a uint32_t). It’s stored in little-endian byte order, so the least significant byte comes first (01), followed by the higher-order bytes (00 00 00). We used memcpy() to copy bytes from our Row struct into the page cache, so that means the struct was laid out in memory in little-endian byte order. That’s an attribute of the machine I compiled the program for. If we wanted to write a database file on my machine, then read it on a big-endian machine, we’d have to change our serialize_row() and deserialize_row() methods to always store and read bytes in the same order.

The next 33 bytes store the username as a null-terminated string. Apparently “cstack” in ASCII hexadecimal is 63 73 74 61 63 6b, followed by a null character (00). The rest of the 33 bytes are unused.

The next 256 bytes store the email in the same way. Here we can see some random junk after the terminating null character. This is most likely due to uninitialized memory in our Row struct. We copy the entire 256-byte email buffer into the file, including any bytes after the end of the string. Whatever was in memory when we allocated that struct is still there. But since we use a terminating null character, it has no effect on behavior.

Conclusion

Alright! We’ve got persistence. It’s not the greatest. For example if you kill the program without typing .exit, you lose your changes. Additionally, we’re writing all pages back to disk, even pages that haven’t changed since we read them from disk. These are issues we can address later.

Next time we’ll introduce cursors, which should make it easier to implement the B-tree.

Until then!

Complete Diff

  1. +#include <errno.h>
  2. +#include <fcntl.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. +#include <unistd.h>
  8. struct InputBuffer_t {
  9. char* buffer;
  10. @@ -61,8 +64,15 @@ const uint32_t TABLE_MAX_PAGES = 100;
  11. const uint32_t ROWS_PER_PAGE = PAGE_SIZE / ROW_SIZE;
  12. const uint32_t TABLE_MAX_ROWS = ROWS_PER_PAGE * TABLE_MAX_PAGES;
  13. -struct Table_t {
  14. +struct Pager_t {
  15. + int file_descriptor;
  16. + uint32_t file_length;
  17. void* pages[TABLE_MAX_PAGES];
  18. +};
  19. +typedef struct Pager_t Pager;
  20. +
  21. +struct Table_t {
  22. + Pager* pager;
  23. uint32_t num_rows;
  24. };
  25. typedef struct Table_t Table;
  26. @@ -83,21 +93,79 @@ void deserialize_row(void* source, Row* destination) {
  27. memcpy(&(destination->email), source + EMAIL_OFFSET, EMAIL_SIZE);
  28. }
  29. +void* get_page(Pager* pager, uint32_t page_num) {
  30. + if (page_num > TABLE_MAX_PAGES) {
  31. + printf("Tried to fetch page number out of bounds. %d > %d\n", page_num,
  32. + TABLE_MAX_PAGES);
  33. + exit(EXIT_FAILURE);
  34. + }
  35. +
  36. + if (pager->pages[page_num] == NULL) {
  37. + // Cache miss. Allocate memory and load from file.
  38. + void* page = malloc(PAGE_SIZE);
  39. + uint32_t num_pages = pager->file_length / PAGE_SIZE;
  40. +
  41. + // We might save a partial page at the end of the file
  42. + if (pager->file_length % PAGE_SIZE) {
  43. + num_pages += 1;
  44. + }
  45. +
  46. + if (page_num <= num_pages) {
  47. + lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET);
  48. + ssize_t bytes_read = read(pager->file_descriptor, page, PAGE_SIZE);
  49. + if (bytes_read == -1) {
  50. + printf("Error reading file: %d\n", errno);
  51. + exit(EXIT_FAILURE);
  52. + }
  53. + }
  54. +
  55. + pager->pages[page_num] = page;
  56. + }
  57. +
  58. + return pager->pages[page_num];
  59. +}
  60. +
  61. void* row_slot(Table* table, uint32_t row_num) {
  62. uint32_t page_num = row_num / ROWS_PER_PAGE;
  63. - void* page = table->pages[page_num];
  64. - if (!page) {
  65. - // Allocate memory only when we try to access page
  66. - page = table->pages[page_num] = malloc(PAGE_SIZE);
  67. - }
  68. + void* page = get_page(table->pager, page_num);
  69. uint32_t row_offset = row_num % ROWS_PER_PAGE;
  70. uint32_t byte_offset = row_offset * ROW_SIZE;
  71. return page + byte_offset;
  72. }
  73. -Table* new_table() {
  74. +Pager* pager_open(const char* filename) {
  75. + int fd = open(filename,
  76. + O_RDWR | // Read/Write mode
  77. + O_CREAT, // Create file if it does not exist
  78. + S_IWUSR | // User write permission
  79. + S_IRUSR // User read permission
  80. + );
  81. +
  82. + if (fd == -1) {
  83. + printf("Unable to open file\n");
  84. + exit(EXIT_FAILURE);
  85. + }
  86. +
  87. + off_t file_length = lseek(fd, 0, SEEK_END);
  88. +
  89. + Pager* pager = malloc(sizeof(Pager));
  90. + pager->file_descriptor = fd;
  91. + pager->file_length = file_length;
  92. +
  93. + for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) {
  94. + pager->pages[i] = NULL;
  95. + }
  96. +
  97. + return pager;
  98. +}
  99. +
  100. +Table* db_open(const char* filename) {
  101. + Pager* pager = pager_open(filename);
  102. + uint32_t num_rows = pager->file_length / ROW_SIZE;
  103. +
  104. Table* table = malloc(sizeof(Table));
  105. - table->num_rows = 0;
  106. + table->pager = pager;
  107. + table->num_rows = num_rows;
  108. return table;
  109. }
  110. @@ -127,8 +195,71 @@ void read_input(InputBuffer* input_buffer) {
  111. input_buffer->buffer[bytes_read - 1] = 0;
  112. }
  113. -MetaCommandResult do_meta_command(InputBuffer* input_buffer) {
  114. +void pager_flush(Pager* pager, uint32_t page_num, uint32_t size) {
  115. + if (pager->pages[page_num] == NULL) {
  116. + printf("Tried to flush null page\n");
  117. + exit(EXIT_FAILURE);
  118. + }
  119. +
  120. + off_t offset = lseek(pager->file_descriptor, page_num * PAGE_SIZE, SEEK_SET);
  121. +
  122. + if (offset == -1) {
  123. + printf("Error seeking: %d\n", errno);
  124. + exit(EXIT_FAILURE);
  125. + }
  126. +
  127. + ssize_t bytes_written =
  128. + write(pager->file_descriptor, pager->pages[page_num], size);
  129. +
  130. + if (bytes_written == -1) {
  131. + printf("Error writing: %d\n", errno);
  132. + exit(EXIT_FAILURE);
  133. + }
  134. +}
  135. +
  136. +void db_close(Table* table) {
  137. + Pager* pager = table->pager;
  138. + uint32_t num_full_pages = table->num_rows / ROWS_PER_PAGE;
  139. +
  140. + for (uint32_t i = 0; i < num_full_pages; i++) {
  141. + if (pager->pages[i] == NULL) {
  142. + continue;
  143. + }
  144. + pager_flush(pager, i, PAGE_SIZE);
  145. + free(pager->pages[i]);
  146. + pager->pages[i] = NULL;
  147. + }
  148. +
  149. + // There may be a partial page to write to the end of the file
  150. + // This should not be needed after we switch to a B-tree
  151. + uint32_t num_additional_rows = table->num_rows % ROWS_PER_PAGE;
  152. + if (num_additional_rows > 0) {
  153. + uint32_t page_num = num_full_pages;
  154. + if (pager->pages[page_num] != NULL) {
  155. + pager_flush(pager, page_num, num_additional_rows * ROW_SIZE);
  156. + free(pager->pages[page_num]);
  157. + pager->pages[page_num] = NULL;
  158. + }
  159. + }
  160. +
  161. + int result = close(pager->file_descriptor);
  162. + if (result == -1) {
  163. + printf("Error closing db file.\n");
  164. + exit(EXIT_FAILURE);
  165. + }
  166. + for (uint32_t i = 0; i < TABLE_MAX_PAGES; i++) {
  167. + void* page = pager->pages[i];
  168. + if (page) {
  169. + free(page);
  170. + pager->pages[i] = NULL;
  171. + }
  172. + }
  173. + free(pager);
  174. +}
  175. +
  176. +MetaCommandResult do_meta_command(InputBuffer* input_buffer, Table* table) {
  177. if (strcmp(input_buffer->buffer, ".exit") == 0) {
  178. + db_close(table);
  179. exit(EXIT_SUCCESS);
  180. } else {
  181. return META_COMMAND_UNRECOGNIZED_COMMAND;
  182. @@ -210,14 +341,21 @@ ExecuteResult execute_statement(Statement* statement, Table* table) {
  183. }
  184. int main(int argc, char* argv[]) {
  185. - Table* table = new_table();
  186. + if (argc < 2) {
  187. + printf("Must supply a database filename.\n");
  188. + exit(EXIT_FAILURE);
  189. + }
  190. +
  191. + char* filename = argv[1];
  192. + Table* table = db_open(filename);
  193. +
  194. InputBuffer* input_buffer = new_input_buffer();
  195. while (true) {
  196. print_prompt();
  197. read_input(input_buffer);
  198. if (input_buffer->buffer[0] == '.') {
  199. - switch (do_meta_command(input_buffer)) {
  200. + switch (do_meta_command(input_buffer, table)) {
  201. case (META_COMMAND_SUCCESS):
  202. continue;
  203. case (META_COMMAND_UNRECOGNIZED_COMMAND):
  204. diff --git a/spec/main_spec.rb b/spec/main_spec.rb
  205. index 21561ce..bc0180a 100644
  206. --- a/spec/main_spec.rb
  207. +++ b/spec/main_spec.rb
  208. @@ -1,7 +1,11 @@
  209. describe 'database' do
  210. + before do
  211. + `rm -rf test.db`
  212. + end
  213. +
  214. def run_script(commands)
  215. raw_output = nil
  216. - IO.popen("./db", "r+") do |pipe|
  217. + IO.popen("./db test.db", "r+") do |pipe|
  218. commands.each do |command|
  219. pipe.puts command
  220. end
  221. @@ -28,6 +32,27 @@ describe 'database' do
  222. ])
  223. end
  224. + it 'keeps data after closing connection' do
  225. + result1 = run_script([
  226. + "insert 1 user1 person1@example.com",
  227. + ".exit",
  228. + ])
  229. + expect(result1).to match_array([
  230. + "db > Executed.",
  231. + "db > ",
  232. + ])
  233. +
  234. + result2 = run_script([
  235. + "select",
  236. + ".exit",
  237. + ])
  238. + expect(result2).to match_array([
  239. + "db > (1, user1, person1@example.com)",
  240. + "Executed.",
  241. + "db > ",
  242. + ])
  243. + end
  244. +
  245. it 'prints error message when table is full' do
  246. script = (1..1401).map do |i|
  247. "insert #{i} user#{i} person#{i}@example.com"

And the diff to our tests:

  1. describe 'database' do
  2. + before do
  3. + `rm -rf test.db`
  4. + end
  5. +
  6. def run_script(commands)
  7. raw_output = nil
  8. - IO.popen("./db", "r+") do |pipe|
  9. + IO.popen("./db test.db", "r+") do |pipe|
  10. commands.each do |command|
  11. pipe.puts command
  12. end
  13. @@ -28,6 +32,27 @@ describe 'database' do
  14. ])
  15. end
  16. + it 'keeps data after closing connection' do
  17. + result1 = run_script([
  18. + "insert 1 user1 person1@example.com",
  19. + ".exit",
  20. + ])
  21. + expect(result1).to match_array([
  22. + "db > Executed.",
  23. + "db > ",
  24. + ])
  25. +
  26. + result2 = run_script([
  27. + "select",
  28. + ".exit",
  29. + ])
  30. + expect(result2).to match_array([
  31. + "db > (1, user1, person1@example.com)",
  32. + "Executed.",
  33. + "db > ",
  34. + ])
  35. + end
  36. +
  37. it 'prints error message when table is full' do
  38. script = (1..1401).map do |i|
  39. "insert #{i} user#{i} person#{i}@example.com"

Part 4 - Our First Tests (and Bugs)

Part 6 - The Cursor Abstraction

原文: https://cstack.github.io/db_tutorial/parts/part5.html