MCPcopy Index your code
hub / github.com/MohamedAbdeen21/niwid-db

github.com/MohamedAbdeen21/niwid-db @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
472 symbols 1,557 edges 42 files 34 documented · 7%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

niwid-db

niwid-db is a toy database management system built from scratch, designed to explore fundamental database concepts, including memory and disk management, ACID properties, basic transaction handling, indexing, and query execution.

Table of Contents

  1. Features
  2. Getting Started
  3. Examples
  4. Some Implementation Details
  5. Contributing

Features

  • ACID Compliance via Shadow Paging: Ensures Atomicity, Consistency, Isolation (read-committed), and Durability by implementing shadow paging, maintaining a stable, crash-resistant state. Shadow paging allows a table to have only a single writer for simplified concurrency control.

  • Transaction Management: Supports commits and rollbacks to execute or discard changes within a transaction block to ensure transactional atomicity and isolation.

  • Custom Execution Engine: A simple query engine that processes SQL statements, validates the query and datatypes, and performs basic DDL, TCL, and DML operations. Also supports EXPLAIN and EXPLAIN ANALYZE. Unoptimized logical plans are executed directly, without physical plan generation or optimizations yet.

  • SQL Parsing: The only part not written from scratch. Leverages the sqlparser-rs crate for SQL syntax parsing.

  • B+ Tree Indexing: Implements a B+ Tree index to ensure uniqueness, enabling efficient lookups and range queries for unique columns. Since there is no optimizer yet, you can force an index lookup using the PREWHERE clause. Check out index.slt for more examples.

  • Constraints: Supports NOT NULL and UNIQUE constraints to enforce data integrity, although primary keys and foreign keys (referential integrity) are not supported.

  • Joins: Performs basic nested loop join operations between tables, allowing for relational queries.

Getting Started

Prerequisites

  • Rust: niwid-db is written in Rust.

Installation

  1. Clone the repository: bash git clone https://github.com/MohamedAbdeen21/niwid-db.git cd niwid-db

  2. Run the server: bash cargo run --release --bin idk-server

  3. Connect to the server from a different terminal: bash nc localhost 8080

Usage

niwid-db can execute SQL queries for managing tables, running transactions, and executing basic SQL commands.

Example:

CREATE TABLE users (
    id INT PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT
);

INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');

You can also use transactions to batch operations:

BEGIN;
UPDATE users SET email = 'bob@example.com' WHERE id = 1;
ROLLBACK; -- Discards the update

Examples

For more examples, check out:

Some implementation details

  • Slotted pages, with string indirection and a B+ Tree Index.
  • LRU page eviction policy
  • Shadow-Paging for ACID. No MVCC, OCC, 2PL, or WAL.
  • Simple query engine, directly executes the raw logical plan. No optimizer, or physical plan builder. The project focused on exploring the internals of the database, not the query engine.
  • Catalog as a read-only Table, check it out using SELECT * FROM __CATALOG__; Using sqlparser.rs is the only part not written from scratch. I wrote my fair share of parsers (and contributed some to sqlparser-rs), but again, this was not the main focus of the project.
  • Demo website made with HTMX and Askama.

Contributing

Contributions are always welcome! Feel free to submit issues or pull requests.

Extension points exported contracts — how you extend this code

AsBytes (Interface)
(no doc) [6 implementers]
src/types/mod.rs
Executable (Interface)
(no doc) [16 implementers]
src/execution/mod.rs
Serialize (Interface)
(no doc) [4 implementers]
src/pages/traits.rs
DiskWritable (Interface)
Any type that can be written to disk must implement this trait currently only implemented by Page, but maybe I'll need a [1 …
src/disk_manager/mod.rs
TupleExt (Interface)
(no doc) [2 implementers]
src/tuple/mod.rs
Replacer (Interface)
(no doc) [1 implementers]
src/buffer_pool/replacer.rs
Primitive (Interface)
(no doc) [5 implementers]
src/types/mod.rs

Core symbols most depended-on inside this repo

iter
called by 116
src/table/mod.rs
execute_sql
called by 54
src/context/mod.rs
get_page_id
called by 33
src/pages/table_page.rs
insert
called by 26
src/table/mod.rs
reader
called by 24
src/buffer_pool/frame.rs
fields
called by 21
src/execution/result_set.rs
fetch_frame
called by 20
src/buffer_pool/mod.rs
insert
called by 20
src/indexes/b_plus_tree/btree.rs

Shape

Method 314
Function 88
Class 57
Interface 7
Enum 6

Languages

Rust100%

Modules by API surface

src/types/mod.rs38 symbols
src/indexes/b_plus_tree/btree.rs36 symbols
src/table/mod.rs35 symbols
src/pages/table_page.rs30 symbols
src/sql/logical_plan/mod.rs27 symbols
src/sql/logical_plan/plan.rs25 symbols
src/pages/indexes/b_plus_tree/mod.rs24 symbols
src/context/mod.rs24 symbols
src/buffer_pool/mod.rs22 symbols
src/execution/result_set.rs20 symbols
src/tuple/mod.rs18 symbols
src/disk_manager/mod.rs17 symbols

For agents

$ claude mcp add niwid-db \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact