use std::{collections::HashMap, path::PathBuf};
use crate::supertable::{Consistency, reader_cache::ColdFetchMode as InternalColdFetchMode};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColdFetchMode {
HybridWithPrefetch,
RangeOnly,
#[default]
LazyForegroundWithBackgroundFill,
}
impl ColdFetchMode {
pub(crate) fn to_internal(self) -> InternalColdFetchMode {
match self {
ColdFetchMode::HybridWithPrefetch => InternalColdFetchMode::HybridWithPrefetch,
ColdFetchMode::RangeOnly => InternalColdFetchMode::RangeOnly,
ColdFetchMode::LazyForegroundWithBackgroundFill => {
InternalColdFetchMode::LazyForegroundWithBackgroundFill
}
}
}
}
#[derive(Debug, Clone, Default)]
pub struct ConnectOptions {
pub(crate) storage_options: HashMap<String, String>,
pub(crate) cache_dir: Option<PathBuf>,
pub(crate) cache_budget_bytes: Option<u64>,
pub(crate) cold_fetch_mode: ColdFetchMode,
pub(crate) connection_memory_budget_bytes: Option<u64>,
pub(crate) read_consistency: Consistency,
pub(crate) validate: bool,
pub(crate) api_key: Option<String>,
}
impl ConnectOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_cache_dir(mut self, dir: impl Into<PathBuf>) -> Self {
self.cache_dir = Some(dir.into());
self
}
pub fn with_cache_budget_bytes(mut self, bytes: u64) -> Self {
self.cache_budget_bytes = Some(bytes);
self
}
pub fn with_cold_fetch_mode(mut self, mode: ColdFetchMode) -> Self {
self.cold_fetch_mode = mode;
self
}
pub fn with_connection_memory_budget_bytes(mut self, bytes: u64) -> Self {
self.connection_memory_budget_bytes = Some(bytes);
self
}
pub fn with_storage_option(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.storage_options.insert(key.into(), value.into());
self
}
pub fn with_read_consistency(mut self, consistency: Consistency) -> Self {
self.read_consistency = consistency;
self
}
pub fn with_validate(mut self, validate: bool) -> Self {
self.validate = validate;
self
}
pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
self.api_key = Some(key.into());
self
}
#[cfg_attr(not(feature = "remote"), allow(dead_code))]
pub(crate) fn api_key(&self) -> Option<&str> {
self.api_key.as_deref()
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn read_consistency_defaults_to_bounded_staleness() {
assert_eq!(
ConnectOptions::new().read_consistency,
Consistency::BoundedStaleness(Duration::from_secs(1)),
"unset read consistency defaults to the engine's BoundedStaleness(1s)"
);
}
#[test]
fn with_read_consistency_overrides_the_default() {
let opts = ConnectOptions::new().with_read_consistency(Consistency::Strong);
assert_eq!(opts.read_consistency, Consistency::Strong);
}
#[test]
fn with_storage_option_round_trips() {
let o = ConnectOptions::new().with_storage_option("aws_region", "us-east-1");
assert_eq!(
o.storage_options.get("aws_region").map(String::as_str),
Some("us-east-1")
);
}
#[test]
fn with_api_key_round_trips() {
let o = ConnectOptions::new().with_api_key("ik_test");
assert_eq!(o.api_key(), Some("ik_test"));
assert_eq!(ConnectOptions::new().api_key(), None);
}
}