GrumpyDB
A document-oriented object database written in Rust.
GrumpyDB stores schema-less JSON-like documents on disk with B+Tree indexing, page-based storage, WAL durability, and multi-tenant isolation. It can be used as an embedded library (linked directly into your Rust app) or as a standalone server accessed over TCP+TLS with JWT authentication and role-based access control.
Quick Start
Embedded — no server needed
grumpy> use myapp
Switched to database "myapp"
grumpy > db.
Collection "users" created
grumpy > db..
Inserted: 3df9dde6-...
grumpy > db..
Inserted: e7f8a9b0-...
grumpy > db..
grumpy > db..
Index "by_age" created on field "age"
grumpy > db..
grumpy > db..
Client/Server — multi-tenant with auth
# Terminal 1: Start the server
# Terminal 2: Connect with the shell
Connected to GrumpyDB at localhost:6380
Authenticated as admin@_system
grumpy> use myapp
Switched to database "myapp"
grumpy > db..
Inserted: a1b2c3d4-...
grumpy > db..
1
Use as a Rust Library
Add GrumpyDB to your Cargo.toml:
[]
= "3.1"
Single-collection (simple key-value)
use ;
use Uuid;
use BTreeMap;
let mut db = open.unwrap;
let key = new_v4;
let doc = Object;
db.insert.unwrap;
let result = db.get.unwrap;
assert!;
db.close.unwrap;
Multi-collection with secondary indexes
use Database;
let mut db = open.unwrap;
db.create_collection.unwrap;
db.create_index.unwrap;
let key = new_v4;
db.insert.unwrap;
// Query by index
let results = db.query.unwrap;
db.close.unwrap;
Thread-safe concurrent access
use SharedDatabase;
let db = open.unwrap;
// Clone is cheap (Arc), share across threads
let db2 = db.clone;
spawn;
let count = db.document_count.unwrap;
grumpy-repl
An interactive REPL with JavaScript-like syntax, relaxed JSON (unquoted keys, single quotes, trailing commas), and line editing with history.
# Embedded (no server)
# Connected (TCP)
Commands
| Category | Commands |
|---|---|
| Database | use <name> |
| Collections | db.createCollection("x"), db.dropCollection("x"), db.collections() |
| CRUD | db.x.insert({...}), db.x.get("id"), db.x.find(), db.x.find({age: 30}), db.x.update("id", {...}), db.x.delete("id"), db.x.count() |
| Indexes | db.x.createIndex("name", "field"), db.x.query("name", value), db.x.queryRange("name", start, end), db.x.indexes() |
| References | $ref("coll", "uuid"), db.x.resolve("id"), db.x.resolveDeep("id") |
| Maintenance | db.x.compact(), db.x.stats(), db.flush() |
Server
Architecture
Clients (grumpy-repl, Rust driver, TypeScript driver, nc/telnet)
│
│ TCP + TLS 1.3 (rustls)
│ RESP-like text protocol
│ JWT authentication
│
┌───▼──────────────────────────────────────────┐
│ GrumpyDB Server │
│ ┌─────────────────────────────────────────┐ │
│ │ TLS · Protocol Parser · RBAC Enforcer │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────▼────────────────────────┐ │
│ │ Auth Store (argon2 + JWT HS256) │ │
│ └────────────────┬────────────────────────┘ │
│ ┌────────────────▼────────────────────────┐ │
│ │ Engine: Tenants · Databases · │ │
│ │ Collections · B+Tree · WAL · Buffer │ │
│ └─────────────────────────────────────────┘ │
└──────────────────────────────────────────────┘
Running the server
# Plaintext (dev)
# TLS (auto-generates self-signed cert)
# With config file
At first launch, a default admin is created: admin (password: admin) in tenant _system.
Configuration (grumpydb.toml)
[]
= "0.0.0.0:6380"
= 1024
= "./data"
[]
= true
# cert_file = "server.crt" # auto-generated if absent
# key_file = "server.key"
[]
= 3600 # 1 hour
= 604800 # 7 days
User & tenant management
Connect as server admin via nc localhost 6380:
LOGIN _system admin admin
TOKEN <jwt>
CREATE TENANT acme
CREATE USER alice@acme s3cr3t
GRANT tenant_admin ON @acme TO alice@acme
LIST TENANTS
LIST USERS @acme
Notation
| Syntax | Meaning |
|---|---|
alice |
User alice in current tenant |
alice@acme |
User alice in tenant acme |
mydb |
Database (or collection if USE is active) |
mydb@acme |
Database in tenant acme |
users:mydb |
Collection users in database mydb |
users:mydb@acme |
Collection in database in tenant |
@acme |
Tenant scope (for GRANT/REVOKE) |
RBAC roles
| Role | Permissions |
|---|---|
server_admin |
Everything (cross-tenant) |
tenant_admin |
Manage databases, users, full CRUD within tenant |
db_admin |
Manage collections, indexes, CRUD within a database |
read_write |
INSERT, GET, UPDATE, DELETE, SCAN, QUERY |
read_only |
GET, SCAN, QUERY |
Client Drivers
Rust (grumpydb-client)
use GrumpyClient;
let mut client = connect.await?;
client.login.await?;
let db = client.database.await?;
let key = new_v4;
db.insert.await?;
let doc = db.get.await?;
TypeScript (@grumpydb/client)
import { GrumpyClient } from '@grumpydb/client';
const client = await GrumpyClient.connect({
host: 'localhost', port: 6380, tls: false,
tenant: 'acme', username: 'alice', password: 's3cr3t',
});
const db = client.database('myapp');
await db.insert('users', crypto.randomUUID(), { name: 'Bob' });
const doc = await db.get('users', '<uuid>');
await client.close();
Storage Engine
Under the hood, GrumpyDB is a page-based storage engine:
- 8 KiB pages with slotted layout and overflow chains for large documents
- B+Tree indexes — fixed-key (UUID primary) and variable-key (secondary)
- Write-Ahead Log for crash recovery (before-image undo)
- Buffer pool with LRU eviction and dirty page tracking
- SWMR concurrency — one writer or many readers per database
- Compaction — defragments data pages and rebuilds indexes
- Document references —
$ref("collection", "uuid")with cycle-safe resolution
On-disk layout
<data_dir>/
_auth/ # JWT secret + user records
<tenant>/
<database>/
wal.log # Write-Ahead Log
<collection>/
data.db # Slotted pages (documents)
primary.idx # B+Tree: UUID → (page, slot)
idx_<name>.idx # Secondary B+Tree indexes
See docs/ARCHITECTURE.md for full technical details.
Building & Testing
Demo App
The examples/taskman/ directory is a complete task manager CLI demonstrating every engine feature:
- Tutorial — 7-chapter guide
- Cookbook — recipes for common patterns
- Performance Guide — buffer pool tuning
License
MIT