|
| 1 | +// SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// Persistent provenance store backed by redb via verisim-storage. |
| 5 | + |
| 6 | +use std::collections::HashMap; |
| 7 | +use std::path::Path; |
| 8 | +use std::sync::{Arc, RwLock}; |
| 9 | + |
| 10 | +use async_trait::async_trait; |
| 11 | +use tracing::info; |
| 12 | +use verisim_storage::redb_backend::RedbBackend; |
| 13 | +use verisim_storage::typed::TypedStore; |
| 14 | + |
| 15 | +use crate::{ProvenanceChain, ProvenanceError, ProvenanceRecord, ProvenanceStore}; |
| 16 | + |
| 17 | +/// Persistent provenance store: redb for durability, in-memory cache for queries. |
| 18 | +pub struct RedbProvenanceStore { |
| 19 | + store: TypedStore<RedbBackend>, |
| 20 | + cache: Arc<RwLock<HashMap<String, ProvenanceChain>>>, |
| 21 | +} |
| 22 | + |
| 23 | +impl RedbProvenanceStore { |
| 24 | + pub async fn open(path: impl AsRef<Path>) -> Result<Self, ProvenanceError> { |
| 25 | + let backend = RedbBackend::open(path.as_ref()) |
| 26 | + .map_err(|e| ProvenanceError::StorageError(format!("redb open: {}", e)))?; |
| 27 | + let store = TypedStore::new(backend, "prov"); |
| 28 | + |
| 29 | + let entries: Vec<(String, ProvenanceChain)> = store |
| 30 | + .scan_prefix("", 1_000_000) |
| 31 | + .await |
| 32 | + .map_err(|e| ProvenanceError::StorageError(format!("scan: {}", e)))?; |
| 33 | + |
| 34 | + let mut cache = HashMap::new(); |
| 35 | + for (id, chain) in entries { |
| 36 | + cache.insert(id, chain); |
| 37 | + } |
| 38 | + |
| 39 | + info!(count = cache.len(), "Loaded provenance store from redb"); |
| 40 | + Ok(Self { store, cache: Arc::new(RwLock::new(cache)) }) |
| 41 | + } |
| 42 | + |
| 43 | + async fn persist_chain(&self, entity_id: &str) -> Result<(), ProvenanceError> { |
| 44 | + let c = self.cache.read().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 45 | + if let Some(chain) = c.get(entity_id) { |
| 46 | + self.store.put(entity_id, chain).await |
| 47 | + .map_err(|e| ProvenanceError::StorageError(format!("put: {}", e)))?; |
| 48 | + } |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[async_trait] |
| 54 | +impl ProvenanceStore for RedbProvenanceStore { |
| 55 | + async fn record(&self, record: ProvenanceRecord) -> Result<(), ProvenanceError> { |
| 56 | + let entity_id = record.entity_id.clone(); |
| 57 | + { |
| 58 | + let mut c = self.cache.write().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 59 | + let chain = c.entry(entity_id.clone()).or_insert_with(|| ProvenanceChain { |
| 60 | + entity_id: entity_id.clone(), |
| 61 | + records: Vec::new(), |
| 62 | + }); |
| 63 | + chain.records.push(record); |
| 64 | + } |
| 65 | + self.persist_chain(&entity_id).await |
| 66 | + } |
| 67 | + |
| 68 | + async fn get_chain(&self, entity_id: &str) -> Result<Option<ProvenanceChain>, ProvenanceError> { |
| 69 | + let c = self.cache.read().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 70 | + Ok(c.get(entity_id).cloned()) |
| 71 | + } |
| 72 | + |
| 73 | + async fn verify_chain(&self, entity_id: &str) -> Result<bool, ProvenanceError> { |
| 74 | + let c = self.cache.read().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 75 | + match c.get(entity_id) { |
| 76 | + Some(chain) => Ok(chain.verify()), |
| 77 | + None => Err(ProvenanceError::NotFound(entity_id.to_string())), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + async fn get_latest(&self, entity_id: &str) -> Result<Option<ProvenanceRecord>, ProvenanceError> { |
| 82 | + let c = self.cache.read().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 83 | + Ok(c.get(entity_id).and_then(|chain| chain.records.last().cloned())) |
| 84 | + } |
| 85 | + |
| 86 | + async fn search_by_actor(&self, actor: &str) -> Result<Vec<ProvenanceRecord>, ProvenanceError> { |
| 87 | + let c = self.cache.read().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 88 | + let mut results = Vec::new(); |
| 89 | + for chain in c.values() { |
| 90 | + for record in &chain.records { |
| 91 | + if record.actor == actor { |
| 92 | + results.push(record.clone()); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + Ok(results) |
| 97 | + } |
| 98 | + |
| 99 | + async fn delete_chain(&self, entity_id: &str) -> Result<(), ProvenanceError> { |
| 100 | + self.store.delete(entity_id).await |
| 101 | + .map_err(|e| ProvenanceError::StorageError(format!("delete: {}", e)))?; |
| 102 | + let mut c = self.cache.write().map_err(|_| ProvenanceError::LockPoisoned)?; |
| 103 | + c.remove(entity_id); |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use super::*; |
| 111 | + |
| 112 | + #[tokio::test] |
| 113 | + async fn test_persistent_provenance_roundtrip() { |
| 114 | + let dir = tempfile::tempdir().unwrap(); |
| 115 | + let path = dir.path().join("prov.redb"); |
| 116 | + |
| 117 | + { |
| 118 | + let store = RedbProvenanceStore::open(&path).await.unwrap(); |
| 119 | + let record = ProvenanceRecord::new("e1", "create", "user1"); |
| 120 | + store.record(record).await.unwrap(); |
| 121 | + } |
| 122 | + |
| 123 | + { |
| 124 | + let store = RedbProvenanceStore::open(&path).await.unwrap(); |
| 125 | + let chain = store.get_chain("e1").await.unwrap().unwrap(); |
| 126 | + assert_eq!(chain.records.len(), 1); |
| 127 | + assert_eq!(chain.records[0].actor, "user1"); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments