use crate::GraphLimits;
use crate::broker::protocol::HostCommand;
use crate::broker::{
BrokerError, BrokerLimits, BrokerStorageOptions, ContextBroker, SourceRevision,
};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[cfg(all(feature = "broker-persistence", any(unix, windows)))]
mod sqlite;
#[derive(Default)]
pub(super) struct Persistence {
active: bool,
#[cfg(all(feature = "broker-persistence", any(unix, windows)))]
store: Option<sqlite::Store>,
}
#[derive(Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum Operation {
Initialize,
Resume,
ReplaceSource,
SetEdge,
AdmitProposal,
}
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Event {
operation: Operation,
epoch: String,
expected: BTreeMap<String, SourceRevision>,
resulting: BTreeMap<String, SourceRevision>,
}
impl Persistence {
pub(super) fn open(
domain: String,
graph: GraphLimits,
limits: BrokerLimits,
options: Option<BrokerStorageOptions>,
) -> Result<(ContextBroker, Self, bool), BrokerError> {
if let Some(options) = options {
#[cfg(all(feature = "broker-persistence", any(unix, windows)))]
{
let (broker, store, restored) =
sqlite::Store::open(domain, graph, limits, options)?;
return Ok((
broker,
Self {
active: true,
store: Some(store),
},
restored,
));
}
#[cfg(not(all(feature = "broker-persistence", any(unix, windows))))]
{
let _ = options;
return Err(BrokerError::Unavailable);
}
}
Ok((
ContextBroker::new(domain, graph, limits)?,
Self::default(),
false,
))
}
pub(super) fn active(&self) -> bool {
self.active
}
pub(super) fn prepare(
&self,
broker: &ContextBroker,
command: &HostCommand,
) -> Result<Option<Event>, BrokerError> {
if !self.active {
return Ok(None);
}
let (operation, expected) = match command {
HostCommand::ReplaceSource {
source, expected, ..
} => (
Operation::ReplaceSource,
BTreeMap::from([(source.clone(), expected.clone())]),
),
HostCommand::SetEdge { expected, .. } => (Operation::SetEdge, expected.clone()),
HostCommand::CommitSourceReplace { transaction } => {
let (source, expected, _) = broker.host_staged_source(transaction)?;
(
Operation::ReplaceSource,
BTreeMap::from([(source.into(), expected.clone())]),
)
}
HostCommand::AdmitProposal { proposal_id, .. } => {
let (source, expected, _) = broker.host_proposal(proposal_id)?;
(
Operation::AdmitProposal,
BTreeMap::from([(source.into(), expected.clone())]),
)
}
_ => return Ok(None),
};
Ok(Some(Event {
operation,
epoch: broker.epoch().into(),
expected,
resulting: BTreeMap::new(),
}))
}
pub(super) fn commit(
&mut self,
broker: &ContextBroker,
mut event: Event,
) -> Result<(), BrokerError> {
for source in event.expected.keys() {
event
.resulting
.insert(source.clone(), broker.host_source_revision(source)?);
}
#[cfg(all(feature = "broker-persistence", any(unix, windows)))]
if let Some(store) = &mut self.store {
return store.commit(broker, &event);
}
Err(BrokerError::Unavailable)
}
}