You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: part3.md
+17-13Lines changed: 17 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -59,13 +59,15 @@ We store those parsed arguments into a new `Row` data structure inside the state
59
59
60
60
Now we need to copy that data into some data structure representing the table. SQLite uses a B-tree for fast lookups, inserts and deletes. We'll start with something simpler. Like a B-tree, it will group rows into pages, but instead of arranging those pages as a tree it will arrange them as an array.
61
61
62
+
Here's my plan:
63
+
62
64
- Store rows in blocks of memory called pages
63
65
- Each page stores as many rows as it can fit
64
66
- Rows are serialized into a compact representation with each page
65
67
- Pages are only allocated as needed
66
68
- Keep a fixed-size array of pointers to pages
67
69
68
-
First, the code to convert rows to and from their compact representation:
70
+
First we'll define the compact representation of a row:
@@ -91,16 +104,7 @@ First, the code to convert rows to and from their compact representation:
91
104
+}
92
105
```
93
106
94
-
This means the layout of a serialized row will look like this:
95
-
96
-
| column | size (bytes) | offset |
97
-
|----------|--------------|--------------|
98
-
| id | 4 | 0 |
99
-
| username | 32 | 4 |
100
-
| email | 255 | 36 |
101
-
| total | 291 ||
102
-
103
-
Next, a Table structure that points to pages of rows and keeps track of how many rows there are:
107
+
Next, a `Table` structure that points to pages of rows and keeps track of how many rows there are:
104
108
```diff
105
109
+const uint32_t PAGE_SIZE = 4096;
106
110
+const uint32_t TABLE_MAX_PAGES = 100;
@@ -114,9 +118,9 @@ Next, a Table structure that points to pages of rows and keeps track of how many
114
118
+typedef struct Table_t Table;
115
119
```
116
120
117
-
I'm making a page 4 kilobytes because that's the default page size on most computer architectures. This means one page in our database corresponds to one page used by the operating system. The operating system will move pages in and out of memory as whole units instead of breaking them up.
121
+
I'm making our page size 4 kilobytes because it's the same size as a page used in the virtual memory systems of most computer architectures. This means one page in our database corresponds to one page used by the operating system. The operating system will move pages in and out of memory as whole units instead of breaking them up.
118
122
119
-
I'm setting an arbitrary limit of 100 pages that we will allocate. When we switch to a tree structure, our database won't have a maximum size. (Although we'll still limit how many pages we keep in memory at once)
123
+
I'm setting an arbitrary limit of 100 pages that we will allocate. When we switch to a tree structure, our database's maximum size will only be limited by the maximum size of a file. (Although we'll still limit how many pages we keep in memory at once)
120
124
121
125
Rows should not cross page boundaries. Since pages probably won't exist next to each other in memory, this assumption makes it easier to read/write rows.
0 commit comments