oris_kernel/kernel/
snapshot.rs1use serde::{Deserialize, Serialize};
10
11use crate::kernel::identity::{RunId, Seq};
12use crate::kernel::KernelError;
13
14#[derive(Clone, Debug, Serialize, Deserialize)]
19pub struct Snapshot<S> {
20 pub run_id: RunId,
22 pub at_seq: Seq,
24 pub state: S,
26}
27
28pub trait SnapshotStore<S>: Send + Sync {
30 fn load_latest(&self, run_id: &RunId) -> Result<Option<Snapshot<S>>, KernelError>;
32
33 fn save(&self, snapshot: &Snapshot<S>) -> Result<(), KernelError>;
35}
36
37pub struct InMemorySnapshotStore<S> {
39 latest: std::sync::RwLock<std::collections::HashMap<RunId, Snapshot<S>>>,
40}
41
42impl<S: Clone + Send + Sync> InMemorySnapshotStore<S> {
43 pub fn new() -> Self {
44 Self {
45 latest: std::sync::RwLock::new(std::collections::HashMap::new()),
46 }
47 }
48}
49
50impl<S: Clone + Send + Sync> Default for InMemorySnapshotStore<S> {
51 fn default() -> Self {
52 Self::new()
53 }
54}
55
56impl<S: Clone + Send + Sync> SnapshotStore<S> for InMemorySnapshotStore<S> {
57 fn load_latest(&self, run_id: &RunId) -> Result<Option<Snapshot<S>>, KernelError> {
58 let guard = self
59 .latest
60 .read()
61 .map_err(|e| KernelError::SnapshotStore(e.to_string()))?;
62 Ok(guard.get(run_id).cloned())
63 }
64
65 fn save(&self, snapshot: &Snapshot<S>) -> Result<(), KernelError> {
66 let mut guard = self
67 .latest
68 .write()
69 .map_err(|e| KernelError::SnapshotStore(e.to_string()))?;
70 guard.insert(snapshot.run_id.clone(), snapshot.clone());
71 Ok(())
72 }
73}