use std::collections::BTreeMap;
use async_trait::async_trait;
use tokio::sync::RwLock;
use crate::error::Result;
use crate::partition::PartitionId;
use crate::proc::{
LedgerEntry, LedgerQuery, LedgerStore, Observation, ObservationQuery, PassState,
order_observations,
};
#[derive(Debug, Default)]
pub struct InMemoryLedgerStore {
rows: RwLock<Vec<LedgerEntry>>,
observations: RwLock<BTreeMap<String, Observation>>,
states: RwLock<BTreeMap<PartitionId, PassState>>,
}
impl InMemoryLedgerStore {
pub fn new() -> Self {
Self::default()
}
}
#[async_trait]
impl LedgerStore for InMemoryLedgerStore {
async fn append(&self, mut entry: LedgerEntry) -> Result<LedgerEntry> {
let mut rows = self.rows.write().await;
entry.seq = rows.last().map_or(1, |last| last.seq + 1);
rows.push(entry.clone());
Ok(entry)
}
async fn list(&self, query: &LedgerQuery) -> Result<Vec<LedgerEntry>> {
Ok(self
.rows
.read()
.await
.iter()
.filter(|entry| query.matches(entry))
.take(query.limit.unwrap_or(usize::MAX))
.cloned()
.collect())
}
async fn put_observation(&self, observation: Observation) -> Result<()> {
self.observations.write().await.insert(observation.id.clone(), observation);
Ok(())
}
async fn observations(&self, query: &ObservationQuery) -> Result<Vec<Observation>> {
let mut observations: Vec<Observation> = self
.observations
.read()
.await
.values()
.filter(|observation| query.matches(observation))
.cloned()
.collect();
order_observations(&mut observations);
observations.truncate(query.limit.unwrap_or(usize::MAX));
Ok(observations)
}
async fn pass_state(&self, partition: &PartitionId) -> Result<Option<PassState>> {
Ok(self.states.read().await.get(partition).cloned())
}
async fn pass_states(&self) -> Result<Vec<PassState>> {
Ok(self.states.read().await.values().cloned().collect())
}
async fn put_pass_state(&self, state: PassState) -> Result<()> {
self.states.write().await.insert(state.partition.clone(), state);
Ok(())
}
}