moltendb-core
๐ The Pure Engine Crate
In-memory document store ยท Append-only WAL ยท Query evaluator ยท Analytics (๐ง WIP)
Zero knowledge of HTTP, auth, JWT, or WASM bindings.
What is this crate?
moltendb-core is the heart of MoltenDB. It contains every piece of logic that is shared between the HTTP server (moltendb-server) and the browser WASM adapter (moltendb-wasm):
- In-memory store โ
DashMap-backed document collections, keyed by(collection, key). - Append-only WAL โ every write is appended to a log file (
LogEntry: INSERT, DELETE, DROP, INDEX, ENC). On startup the log is replayed into memory. - Storage backends โ
DiskStorage(sync/async),TieredStorage(hot + cold log),EncryptedStorage(ChaCha20-Poly1305),OpfsStorage(WASM / browser OPFS). - Query evaluator โ
$eq,$ne,$gt,$gte,$lt,$lte,$in,$nin,$contains,$or,$and, field projection (include / exclude), dot-notation for nested fields, joins, sort, count, offset. - Analytics engine โ COUNT, SUM, AVG, MIN, MAX with optional WHERE filtering. โ ๏ธ Under active development โ not ready for production use.
- Auto-indexing โ
query_heatmaptracks hot fields and builds indexes automatically. - Handler pipeline โ
process_get,process_set,process_update,process_delete,process_analyticsโ the single source of truth consumed by both the server and the WASM adapter. - Input validation โ collection name, key, and field name rules enforced before any operation reaches the engine.
Crate type
[]
= ["rlib"]
moltendb-core compiles to a native rlib. It is not a cdylib โ WASM bindings live in the separate moltendb-wasm crate. This keeps the native dependency tree clean (no wasm-bindgen, no web-sys).
WASM-specific code (OpfsStorage, Db::open_wasm) is gated behind #[cfg(target_arch = "wasm32")] and only compiled when the crate is used as a dependency of moltendb-wasm.
Add to your project
[]
= "0.4.0"
Minimal example
use Db;
async
Using the handler pipeline (same API as the HTTP server)
use ;
use json;
let db = open.await?;
let payload = json!;
let = process_get;
println!;
Hybrid Bitcask Storage
MoltenDB uses a Hybrid Bitcask-inspired Storage Model. Frequently accessed data is kept in RAM (Hot) as parsed JSON for sub-microsecond reads. Less frequently used data is paged out to disk (Cold) as byte-offsets, freeing up memory. This allows MoltenDB to handle datasets much larger than the available RAM while maintaining high performance for the active working set.
By default, any collection exceeding 50,000 documents will automatically evict the oldest documents to the Cold tier (disk/OPFS). This limit is configurable when opening the database.
Module overview
| Module | Responsibility |
|---|---|
engine |
Db struct, storage backends, WAL replay, operations |
engine::storage |
DiskStorage, TieredStorage, EncryptedStorage, OpfsStorage |
query |
Query condition evaluation, field projection, joins, sort, pagination |
analytics |
Aggregate functions: COUNT, SUM, AVG, MIN, MAX โ โ ๏ธ under development, not ready for use |
handlers |
process_get, process_set, process_update, process_delete, process_analytics |
validation |
Collection / key / field name validation rules |
Storage modes
| Mode | Use case |
|---|---|
DiskStorage (sync) |
Durable writes. Each write is flushed to disk before returning. Slower but safer for mission-critical data. |
DiskStorage (async) |
Blazing fast, high-throughput writes. Data is buffered and flushed in the background. Recommended for most web use-cases. |
TieredStorage |
100k+ documents โ separates hot and cold log files |
EncryptedStorage |
At-rest encryption with ChaCha20-Poly1305 |
OpfsStorage |
Browser WASM โ Origin Private File System |
Design constraints
- No longer limited by RAM. While MoltenDB is "Memory-First," the Hybrid Bitcask model allows it to page out documents to disk while keeping only the keys and offsets in RAM. A 10GB database can now comfortably run on a machine with 512MB of RAM.
- No HTTP, no auth, no JWT. This crate has zero knowledge of the network layer. It is safe to embed in any Rust application without pulling in Axum, Tokio TLS, or any auth dependency.
- Single writer, many readers. The
DashMapstore is safe for concurrent reads. Writes are serialised through the storage backend.
Testing
moltendb-core includes a comprehensive test suite to ensure engine reliability and query correctness.
Unit Tests
Unit tests are located within the source files (e.g., src/query.rs, src/engine/storage/mod.rs).
Integration Tests
Integration tests are located in the tests/ directory and verify the interaction between the engine, handlers, and storage backends.
# Run all core integration tests
Part of the MoltenDB workspace
MoltenDB/
โโโ moltendb-core/ โ you are here
โโโ moltendb-wasm/ โ browser adapter (wasm-bindgen glue, WorkerDb, OPFS)
โโโ moltendb-auth/ โ identity layer (JWT, Argon2, UserStore)
โโโ moltendb-server/ โ network layer (Axum, TLS, CORS, CLI config)
See the root README for the full architecture overview and feature list.