pub mod memory;
pub mod redb;
pub use memory::MemoryBackend;
pub use redb::RedbBackend;
use crate::{
entry::EventStoreEntry,
error::EventStoreError,
manifest::{RunManifest, RunStatus},
snapshot::SnapshotAnchor,
};
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum IndexKind {
ClientOrderId,
VenueOrderId,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ScanDirection {
Forward,
Reverse,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct IndexKey {
pub kind: IndexKind,
pub key: String,
}
impl IndexKey {
#[must_use]
pub const fn new(kind: IndexKind, key: String) -> Self {
Self { kind, key }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppendEntry {
pub entry: EventStoreEntry,
pub index_keys: Vec<IndexKey>,
}
impl AppendEntry {
#[must_use]
pub const fn new(entry: EventStoreEntry, index_keys: Vec<IndexKey>) -> Self {
Self { entry, index_keys }
}
#[must_use]
pub const fn without_indices(entry: EventStoreEntry) -> Self {
Self {
entry,
index_keys: Vec::new(),
}
}
}
pub trait EventStore: Send {
fn open_run(&mut self, manifest: RunManifest) -> Result<(), EventStoreError>;
fn append_batch(&mut self, entries: &[AppendEntry]) -> Result<u64, EventStoreError>;
fn scan_range(
&self,
from: u64,
to: u64,
direction: ScanDirection,
) -> Result<Vec<EventStoreEntry>, EventStoreError>;
fn scan_seq(&self, seq: u64) -> Result<Option<EventStoreEntry>, EventStoreError>;
fn lookup(&self, kind: IndexKind, key: &str) -> Result<Option<u64>, EventStoreError>;
fn iter_index_keys(&self, kind: IndexKind) -> Result<Vec<(String, u64)>, EventStoreError>;
fn record_snapshot_anchor(&mut self, _anchor: SnapshotAnchor) -> Result<(), EventStoreError> {
Err(EventStoreError::Backend(
"snapshot anchors are not supported by this backend".to_string(),
))
}
fn latest_snapshot_anchor(&self) -> Result<Option<SnapshotAnchor>, EventStoreError> {
Err(EventStoreError::Backend(
"snapshot anchors are not supported by this backend".to_string(),
))
}
fn seal(&mut self, status: RunStatus) -> Result<(), EventStoreError>;
fn manifest(&self) -> Result<RunManifest, EventStoreError>;
fn high_watermark(&self) -> Result<u64, EventStoreError>;
}