KeraDB Rust SDK
The native Rust SDK for KeraDB - a lightweight, embedded NoSQL document database with vector search capabilities.
Features
- Document Storage: JSON document storage with collections
- Vector Database: HNSW-based similarity search with multiple distance metrics
- LEANN-Style Compression: Up to 97% storage savings for vectors
- Lazy Embeddings: Store text, compute embeddings on-demand
- Cross-Platform: Windows, macOS, Linux support
Installation
Add this to your Cargo.toml:
[]
= "0.1"
= "1.0"
For development from source:
[]
= { = "../.." }
= "1.0"
Quick Start
Document Database
use Database;
use json;
With Custom Configuration
use ;
let config = Config ;
let db = create_with_config?;
Pagination
// Get first 10 documents
let page1 = db.find_all?;
// Get next 10 documents (skip first 10)
let page2 = db.find_all?;
Error Handling
use ;
match db.find_by_id
Vector Database
KeraDB includes powerful vector database capabilities for AI/ML applications, semantic search, and similarity queries.
Creating a Vector Collection
use ;
use json;
Inserting Vectors
use ;
use json;
Vector Similarity Search
use ;
use json;
Distance Metrics
KeraDB supports multiple distance metrics:
use ;
// Cosine similarity (default) - best for normalized embeddings
let config = new.with_distance;
// Euclidean (L2) distance - best for spatial data
let config = new.with_distance;
// Dot product - best for unnormalized embeddings
let config = new.with_distance;
// Manhattan (L1) distance
let config = new.with_distance;
LEANN-Style Compression (97% Storage Savings)
Enable delta or quantized compression for massive storage savings:
use ;
Lazy Embeddings (Text-to-Vector)
Store text and compute embeddings on-demand:
use ;
Vector Collection Statistics
use ;
API Reference
Database
Creation and Opening
| Method | Description |
|---|---|
Database::create(path) |
Create a new database |
Database::create_with_config(path, config) |
Create with custom config |
Database::open(path) |
Open an existing database |
Database::open_with_config(path, config) |
Open with custom config |
Document Operations
| Method | Description |
|---|---|
insert(collection, data) |
Insert a document, returns document ID |
find_by_id(collection, doc_id) |
Find a document by ID |
update(collection, doc_id, data) |
Update a document |
delete(collection, doc_id) |
Delete a document |
find_all(collection, limit, skip) |
Find all documents with pagination |
Collection Operations
| Method | Description |
|---|---|
count(collection) |
Count documents in a collection |
list_collections() |
List all collections with document counts |
sync() |
Flush all changes to disk |
Vector Operations
| Method | Description |
|---|---|
create_vector_collection(name, config) |
Create a vector-enabled collection |
insert_vector(collection, vector, metadata) |
Insert a vector with optional metadata |
vector_search(collection, query, k) |
Search for k nearest neighbors |
vector_stats(collection) |
Get vector collection statistics |
Types
| Type | Description |
|---|---|
Document |
A document with an ID and JSON data |
DocumentId |
String type for document IDs (UUIDs) |
Config |
Database configuration |
VectorConfig |
Configuration for vector collections |
VectorDocument |
A vector with ID, embedding, and metadata |
VectorSearchResult |
Search result with document and score |
Distance |
Distance metric enum (Cosine, Euclidean, DotProduct, Manhattan) |
CompressionConfig |
Compression settings for vectors |
KeraDBError |
Error type for database operations |
VectorConfig Builder
new
.with_distance // Distance metric
.with_m // HNSW M parameter
.with_ef_construction // HNSW build quality
.with_delta_compression // Enable LEANN compression
.with_lazy_embedding // Enable lazy embeddings
Building the C Library
To build the FFI-compatible library for use with other languages:
# Build dynamic library
# The library will be at:
# - Linux: target/release/libkeradb.so
# - macOS: target/release/libkeradb.dylib
# - Windows: target/release/keradb.dll
# Static library: target/release/libkeradb.a
Examples
See the examples/ directory for more examples:
Testing
The SDK has 67 tests across three categories:
Unit tests (co-located in src/)
These test pure Rust logic with no native library required — always runnable.
| Suite | Count | What it tests |
|---|---|---|
error::tests |
9 | KeraDbError variant messages, From<serde_json::Error> conversion, Result<T> alias |
results::tests |
8 | InsertOneResult, InsertManyResult, UpdateResult, DeleteResult — construction, field values, Display formatting |
vector::tests |
22 | Distance / CompressionMode string values; VectorConfig builder chain and JSON serialisation; MetadataFilter shorthands (eq, gt, lt) and JSON output; VectorDocument and VectorSearchResult deserialisation from native JSON shape |
Integration tests (tests/integration_test.rs)
Logic tests that also run without the native library:
| Group | Count | What it tests |
|---|---|---|
| Filter matching | 4 | matches_filter() — direct equality, $gt/$gte/$lt/$lte/$ne, $in/$nin, $and/$or |
| Update operators | 5 | apply_update() — $set, $unset, $inc, $push, full document replacement (with _id preservation) |
| Cursor | 4 | limit(), skip(), combined limit + skip, IntoIterator |
| VectorConfig JSON | 3 | to_json() roundtrip, delta compression flag, lazy embedding fields |
CRUD tests that require the native keradb shared library (7 tests):
Covers insert_one, find_one by ID, find (all), update_one, delete_one, count_documents, insert_many.
Doc-tests
5 compile-checked code examples embedded in the source docs (lib.rs, client.rs).
Run all non-native tests
test result: ok. 39 passed; 0 failed (lib unit tests)
test result: ok. 16 passed; 0 failed (integration logic tests)
test result: ok. 5 passed; 0 failed (doc-tests)
License
MIT License