use crate::model::ListEventsOptions;
use crate::{
MemoryEvent, MemoryObject, QueueClaimOptions, QueueJob, QueueNackOptions, ThingdResult,
};
pub trait ObjectStore {
fn put_object(&mut self, object: MemoryObject) -> ThingdResult<MemoryObject>;
fn put_objects_batch(&mut self, objects: Vec<MemoryObject>) -> ThingdResult<Vec<MemoryObject>> {
let mut results = Vec::with_capacity(objects.len());
for object in objects {
results.push(self.put_object(object)?);
}
Ok(results)
}
fn get_object(&self, collection: &str, id: &str) -> ThingdResult<Option<MemoryObject>>;
fn list_objects(&self, collections: Option<&[String]>) -> ThingdResult<Vec<MemoryObject>>;
fn delete_object(&mut self, collection: &str, id: &str) -> ThingdResult<bool>;
fn count_objects(&self) -> ThingdResult<u64>;
fn list_collections(&self) -> ThingdResult<Vec<String>>;
}
pub trait EventLog {
fn append_event(&mut self, event: MemoryEvent) -> ThingdResult<MemoryEvent>;
fn append_events_batch(&mut self, events: Vec<MemoryEvent>) -> ThingdResult<Vec<MemoryEvent>> {
let mut results = Vec::with_capacity(events.len());
for event in events {
results.push(self.append_event(event)?);
}
Ok(results)
}
fn list_events(
&self,
stream: Option<&str>,
options: ListEventsOptions,
) -> ThingdResult<Vec<MemoryEvent>>;
fn count_events(&self) -> ThingdResult<u64>;
fn list_streams(&self) -> ThingdResult<Vec<String>>;
}
pub trait QueueStore {
fn push_job(&mut self, job: QueueJob) -> ThingdResult<QueueJob>;
fn push_jobs_batch(&mut self, jobs: Vec<QueueJob>) -> ThingdResult<Vec<QueueJob>> {
let mut results = Vec::with_capacity(jobs.len());
for job in jobs {
results.push(self.push_job(job)?);
}
Ok(results)
}
fn claim_job(&mut self, queue: &str) -> ThingdResult<Option<QueueJob>> {
self.claim_job_with_options(queue, QueueClaimOptions::default())
}
fn claim_job_with_options(
&mut self,
queue: &str,
options: QueueClaimOptions,
) -> ThingdResult<Option<QueueJob>>;
fn ack_job(&mut self, queue: &str, id: &str) -> ThingdResult<Option<QueueJob>>;
fn claim_and_ack(
&mut self,
queue: &str,
options: QueueClaimOptions,
) -> ThingdResult<Option<QueueJob>> {
if let Some(job) = self.claim_job_with_options(queue, options)? {
self.ack_job(queue, &job.id)
} else {
Ok(None)
}
}
fn nack_job(&mut self, queue: &str, id: &str) -> ThingdResult<Option<QueueJob>> {
self.nack_job_with_options(queue, id, QueueNackOptions::default())
}
fn nack_job_with_options(
&mut self,
queue: &str,
id: &str,
options: QueueNackOptions,
) -> ThingdResult<Option<QueueJob>>;
fn list_jobs(&self, queue: &str) -> ThingdResult<Vec<QueueJob>>;
fn list_dead_jobs(&self, queue: &str) -> ThingdResult<Vec<QueueJob>>;
fn list_queues(&self) -> ThingdResult<Vec<String>>;
fn count_active_jobs(&self) -> ThingdResult<u64>;
fn count_dead_jobs(&self) -> ThingdResult<u64>;
}
pub trait Searcher {
fn search(
&self,
query: &str,
options: crate::SearchOptions,
) -> ThingdResult<Vec<crate::SearchHit>>;
}
pub trait LinkStore {
fn create_link(&mut self, link: crate::Link) -> ThingdResult<crate::Link>;
fn delete_link(&mut self, id: &str) -> ThingdResult<bool>;
fn get_link(&self, id: &str) -> ThingdResult<Option<crate::Link>>;
fn get_neighbors(
&self,
reference: &str,
direction: crate::LinkDirection,
options: crate::LinkQueryOptions,
) -> ThingdResult<Vec<crate::Link>>;
fn count_links(&self) -> ThingdResult<u64>;
}
pub trait ThingStore: EventLog + ObjectStore + QueueStore + Searcher + LinkStore {}
impl<T> ThingStore for T where T: EventLog + ObjectStore + QueueStore + Searcher + LinkStore {}