use std::path::Path;
use std::sync::Arc;
use boatramp_core::kv::{KvStore, MemoryKv};
use crate::error::Result;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum BlobBackend {
Fs,
S3,
Gcs,
Azure,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum KvBackend {
Slatedb,
Memory,
Cloudflare,
}
pub const CONTROL_PLANE_FLUSH: std::time::Duration = std::time::Duration::from_millis(5);
pub async fn build_kv(kv: KvBackend, data_dir: &Path) -> Result<Arc<dyn KvStore>> {
match kv {
KvBackend::Slatedb => build_slatedb_kv(data_dir).await,
KvBackend::Memory => Ok(Arc::new(MemoryKv::new())),
KvBackend::Cloudflare => build_cloudflare_kv(),
}
}
#[cfg(feature = "slatedb")]
async fn build_slatedb_kv(data_dir: &Path) -> Result<Arc<dyn KvStore>> {
Ok(Arc::new(
boatramp_storage::SlateKv::open_local_with_flush(
data_dir.join("kv-slate"),
CONTROL_PLANE_FLUSH,
)
.await?,
))
}
#[cfg(not(feature = "slatedb"))]
async fn build_slatedb_kv(_data_dir: &Path) -> Result<Arc<dyn KvStore>> {
Err(crate::error::Error::NoSlatedbSupport)
}
#[cfg(feature = "cloudflare-kv")]
fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
Ok(Arc::new(boatramp_storage::CloudflareKv::from_env()?))
}
#[cfg(not(feature = "cloudflare-kv"))]
fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
Err(crate::error::Error::NoCloudflareKvSupport)
}