Skip to content

Commit

Permalink
persistence: update load/save workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 5, 2024
1 parent f101040 commit c837cdb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/persistence/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub trait LoadFs: Sized {
}

pub trait StoreFs {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError>;
fn store(&self) -> Result<(), SerializeError>;
}

impl<S: StashProvider, H: StateProvider, I: IndexProvider> LoadFs for Stock<S, H, I>
Expand All @@ -58,11 +58,10 @@ where
H: StoreFs,
I: StoreFs,
{
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let path = path.as_ref();
self.as_stash_provider().store(path)?;
self.as_state_provider().store(path)?;
self.as_index_provider().store(path)?;
fn store(&self) -> Result<(), SerializeError> {
self.as_stash_provider().store()?;
self.as_state_provider().store()?;
self.as_index_provider().store()?;

Ok(())
}
Expand All @@ -72,46 +71,58 @@ impl LoadFs for MemStash {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("stash.dat");
Self::strict_deserialize_from_file::<U32>(file)
let mut me = Self::strict_deserialize_from_file::<U32>(&file)?;
me.set_filename(file);
Ok(me)
}
}

impl StoreFs for MemStash {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("stash.dat");
self.strict_serialize_to_file::<U32>(file)
fn store(&self) -> Result<(), SerializeError> {
if self.is_dirty() {
self.strict_serialize_to_file::<U32>(&self.filename())
} else {
Ok(())
}
}
}

impl LoadFs for MemState {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("state.dat");
Self::strict_deserialize_from_file::<U32>(file)
let mut me = Self::strict_deserialize_from_file::<U32>(&file)?;
me.set_filename(file);
Ok(me)
}
}

impl StoreFs for MemState {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("state.dat");
self.strict_serialize_to_file::<U32>(file)
fn store(&self) -> Result<(), SerializeError> {
if self.is_dirty() {
self.strict_serialize_to_file::<U32>(&self.filename())
} else {
Ok(())
}
}
}

impl LoadFs for MemIndex {
fn load(path: impl AsRef<Path>) -> Result<Self, DeserializeError> {
let mut file = path.as_ref().to_owned();
file.push("index.dat");
Self::strict_deserialize_from_file::<U32>(file)
let mut me = Self::strict_deserialize_from_file::<U32>(&file)?;
me.set_filename(file);
Ok(me)
}
}

impl StoreFs for MemIndex {
fn store(&self, path: impl AsRef<Path>) -> Result<(), SerializeError> {
let mut file = path.as_ref().to_owned();
file.push("index.dat");
self.strict_serialize_to_file::<U32>(file)
fn store(&self) -> Result<(), SerializeError> {
if self.is_dirty() {
self.strict_serialize_to_file::<U32>(&self.filename())
} else {
Ok(())
}
}
}
34 changes: 34 additions & 0 deletions src/persistence/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

use std::collections::BTreeSet;
use std::convert::Infallible;
#[cfg(feature = "fs")]
use std::path::{Path, PathBuf};

use aluvm::library::{Lib, LibId};
use amplify::confinement::{
Expand Down Expand Up @@ -60,7 +62,11 @@ use crate::LIB_NAME_RGB_STD;
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct MemStash {
#[strict_type(skip)]
dirty: bool,
#[cfg(feature = "fs")]
#[strict_type(skip)]
filename: PathBuf,

schemata: TinyOrdMap<SchemaId, SchemaIfaces>,
ifaces: TinyOrdMap<IfaceId, Iface>,
Expand All @@ -82,6 +88,12 @@ impl StrictDeserialize for MemStash {}

impl MemStash {
pub fn new() -> Self { MemStash::default() }

pub(crate) fn is_dirty(&self) -> bool { self.dirty }
#[cfg(feature = "fs")]
pub(crate) fn filename(&self) -> &Path { &self.filename }
#[cfg(feature = "fs")]
pub(crate) fn set_filename(&mut self, filename: PathBuf) { self.filename = filename }
}

impl StoreTransaction for MemStash {
Expand Down Expand Up @@ -422,7 +434,12 @@ impl From<confinement::Error> for StateUpdateError<confinement::Error> {
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct MemState {
#[strict_type(skip)]
dirty: bool,
#[cfg(feature = "fs")]
#[strict_type(skip)]
filename: PathBuf,

history: TinyOrdMap<ContractId, ContractHistory>,
}

Expand All @@ -431,6 +448,12 @@ impl StrictDeserialize for MemState {}

impl MemState {
pub fn new() -> Self { MemState::default() }

pub(crate) fn is_dirty(&self) -> bool { self.dirty }
#[cfg(feature = "fs")]
pub(crate) fn filename(&self) -> &Path { &self.filename }
#[cfg(feature = "fs")]
pub(crate) fn set_filename(&mut self, filename: PathBuf) { self.filename = filename }
}

impl StoreTransaction for MemState {
Expand Down Expand Up @@ -521,7 +544,12 @@ pub struct ContractIndex {
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_RGB_STD)]
pub struct MemIndex {
#[strict_type(skip)]
dirty: bool,
#[cfg(feature = "fs")]
#[strict_type(skip)]
filename: PathBuf,

op_bundle_index: MediumOrdMap<OpId, BundleId>,
bundle_contract_index: MediumOrdMap<BundleId, ContractId>,
bundle_witness_index: MediumOrdMap<BundleId, XWitnessId>,
Expand All @@ -534,6 +562,12 @@ impl StrictDeserialize for MemIndex {}

impl MemIndex {
pub fn new() -> Self { MemIndex::default() }

pub(crate) fn is_dirty(&self) -> bool { self.dirty }
#[cfg(feature = "fs")]
pub(crate) fn filename(&self) -> &Path { &self.filename }
#[cfg(feature = "fs")]
pub(crate) fn set_filename(&mut self, filename: PathBuf) { self.filename = filename }
}

impl StoreTransaction for MemIndex {
Expand Down

0 comments on commit c837cdb

Please sign in to comment.