2023-10-02 22:16:15 +02:00
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, VecDeque},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
use crunch_traits::{errors::PersistenceError, EventInfo};
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
enum MsgState {
|
|
|
|
Pending,
|
|
|
|
Published,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Msg {
|
|
|
|
id: String,
|
|
|
|
info: EventInfo,
|
|
|
|
msg: Vec<u8>,
|
|
|
|
state: MsgState,
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:08:12 +02:00
|
|
|
pub struct InMemoryTx {}
|
|
|
|
|
|
|
|
impl crunch_traits::Tx for InMemoryTx {}
|
|
|
|
|
2023-10-02 22:16:15 +02:00
|
|
|
pub struct InMemoryPersistence {
|
|
|
|
pub outbox: Arc<RwLock<VecDeque<Msg>>>,
|
|
|
|
pub store: Arc<RwLock<BTreeMap<String, Msg>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl crunch_traits::Persistence for InMemoryPersistence {
|
|
|
|
async fn insert(&self, event_info: &EventInfo, content: Vec<u8>) -> anyhow::Result<()> {
|
2023-10-05 22:09:56 +02:00
|
|
|
let msg =
|
|
|
|
crunch_envelope::proto::wrap(&event_info.domain, &event_info.entity_type, &content);
|
2023-10-02 22:16:15 +02:00
|
|
|
let msg = Msg {
|
|
|
|
id: uuid::Uuid::new_v4().to_string(),
|
2023-10-05 22:09:56 +02:00
|
|
|
info: event_info.to_owned(),
|
2023-10-02 22:16:15 +02:00
|
|
|
msg,
|
|
|
|
state: MsgState::Pending,
|
|
|
|
};
|
|
|
|
let mut outbox = self.outbox.write().await;
|
|
|
|
outbox.push_back(msg.clone());
|
|
|
|
self.store.write().await.insert(msg.id.clone(), msg);
|
|
|
|
|
|
|
|
tracing::debug!(
|
|
|
|
event_info = event_info.to_string(),
|
|
|
|
content_len = content.len(),
|
|
|
|
"inserted event"
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-03 23:08:12 +02:00
|
|
|
async fn next(&self) -> Result<Option<(String, crunch_traits::DynTx)>, PersistenceError> {
|
2023-10-02 22:16:15 +02:00
|
|
|
let mut outbox = self.outbox.write().await;
|
2023-10-03 23:08:12 +02:00
|
|
|
Ok(outbox
|
|
|
|
.pop_front()
|
|
|
|
.map(|i| (i.id, Box::new(InMemoryTx {}) as crunch_traits::DynTx)))
|
2023-10-02 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get(&self, event_id: &str) -> Result<Option<(EventInfo, Vec<u8>)>, PersistenceError> {
|
|
|
|
let store = self.store.read().await;
|
|
|
|
|
|
|
|
let event = match store.get(event_id).filter(|m| m.state == MsgState::Pending) {
|
|
|
|
Some(event) => event,
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let (content, _) = crunch_envelope::proto::unwrap(event.msg.as_slice())
|
|
|
|
.map_err(|e| PersistenceError::GetErr(anyhow::anyhow!(e)))?;
|
|
|
|
|
2023-10-05 22:09:56 +02:00
|
|
|
Ok(Some((event.info.to_owned(), content)))
|
2023-10-02 22:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn update_published(&self, event_id: &str) -> Result<(), PersistenceError> {
|
|
|
|
match self.store.write().await.get_mut(event_id) {
|
|
|
|
Some(msg) => msg.state = MsgState::Published,
|
|
|
|
None => {
|
|
|
|
return Err(PersistenceError::UpdatePublished(anyhow::anyhow!(
|
|
|
|
"event was not found on id: {}",
|
|
|
|
event_id
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|