Skip to content

Commit 9d87b5f

Browse files
committed
More copy edits.
1 parent 4c3a67f commit 9d87b5f

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

part3.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ We store those parsed arguments into a new `Row` data structure inside the state
5959

6060
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.
6161

62+
Here's my plan:
63+
6264
- Store rows in blocks of memory called pages
6365
- Each page stores as many rows as it can fit
6466
- Rows are serialized into a compact representation with each page
6567
- Pages are only allocated as needed
6668
- Keep a fixed-size array of pointers to pages
6769

68-
First, the code to convert rows to and from their compact representation:
70+
First we'll define the compact representation of a row:
6971
```diff
7072
+#define size_of_attribute(Struct, Attribute) sizeof(((Struct*)0)->Attribute)
7173
+
@@ -77,6 +79,17 @@ First, the code to convert rows to and from their compact representation:
7779
+const uint32_t EMAIL_OFFSET = USERNAME_OFFSET + USERNAME_SIZE;
7880
+const uint32_t ROW_SIZE = ID_SIZE + USERNAME_SIZE + EMAIL_SIZE;
7981
```
82+
83+
This means the layout of a serialized row will look like this:
84+
85+
| column | size (bytes) | offset |
86+
|----------|--------------|--------------|
87+
| id | 4 | 0 |
88+
| username | 32 | 4 |
89+
| email | 255 | 36 |
90+
| total | 291 | |
91+
92+
We also need code to convert to and from the compact representation.
8093
```diff
8194
+void serialize_row(Row* source, void* destination) {
8295
+ memcpy(destination + ID_OFFSET, &(source->id), ID_SIZE);
@@ -91,16 +104,7 @@ First, the code to convert rows to and from their compact representation:
91104
+}
92105
```
93106

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:
104108
```diff
105109
+const uint32_t PAGE_SIZE = 4096;
106110
+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
114118
+typedef struct Table_t Table;
115119
```
116120

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.
118122

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)
120124

121125
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.
122126

0 commit comments

Comments
 (0)