Skip to content

Commit

Permalink
storage rebuilder regex cleanup (solana-labs#29408)
Browse files Browse the repository at this point in the history
* storage rebuilder regex cleanup

* Update runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs

Co-authored-by: apfitzge <[email protected]>

Co-authored-by: apfitzge <[email protected]>
  • Loading branch information
2 people authored and gnapoli23 committed Jan 10, 2023
1 parent ccf01b0 commit ee67ef4
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)]
Expand Down

0 comments on commit ee67ef4

Please sign in to comment.