datastore: improve backup group/snapshot iters

move the check for directory before doing the OSString -> String
conversion, which should be a bit more efficient.

Also let the match return the entry in the non-skip/return case to
reduce indentation level for the inner "yield element" part, making
it slightly easier to follow.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2022-04-24 18:06:17 +02:00
parent 4b77d300a2
commit cc295e2c7a
1 changed files with 44 additions and 46 deletions

View File

@ -1219,14 +1219,17 @@ impl Iterator for ListSnapshots {
fn next(&mut self) -> Option<Self::Item> {
loop {
let item = self.fd.next()?;
match item {
let item = self.fd.next()?; // either get a entry to check or return None if exhausted
let entry = match item {
Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK
Some(nix::dir::Type::Directory) => entry, // OK
_ => continue,
}
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_DATE_REGEX.is_match(name) {
let backup_time = match proxmox_time::parse_rfc3339(&name) {
Ok(time) => time,
@ -1236,10 +1239,6 @@ impl Iterator for ListSnapshots {
return Some(BackupDir::with_group(self.group.clone(), backup_time));
}
}
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
}
}
}
@ -1274,13 +1273,16 @@ impl Iterator for ListGroups {
continue; // exhausted all IDs for the current group type, try others
}
};
match item {
let entry = match item {
Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK
Some(nix::dir::Type::Directory) => entry, // OK
_ => continue,
}
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if BACKUP_ID_REGEX.is_match(name) {
return Some(Ok(BackupGroup::new(
Arc::clone(&self.store),
@ -1288,19 +1290,19 @@ impl Iterator for ListGroups {
)));
}
}
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
} else {
let item = self.type_fd.next()?;
match item {
let entry = match item {
// filter directories
Ok(ref entry) => {
if let Ok(name) = entry.file_name().to_str() {
match entry.file_type() {
Some(nix::dir::Type::Directory) => {} // OK
Some(nix::dir::Type::Directory) => entry, // OK
_ => continue,
}
}
Err(err) => return Some(Err(err)),
};
if let Ok(name) = entry.file_name().to_str() {
if let Ok(group_type) = BackupType::from_str(name) {
// found a backup group type, descend into it to scan all IDs in it
// by switching to the id-state branch
@ -1312,10 +1314,6 @@ impl Iterator for ListGroups {
self.id_state = Some((group_type, id_dirfd));
}
}
continue; // file did not match regex or isn't valid utf-8
}
Err(err) => return Some(Err(err)),
}
}
}
}