|
|
@ -6,8 +6,7 @@ use crate::{ChangePayload, StoreState};
|
|
|
|
use serde_json::Value as JsonValue;
|
|
|
|
use serde_json::Value as JsonValue;
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
collections::HashMap,
|
|
|
|
fs::{create_dir_all, read, File},
|
|
|
|
fs,
|
|
|
|
io::Write,
|
|
|
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
time::Duration,
|
|
|
|
time::Duration,
|
|
|
@ -298,18 +297,17 @@ impl<R: Runtime> StoreInner<R> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Saves the store to disk at the store's `path`.
|
|
|
|
/// Saves the store to disk at the store's `path`.
|
|
|
|
pub fn save(&self) -> crate::Result<()> {
|
|
|
|
pub fn save(&self) -> crate::Result<()> {
|
|
|
|
create_dir_all(self.path.parent().expect("invalid store path"))?;
|
|
|
|
fs::create_dir_all(self.path.parent().expect("invalid store path"))?;
|
|
|
|
|
|
|
|
|
|
|
|
let bytes = (self.serialize_fn)(&self.cache).map_err(crate::Error::Serialize)?;
|
|
|
|
let bytes = (self.serialize_fn)(&self.cache).map_err(crate::Error::Serialize)?;
|
|
|
|
let mut f = File::create(&self.path)?;
|
|
|
|
fs::write(&self.path, bytes)?;
|
|
|
|
f.write_all(&bytes)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Update the store from the on-disk state
|
|
|
|
/// Update the store from the on-disk state
|
|
|
|
pub fn load(&mut self) -> crate::Result<()> {
|
|
|
|
pub fn load(&mut self) -> crate::Result<()> {
|
|
|
|
let bytes = read(&self.path)?;
|
|
|
|
let bytes = fs::read(&self.path)?;
|
|
|
|
|
|
|
|
|
|
|
|
self.cache
|
|
|
|
self.cache
|
|
|
|
.extend((self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?);
|
|
|
|
.extend((self.deserialize_fn)(&bytes).map_err(crate::Error::Deserialize)?);
|
|
|
|