boatramp_node/
backends.rs1use std::path::Path;
11use std::sync::Arc;
12
13use boatramp_core::kv::{KvStore, MemoryKv};
14
15use crate::error::Result;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
20pub enum BlobBackend {
21 Fs,
23 S3,
25 Gcs,
27 Azure,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
34pub enum KvBackend {
35 Slatedb,
38 Memory,
40 Cloudflare,
42}
43
44pub const CONTROL_PLANE_FLUSH: std::time::Duration = std::time::Duration::from_millis(5);
47
48pub async fn build_kv(kv: KvBackend, data_dir: &Path) -> Result<Arc<dyn KvStore>> {
50 match kv {
51 KvBackend::Slatedb => build_slatedb_kv(data_dir).await,
52 KvBackend::Memory => Ok(Arc::new(MemoryKv::new())),
53 KvBackend::Cloudflare => build_cloudflare_kv(),
54 }
55}
56
57#[cfg(feature = "slatedb")]
58async fn build_slatedb_kv(data_dir: &Path) -> Result<Arc<dyn KvStore>> {
59 Ok(Arc::new(
60 boatramp_storage::SlateKv::open_local_with_flush(
61 data_dir.join("kv-slate"),
62 CONTROL_PLANE_FLUSH,
63 )
64 .await?,
65 ))
66}
67
68#[cfg(not(feature = "slatedb"))]
69async fn build_slatedb_kv(_data_dir: &Path) -> Result<Arc<dyn KvStore>> {
70 Err(crate::error::Error::NoSlatedbSupport)
71}
72
73#[cfg(feature = "cloudflare-kv")]
74fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
75 Ok(Arc::new(boatramp_storage::CloudflareKv::from_env()?))
76}
77
78#[cfg(not(feature = "cloudflare-kv"))]
79fn build_cloudflare_kv() -> Result<Arc<dyn KvStore>> {
80 Err(crate::error::Error::NoCloudflareKvSupport)
81}