LightDB is an in-memory, append-only, single-table database.
-
In-memory: Database files are located in local device.
-
Append-only: Only insert operation is available, no delete or update.
-
Single-table: There is only a pre-defined table generated in the source code.
LightDB is a tiny clone of SQLite, which uses BTrees for fast lookups and hence insert/delete/update operations.
- If you are not familiar with Btrees, read this tutorial before starting.
BTree is one type of the balanced trees;
-
It provides O(logn) search cost in the worst case.
-
Insert/delete a cell that is already found is O(1).
-
Re-balancing after an insert is 0(1).
-
Traversing a range of values is fast! (Unlike hashmap)
- This property allows us to keep rows sorted in database easily.
column | type |
---|---|
id | integer |
username | varchar(32) |
varchar(255) |
- Note that this table can be adjusted within the code.
make
./lightdb {db name}.db
.db extension is not required however .db files can be easily removed by:
make cleandb
insert {id} {username} {email}
select
Note that, rows are printed in ascending order by id.
.exit
.btree
.constants
LightDB sends a warning message to the prompt in the following cases:
-
Unrecognized commands.
-
Negative id field.
-
Existing id field. ID is the primary key of the table, so it must be unique.
-
Too long username field.
-
Too long email field.
-
Syntax error.
- E-mail regex validation.
I give special thanks and credit to Connor Stack and his great tutorial here.