DirectoryStore

Struct DirectoryStore 

Source
pub struct DirectoryStore { /* private fields */ }
Expand description

A data store that persists items to files in a directory.

Files are created with incrementing numerical prefixes and contain batched JSON data. When a file reaches the configured size limit, a new file is created. Completed files are marked with a .temp extension to indicate they are ready for processing.

Each file contains a JSON object with:

  • A batch array containing the stored items
  • A sentAt timestamp in RFC3339 format
  • The store’s writeKey

Implementations§

Source§

impl DirectoryStore

Source

pub fn new(config: DirectoryConfig) -> Result<Self>

Creates a new DirectoryStore with the specified configuration.

Creates the storage directory if it doesn’t exist. The store will initialize its file index by scanning existing files in the directory.

§Arguments
  • config - Configuration options for the store
§Errors

Returns an IO error if:

  • The storage directory cannot be created
  • The directory cannot be read when scanning for existing files
§Panics
  • If max_file_size is less than 100 bytes
Source

pub fn set_file_validator<F>(&mut self, validator: F)
where F: Fn(&Path) -> Result<()> + 'static + Send + Sync,

Sets a validator function that will be called before finalizing each data file.

The validator is called just before a file is marked as complete (renamed with .temp extension). If the validator returns an error, the file will not be finalized and the error will be propagated.

§Arguments
  • validator - Function that takes a file path and returns Ok(()) if the file is valid
§Examples
use std::path::PathBuf;
use std::fs;
use std::io;
use transientdb::{DirectoryConfig, DirectoryStore};

let mut store = DirectoryStore::new(DirectoryConfig {
    write_key: "test".into(),
    storage_location: PathBuf::from("/tmp/data"),
    base_filename: "events".into(),
    max_file_size: 1024,
})?;

// Add a validator that checks file size and JSON validity
store.set_file_validator(|path| {
    // Verify minimum file size
    let metadata = fs::metadata(path)?;
    if metadata.len() < 10 {
        return Err(io::Error::new(io::ErrorKind::Other, "File too small"));
    }

    // Verify file contains valid JSON
    let content = fs::read_to_string(path)?;
    serde_json::from_str::<serde_json::Value>(&content)?;

    Ok(())
});

Trait Implementations§

Source§

impl DataStore for DirectoryStore

Source§

type Output = Vec<PathBuf>

The type of data returned by fetch operations.
Source§

fn has_data(&self) -> bool

Checks if the store contains any data that can be fetched.
Source§

fn reset(&mut self)

Removes all data from the store and resets it to initial state.
Source§

fn append(&mut self, data: Value) -> Result<()>

Appends a new item to the store. Read more
Source§

fn fetch( &mut self, count: Option<usize>, max_bytes: Option<usize>, ) -> Result<Option<DataResult<Self::Output>>>

Fetches a batch of data from the store, respecting optional count and size limits. Read more
Source§

fn remove(&mut self, data: &[Box<dyn Equivalent>]) -> Result<()>

Removes previously fetched data from the store. Read more

Auto Trait Implementations§

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.