storage rebuilder regex cleanup (#29408)

* storage rebuilder regex cleanup

* Update runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs

Co-authored-by: apfitzge <apfitzge@gmail.com>

Co-authored-by: apfitzge <apfitzge@gmail.com>
This commit is contained in:
Xiang Zhu 2023-01-05 16:16:24 -08:00 committed by GitHub
parent a6b492b2c1
commit a0f045d2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 13 deletions

View File

@ -31,6 +31,14 @@ use {
time::Instant,
},
};
lazy_static! {
static ref VERSION_FILE_REGEX: Regex = Regex::new(r"^version$").unwrap();
static ref BANK_FIELDS_FILE_REGEX: Regex = Regex::new(r"^[0-9]+(\.pre)?$").unwrap();
static ref STORAGE_FILE_REGEX: Regex =
Regex::new(r"^(?P<slot>[0-9]+)\.(?P<id>[0-9]+)$").unwrap();
}
/// Convenient wrapper for snapshot version and rebuilt storages
pub(crate) struct RebuiltSnapshotStorage {
/// Snapshot version
@ -367,12 +375,6 @@ enum SnapshotFileKind {
/// Determines `SnapshotFileKind` for `filename` if any
fn get_snapshot_file_kind(filename: &str) -> Option<SnapshotFileKind> {
lazy_static! {
static ref VERSION_FILE_REGEX: Regex = Regex::new(r"^version$").unwrap();
static ref BANK_FIELDS_FILE_REGEX: Regex = Regex::new(r"^[0-9]+$").unwrap();
static ref STORAGE_FILE_REGEX: Regex = Regex::new(r"^[0-9]+\.[0-9]+$").unwrap();
};
if VERSION_FILE_REGEX.is_match(filename) {
Some(SnapshotFileKind::Version)
} else if BANK_FIELDS_FILE_REGEX.is_match(filename) {
@ -385,13 +387,17 @@ fn get_snapshot_file_kind(filename: &str) -> Option<SnapshotFileKind> {
}
/// Get the slot and append vec id from the filename
fn get_slot_and_append_vec_id(filename: &str) -> (Slot, usize) {
let mut split = filename.split('.');
let slot = split.next().unwrap().parse().unwrap();
let append_vec_id = split.next().unwrap().parse().unwrap();
assert!(split.next().is_none());
(slot, append_vec_id)
pub(crate) fn get_slot_and_append_vec_id(filename: &str) -> (Slot, usize) {
STORAGE_FILE_REGEX
.captures(filename)
.map(|cap| {
let slot_str = cap.name("slot").map(|m| m.as_str()).unwrap();
let id_str = cap.name("id").map(|m| m.as_str()).unwrap();
let slot = slot_str.parse().unwrap();
let id = id_str.parse().unwrap();
(slot, id)
})
.unwrap()
}
#[cfg(test)]