Skip to content

Commit

Permalink
Use regex in get_snapshot_file_kind (#27728)
Browse files Browse the repository at this point in the history
refactor get_snapshot_file_kind to use regex
  • Loading branch information
apfitzge authored Sep 22, 2022
1 parent 1ee595c commit 64f64de
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use {
iter::{IntoParallelIterator, ParallelIterator},
ThreadPool, ThreadPoolBuilder,
},
regex::Regex,
solana_sdk::clock::Slot,
std::{
collections::HashMap,
Expand Down Expand Up @@ -348,32 +349,20 @@ enum SnapshotFileKind {

/// Determines `SnapshotFileKind` for `filename` if any
fn get_snapshot_file_kind(filename: &str) -> Option<SnapshotFileKind> {
if filename == "version" {
return Some(SnapshotFileKind::Version);
}

let mut periods = 0;
let mut saw_numbers = false;
for x in filename.chars() {
if !x.is_ascii_digit() {
if x == '.' {
if periods > 0 || !saw_numbers {
return None;
}
saw_numbers = false;
periods += 1;
} else {
return None;
}
} else {
saw_numbers = true;
}
}

match (periods, saw_numbers) {
(0, true) => Some(SnapshotFileKind::BankFields),
(1, true) => Some(SnapshotFileKind::Storage),
(_, _) => None,
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) {
Some(SnapshotFileKind::BankFields)
} else if STORAGE_FILE_REGEX.is_match(filename) {
Some(SnapshotFileKind::Storage)
} else {
None
}
}

Expand Down

0 comments on commit 64f64de

Please sign in to comment.