TransientDB
TransientDB is a lightweight, thread-safe temporary data storage system designed for efficient handling of transient data in Rust applications. It provides flexible storage options with both in-memory and file-based implementations, making it ideal for scenarios requiring temporary data buffering, event batching, or intermediate data storage.
Features
-
Multiple Storage Backends
- In-memory storage with configurable size limits
- File-based storage with automatic file rotation
- Browser-based IndexedDB storage for WASM targets
- Extensible interface for custom implementations
-
Thread-Safe Operations
- Concurrent append and fetch operations
- Atomic file operations for the directory-based store
- Robust error handling and recovery
- Mutex-based synchronization
-
Configurable Behavior
- Customizable maximum file/batch sizes
- File validation hooks
- Flexible fetch limits (count and size-based)
- Consistent data format across stores
-
FIFO (First-In-First-Out) Data Management
- Automatic cleanup of old data
- Efficient batch processing
- Controlled memory usage
- Safe removal of processed data
Installation
Add TransientDB to your Cargo.toml:
[]
= "0.2" # Replace with actual version
For WASM/browser targets, enable the web feature:
[]
= { = "0.2", = ["web"] }
Note: The
webfeature only compiles on WASM targets. On native targets, it's automatically excluded.
Core Types
TransientDB
The main wrapper type providing thread-safe access to any storage implementation. The type parameter T determines the output type of fetch operations (e.g., Value for MemoryStore or Vec<PathBuf> for DirectoryStore).
DataResult
A container for fetch results that includes:
data: The fetched data of typeT(JSON Value for MemoryStore or file paths for DirectoryStore)removable: Internal tracking data used byremove()to clean up processed items
DataStore Trait
The core interface that storage implementations must provide:
append(): Add new items to the storefetch(): Retrieve batches of data with optional limitsremove(): Clean up processed datahas_data(): Check if data is availablereset(): Clear all stored data
Usage
Memory Store Example
use ;
use json;
// Configure an in-memory store
let config = MemoryConfig ;
// Create the store
let store = new;
let db = new;
// Append data
db.append?;
// Fetch data (up to 100 items)
if let Some = db.fetch?
Directory Store Example
use PathBuf;
use ;
use json;
// Configure a file-based store
let config = DirectoryConfig ;
// Create the store with custom validation
let mut store = new?;
store.set_file_validator;
let db = new;
// Append events
db.append?;
// Fetch completed files
if let Some = db.fetch?
Web Store Example (WASM)
use ;
use json;
// Configure a browser-based store
let config = WebConfig ;
// Create the store (async - opens IndexedDB)
let store = new.await;
// Check persistence state - IndexedDB may be unavailable
// in private browsing or third-party iframe contexts
match store.persistence_state
// Wrap in TransientDB for thread-safe access
let db = new;
// Append data
db.append?;
// Fetch data
if let Some = db.fetch?
See also: The
examples/web/directory contains a complete, runnable WASM example with HTML and build instructions.
Storage Implementations
MemoryStore
- Stores data in memory using a FIFO queue
- Automatically removes old items when max_items is reached
- Returns data as serde_json::Value
- Ideal for high-throughput, temporary data storage
- No cleanup required (fetch automatically removes returned items)
DirectoryStore
- Stores data in rotating files in a specified directory
- Automatic file management and rotation
- Returns paths to completed files
- Supports custom file validation
- Requires explicit cleanup via remove()
- Ideal for larger datasets and persistent storage needs
WebStore (WASM)
- Browser-based storage using IndexedDB
- Async construction, sync operations (fire-and-forget persistence)
- Graceful fallback to memory-only if IndexedDB unavailable
- Automatic hydration from IndexedDB on startup
- Ideal for web applications and browser-based analytics
- Requires the
webfeature flag
Persistence States:
PersistenceState::Persisted- IndexedDB available, data survives page refreshPersistenceState::MemoryOnly- IndexedDB unavailable (private browsing, third-party context), data lost on refresh
When in memory-only mode, consider increasing flush frequency to minimize data loss window.
Configuration Options
MemoryConfig
write_key: Identifier for the data sourcemax_items: Maximum number of items to store (must be > 0)max_fetch_size: Maximum size in bytes for a single fetch operation (must be ≥ 100)
DirectoryConfig
write_key: Identifier for the data sourcestorage_location: Directory path for storing filesbase_filename: Base name for generated filesmax_file_size: Maximum size in bytes for individual files (must be ≥ 100)
WebConfig
write_key: Identifier for the data sourcedatabase_name: Name of the IndexedDB database (use unique names per store)max_items: Maximum number of items to store (must be > 0)max_fetch_size: Maximum size in bytes for a single fetch operation (must be ≥ 100)
Data Format
All stores produce data in a consistent JSON format:
Thread Safety
TransientDB is designed to be thread-safe and can handle concurrent operations from multiple threads:
- All public methods are thread-safe
- The DirectoryStore uses atomic operations for file management
- Batch operations are atomic
- Safe concurrent append and fetch operations
Error Handling
All operations that could fail return Result<T, std::io::Error>. The library includes comprehensive error handling and recovery mechanisms:
- Safe handling of concurrent access
- Atomic file operations
- Validation of configuration values
- Recovery from interrupted operations
- File system error handling
- JSON parsing error handling
Testing
The library includes an extensive test suite covering:
- Basic operations
- Concurrent access patterns
- Edge cases and error conditions
- Recovery scenarios
- Performance under load
- File system interactions
- Data format validation
- Configuration validation
Run the tests using:
# Run all tests (native + WASM)
# Native tests only
# WASM tests only (requires wasm-pack)
# Or use Firefox/Safari
License
Copyright 2024 Sovran.la, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.