use std::sync::{Mutex, MutexGuard};
use crate::v2::context::V2Context;
use crate::v2::errors::V2Result;
use crate::v2::journal::{
empty_security_journal_v2, validate_security_journal_transition_inner, SecurityJournalHeadV2,
SecurityJournalStoreErrorV2, SecurityJournalStoreV2, SecurityJournalV2,
};
use crate::v2::journal_snapshot::{
export_security_journal_snapshot_v2, import_security_journal_snapshot_v2,
};
struct Anchored {
head: SecurityJournalHeadV2,
snapshot: Vec<u8>,
}
pub struct JournalStore {
state: Mutex<Anchored>,
}
impl std::fmt::Debug for JournalStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JournalStore")
.field("head", &self.head())
.finish()
}
}
impl JournalStore {
pub fn new() -> V2Result<Self> {
let genesis = empty_security_journal_v2();
let snapshot = export_security_journal_snapshot_v2(&genesis)?;
Ok(JournalStore {
state: Mutex::new(Anchored {
head: SecurityJournalHeadV2 {
generation: genesis.generation,
digest: genesis.digest,
},
snapshot,
}),
})
}
fn lock(&self) -> MutexGuard<'_, Anchored> {
self.state
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner())
}
pub fn head(&self) -> SecurityJournalHeadV2 {
self.lock().head.clone()
}
pub(crate) fn anchored(&self) -> (SecurityJournalHeadV2, Vec<u8>) {
let state = self.lock();
(state.head.clone(), state.snapshot.clone())
}
pub(crate) fn from_anchored(head: SecurityJournalHeadV2, snapshot: Vec<u8>) -> Self {
JournalStore {
state: Mutex::new(Anchored { head, snapshot }),
}
}
pub(crate) fn restore_anchored(&self, head: SecurityJournalHeadV2, snapshot: Vec<u8>) {
let mut state = self.lock();
state.head = head;
state.snapshot = snapshot;
}
}
impl SecurityJournalStoreV2 for JournalStore {
fn load(&self, _ctx: &V2Context) -> V2Result<(SecurityJournalV2, SecurityJournalHeadV2)> {
let state = self.lock();
let journal = import_security_journal_snapshot_v2(&state.snapshot, &state.head)?;
Ok((journal, state.head.clone()))
}
fn compare_and_swap(
&self,
_ctx: &V2Context,
expected: &SecurityJournalHeadV2,
next: &SecurityJournalV2,
) -> Result<(), SecurityJournalStoreErrorV2> {
let mut state = self.lock();
if *expected != state.head {
return Err(SecurityJournalStoreErrorV2::Conflict);
}
let current = import_security_journal_snapshot_v2(&state.snapshot, &state.head)?;
validate_security_journal_transition_inner(¤t, next)?;
let snapshot = export_security_journal_snapshot_v2(next)?;
state.snapshot = snapshot;
state.head = SecurityJournalHeadV2 {
generation: next.generation,
digest: next.digest.clone(),
};
Ok(())
}
}