| Crates.io | tegdb |
| lib.rs | tegdb |
| version | 0.3.0 |
| created_at | 2024-08-30 06:33:27.30352+00 |
| updated_at | 2025-07-02 10:56:15.90785+00 |
| description | The name TegridyDB (short for TegDB) is inspired by the Tegridy Farm in South Park and tries to correct some of the wrong database implementations, such as null support, implicit conversion support, etc. |
| homepage | https://github.com/minifish-org/tegdb |
| repository | https://github.com/minifish-org/tegdb.git |
| max_upload_size | |
| id | 1357414 |
| size | 967,157 |
TegDB is a lightweight, embedded database engine with a SQL-like interface designed for simplicity, performance, and reliability. It provides ACID transactions, crash recovery, and efficient key-value storage.
Design Philosophy: TegDB prioritizes simplicity and reliability over complexity. It uses a single-threaded design to eliminate concurrency bugs, reduce memory overhead, and provide predictable performance - making it ideal for embedded systems and applications where resource efficiency matters more than parallel processing.
TegDB implements a clean layered architecture with four distinct layers:
┌─────────────────────────────────────────────────────────────┐
│ Database API │
│ (SQLite-like interface with schema caching) │
├─────────────────────────────────────────────────────────────┤
│ SQL Executor │
│ (Query optimization and statement execution) │
├─────────────────────────────────────────────────────────────┤
│ SQL Parser │
│ (nom-based SQL parsing to AST) │
├─────────────────────────────────────────────────────────────┤
│ Storage Engine │
│ (Key-value store with WAL and transaction support) │
└─────────────────────────────────────────────────────────────┘
fs2 for file locking)Add TegDB to your Cargo.toml:
[dependencies]
tegdb = "0.2.0"
use tegdb::Database;
fn main() -> tegdb::Result<()> {
// Open or create a database (no configuration needed!)
let mut db = Database::open("my_app.db")?;
// Create a table
db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")?;
// Insert data
db.execute("INSERT INTO users (id, name, age) VALUES (1, 'Alice', 30)")?;
db.execute("INSERT INTO users (id, name, age) VALUES (2, 'Bob', 25)")?;
// Query data
let result = db.query("SELECT name, age FROM users WHERE age > 25")?;
println!("Found {} users:", result.len());
for row in result.rows() {
if let (Some(name), Some(age)) = (row.first()), row.get(1)) {
println!("User: {:?}, Age: {:?}", name, age);
}
}
Ok(())
}
use tegdb::Database;
fn main() -> tegdb::Result<()> {
let mut db = Database::open("bank.db")?;
// Create accounts table
db.execute("CREATE TABLE accounts (id INTEGER PRIMARY KEY, name TEXT, balance INTEGER)")?;
db.execute("INSERT INTO accounts (id, name, balance) VALUES (1, 'Alice', 1000)")?;
db.execute("INSERT INTO accounts (id, name, balance) VALUES (2, 'Bob', 500)")?;
// Transfer funds using explicit transaction
let mut tx = db.begin_transaction()?;
// Debit from Alice's account
tx.execute("UPDATE accounts SET balance = balance - 100 WHERE id = 1")?;
// Credit to Bob's account
tx.execute("UPDATE accounts SET balance = balance + 100 WHERE id = 2")?;
// Commit the transaction (or it will auto-rollback on drop)
tx.commit()?;
println!("Transfer completed successfully!");
Ok(())
}
TegDB supports a comprehensive subset of SQL:
-- Create tables with constraints
CREATE TABLE products (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
price REAL,
category TEXT
);
-- Drop tables
DROP TABLE IF EXISTS old_table;
-- Insert single or multiple rows
INSERT INTO products (id, name, price) VALUES (1, 'Widget', 19.99);
INSERT INTO products (id, name, price) VALUES
(2, 'Gadget', 29.99),
(3, 'Tool', 39.99);
-- Update with conditions
UPDATE products SET price = 24.99 WHERE name = 'Widget';
-- Delete with conditions
DELETE FROM products WHERE price < 20.00;
-- Query with filtering, ordering, and limits
SELECT name, price FROM products
WHERE category = 'Electronics'
ORDER BY price DESC
LIMIT 10;
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
-- or ROLLBACK;
INTEGER - 64-bit signed integersREAL - 64-bit floating point numbersTEXT - UTF-8 stringsBLOB - Binary dataNULL - Null valuesuse tegdb::EngineConfig;
let config = EngineConfig {
max_key_size: 1024, // 1KB max key size
max_value_size: 256 * 1024, // 256KB max value size
sync_on_write: false, // Performance over durability
auto_compact: true, // Auto-compact on open
};
// Note: Custom config requires dev feature and low-level API
For advanced use cases, enable the dev feature to access low-level APIs:
[dependencies]
tegdb = { version = "0.2", features = ["dev"] }
use tegdb::{Engine, EngineConfig};
// Direct key-value operations
let mut engine = Engine::new("data.db".into())?;
engine.set(b"key", b"value".to_vec())?;
let value = engine.get(b"key");
// Transaction control
let mut tx = engine.begin_transaction();
tx.set(b"key1", b"value1".to_vec())?;
tx.set(b"key2", b"value2".to_vec())?;
tx.commit()?;
Run performance benchmarks against other embedded databases:
cargo bench --features dev
Included benchmarks compare against:
# Standard build
cargo build
# With development features
cargo build --features dev
# Run tests
cargo test --features dev
# Run benchmarks
cargo bench --features dev
TegDB includes comprehensive tests covering:
See ARCHITECTURE.md for detailed information about:
Licensed under AGPL-3.0. See LICENSE for details.
The AGPL-3.0 ensures that any modifications to TegDB remain open source and available to the community.
Contributions welcome! Please:
See CONTRIBUTING.md for detailed guidelines.