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
batcharray containing the stored items - A
sentAttimestamp in RFC3339 format - The store’s
writeKey
Implementations§
Source§impl DirectoryStore
impl DirectoryStore
Sourcepub fn new(config: DirectoryConfig) -> Result<Self>
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
Sourcepub fn set_file_validator<F>(&mut self, validator: F)
pub fn set_file_validator<F>(&mut self, validator: F)
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(())
});