pub struct FileStorage { /* private fields */ }Expand description
File storage with ACID guarantees and automatic migrations.
Provides:
- Atomicity: Updates are all-or-nothing via tmp file + atomic rename
- Consistency: Format validation on load/save
- Isolation: File locking prevents concurrent modifications
- Durability: Explicit fsync before rename
Implementations§
Source§impl FileStorage
impl FileStorage
Sourcepub fn new(
path: PathBuf,
migrator: Migrator,
strategy: FileStorageStrategy,
) -> Result<Self, MigrationError>
pub fn new( path: PathBuf, migrator: Migrator, strategy: FileStorageStrategy, ) -> Result<Self, MigrationError>
Create a new FileStorage instance and load data from file.
This combines initialization and loading into a single operation.
§Arguments
path- Path to the storage filemigrator- Migrator instance with registered migration pathsstrategy- Storage strategy configuration
§Behavior
Depends on strategy.load_behavior:
CreateIfMissing: Creates empty config if file doesn’t existErrorIfMissing: Returns error if file doesn’t exist
§Example
ⓘ
let strategy = FileStorageStrategy::default();
let migrator = Migrator::new();
let storage = FileStorage::new(
PathBuf::from("config.toml"),
migrator,
strategy
)?;Sourcepub fn save(&self) -> Result<(), MigrationError>
pub fn save(&self) -> Result<(), MigrationError>
Save current state to file atomically.
Uses a temporary file + atomic rename to ensure durability.
Retries according to strategy.atomic_write.retry_count.
Sourcepub fn config(&self) -> &ConfigMigrator
pub fn config(&self) -> &ConfigMigrator
Get immutable reference to the ConfigMigrator.
Sourcepub fn config_mut(&mut self) -> &mut ConfigMigrator
pub fn config_mut(&mut self) -> &mut ConfigMigrator
Get mutable reference to the ConfigMigrator.
Sourcepub fn query<T>(&self, key: &str) -> Result<Vec<T>, MigrationError>where
T: Queryable + for<'de> Deserialize<'de>,
pub fn query<T>(&self, key: &str) -> Result<Vec<T>, MigrationError>where
T: Queryable + for<'de> Deserialize<'de>,
Query entities from storage.
Delegates to ConfigMigrator::query().
Sourcepub fn update<T>(
&mut self,
key: &str,
value: Vec<T>,
) -> Result<(), MigrationError>
pub fn update<T>( &mut self, key: &str, value: Vec<T>, ) -> Result<(), MigrationError>
Update entities in memory (does not save to file).
Delegates to ConfigMigrator::update().
Sourcepub fn update_and_save<T>(
&mut self,
key: &str,
value: Vec<T>,
) -> Result<(), MigrationError>
pub fn update_and_save<T>( &mut self, key: &str, value: Vec<T>, ) -> Result<(), MigrationError>
Update entities and immediately save to file atomically.
Auto Trait Implementations§
impl Freeze for FileStorage
impl !RefUnwindSafe for FileStorage
impl Send for FileStorage
impl Sync for FileStorage
impl Unpin for FileStorage
impl !UnwindSafe for FileStorage
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