diff --git a/runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs b/runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs index 35701121922471..3d87b56d51d402 100644 --- a/runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs +++ b/runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs @@ -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[0-9]+)\.(?P[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 { - 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 { } /// 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)]