Anda-DB B-tree Index Library
A high-performance B-tree based index implementation for Anda-DB, optimized for concurrent access and efficient range queries.
Features
- Support for various data types: Index fields of u64, i64, String, binary data and more
- Efficient range queries: Optimized for fast range-based lookups
- Prefix search: Specialized support for string prefix searches
- Efficient serialization: Fast CBOR-based serialization and deserialization
- Incremental Persistent: Support incremental index updates persistent (insertions and deletions)
- Thread-safe concurrent access: Safely use the index from multiple threads
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic Example
use ;
// Create a new B-tree index
let config = BtreeConfig ;
let index = new;
// Insert some data
let now_ms = now
.duration_since
.unwrap
.as_millis as u64;
index.insert.unwrap;
index.insert.unwrap;
index.insert.unwrap;
// Batch insert
let items = vec!;
index.batch_insert.unwrap;
// Search for exact matches
let result = index.search_with;
assert!;
println!;
// Range queries
let query = Between;
let results = index.search_range_with;
println!;
// Prefix search (for String keys)
let results = index.search_prefix_with;
println!;
// Remove data
index.remove;
Persistence
The B-tree index supports serialization and deserialization for persistence:
use Cursor;
// Serialize index metadata
let mut buf = Vecnew;
index.store_metadata.await.unwrap;
// Store dirty buckets
index.store_dirty_buckets.await.unwrap;
// Later, load the index
let loaded_index = load_metadata.await.unwrap;
// Load buckets
loaded_index.load_buckets.await.unwrap;
Configuration
The BtreeConfig struct allows customizing the index behavior:
let config = BtreeConfig ;
Performance Considerations
- Bucket Size: Adjust
bucket_overload_sizebased on your data characteristics. - Concurrency: The index is designed for concurrent access, using lock-free data structures where possible.
- Memory Usage: The index keeps all data in memory for fast access. For very large datasets, consider using multiple smaller indices.
Error Handling
The library provides a comprehensive error type BtreeError that covers various failure scenarios:
BtreeError::Generic: General index-related errorsBtreeError::Serialization: CBOR serialization/deserialization errorsBtreeError::NotFound: When a requested value is not found in the indexBtreeError::AlreadyExists: When trying to insert a duplicate key withallow_duplicatesset to false
License
Copyright © 2025 LDC Labs.
ldclabs/anda-db is licensed under the MIT License. See LICENSE for the full license text.