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
Raw IO (atomic_rename, get_temp_path, cleanup_temp_files) lives
exclusively inside local_store::FileStorage.
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 existSaveIfMissing: Creates empty config and saves it if file doesn’t existErrorIfMissing: Returns error if file doesn’t exist
Sourcepub fn save(&self) -> Result<(), MigrationError>
pub fn save(&self) -> Result<(), MigrationError>
Save current state to file atomically.
Serialises the ConfigMigrator value to the configured format (TOML or
JSON) and delegates the atomic write (tmp file + fsync + rename) to
local_store::FileStorage::write_string.
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.