sim_lib_journal/
persistent.rs1use sim_kernel::{ContentId, Datum};
2use thiserror::Error;
3
4use crate::{JournalBackend, JournalError, JournalObject};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct StoredDatumRef {
9 pub meaning: ContentId,
11 pub storage: ContentId,
13}
14
15#[derive(Clone, Debug, PartialEq, Eq, Error)]
17pub enum StoreError {
18 #[error(transparent)]
20 Journal(#[from] JournalError),
21}
22
23pub trait PersistentSemanticObjects {
25 fn put(&mut self, value: Datum) -> Result<StoredDatumRef, StoreError>;
27 fn get(&self, meaning: &ContentId) -> Result<Datum, StoreError>;
29 fn rebuild_index(&mut self) -> Result<Vec<StoredDatumRef>, StoreError>;
31}
32
33pub struct PersistentObjectStore<B> {
35 backend: B,
36}
37
38impl<B: JournalBackend> PersistentObjectStore<B> {
39 pub fn open(backend: B) -> Result<Self, StoreError> {
41 backend.rebuild_datum_index()?;
42 Ok(Self { backend })
43 }
44
45 pub fn into_inner(self) -> B {
47 self.backend
48 }
49}
50
51impl<B: JournalBackend> PersistentSemanticObjects for PersistentObjectStore<B> {
52 fn put(&mut self, value: Datum) -> Result<StoredDatumRef, StoreError> {
53 Ok(self.backend.put_datum(JournalObject::from_datum(value)?)?)
54 }
55
56 fn get(&self, meaning: &ContentId) -> Result<Datum, StoreError> {
57 let value = self.backend.get_datum(meaning)?;
58 if value
59 .content_id()
60 .map_err(|_| JournalError::NonCanonicalDatum)?
61 != *meaning
62 {
63 return Err(JournalError::CorruptObject(meaning.clone()).into());
64 }
65 Ok(value)
66 }
67
68 fn rebuild_index(&mut self) -> Result<Vec<StoredDatumRef>, StoreError> {
69 Ok(self.backend.rebuild_datum_index()?)
70 }
71}