#[cfg(feature = "eventstore")]
use super::super::cloudevents::CloudEvent;
use super::super::Event;
use super::super::Result;
#[cfg(feature = "eventstore")]
use super::EventStore;
use chrono::prelude::*;
use std::sync::Mutex;
#[cfg(feature = "eventstore")]
pub struct MemoryEventStore {
evts: Mutex<Vec<CloudEvent>>,
}
#[cfg(feature = "eventstore")]
impl MemoryEventStore {
pub fn new() -> MemoryEventStore {
MemoryEventStore {
evts: Mutex::new(Vec::<CloudEvent>::new()),
}
}
}
#[cfg(feature = "eventstore")]
impl EventStore for MemoryEventStore {
fn append(&self, evt: impl Event, _stream: &str) -> Result<CloudEvent> {
let mut guard = self.evts.lock().unwrap();
let cloud_event = CloudEvent::from(evt);
guard.push(cloud_event.clone());
Ok(cloud_event)
}
}
#[cfg(feature = "eventstore")]
impl MemoryEventStore {
pub fn get_all(&self, event_type: &str) -> Result<Vec<CloudEvent>> {
let guard = self.evts.lock().unwrap();
let matches = guard
.iter()
.filter(|evt| evt.event_type == event_type)
.cloned()
.collect();
Ok(matches)
}
pub fn get_from(&self, event_type: &str, start: DateTime<Utc>) -> Result<Vec<CloudEvent>> {
let guard = self.evts.lock().unwrap();
let matches = guard
.iter()
.filter(|evt| evt.event_type == event_type && evt.event_time >= start)
.cloned()
.collect();
Ok(matches)
}
pub fn get_range(
&self,
event_type: &str,
start: DateTime<Utc>,
end: DateTime<Utc>,
) -> Result<Vec<CloudEvent>> {
let guard = self.evts.lock().unwrap();
let matches = guard
.iter()
.filter(|evt| {
evt.event_type == event_type && evt.event_time >= start && evt.event_time <= end
})
.cloned()
.collect();
Ok(matches)
}
}