use async_trait::async_trait;
use std::sync::Arc;
use std::time::Duration;
use crate::kv::{KvError, KvPurge, KvReader, KvWatcher, KvWriter};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum StorageType {
Memory,
#[default]
Persistent,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum DiscardPolicy {
#[default]
New,
Old,
}
impl DiscardPolicy {
pub(crate) fn as_nats(self) -> &'static str {
match self {
DiscardPolicy::New => "new",
DiscardPolicy::Old => "old",
}
}
}
#[derive(Debug, Clone, Default)]
pub struct StoreConfig {
pub name: String,
pub storage: StorageType,
pub max_history: Option<u32>,
pub max_age: Option<Duration>,
pub max_bytes: Option<i64>,
pub num_replicas: Option<usize>,
pub discard: DiscardPolicy,
}
pub trait KvStore: Send + Sync {
fn name(&self) -> &str;
fn reader(&self) -> Arc<dyn KvReader>;
fn watcher(&self) -> Option<Arc<dyn KvWatcher>> {
None
}
fn writer(&self) -> Option<Arc<dyn KvWriter>> {
None
}
fn purge_writer(&self) -> Option<Arc<dyn KvPurge>> {
None
}
}
#[derive(Debug, Clone, Default)]
pub struct ConnectionCapabilities {
pub streaming_watch: bool,
pub prefix_watch: bool,
pub ttl: bool,
pub purge: bool,
pub cas: bool,
pub transactions: bool,
pub max_value_size: usize,
pub global_ordering: bool,
}
#[async_trait]
pub trait Connection: Send + Sync {
async fn connect(&self) -> Result<(), KvError>;
async fn shutdown(&self) -> Result<(), KvError>;
fn is_healthy(&self) -> bool;
async fn store(&self, name: &str) -> Result<Arc<dyn KvStore>, KvError>;
async fn store_with_config(&self, config: StoreConfig) -> Result<Arc<dyn KvStore>, KvError>;
fn capabilities(&self) -> ConnectionCapabilities;
}