From df729017b461a394b4bba055764f7d0a2fe4fe6f Mon Sep 17 00:00:00 2001 From: Stefan Reiter Date: Mon, 19 Oct 2020 16:45:22 +0200 Subject: [PATCH] datastore: cleanup open and load config only once Force consumers to use the lookup_datastore method instead of potentially opening a datastore twice, and pass the config we have already loaded into open_with_path, removing the need for open(1). Signed-off-by: Stefan Reiter --- src/backup/datastore.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index 46039576..a856e2f4 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -46,17 +46,18 @@ impl DataStore { let (config, _digest) = datastore::config()?; let config: datastore::DataStoreConfig = config.lookup("datastore", name)?; + let path = PathBuf::from(&config.path); let mut map = DATASTORE_MAP.lock().unwrap(); if let Some(datastore) = map.get(name) { // Compare Config - if changed, create new Datastore object! - if datastore.chunk_store.base == PathBuf::from(&config.path) { + if datastore.chunk_store.base == path { return Ok(datastore.clone()); } } - let datastore = DataStore::open(name)?; + let datastore = DataStore::open_with_path(name, &path, config)?; let datastore = Arc::new(datastore); map.insert(name.to_string(), datastore.clone()); @@ -64,18 +65,7 @@ impl DataStore { Ok(datastore) } - pub fn open(store_name: &str) -> Result { - - let (config, _digest) = datastore::config()?; - let (_, store_config) = config.sections.get(store_name) - .ok_or(format_err!("no such datastore '{}'", store_name))?; - - let path = store_config["path"].as_str().unwrap(); - - Self::open_with_path(store_name, Path::new(path)) - } - - pub fn open_with_path(store_name: &str, path: &Path) -> Result { + fn open_with_path(store_name: &str, path: &Path, config: DataStoreConfig) -> Result { let chunk_store = ChunkStore::open(store_name, path)?; let gc_status = GarbageCollectionStatus::default();