use std::collections::HashMap;
use std::sync::Arc;
use lance_namespace::LanceNamespace;
use lance_namespace_impls::ConnectBuilder;
use crate::error::HirnDbError;
#[derive(Debug, Clone)]
pub struct NamespaceConfig {
pub root: String,
pub properties: HashMap<String, String>,
}
impl NamespaceConfig {
pub fn local(path: impl Into<String>) -> Self {
Self {
root: path.into(),
properties: HashMap::new(),
}
}
pub fn new(root: impl Into<String>) -> Self {
Self {
root: root.into(),
properties: HashMap::new(),
}
}
pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.properties.insert(key.into(), value.into());
self
}
pub async fn connect(&self) -> Result<Arc<dyn LanceNamespace>, HirnDbError> {
let mut builder = ConnectBuilder::new("dir").property("root", &self.root);
for (k, v) in &self.properties {
builder = builder.property(k, v);
}
let ns = builder
.connect()
.await
.map_err(|e| HirnDbError::NamespaceError(e.to_string()))?;
Ok(ns)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_config_creation() {
let cfg = NamespaceConfig::local("/tmp/test");
assert_eq!(cfg.root, "/tmp/test");
assert!(cfg.properties.is_empty());
}
#[test]
fn config_with_properties() {
let cfg =
NamespaceConfig::new("s3://bucket/data").with_property("storage.region", "us-east-1");
assert_eq!(cfg.root, "s3://bucket/data");
assert_eq!(
cfg.properties.get("storage.region"),
Some(&"us-east-1".to_string())
);
}
}