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: part1.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
1
1
# Part 1 - Introduction and Setting up the REPL
2
2
3
-
As a web developer, I use relational databases every day at my job, but they were always a black box to me. Some questions I had:
3
+
As a web developer, I use relational databases every day at my job, but they're a black box to me. Some questions I have:
4
4
- What format is data saved in? (in memory and on disk)
5
5
- When does it move from memory to disk?
6
6
- Why can there only be one primary key per table?
7
7
- How does rolling back a transaction work?
8
8
- How are indexes formatted?
9
9
- When and how does a full table scan happen?
10
-
- What format is a prepared statement save in?
10
+
- What format is a prepared statement saved in?
11
11
12
-
In other words, how does a database _work_?
12
+
In other words, how does a database **work**?
13
13
14
-
To figure things out, I started writing a database from scratch. It's modeled off sqlite because it is designed to be small with fewer features than MySQL or PostgreSQL, so I have a better hope of understanding it. The entire database is stored in a single file!
14
+
To figure things out, I'm writing a database from scratch. It's modeled off sqlite because it is designed to be small with fewer features than MySQL or PostgreSQL, so I have a better hope of understanding it. The entire database is stored in a single file!
15
15
16
16
# Sqlite
17
17
18
-
There's lots of documentation of [sqlite internals on their website](https://www.sqlite.org/arch.html), plus I've got a copy of [SQLite Database System: Design and Implementation](https://play.google.com/store/books/details?id=9Z6IQQnX1JEC).
18
+
There's lots of [documentation of sqlite internals](https://www.sqlite.org/arch.html) on their website, plus I've got a copy of [SQLite Database System: Design and Implementation](https://play.google.com/store/books/details?id=9Z6IQQnX1JEC).
19
19
20
20
{% include image.html url="assets/images/arch1.gif" description="sqlite architecture (https://www.sqlite.org/zipvfs/doc/trunk/www/howitworks.wiki)" %}
21
21
22
-
A query goes through a chain of components in order to retrieve or modify data. The _front-end_ consists of the:
22
+
A query goes through a chain of components in order to retrieve or modify data. The **front-end** consists of the:
23
23
- tokenizer
24
24
- parser
25
25
- code generator
@@ -32,7 +32,7 @@ The _back-end_ consists of the:
32
32
- pager
33
33
- os interface
34
34
35
-
The **virtual machine** takes bytecode generated by the front-end as instructions. It can then issue operations on one or more tables or indexes, each of which is stored in a data structure called a B-tree. The VM is essentially a big switch statement on the type the bytecode instruction.
35
+
The **virtual machine** takes bytecode generated by the front-end as instructions. It can then perform operations on one or more tables or indexes, each of which is stored in a data structure called a B-tree. The VM is essentially a big switch statement on the type the bytecode instruction.
36
36
37
37
Each **B-tree** consists of many nodes. Each node is one page in length. The B-tree can retrieve a page from disk or save it back to disk by issuing commands to the pager.
38
38
@@ -153,7 +153,7 @@ db > .exit
153
153
~
154
154
```
155
155
156
-
Alright, we've got a working REPL. In the [next part](/part2) we'll start developing our command language. Meanwhile, here's the entire program from this part:
156
+
Alright, we've got a working REPL. In [Part 2](/part2) we'll start developing our command language. Meanwhile, here's the entire program from this part:
Copy file name to clipboardExpand all lines: part2.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ Non-SQL statements like `.exit` are called "meta-commands". They all start with
53
53
54
54
Next, we add a step that converts the line of input into our internal representation of a statement. This is our hacky version of the sqlite front-end.
55
55
56
-
Lastly, we pass the prepared statement to `execute_statement`. This will eventually become our virtual machine.
56
+
Lastly, we pass the prepared statement to `execute_statement`. This function will eventually become our virtual machine.
57
57
58
58
Notice that two of our new functions return enums indicating success or failure:
0 commit comments