pub struct JsonMutexDB { /* private fields */ }Implementations§
Source§impl JsonMutexDB
impl JsonMutexDB
Sourcepub fn new(
path: &str,
pretty: bool,
async_updates: bool,
fast_serialization: bool,
) -> Result<Self, DbError>
pub fn new( path: &str, pretty: bool, async_updates: bool, fast_serialization: bool, ) -> Result<Self, DbError>
Creates a new instance of JsonMutexDB.
§Errors
This function will return an error if the specified file cannot be read, if the JSON data is invalid, or if there are issues with file I/O operations.
Sourcepub fn get(&self) -> Result<Value, DbError>
pub fn get(&self) -> Result<Value, DbError>
Returns a clone of the JSON data.
If async_updates is enabled, fetches the latest state from the background thread.
§Errors
This function will return an error if:
- The background thread fails to send or receive data in async mode.
- The mutex is poisoned in sync mode.
- The database is in an invalid state (neither sync nor async mode).
Sourcepub fn update<F>(&self, update_fn: F) -> Result<(), DbError>
pub fn update<F>(&self, update_fn: F) -> Result<(), DbError>
Updates the JSON data.
If async_updates is enabled, sends the update closure to the background thread.
Otherwise, applies the update synchronously.
Updates the JSON data.
If async_updates is enabled, sends the update closure to the background thread.
Otherwise, applies the update synchronously.
§Errors
This function will return an error if:
- The background thread fails to send the update task in async mode.
- The mutex is poisoned in sync mode.
- The database is in an invalid state (neither sync nor async mode).
Sourcepub fn save_sync(&self) -> Result<(), DbError>
pub fn save_sync(&self) -> Result<(), DbError>
Synchronously saves the current JSON data to disk atomically.
If async_updates is enabled, fetches the latest state before saving.
§Errors
This function will return an error if:
- The current state cannot be fetched (e.g., due to background thread issues in async mode).
- There are file I/O errors during the save operation.
Sourcepub fn save_async(&self) -> Result<(), DbError>
pub fn save_async(&self) -> Result<(), DbError>
Asynchronously saves the current JSON data atomically.
If async_updates is enabled, fetches the latest state first, then spawns the save thread.
Asynchronously saves the current JSON data atomically.
If async_updates is enabled, fetches the latest state first, then spawns the save thread.
§Errors
This function will return an error if:
- The current state cannot be fetched (e.g., due to background thread issues in async mode).
- There are file I/O errors during the save operation.