api: tape restore: split/rework datastore/namespace map implementation

The split out helpers will (partially) be used in later patches for
call sites where we only need parts of the info assembled here.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-06-05 16:47:24 +02:00
parent 7bc2e240b1
commit 2ad96e1635
1 changed files with 22 additions and 16 deletions

View File

@ -53,12 +53,6 @@ use crate::{
tools::parallel_handler::ParallelHandler, tools::parallel_handler::ParallelHandler,
}; };
pub struct DataStoreMap {
map: HashMap<String, Arc<DataStore>>,
default: Option<Arc<DataStore>>,
ns_map: Option<NamespaceMap>,
}
struct NamespaceMap { struct NamespaceMap {
map: HashMap<String, HashMap<BackupNamespace, (BackupNamespace, usize)>>, map: HashMap<String, HashMap<BackupNamespace, (BackupNamespace, usize)>>,
} }
@ -124,6 +118,12 @@ impl NamespaceMap {
} }
} }
pub struct DataStoreMap {
map: HashMap<String, Arc<DataStore>>,
default: Option<Arc<DataStore>>,
ns_map: Option<NamespaceMap>,
}
impl TryFrom<String> for DataStoreMap { impl TryFrom<String> for DataStoreMap {
type Error = Error; type Error = Error;
@ -181,20 +181,26 @@ impl DataStoreMap {
map map
} }
fn get_targets( fn target_ns(&self, datastore: &str, ns: &BackupNamespace) -> Option<Vec<BackupNamespace>> {
&self, self.ns_map
source_ds: &str,
source_ns: &BackupNamespace,
) -> Option<(Arc<DataStore>, Option<Vec<BackupNamespace>>)> {
if let Some(store) = self.map.get(source_ds).or(self.default.as_ref()) {
let ns = self
.ns_map
.as_ref() .as_ref()
.map(|map| map.get_namespaces(source_ds, source_ns)); .map(|mapping| mapping.get_namespaces(datastore, ns))
return Some((Arc::clone(store), ns));
} }
None fn target_store(&self, source_datastore: &str) -> Option<Arc<DataStore>> {
self.map
.get(source_datastore)
.or_else(|| self.default.as_ref())
.map(|store| Arc::clone(store))
}
fn get_targets(
&self,
source_datastore: &str,
source_ns: &BackupNamespace,
) -> Option<(Arc<DataStore>, Option<Vec<BackupNamespace>>)> {
self.target_store(source_datastore)
.map(|store| (store, self.target_ns(source_datastore, source_ns)))
} }
} }