ToonDB Rust Client
The official Rust client SDK for ToonDB — a high-performance embedded document database with HNSW vector search, built-in multi-tenancy, and SQL support.
Features
- ✅ Zero-Copy Reads — Direct access to memory-mapped data
- ✅ Native Vector Search — Built-in HNSW index for embeddings
- ✅ SQL Support — Full SQL via toondb-query integration
- ✅ IPC Client — Connect to ToonDB server (async)
- ✅ Multi-Tenancy — Efficient prefix scanning for data isolation
- ✅ ACID Transactions — Snapshot isolation with automatic commit/abort
- ✅ Thread-Safe — Safe concurrent access with MVCC
- ✅ Columnar Storage — Efficient for analytical queries
Installation
Add to your Cargo.toml:
[]
= "0.2.8"
= { = "1", = ["full"] } # For async IPC
Quick Start
IPC Client (Async)
use IpcClient;
async
Start server first:
# Output: [IpcServer] Listening on "./my_database/toondb.sock"
Embedded Mode (Direct FFI)
For single-process applications with maximum performance:
use Database;
Core Operations
Basic Key-Value
// Put
client.put.await?;
// Get
match client.get.await?
// Delete
client.delete.await?;
Output:
value
Key not found (after delete)
Path Operations
// Hierarchical data storage
client.put_path.await?;
client.put_path.await?;
client.put_path.await?;
// Retrieve by path
if let Some = client.get_path.await?
Output:
Alice's email: alice@example.com
Prefix Scanning ⭐
The most efficient way to iterate keys with a common prefix:
use IpcClient;
async
Output:
ACME Corp has 3 items:
tenants/acme/orders/1: {"total":100}
tenants/acme/users/1: {"name":"Alice"}
tenants/acme/users/2: {"name":"Bob"}
Why use scan():
- Fast: O(|prefix|) performance
- Isolated: Perfect for multi-tenant apps
- Efficient: Zero-copy reads from storage
Transactions
Automatic Transactions
// Transaction with automatic commit/abort
client.with_transaction.await?;
Output:
✅ Transaction committed
Manual Transaction Control
let txn = client.begin_transaction.await?;
txn.put.await?;
txn.put.await?;
// Commit or abort
if success else
SQL Operations
ToonDB supports full SQL via the toondb-query crate:
use QueryEngine;
use Database;
Output:
Row { id: 1, name: "Alice", email: "alice@example.com", age: 30 }
Complex SQL Queries
// JOIN query
let results = query_engine.execute?;
for row in results
Output:
Alice bought Laptop for $999.99
Bob bought Keyboard for $75
Aggregations
// GROUP BY with aggregations
let results = query_engine.execute?;
for row in results
Output:
Alice: 2 orders, $1024.99 total
Bob: 1 orders, $75 total
Vector Search
HNSW Index
use ;
Output:
1. doc1 (distance: 0.0234)
2. doc2 (distance: 0.1567)
Complete Example: Multi-Tenant SaaS App
use IpcClient;
use Value;
async
Output:
ACME Corp: 2 users
tenants/acme/users/alice: alice@acme.com (admin)
tenants/acme/users/bob: bob@acme.com (user)
Globex Inc: 1 users
tenants/globex/users/charlie: charlie@globex.com (admin)
API Reference
IpcClient (Async)
| Method | Description |
|---|---|
IpcClient::connect(path) |
Connect to IPC server |
put(key, value) |
Store key-value pair |
get(key) |
Retrieve value (Option) |
delete(key) |
Delete a key |
put_path(path, value) |
Store at hierarchical path |
get_path(path) |
Retrieve by path |
scan(prefix) |
Scan keys with prefix |
begin_transaction() |
Start transaction |
commit_transaction(txn) |
Commit transaction |
abort_transaction(txn) |
Abort transaction |
Database (Embedded)
| Method | Description |
|---|---|
Database::open(path) |
Open/create database |
put(key, value) |
Store key-value pair |
get(key) |
Retrieve value (Option) |
delete(key) |
Delete a key |
scan(start, end) |
Iterate key range |
checkpoint() |
Force durability checkpoint |
stats() |
Get storage statistics |
Configuration
use Config;
let config = Config ;
let db = open_with_config?;
Error Handling
use ;
match client.get.await
Best Practices
✅ Use IPC for multi-process — Better for microservices ✅ Use embedded for single-process — Maximum performance ✅ Use scan() for multi-tenancy — Efficient prefix-based isolation ✅ Use transactions — Atomic multi-key operations ✅ Use async/await — Non-blocking I/O for IPC ✅ Handle errors properly — Match on Error variants
Crate Organization
| Crate | Purpose |
|---|---|
toondb |
High-level client SDK (this crate) |
toondb-core |
Core database engine |
toondb-storage |
Storage layer with IPC server |
toondb-index |
Vector search (HNSW) |
toondb-query |
SQL query engine |
toondb-client |
Low-level client bindings |
Building from Source
# Clone repository
# Build all crates
# Run tests
# Build specific crate
Examples
See the examples directory for more:
basic_operations.rs- Simple key-value operationsmulti_tenant.rs- Multi-tenant data isolationtransactions.rs- ACID transactionsvector_search.rs- HNSW vector searchsql_queries.rs- SQL operations
Platform Support
- Linux (x86_64, aarch64)
- macOS (Intel, Apple Silicon)
- Windows (x64)
Requires Rust 1.70 or later.
License
Apache License 2.0
Links
Support
- GitHub Issues: https://github.com/toondb/toondb/issues
- Email: sushanth@toondb.dev
Author
Sushanth - GitHub