pub struct TransientDB<T> { /* private fields */ }Expand description
A thread-safe wrapper around a DataStore implementation that provides temporary data storage with batch processing capabilities.
TransientDB uses interior mutability through a Mutex to allow concurrent access to the underlying data store. It’s designed for scenarios where data needs to be temporarily stored and processed in batches, such as queuing events or logs.
Implementations§
Source§impl<T> TransientDB<T>
impl<T> TransientDB<T>
Sourcepub fn new(store: impl DataStore<Output = T> + Send + 'static) -> Self
pub fn new(store: impl DataStore<Output = T> + Send + 'static) -> Self
Creates a new TransientDB instance with the provided data store implementation.
§Arguments
store- Any implementation of DataStore that is Send + ’static (on native) or just DataStore + ’static (on WASM)
§Examples
use transientdb::{TransientDB, MemoryConfig, MemoryStore};
let config = MemoryConfig {
write_key: "my-store".into(),
max_items: 1000,
max_fetch_size: 1024 * 1024, // 1MB
};
let store = MemoryStore::new(config);
let db = TransientDB::new(store);Sourcepub fn has_data(&self) -> bool
pub fn has_data(&self) -> bool
Checks if the store contains any data that can be fetched.
§Examples
use transientdb::{TransientDB, MemoryStore, MemoryConfig};
use serde_json::json;
let db = TransientDB::new(MemoryStore::new(MemoryConfig {
write_key: "test".into(),
max_items: 100,
max_fetch_size: 1024,
}));
assert!(!db.has_data());
db.append(json!({"test": "data"})).unwrap();
assert!(db.has_data());Sourcepub fn reset(&self)
pub fn reset(&self)
Removes all data from the store and resets it to initial state.
§Examples
use transientdb::{TransientDB, MemoryStore, MemoryConfig};
use serde_json::json;
let db = TransientDB::new(MemoryStore::new(MemoryConfig {
write_key: "test".into(),
max_items: 100,
max_fetch_size: 1024,
}));
db.append(json!({"test": "data"})).unwrap();
assert!(db.has_data());
db.reset();
assert!(!db.has_data());Sourcepub fn append(&self, data: Value) -> Result<()>
pub fn append(&self, data: Value) -> Result<()>
Appends a new item to the store.
§Arguments
data- JSON value to store
§Examples
use transientdb::{TransientDB, MemoryStore, MemoryConfig};
use serde_json::json;
let db = TransientDB::new(MemoryStore::new(MemoryConfig {
write_key: "test".into(),
max_items: 100,
max_fetch_size: 1024,
}));
// Append a single value
db.append(json!({"event": "user_login", "user_id": 123})).unwrap();
// Append structured data
db.append(json!({
"event": "purchase",
"details": {
"item_id": "ABC123",
"amount": 99.99,
"currency": "USD"
}
})).unwrap();Sourcepub fn fetch(
&self,
count: Option<usize>,
max_bytes: Option<usize>,
) -> Result<Option<DataResult<T>>>
pub fn fetch( &self, count: Option<usize>, max_bytes: Option<usize>, ) -> Result<Option<DataResult<T>>>
Fetches a batch of data from the store, respecting optional count and size limits.
§Arguments
count- Optional maximum number of items to fetchmax_bytes- Optional maximum total size in bytes to fetch
§Examples
use transientdb::{TransientDB, MemoryStore, MemoryConfig};
use serde_json::json;
let db = TransientDB::new(MemoryStore::new(MemoryConfig {
write_key: "test".into(),
max_items: 100,
max_fetch_size: 1024,
}));
// Add some data
for i in 0..5 {
db.append(json!({"index": i})).unwrap();
}
// Fetch up to 3 items
if let Ok(Some(result)) = db.fetch(Some(3), None) {
// Process the data
if let Some(data) = result.data {
println!("Fetched data: {:?}", data);
}
// Clean up the fetched items
if let Some(removable) = result.removable {
db.remove(&removable).unwrap();
}
}
// Fetch items with size limit (1KB)
let result = db.fetch(None, Some(1024));Sourcepub fn remove(&self, data: &[Box<dyn Equivalent>]) -> Result<()>
pub fn remove(&self, data: &[Box<dyn Equivalent>]) -> Result<()>
Removes previously fetched data from the store.
§Arguments
data- Slice of removable items from a previous fetch operation
§Examples
use transientdb::{TransientDB, MemoryStore, MemoryConfig};
use serde_json::json;
let db = TransientDB::new(MemoryStore::new(MemoryConfig {
write_key: "test".into(),
max_items: 100,
max_fetch_size: 1024,
}));
// Add and fetch data
db.append(json!({"test": "data"})).unwrap();
if let Ok(Some(result)) = db.fetch(None, None) {
// Process the data...
// Then remove the processed items
if let Some(removable) = result.removable {
db.remove(&removable).unwrap();
}
}Auto Trait Implementations§
impl<T> !Freeze for TransientDB<T>
impl<T> RefUnwindSafe for TransientDB<T>
impl<T> Send for TransientDB<T>
impl<T> Sync for TransientDB<T>
impl<T> Unpin for TransientDB<T>
impl<T> UnwindSafe for TransientDB<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more