use crate::NodeId;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VortexError {
Network(String),
Storage(String),
Timeout,
Unavailable,
Custom(String),
}
impl std::fmt::Display for VortexError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VortexError::Network(s) => write!(f, "network error: {s}"),
VortexError::Storage(s) => write!(f, "storage error: {s}"),
VortexError::Timeout => write!(f, "timeout"),
VortexError::Unavailable => write!(f, "unavailable"),
VortexError::Custom(s) => write!(f, "{s}"),
}
}
}
impl std::error::Error for VortexError {}
pub type Result<T> = std::result::Result<T, VortexError>;
pub trait VortexNetwork {
fn send(&mut self, to: NodeId, payload: Vec<u8>) -> Result<()>;
fn recv(&mut self) -> Option<(NodeId, Vec<u8>)>;
fn broadcast(&mut self, nodes: &[NodeId], payload: Vec<u8>) -> Result<()> {
for &node in nodes {
self.send(node, payload.clone())?;
}
Ok(())
}
}
pub trait VortexStorage {
fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>;
fn put(&mut self, key: &[u8], value: &[u8]) -> Result<()>;
fn delete(&mut self, key: &[u8]) -> Result<()>;
fn scan(&self, start: &[u8], end: &[u8]) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
fn write_batch(&mut self, ops: Vec<StorageOp>) -> Result<()>;
fn flush(&mut self) -> Result<()>;
fn snapshot(&self) -> Result<Vec<(Vec<u8>, Vec<u8>)>>;
}
#[derive(Debug, Clone)]
pub enum StorageOp {
Put { key: Vec<u8>, value: Vec<u8> },
Delete { key: Vec<u8> },
}
pub trait VortexClock {
fn now_us(&self) -> u64;
fn sleep_us(&self, duration_us: u64);
}
pub trait VortexScheduler {
fn schedule(&mut self, delay_ticks: u64, tag: String) -> u64;
fn cancel(&mut self, handle: u64) -> bool;
}