TransientDB

Struct TransientDB 

Source
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>

Source

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);
Source

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());
Source

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());
Source

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();
Source

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 fetch
  • max_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));
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.