SochDB Rust Client
The official Rust client SDK for SochDB — 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 sochdb-query integration
- ✅ IPC Client — Connect to SochDB 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.3" # Or specific version like "0.3.0"
= { = "1", = ["full"] } # For async IPC
What's New in Latest Release
🎯 Namespace Isolation
Logical database namespaces for true multi-tenancy without key prefixing:
use ;
let db = open?;
// Create isolated namespaces
let user_db = db.namespace?;
let orders_db = db.namespace?;
// Keys don't collide across namespaces
user_db.put?;
orders_db.put?; // Different "123"!
// Each namespace has isolated collections
user_db.create_collection?;
🔍 Hybrid Search
Combine dense vectors (HNSW) with sparse BM25 text search:
use ;
// Create collection with hybrid search
let config = CollectionConfig ;
let collection = db.create_collection?;
// Insert documents with text and vectors
let doc = Document ;
collection.insert?;
// Hybrid search (vector + text)
let query = HybridQuery ;
let results = collection.hybrid_search?;
📄 Multi-Vector Documents
Store multiple embeddings per document (e.g., title + content):
use MultiVectorDocument;
use HashMap;
// Insert document with multiple vectors
let mut vectors = new;
vectors.insert;
vectors.insert;
vectors.insert;
let multi_doc = MultiVectorDocument ;
collection.insert_multi_vector?;
// Search with aggregation strategy
let mut query_vectors = new;
query_vectors.insert;
query_vectors.insert;
let results = collection.multi_vector_search?;
🧩 Context-Aware Queries
Optimize retrieval for LLM context windows:
use ContextQuery;
// Query with token budget
let config = ContextQueryConfig ;
let results = collection.context_query?;
// Results fit within 4000 tokens, deduplicated for relevance
Quick Start
IPC Client (Async)
use IpcClient;
async
Start server first:
# Output: [IpcServer] Listening on "./my_database/sochdb.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
SochDB supports full SQL via the sochdb-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 |
|---|---|
sochdb |
High-level client SDK (this crate) |
sochdb-core |
Core database engine |
sochdb-storage |
Storage layer with IPC server |
sochdb-index |
Vector search (HNSW) |
sochdb-query |
SQL query engine |
sochdb-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/sochdb/sochdb/issues
- Email: sushanth@sochdb.dev
Author
Sushanth - GitHub