brk_store
High-performance transactional key-value store wrapper around Fjall with blockchain-aware versioning.
Overview
This crate provides a type-safe wrapper around the Fjall LSM-tree database engine, specifically designed for Bitcoin blockchain data storage. It offers transactional operations, automatic version management, height-based synchronization, and optimized configuration for blockchain workloads with support for batch operations and efficient range queries.
Key Features:
- Transactional key-value storage with ACID guarantees
- Blockchain height-based synchronization and versioning
- Automatic metadata management with version compatibility checking
- Optimized configuration for Bitcoin data patterns (32MB write buffers, 8MB memtables)
- Type-safe generic interface with zero-copy ByteView integration
- Batch operations with deferred commits for performance
- Optional bloom filter configuration for space/speed tradeoffs
Target Use Cases:
- Bitcoin blockchain indexing with transactional consistency
- UTXO set management requiring atomic updates
- Address-to-transaction mapping with range queries
- Any Bitcoin data storage requiring versioned, transactional access
Installation
Quick Start
use ;
use ;
use Path;
// Open keyspace (database instance)
let keyspace = open_keyspace?;
// Create typed store for height-to-blockhash mapping
let mut store: = import?;
// Insert data (batched in memory)
store.insert_if_needed;
// Commit transaction to disk
store.commit?;
// Query data
if let Some = store.get?
API Overview
Core Types
Store<Key, Value>: Generic transactional store with type-safe operationsAnyStore: Trait for height-based synchronization and metadata operationsStoreMeta: Version and height metadata managementTransactionalKeyspace: Fjall keyspace wrapper for database management
Key Methods
Store::import(keyspace, path, name, version, bloom_filters) -> Result<Self>
Creates or opens a store with automatic version checking and migration.
get(&self, key: &Key) -> Result<Option<Cow<Value>>>
Retrieves value by key, checking both pending writes and committed data.
insert_if_needed(&mut self, key: Key, value: Value, height: Height)
Conditionally inserts data based on blockchain height requirements.
commit(&mut self, height: Height) -> Result<()>
Atomically commits all pending operations and updates metadata.
Height-Based Synchronization
The store implements blockchain-aware synchronization:
has(height): Checks if store contains data up to specified heightneeds(height): Determines if store requires data for specified heightheight(): Returns current synchronized height
Examples
Basic Key-Value Operations
use ;
use ;
let keyspace = open_keyspace?;
// Create store for transaction index
let mut tx_store: = import?;
// Insert transaction mapping
let txid = from_str?;
let height = new;
tx_store.insert_if_needed;
tx_store.commit?;
// Query transaction height
if let Some = tx_store.get?
Batch Processing with Height Synchronization
use ;
let mut store: = import?;
// Process blocks sequentially
for block_height in 750000..750100
// Ensure data is persisted to disk
store.persist?;
Version Migration and Reset
use ;
use Version;
// Open store with new version
let mut store: = import?;
// Check if reset is needed for data consistency
if store.version != TWO
// Verify store is empty after reset
assert!;
Iterator-Based Data Access
use Store;
let store: = import?;
// Iterate over all key-value pairs
for in store.iter
Architecture
Storage Engine
Built on Fjall LSM-tree engine with optimizations:
- Write Buffers: 32MB for high-throughput blockchain ingestion
- Memtables: 8MB for balanced memory usage
- Manual Journal Persist: Explicit control over durability guarantees
- Bloom Filters: Configurable for read-heavy vs. space-constrained workloads
Transaction Model
- Read Transactions: Consistent point-in-time snapshots
- Write Transactions: ACID-compliant with rollback support
- Batch Operations: In-memory accumulation with atomic commits
- Height Synchronization: Blockchain-aware conflict resolution
Version Management
Automatic handling of schema evolution:
- Version Detection: Reads stored version from metadata
- Compatibility Check: Compares with expected version
- Migration: Automatic store reset for incompatible versions
- Metadata Update: Persistent version tracking
Memory Management
- Zero-Copy: ByteView integration for efficient serialization
- Copy-on-Write: Cow for memory-efficient reads
- Parking Lot: RwLock for concurrent partition access
- Deferred Operations: BTreeMap/BTreeSet for batched writes
Configuration
Keyspace Options
use Config;
let keyspace = new
.max_write_buffer_size // 32MB write buffers
.open_transactional?;
Partition Options
use PartitionCreateOptions;
let options = default
.max_memtable_size // 8MB memtables
.manual_journal_persist // Manual sync control
.bloom_filter_bits; // Disable bloom filters
Code Analysis Summary
Main Structure: Store<Key, Value> generic wrapper around Fjall with typed operations and metadata management
Transaction Layer: Read/write transaction abstraction with deferred batch operations via BTreeMap/BTreeSet
Metadata System: StoreMeta for version compatibility and height tracking with automatic migration
Height Synchronization: Blockchain-aware operations with needs(), has(), and conditional insertion logic
Memory Efficiency: Zero-copy ByteView integration with parking_lot RwLock for concurrent access
Storage Engine: Fjall LSM-tree with optimized configuration for blockchain workloads
Architecture: Type-safe database abstraction with ACID guarantees and blockchain-specific synchronization patterns
This README was generated by Claude Code