Skip to content

FaaizS/DiskDBLite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DiskDBLite

Overview

DiskDBLite is a from-scratch, disk-backed database engine built to understand how real databases store, index, and retrieve data at the byte and page level. The project focuses on core systems concepts such as B-Tree indexing, slotted page layouts, and SQL parsing — without relying on existing database libraries.

Features

  • Disk-backed storage - Data persists to disk using fixed-size 4KB pages
  • B-Tree indexing - Efficient O(log n) key lookups with automatic page splitting
  • SQL interface - Supports CREATE TABLE, INSERT, and SELECT statements
  • Interactive shell - REPL for executing SQL commands
  • Persistence - Database survives restarts with full data integrity

Quick Start

Build

mvn clean package

Run Interactive Shell

# Using Maven
mvn exec:java -Dexec.mainClass="com.diskdblite.Main" -Dexec.args="mydb.db"

# Or using the JAR directly
java -jar target/diskdblite-1.0-SNAPSHOT.jar mydb.db

Example Session

╔═══════════════════════════════════════════════════════╗
║         DiskDBLite v1.0.0 - Disk-Backed Database      ║
╚═══════════════════════════════════════════════════════╝

Created new database: mydb.db

Type 'help' for commands, 'exit' to quit.

diskdb> CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR, email VARCHAR);
Table 'users' created.
Time: 5ms

diskdb> INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
1 row inserted.
Time: 2ms

diskdb> INSERT INTO users VALUES (2, 'Bob', 'bob@example.com');
1 row inserted.
Time: 1ms

diskdb> SELECT * FROM users;
id | name | email
------------------
1 | Alice | alice@example.com
2 | Bob | bob@example.com
(2 row(s))
Time: 1ms

diskdb> SELECT * FROM users WHERE id = 1;
id | name | email
------------------
1 | Alice | alice@example.com
(1 row(s))
Time: 0ms

diskdb> exit
Goodbye!

Supported SQL

CREATE TABLE

CREATE TABLE tablename (
    column1 INT PRIMARY KEY,
    column2 VARCHAR,
    column3 INTEGER
)

INSERT

INSERT INTO tablename VALUES (1, 'text', 42)

SELECT

-- Select all rows
SELECT * FROM tablename

-- Select with WHERE clause
SELECT * FROM tablename WHERE column = value

Shell Commands

Command Description
help Show help
tables List all tables
describe <table> Show table schema
exit Exit the shell

Architecture

┌─────────────────────────────────────────────────────────────┐
│                        SQL Shell                            │
│                      (Main.java)                            │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                      SQL Engine                             │
│                    (sql package)                            │
│         Lexer → Parser → Statement Objects                  │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                    Query Executor                           │
│                   (query package)                           │
│              High-level CRUD operations                     │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                       Catalog                               │
│                  (catalog package)                          │
│              Table metadata management                      │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                        B-Tree                               │
│                   (btree package)                           │
│          Key-value storage with indexing                    │
│     BTree ← BTreeLeafPage / BTreeInternalPage               │
└─────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌─────────────────────────────────────────────────────────────┐
│                      Storage Layer                          │
│                  (storage package)                          │
│   Page → SlottedPage → FileManager → Disk                   │
└─────────────────────────────────────────────────────────────┘

Project Structure

src/main/java/com/diskdblite/
├── Main.java                    # Interactive SQL shell
├── storage/
│   ├── Constants.java           # PAGE_SIZE, etc.
│   ├── FileManager.java         # Low-level file I/O
│   ├── Page.java                # 4KB page abstraction
│   ├── PageType.java            # Page type enum
│   └── SlottedPage.java         # Variable-length record storage
├── btree/
│   ├── BTree.java               # B-Tree index structure
│   ├── BTreeLeafPage.java       # Leaf nodes (store data)
│   └── BTreeInternalPage.java   # Internal nodes (routing)
├── catalog/
│   ├── Catalog.java             # Table registry
│   ├── TableInfo.java           # Table metadata
│   ├── ColumnInfo.java          # Column metadata
│   ├── ColumnType.java          # INT, VARCHAR types
│   └── Row.java                 # Row representation
├── query/
│   ├── QueryExecutor.java       # Execute operations
│   └── QueryResult.java         # Query results
└── sql/
    ├── Lexer.java               # SQL tokenizer
    ├── Token.java               # Token types
    ├── Parser.java              # SQL parser
    ├── Statement.java           # AST nodes
    ├── ParseException.java      # Parse errors
    └── SQLEngine.java           # Parsing + execution

Design Principles

  1. Disk-backed storage - All data persists to disk in 4KB pages
  2. Explicit page layout - Clear byte-level serialization
  3. Separation of concerns - Each layer has a single responsibility
  4. Correctness over performance - Readable code, not optimized
  5. Minimal features - INSERT and SELECT only (no UPDATE/DELETE)
  6. No concurrency - Single-threaded, no locks needed
  7. No transactions - No ACID guarantees beyond durability

Running Tests

# Run all tests
mvn test

# Run specific test class
mvn test -Dtest=IntegrationTest

# Run with output
mvn test -Dtest=IntegrationTest -q

Key Concepts Demonstrated

  • B-Tree indexing with automatic page splitting
  • Slotted page layout for variable-length records
  • Serialization of structured data to/from bytes
  • SQL parsing with lexer/parser architecture
  • Layered architecture for database systems

Limitations

  • Primary key must be INTEGER type
  • No UPDATE or DELETE operations
  • No transactions or recovery
  • No concurrent access
  • No query optimizer
  • SELECT only supports * (no column selection)
  • WHERE only supports equality (=)

License

Educational project - not for production use.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages