Repository navigation
Expand file tree
/
Copy pathplan.txt
More file actions
1176 lines (871 loc) · 20.5 KB
/
Copy pathplan.txt
File metadata and controls
1176 lines (871 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MYDB — DATABASE IMPLEMENTATION PLAN
====================================
Goal
----
Build a learning-focused database engine primarily in C.
Architecture:
Client / Test CLI / Web Server
|
v
Database API
|
v
Database Engine
|
+---------+----------+
| | |
v v v
Catalog Storage Transactions
|
+--------+--------+
| | |
v v v
Pages Buffer Records
Pool
|
v
Disk Files
Later:
Rust + DataFusion
|
TableProvider
|
FFI
|
Database API
|
C Database
IMPORTANT ARCHITECTURAL RULES
------------------------------
1. C is the database engine.
2. Rust/DataFusion is added later and is NOT part of the first C implementation.
3. The C engine should not know about SQL or DataFusion.
4. The web server is a client of the database, not part of the storage engine.
5. Only the low-level storage/file layer should directly perform filesystem I/O.
6. Higher layers work with database abstractions such as pages, records, table IDs,
page IDs, and record IDs.
7. Do not make CSV the native storage format.
8. The native database format should be page-based binary storage.
9. Build the system incrementally. Do not implement WAL, MVCC, B+ trees, etc.
before the basic page/storage system works.
============================================================
1. DATABASE API
============================================================
Purpose
-------
Provide the stable interface through which clients access the database.
Possible API:
Database *db_open(const char *path);
void db_close(Database *db);
int db_create_table(...);
int db_drop_table(...);
int db_insert(...);
int db_get(...);
int db_update(...);
int db_delete(...);
int db_scan(...);
The API should not expose internal storage details.
Clients can eventually include:
- C test programs
- a CLI
- a web server
- Rust/DataFusion through FFI
============================================================
2. DATABASE / CATALOG
============================================================
Purpose
-------
The catalog describes what exists in the database.
Logical structure:
Database
|
+-- Table: users
| +-- id
| +-- name
| +-- age
|
+-- Table: orders
|
+-- Table: products
The catalog stores metadata such as:
- database name
- table ID
- table name
- schema
- column names
- column types
- table storage metadata
- indexes associated with a table
- eventually statistics
Important distinction:
Catalog = "What exists?"
Storage = "Where are the actual bytes?"
Do NOT store all table rows inside the Table struct.
============================================================
3. DATABASE STRUCTURE
============================================================
Logical representation:
Database
|
+-- tables[]
|
+-- catalog metadata
|
+-- storage metadata
Conceptually:
typedef struct {
uint32_t table_id;
char name[64];
Schema *schema;
...
} Table;
typedef struct {
char name[64];
Table *tables;
uint32_t table_count;
...
} Database;
These structures represent metadata, not the entire physical contents
of the database.
A table should point indirectly to its physical storage through table IDs,
page IDs, roots, etc.
============================================================
4. NATIVE STORAGE FORMAT — PAGES
============================================================
Do NOT use CSV as the native database format.
CSV is useful for:
- importing data
- exporting data
- simple early experiments
But the actual database should eventually use binary page files.
Example:
mydb/
catalog
data/
table_001.dat
table_002.dat
table_003.dat
A table file can be divided into fixed-size pages:
table_001.dat
+----------+
| Page 0 |
+----------+
| Page 1 |
+----------+
| Page 2 |
+----------+
| Page 3 |
+----------+
| ... |
+----------+
Start with a page size such as:
PAGE_SIZE = 4096 bytes
Later this can be made configurable if desired.
============================================================
5. PAGE MANAGER / FILE MANAGER
============================================================
Purpose
-------
Provide low-level access to database pages.
Responsibilities:
- open database files
- close database files
- read a page
- write a page
- allocate a new page
- identify page offsets
- manage file growth
Core operations:
read_page(page_id, buffer)
write_page(page_id, buffer)
allocate_page()
For a fixed page size:
byte_offset = page_id * PAGE_SIZE
The rest of the database should not repeatedly call:
fopen()
fread()
fwrite()
fseek()
pread()
pwrite()
Instead, those operations should be concentrated in the low-level
file/page layer.
============================================================
6. PAGE STRUCTURE
============================================================
A page is a fixed-size block of database storage.
Example:
4096 byte page
+--------------------------------+
| Page Header |
+--------------------------------+
| |
| Records / tuples |
| |
| |
| free space |
| |
| |
+--------------------------------+
| Slot Directory |
+--------------------------------+
The page header can eventually contain:
- page ID
- page type
- number of slots
- free-space information
- other metadata
Do not initially overdesign the header.
============================================================
7. RECORD / TUPLE MANAGER
============================================================
Purpose
-------
Convert logical records into physical bytes stored inside pages.
Example logical record:
User:
id = 42
name = "Alice"
age = 24
Physical representation:
bytes stored inside a page
Responsibilities:
- insert record into a page
- find record in a page
- update record
- delete record
- serialize record
- deserialize record
- manage free space
Use a Record ID / RID concept:
RecordId:
page_id
slot_id
Example:
users
|
+-- RecordId
page_id = 42
slot_id = 7
This allows the database to locate a physical record.
============================================================
8. SLOTTED PAGES
============================================================
Use a slotted-page design for variable-size records.
Conceptually:
+--------------------------+
| Page Header |
+--------------------------+
| Record |
| Record |
| Record |
| |
| Free Space |
| |
+--------------------------+
| Slot 0 |
| Slot 1 |
| Slot 2 |
+--------------------------+
The slot directory maps logical slot IDs to physical record locations.
This makes records movable within a page without changing their
logical RecordId.
============================================================
9. SERIALIZATION
============================================================
Never assume that a C struct can simply be dumped to disk forever.
Avoid making this the database format:
fwrite(&struct, sizeof(struct), 1, file);
because of:
- padding
- alignment
- platform differences
- future schema changes
Instead define an explicit binary representation.
Example:
uint64_t id
int32_t age
uint32_t name_length
name bytes
Then provide:
serialize_record()
deserialize_record()
Use fixed-width integer types:
uint8_t
uint16_t
uint32_t
uint64_t
============================================================
10. BUFFER POOL
============================================================
Purpose
-------
Keep frequently used database pages in RAM.
Without a buffer pool:
Database
|
v
Disk
|
v
Database
|
v
Disk
With a buffer pool:
Database
|
v
Buffer Pool
|
+-- Page 5
+-- Page 18
+-- Page 42
+-- Page 91
|
v
Disk
Responsibilities:
- fetch page
- pin page
- unpin page
- mark page dirty
- evict page
- flush page
Later implement a replacement strategy such as:
LRU
Clock
Do not start with a sophisticated eviction policy.
============================================================
11. INDEX MANAGER
============================================================
Purpose
-------
Find records efficiently without scanning every table page.
Without an index:
search id=5000
|
v
scan every page
With an index:
id=5000
|
v
B+ Tree
|
v
RecordId
|
v
page + slot
|
v
Buffer Pool
|
v
Record
Start with a B+ tree.
Index API can eventually resemble:
index_insert(key, record_id)
index_search(key)
index_delete(key)
Do NOT implement the index before basic page and record storage works.
============================================================
12. TRANSACTION MANAGER
============================================================
Later component.
Responsibilities:
- begin transaction
- commit
- abort
- transaction IDs
- transaction state
Example:
BEGIN
update A
update B
COMMIT
============================================================
13. CONCURRENCY / LOCK MANAGER
============================================================
Later component.
Responsibilities:
- coordinate concurrent transactions
- shared locks
- exclusive locks
- lock table
- eventually deadlock handling
Do not build this initially.
============================================================
14. WAL / RECOVERY
============================================================
Later component.
WAL = Write-Ahead Logging.
Purpose:
protect the database from crashes and incomplete writes.
Conceptually:
Transaction
|
v
WAL
|
v
Database Pages
|
v
Disk
After a crash:
Open database
|
v
Read WAL
|
v
Recover database
============================================================
15. CHECKPOINTING
============================================================
Later.
Checkpointing limits how much WAL must be processed during recovery.
Conceptually:
WAL
|
+---- changes
|
v
Checkpoint
|
v
consistent database state
============================================================
16. RECOVERY MANAGER
============================================================
Later.
Responsibilities:
- detect incomplete work
- replay WAL
- restore consistent state
- coordinate checkpoints
============================================================
17. QUERY / DATA ACCESS BOUNDARY
============================================================
The C database should NOT become a SQL engine.
Eventually:
SQL
|
v
DataFusion
|
v
Rust TableProvider
|
v
Rust FFI
|
v
C Database API
|
v
Storage Engine
DataFusion handles:
- SQL parsing
- logical planning
- optimization
- physical planning
- query execution
C handles:
- catalog/storage access
- records
- pages
- indexes
- transactions
- persistence
- recovery
The C engine should return data rather than printing it.
============================================================
18. WEB SERVER
============================================================
The web server is a client of the C database.
Example:
POST /users
|
v
db_insert(...)
GET /users/42
|
v
db_get(...)
PUT /users/42
|
v
db_update(...)
The web server should NOT directly access database files.
Architecture:
Web Server
|
v
Database API
|
v
C Database
|
v
Storage Engine
|
v
Disk
============================================================
19. TEST CLIENT / CLI
============================================================
Before building the web server, create a simple C test program.
Example:
./mydb_test
create database
create table
insert record
get record
update record
delete record
scan table
This allows the storage engine to be developed independently of HTTP.
============================================================
20. PHYSICAL DATABASE LAYOUT
============================================================
Do not commit too early to one exact directory structure.
A possible early layout:
mydb/
catalog/
data/
table_001.dat
table_002.dat
Later:
mydb/
catalog/
data/
table_001.dat
table_002.dat
indexes/
table_001/
wal/
...
The important abstraction is:
database
|
+-- catalog
|
+-- tables
|
+-- pages
|
+-- records
NOT:
database
|
+-- table.csv
============================================================
21. IMPLEMENTATION ORDER
============================================================
PHASE 0 — C BASICS
Know:
- pointers
- structs
- malloc/free
- arrays
- uint32_t / uint64_t
- memcpy/memset
- file descriptors
- open/close
- pread/pwrite
- offsets
- error handling
PHASE 1 — FILE / PAGE STORAGE
Implement:
- database creation/open
- database close
- page size
- page IDs
- allocate page
- read page
- write page
First test:
create database
|
v
allocate page
|
v
write bytes
|
v
close
|
v
reopen
|
v
read page
|
v
verify bytes
PHASE 2 — PAGE FORMAT
Implement:
- page header
- free-space tracking
- page IDs
- basic slot directory
Test:
insert bytes into page
read them back
update them
delete them
PHASE 3 — RECORD MANAGER
Implement:
- schema representation
- record serialization
- record deserialization
- insert record
- get record
- update record
- delete record
- RecordId
PHASE 4 — TABLE / CATALOG
Implement:
- database metadata
- table metadata
- table IDs
- table schemas
- create table
- drop table
- find table by name
Now:
Database
|
+-- users
+-- orders
+-- products
maps to physical storage.
PHASE 5 — BUFFER POOL
Implement:
- buffer frames
- page fetching
- pin/unpin
- dirty pages
- flushing
- basic eviction
PHASE 6 — TABLE SCANS
Implement:
db_scan(table_id)
Flow:
table
|
v
pages
|
v
records
|
v
return records
This is the first point where your database can behave like a basic
persistent table store.
PHASE 7 — B+ TREE
Implement:
- search
- insert
- split
- delete
- merge/redistribution
Connect:
key
|
v
B+ Tree
|
v
RecordId
|
v
Buffer Pool
|
v
Page
|
v
Record
PHASE 8 — TRANSACTIONS
Implement:
- transaction IDs
- begin
- commit
- abort
PHASE 9 — CONCURRENCY
Implement:
- locking
- shared/exclusive locks
- basic conflict handling
Later:
- MVCC
- snapshots
- isolation levels
PHASE 10 — WAL
Implement:
- log records
- append log
- flush log
- LSNs
- page LSNs
PHASE 11 — RECOVERY
Implement:
- crash detection
- WAL replay
- recovery
- checkpoints
PHASE 12 — WEB SERVER
Only after the database API is reasonably stable.
The server should simply translate HTTP requests into database API calls.
PHASE 13 — RUST / DATAFUSION
Only after the C database works.