| Crates.io | dblite |
| lib.rs | dblite |
| version | 0.1.2 |
| created_at | 2025-11-12 21:04:06.052756+00 |
| updated_at | 2026-01-05 05:01:23.443589+00 |
| description | A lightweight, embeddable Key/Value store. Inspired by SQLite. Vibe coded over a holiday. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1929974 |
| size | 84,107 |
A lightweight, embeddable key-value store inspired by SQLite. Simple, fast, and built for embedding into your Rust applications or using as a standalone CLI tool.
git clone https://github.com/yourusername/dblite.git
cd dblite
cargo build --release
The binary will be available at target/release/dblite
cargo install --path .
dblite /path/to/database.db
This opens an interactive shell where you can execute commands.
SET - Store a key-value pair
dblite> SET mykey "Hello, World!"
OK
dblite> SET user:123 '{"name":"Alice","age":30}'
OK
SET with TTL - Store a key with expiration time
dblite> SET session:abc token123 30s
OK
dblite> SET cache:data value 5m
OK
TTL formats: 30s (seconds), 5m (minutes), 2h (hours), 1d (days)
GET - Retrieve a value
dblite> GET mykey
Hello, World!
dblite> GET nonexistent
(nil)
DEL - Delete a key
dblite> DEL mykey
1
dblite> DEL nonexistent
0
COMPACT - Reclaim disk space
dblite> COMPACT
OK
Removes deleted records and optimizes file size.
EXIT or QUIT - Close the CLI
dblite> EXIT
bye
Add to your Cargo.toml:
[dependencies]
dblite = "0.1"
For minimal binary size (excludes CLI command parser):
[dependencies]
dblite = { version = "0.1", default-features = false }
This reduces the library from ~300KB to ~260KB by removing the CLI module and its dependencies (rustyline, humantime).
use dblite::{Database, LockMode};
use std::time::Duration;
fn main() -> std::io::Result<()> {
// Open or create a database
let mut db = Database::open_or_create("~/mydb.dbl")?;
// Store a value
db.set("username", b"alice")?;
// Store with TTL (expires in 60 seconds)
db.set_with_ttl("session_token", b"abc123", Duration::from_secs(60))?;
// Retrieve a value
if let Some(value) = db.get("username")? {
println!("Username: {}", String::from_utf8_lossy(&value));
}
// Check if key exists
if db.contains_key("username")? {
println!("User exists!");
}
// Delete a key
let deleted = db.delete("username")?;
println!("Deleted: {}", deleted);
// Get all keys
let keys = db.keys()?;
println!("Keys: {:?}", keys);
// Compact the database
db.compact()?;
Ok(())
}
use dblite::{KeyValueStore, LockMode};
use std::time::Duration;
fn main() -> std::io::Result<()> {
// Open with exclusive lock
let mut store = KeyValueStore::open("data.db", LockMode::Exclusive)?;
// Store data
store.put("key", b"value")?;
// Store with TTL
store.put_with_ttl("temp", b"data", Some(Duration::from_secs(300)))?;
// Retrieve
if let Some(data) = store.get("key")? {
println!("Got: {:?}", data);
}
// Remove
store.remove("key")?;
Ok(())
}
use dblite::{KeyValueStore, LockMode};
fn main() -> std::io::Result<()> {
// Open in shared mode (read-only)
let mut store = KeyValueStore::open("data.db", LockMode::Shared)?;
// Read operations work
let value = store.get("key")?;
// Write operations will fail with PermissionDenied
// store.put("key", b"value")?; // Error!
Ok(())
}
dblite stores data in a single file with the following structure:
DBL1) + version numberThe file format is designed for:
cargo build
cargo build --release
cargo test
This project is licensed under the MIT License.
See the LICENSE file for details.
This is a hobby project created over a holiday. It's functional and tested, but not battle-tested in production environments. Please contact me know if you would like to battle test it :3