CursorDB
CursorDB is a lightweight, append-only database engine designed for sequential data streams and time-series records. It utilizes a memory-mapped sparse index to provide fast timestamp lookups and row-based navigation without the overhead of loading the entire dataset into RAM.
Key Features
- Deterministic Writes: Records are appended with CRC32 integrity validation.
- Sparse Indexing: Maintains a navigational map in RAM every 1024 records (default stride) for logarithmic jumps across the data file.
- Bi-directional Cursor: Efficiently move forward (next()) and backward (back()) through variable-length records.
- Integrity Auditing: Automatically detects orphan data or truncation during startup and provides detailed storage statistics.
Installation
Add this to your Cargo.toml:
[dependencies] cursordb = "0.1.0"
Quick Start
// Define file paths
let data_path: &str = "../data/data.cdb";
let index_path: &str = "../data/index.cdbi";
for path in &
//=========================================================================
// Create database
let mut db: CursorDB = new?;
let total_records: u64 = 20;
// Create records
for i in 0..total_records
//Display stats
let stats: DBStats = db.stats?;
println!;
//=========================================================================
match db.current
//=========================================================================
let ts_0: i64 = 3;
match db.move_cursor_at
//=========================================================================
match db.move_to_first
//=========================================================================
match db.back
//=========================================================================
match db.next
//=========================================================================
let before: u64 = 0;
let after: u64 = total_records;
match db.range_around_cursor
//=========================================================================
match db.move_to_last
//=========================================================================
Binary Storage Format (.cdb)
To ensure portability and recovery, each record is stored using a strictly defined binary layout:
| Offset | Size | Description |
|---|---|---|
| 0 | 8 bytes | Timestamp (i64, Little Endian) |
| 8 | 4 bytes | Payload Size (u32, Little Endian) |
| 12 | 4 bytes | CRC32 Checksum (u32) |
| 16 | N bytes | Raw Payload Data |
Performance & Constraints
- Max Payload: 16 MB per record.
- Write Speed: O(1) sequential appends.
- Search Speed: O(log N) jump via sparse index + O(Stride) linear scan.
- Safety: Uses std::fs::File::flush to ensure data persistence before index updates.
Statistics and Reporting
You can generate a health report of the database state:
let stats = db.stats()?; println!("{}", stats);
License
This project is licensed under the GPL v3 License.