A lightweight SQLite-inspired database implementation in C, built for educational purposes. This project demonstrates fundamental database concepts including B-trees, paging, and basic SQL operations.
- Custom REPL (Read-Eval-Print Loop) interface
- Basic SQL operations (INSERT and SELECT)
- B-tree data structure for efficient data storage and retrieval
- Persistent storage with a simple file-based approach
- Memory management with paging system
- Row-based storage format
- Row Structure:
- ID (integer)
- Username (varchar, max 32 chars)
- Email (varchar, max 255 chars)
- B-tree Implementation:
- Leaf nodes and internal nodes
- Automatic node splitting when full
- Parent pointer for tree traversal
- Page size: 4096 bytes
- Maximum pages: 100
To compile the project, use a C compiler (gcc recommended):
gcc -o db main.c
- Start the database with a file name:
./db mydb.db
- Available commands:
-- Insert a new record
insert 1 user1 user1@example.com
-- Select all records
select
-- Meta commands
.exit -- Exit the program
.btree -- Display the B-tree structure
.constants -- Show internal constants
The project includes RSpec tests. To run them:
- Install Ruby and RSpec
- Run the test suite:
rspec spec/main_spec.rb
main.c
: Main source code containing the database implementationspec/main_spec.rb
: Test specifications.gitignore
: Git ignore rulesREADME.md
: This documentation file
The database is implemented with the following key components:
- Pager: Handles reading/writing pages to disk
- Table: Manages the database structure
- Cursor: Abstracts the position in the table
- B-tree: Implements the tree data structure for storing rows
- Node: Represents leaf and internal nodes in the B-tree
- Fixed schema (id, username, email)
- Basic SQL support (only INSERT and SELECT)
- No DELETE or UPDATE operations
- No transaction support
- Maximum 100 pages
Feel free to submit issues and enhancement requests!
This project is open source and available under the MIT License.
This project is inspired by the "Let's Build a Simple Database" tutorial series by Connor Stack, implementing a SQLite clone from scratch in C. The tutorial provides an excellent step-by-step guide to understanding database internals by building one from the ground up.