219 lines
6.4 KiB
Rust
219 lines
6.4 KiB
Rust
use crate::domain::{Campaign, VaultEvidenceReceipt};
|
|
use anyhow::{Context, Result, anyhow};
|
|
use chrono::Utc;
|
|
use cultcache_rs::{CacheBackingStore, CultCacheEnvelope, OwnedRedbMessagePackBackingStore};
|
|
use serde::{Serialize, de::DeserializeOwned};
|
|
use std::path::Path;
|
|
|
|
#[derive(Clone)]
|
|
pub struct CampaignStore {
|
|
inner: OwnedRedbMessagePackBackingStore,
|
|
}
|
|
|
|
impl CampaignStore {
|
|
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
|
|
Ok(Self {
|
|
inner: OwnedRedbMessagePackBackingStore::new(path.as_ref())?,
|
|
})
|
|
}
|
|
pub fn identity(&self) -> &str {
|
|
self.inner.file_identity()
|
|
}
|
|
|
|
pub fn keys(&self, kind: &str) -> Result<Vec<String>> {
|
|
Ok(self
|
|
.inner
|
|
.pull_all()?
|
|
.into_iter()
|
|
.filter(|r| r.r#type == kind)
|
|
.map(|r| r.key)
|
|
.collect())
|
|
}
|
|
|
|
pub fn load_all<T: DeserializeOwned>(&self, kind: &str) -> Result<Vec<T>> {
|
|
self.inner
|
|
.pull_all()?
|
|
.into_iter()
|
|
.filter(|row| row.r#type == kind)
|
|
.map(|row| rmp_serde::from_slice(&row.payload).context("decode CultCache row"))
|
|
.collect()
|
|
}
|
|
|
|
pub fn load<T: DeserializeOwned>(
|
|
&self,
|
|
kind: &str,
|
|
key: &str,
|
|
) -> Result<Option<(CultCacheEnvelope, T)>> {
|
|
let row = self
|
|
.inner
|
|
.pull_all()?
|
|
.into_iter()
|
|
.find(|r| r.r#type == kind && r.key == key);
|
|
row.map(|r| {
|
|
let value = rmp_serde::from_slice(&r.payload).context("decode CultCache row")?;
|
|
Ok((r, value))
|
|
})
|
|
.transpose()
|
|
}
|
|
|
|
pub fn insert<T: Serialize>(
|
|
&self,
|
|
kind: &str,
|
|
schema: &str,
|
|
key: &str,
|
|
value: &T,
|
|
) -> Result<CultCacheEnvelope> {
|
|
let row = envelope(kind, schema, key, value)?;
|
|
if !self.inner.insert_entry_if_absent(row.clone())? {
|
|
return Err(anyhow!("row already exists: {kind}/{key}"));
|
|
}
|
|
Ok(row)
|
|
}
|
|
|
|
pub fn create_campaign(
|
|
&self,
|
|
campaign: &Campaign,
|
|
receipts: &[VaultEvidenceReceipt],
|
|
) -> Result<CultCacheEnvelope> {
|
|
let campaign_row = envelope(
|
|
"campaign.v1",
|
|
"ghostlight.campaign.v1",
|
|
&campaign.id.to_string(),
|
|
campaign,
|
|
)?;
|
|
let mut rows = vec![campaign_row.clone()];
|
|
rows.push(envelope(
|
|
"campaign_seed.v1",
|
|
"ghostlight.campaign.v1",
|
|
&campaign.id.to_string(),
|
|
campaign,
|
|
)?);
|
|
for receipt in receipts {
|
|
rows.push(envelope(
|
|
"vault_evidence_receipt.v1",
|
|
"ghostlight.vault_evidence_receipt.v1",
|
|
&receipt.id,
|
|
receipt,
|
|
)?);
|
|
}
|
|
for candidate in campaign.canon_candidates.values() {
|
|
rows.push(envelope(
|
|
"canon_candidate.v1",
|
|
"ghostlight.canon_candidate.v1",
|
|
&candidate.id,
|
|
candidate,
|
|
)?);
|
|
}
|
|
if !self.inner.compare_and_swap_batch(&[], rows)? {
|
|
return Err(anyhow!("campaign store is not empty"));
|
|
}
|
|
Ok(campaign_row)
|
|
}
|
|
|
|
pub fn snapshot_to(&self, path: impl AsRef<Path>) -> Result<()> {
|
|
let rows = self.inner.pull_all()?;
|
|
let target = OwnedRedbMessagePackBackingStore::new(path.as_ref())?;
|
|
if !target.compare_and_swap_batch(&[], rows)? {
|
|
return Err(anyhow!("export target is not empty"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub fn replace<T: Serialize>(
|
|
&self,
|
|
expected: &CultCacheEnvelope,
|
|
schema: &str,
|
|
value: &T,
|
|
) -> Result<CultCacheEnvelope> {
|
|
let next = envelope(&expected.r#type, schema, &expected.key, value)?;
|
|
if !self.inner.compare_and_swap_entry(expected, next.clone())? {
|
|
return Err(anyhow!("stale CultCache snapshot"));
|
|
}
|
|
Ok(next)
|
|
}
|
|
|
|
pub fn append_with_replace<T: Serialize, R: Serialize>(
|
|
&self,
|
|
expected: &CultCacheEnvelope,
|
|
next_schema: &str,
|
|
next: &T,
|
|
receipt_kind: &str,
|
|
receipt_schema: &str,
|
|
receipt_key: &str,
|
|
receipt: &R,
|
|
) -> Result<CultCacheEnvelope> {
|
|
let next_row = envelope(&expected.r#type, next_schema, &expected.key, next)?;
|
|
let receipt_row = envelope(receipt_kind, receipt_schema, receipt_key, receipt)?;
|
|
if !self.inner.compare_and_swap_batch(
|
|
std::slice::from_ref(expected),
|
|
vec![next_row.clone(), receipt_row],
|
|
)? {
|
|
return Err(anyhow!("stale CultCache snapshot"));
|
|
}
|
|
Ok(next_row)
|
|
}
|
|
|
|
pub fn append_world_commit<T: Serialize, R: Serialize>(
|
|
&self,
|
|
expected: &CultCacheEnvelope,
|
|
next: &T,
|
|
receipt_key: &str,
|
|
receipt: &R,
|
|
evidence: &[VaultEvidenceReceipt],
|
|
candidates: &[crate::domain::CanonCandidate],
|
|
) -> Result<CultCacheEnvelope> {
|
|
let next_row = envelope(
|
|
&expected.r#type,
|
|
"ghostlight.campaign.v1",
|
|
&expected.key,
|
|
next,
|
|
)?;
|
|
let mut rows = vec![
|
|
next_row.clone(),
|
|
envelope(
|
|
"world_commit_receipt.v1",
|
|
"ghostlight.world_commit_receipt.v1",
|
|
receipt_key,
|
|
receipt,
|
|
)?,
|
|
];
|
|
for item in evidence {
|
|
rows.push(envelope(
|
|
"vault_evidence_receipt.v1",
|
|
"ghostlight.vault_evidence_receipt.v1",
|
|
&item.id,
|
|
item,
|
|
)?);
|
|
}
|
|
for item in candidates {
|
|
rows.push(envelope(
|
|
"canon_candidate.v1",
|
|
"ghostlight.canon_candidate.v1",
|
|
&item.id,
|
|
item,
|
|
)?);
|
|
}
|
|
if !self
|
|
.inner
|
|
.compare_and_swap_batch(std::slice::from_ref(expected), rows)?
|
|
{
|
|
return Err(anyhow!("stale CultCache snapshot"));
|
|
}
|
|
Ok(next_row)
|
|
}
|
|
}
|
|
|
|
fn envelope<T: Serialize>(
|
|
kind: &str,
|
|
schema: &str,
|
|
key: &str,
|
|
value: &T,
|
|
) -> Result<CultCacheEnvelope> {
|
|
Ok(CultCacheEnvelope {
|
|
key: key.into(),
|
|
r#type: kind.into(),
|
|
payload: rmp_serde::to_vec_named(value)?,
|
|
stored_at: Utc::now().to_rfc3339(),
|
|
schema_id: Some(schema.into()),
|
|
})
|
|
}
|