use std::collections::HashMap;
use std::sync::Arc;
use crate::ObjectStore;
pub type ObjectStoreManagerRef = Arc<ObjectStoreManager>;
#[derive(Debug)]
pub struct ObjectStoreManager {
stores: HashMap<String, ObjectStore>,
default_object_store: ObjectStore,
}
impl ObjectStoreManager {
pub fn new(name: &str, object_store: ObjectStore) -> Self {
ObjectStoreManager {
stores: [(name.to_lowercase(), object_store.clone())].into(),
default_object_store: object_store,
}
}
pub fn add(&mut self, name: &str, object_store: ObjectStore) {
let name = name.to_lowercase();
if self.stores.insert(name.clone(), object_store).is_some() {
panic!("Object storage provider name conflicts, the `{name}` already exists");
}
}
pub fn find(&self, name: &str) -> Option<&ObjectStore> {
self.stores.get(&name.to_lowercase())
}
pub fn default_object_store(&self) -> &ObjectStore {
&self.default_object_store
}
}
#[cfg(test)]
mod tests {
use common_test_util::temp_dir::{create_temp_dir, TempDir};
use super::ObjectStoreManager;
use crate::services::Fs as Builder;
use crate::ObjectStore;
fn new_object_store(dir: &TempDir) -> ObjectStore {
let store_dir = dir.path().to_str().unwrap();
let builder = Builder::default().root(store_dir);
ObjectStore::new(builder).unwrap().finish()
}
#[test]
fn test_manager_behavior() {
let dir = create_temp_dir("default");
let mut manager = ObjectStoreManager::new("Default", new_object_store(&dir));
assert!(manager.find("default").is_some());
assert!(manager.find("Default").is_some());
assert!(manager.find("Gcs").is_none());
assert!(manager.find("gcs").is_none());
let dir = create_temp_dir("default");
manager.add("Gcs", new_object_store(&dir));
assert!(manager.find("default").is_some());
assert!(manager.find("Gcs").is_some());
assert!(manager.find("gcs").is_some());
}
}