Skip to content

kritarth1107/GraniteDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸͺ¨ GraniteDB

A blazing-fast, document-oriented NoSQL database engine built from scratch in Rust.

License Rust Status

MongoDB-like flexibility Β· Next-gen performance Β· 100% open source


✨ Features

πŸ”₯ Core Engine

  • Document-Based Data Model β€” Rich BSON-like value types (strings, numbers, arrays, embedded documents, dates, binary, regex, and more)
  • Schema Validation β€” Optional JSON-Schema-like validation with type checks, range/length constraints, regex patterns, enum values, and nested schemas
  • Write-Ahead Logging (WAL) β€” Segmented WAL with CRC32 checksums per entry, LSN tracking, and automatic segment rolling for crash-proof durability
  • Buffer Pool β€” LRU-based page cache with pin/unpin eviction and performance counters
  • Page-Based Storage β€” Fixed-size pages with integrity verification and overflow support

πŸ” Query Engine

  • MongoDB-Style Queries β€” Full support for $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $exists, $regex, $type, $elemMatch
  • Logical Operators β€” $and, $or, $not, $nor for complex filter compositions
  • Query Planner β€” Automatic strategy selection: ID lookup β†’ Index scan β†’ Collection scan
  • Projections β€” Return only the fields you need
  • Sort, Skip & Limit β€” Full pagination support
  • Update Operators β€” $set, $unset, $inc, $push, $pull, $rename
  • Explain Plans β€” Debug and optimize your queries

πŸ“Š Aggregation Pipeline

  • 12 Pipeline Stages β€” $match, $project, $group, $sort, $limit, $skip, $unwind, $count, $addFields, $replaceRoot, $lookup, $out
  • 9 Accumulators β€” $sum, $avg, $min, $max, $count, $push, $addToSet, $first, $last
  • JSON Pipeline Parser β€” Build pipelines from JSON, just like MongoDB

πŸ—‚οΈ Indexing

  • B-Tree Indexes β€” Ordered indexes for range queries with composite key support
  • Hash Indexes β€” O(1) exact-match lookups
  • Unique Constraints β€” Enforce uniqueness across indexed fields
  • Sparse Indexes β€” Skip documents missing the indexed field
  • Index Manager β€” Create, drop, and auto-maintain indexes on insert/update/delete

πŸ”’ Security

  • Role-Based Access Control (RBAC) β€” 5 built-in roles (read, readWrite, dbAdmin, userAdmin, root) + custom roles with 11 action types
  • Argon2 Password Hashing β€” Industry-standard password security
  • AES-256-GCM Encryption at Rest β€” Transparent data encryption with random nonce generation
  • User Management β€” Create, authenticate, grant/revoke roles, enable/disable accounts

πŸ”„ Transactions

  • ACID Transactions β€” Begin/commit/abort with automatic conflict detection
  • Multiple Isolation Levels β€” Read Uncommitted, Read Committed, Repeatable Read, Snapshot, Serializable
  • MVCC β€” Multi-Version Concurrency Control with snapshot reads and garbage collection
  • Timeout Enforcement β€” Automatic transaction abort on timeout

🌐 Networking

  • Async TCP Server β€” Built on Tokio with per-connection task spawning
  • JSON Wire Protocol β€” 25+ command types covering all database operations
  • Connection Pool β€” Configurable max connections with tracking and lifecycle management
  • Interactive CLI β€” MongoDB-like shell with commands: use, insert, find, count, delete, etc.

πŸ“‘ Replication & Sharding

  • Oplog-Based Replication β€” Capped operations log with timestamp-based sync queries
  • Replica Sets β€” Primary/Secondary/Arbiter roles with heartbeat monitoring
  • Consistent Hashing β€” Shard router with virtual nodes for even data distribution
  • Key Range Sharding β€” Automatic routing based on shard key values

πŸ“ˆ Monitoring

  • 17 Atomic Metrics β€” Queries, inserts, updates, deletes, connections, bytes I/O, WAL writes, buffer pool hits/misses, index lookups, collection scans, transactions
  • Server Status β€” Real-time statistics via the wire protocol

πŸ“ Project Structure

GraniteDB/
β”œβ”€β”€ Cargo.toml                      # Dependencies & build config
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ lib.rs                      # Library root (17 module re-exports)
β”‚   β”œβ”€β”€ main.rs                     # Server entry point (CLI + boot)
β”‚   β”‚
β”‚   β”œβ”€β”€ config/                     # βš™οΈ Configuration
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── settings.rs             # Server, storage, auth, replication settings
β”‚   β”‚
β”‚   β”œβ”€β”€ error/                      # ❌ Error Types
β”‚   β”‚   └── mod.rs                  # 30+ typed error variants
β”‚   β”‚
β”‚   β”œβ”€β”€ document/                   # πŸ“„ Document Model
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ bson.rs                 # BSON-like value types with comparisons
β”‚   β”‚   β”œβ”€β”€ document.rs             # Document struct with metadata & TTL
β”‚   β”‚   └── validation.rs           # Schema validation engine
β”‚   β”‚
β”‚   β”œβ”€β”€ storage/                    # πŸ’Ύ Storage Engine
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ engine.rs               # Unified storage (WAL + memory + disk)
β”‚   β”‚   β”œβ”€β”€ wal.rs                  # Write-Ahead Log (segmented, checksummed)
β”‚   β”‚   β”œβ”€β”€ page.rs                 # Fixed-size page management
β”‚   β”‚   β”œβ”€β”€ buffer_pool.rs          # LRU page cache
β”‚   β”‚   └── disk.rs                 # Low-level disk I/O
β”‚   β”‚
β”‚   β”œβ”€β”€ collection/                 # πŸ“¦ Collections
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── collection.rs           # CRUD + operator queries + capped collections
β”‚   β”‚
β”‚   β”œβ”€β”€ database/                   # πŸ—„οΈ Database Management
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── database.rs             # Multi-collection container + stats
β”‚   β”‚
β”‚   β”œβ”€β”€ query/                      # πŸ” Query Engine
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ parser.rs               # JSON β†’ FilterExpr + UpdateOperation
β”‚   β”‚   β”œβ”€β”€ filter.rs               # Filter expression tree
β”‚   β”‚   β”œβ”€β”€ planner.rs              # Strategy selection + explain
β”‚   β”‚   └── executor.rs             # Execution with sort/skip/limit/projection
β”‚   β”‚
β”‚   β”œβ”€β”€ index/                      # πŸ—‚οΈ Indexing
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ btree.rs                # B-Tree index (range + composite)
β”‚   β”‚   β”œβ”€β”€ hash_index.rs           # Hash index (O(1) lookups)
β”‚   β”‚   └── manager.rs              # Index lifecycle management
β”‚   β”‚
β”‚   β”œβ”€β”€ aggregation/                # πŸ“Š Aggregation
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ stages.rs               # Stage & accumulator definitions
β”‚   β”‚   └── pipeline.rs             # Pipeline executor + JSON parser
β”‚   β”‚
β”‚   β”œβ”€β”€ transaction/                # πŸ”„ Transactions
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ manager.rs              # Begin/commit/abort + conflict detection
β”‚   β”‚   └── mvcc.rs                 # Multi-Version Concurrency Control
β”‚   β”‚
β”‚   β”œβ”€β”€ auth/                       # πŸ”’ Authentication & Security
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ user.rs                 # User management (Argon2 hashing)
β”‚   β”‚   β”œβ”€β”€ rbac.rs                 # Role-Based Access Control
β”‚   β”‚   └── encryption.rs           # AES-256-GCM encryption at rest
β”‚   β”‚
β”‚   β”œβ”€β”€ network/                    # 🌐 Networking
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ server.rs               # Async TCP server (Tokio)
β”‚   β”‚   β”œβ”€β”€ protocol.rs             # JSON wire protocol (25+ commands)
β”‚   β”‚   β”œβ”€β”€ handler.rs              # Request routing & execution
β”‚   β”‚   └── connection.rs           # Connection tracking & pool
β”‚   β”‚
β”‚   β”œβ”€β”€ replication/                # πŸ“‘ Replication
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ oplog.rs                # Capped operations log
β”‚   β”‚   └── replica.rs              # Replica set management
β”‚   β”‚
β”‚   β”œβ”€β”€ sharding/                   # πŸ”€ Sharding
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   β”œβ”€β”€ shard.rs                # Shard definition + key ranges
β”‚   β”‚   └── router.rs               # Consistent hashing router
β”‚   β”‚
β”‚   β”œβ”€β”€ cursor/                     # πŸ“œ Cursors
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── cursor.rs               # Batch iteration over results
β”‚   β”‚
β”‚   β”œβ”€β”€ metrics/                    # πŸ“ˆ Monitoring
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── collector.rs            # 17 atomic performance counters
β”‚   β”‚
β”‚   β”œβ”€β”€ utils/                      # πŸ”§ Utilities
β”‚   β”‚   β”œβ”€β”€ mod.rs
β”‚   β”‚   └── helpers.rs              # ID gen, hashing, formatting
β”‚   β”‚
β”‚   └── cli/                        # πŸ’» CLI Client
β”‚       └── main.rs                 # Interactive shell client

πŸš€ Quick Start

Prerequisites

  • Rust β‰₯ 1.85 (2024 edition)

Build & Run

# Clone
git clone https://github.com/kritarth1107/GraniteDB.git
cd GraniteDB

# Build
cargo build --release

# Start the server
cargo run --release -- --port 6380 --data-dir ./data

# In another terminal, connect with the CLI
cargo run --release --bin granite-cli

CLI Usage

granite:default> use mydb
Switched to database: mydb

granite:mydb> createcol users

granite:mydb> insert users {"name": "John", "age": 30, "email": "john@example.com"}

granite:mydb> find users {"age": {"$gt": 25}}

granite:mydb> count users

granite:mydb> delete users {"name": "John"}

Wire Protocol (TCP)

Connect via TCP and send JSON commands:

{
  "request_id": "abc-123",
  "command": {
    "type": "insert_one",
    "database": "mydb",
    "collection": "users",
    "document": {"name": "Alice", "age": 28}
  }
}

πŸ“‹ Configuration

Create a granitedb.json file:

{
  "server": {
    "host": "0.0.0.0",
    "port": 6380,
    "max_connections": 10000
  },
  "storage": {
    "data_dir": "./data/granite",
    "page_size": 16384,
    "buffer_pool_pages": 4096,
    "wal_fsync": true
  },
  "auth": {
    "enabled": false
  },
  "logging": {
    "level": "info"
  }
}

πŸ—οΈ Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  CLI Client  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚ TCP/JSON
                    β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  TCP Server  β”‚   ← Tokio async, connection pool
                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   Handler    β”‚   ← Request routing, auth checks
                    β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚              β”‚              β”‚
     β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
     β”‚   Database  β”‚ β”‚  Query  β”‚ β”‚  Aggregation β”‚
     β”‚  Manager    β”‚ β”‚ Engine  β”‚ β”‚   Pipeline   β”‚
     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
            β”‚              β”‚              β”‚
     β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
     β”‚              Collection Manager            β”‚
     β”‚         (CRUD + Schema Validation)         β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚             β”‚             β”‚
  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β–Όβ”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”
  β”‚   Storage   β”‚ β”‚  Index β”‚ β”‚ Transaction β”‚
  β”‚   Engine    β”‚ β”‚Manager β”‚ β”‚   Manager   β”‚
  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
    β”Œβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”
    β”‚    β”‚    β”‚
  β”Œβ”€β–Όβ”€β”β”Œβ–Όβ”€β”€β”β”Œβ–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚WALβ”‚β”‚Buf β”‚β”‚  Disk   β”‚
  β”‚   β”‚β”‚Poolβ”‚β”‚ Manager β”‚
  β””β”€β”€β”€β”˜β””β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🀝 Contributing

We welcome contributions! GraniteDB is 100% open source under the Apache 2.0 license.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“œ License

This project is licensed under the Apache License 2.0 β€” see the LICENSE file for details.


Built with ❀️ and Rust πŸ¦€

GraniteDB β€” The database that's solid as granite.

About

GraniteDB is a high-performance, document-oriented NoSQL database built for speed, scale, and developer-first experience

Resources

License

Stars

19 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages