Skip to content

Commit ade9035

Browse files
authored
runtime: use str::split instead of regex to parse appendvec filenames (#323)
1 parent 4a67cd4 commit ade9035

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

runtime/src/snapshot_bank_utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2366,7 +2366,8 @@ mod tests {
23662366
fs::read_dir(path).unwrap().for_each(|entry| {
23672367
let path = entry.unwrap().path();
23682368
let filename = path.file_name().unwrap();
2369-
let (_slot, append_vec_id) = get_slot_and_append_vec_id(filename.to_str().unwrap());
2369+
let (_slot, append_vec_id) =
2370+
get_slot_and_append_vec_id(filename.to_str().unwrap()).unwrap();
23702371
max_id = std::cmp::max(max_id, append_vec_id);
23712372
});
23722373
}

runtime/src/snapshot_utils/snapshot_storage_rebuilder.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use {
2525
fs::File,
2626
io::{BufReader, Error as IoError},
2727
path::{Path, PathBuf},
28+
str::FromStr as _,
2829
sync::{
2930
atomic::{AtomicUsize, Ordering},
3031
Arc, Mutex,
@@ -36,8 +37,6 @@ use {
3637
lazy_static! {
3738
static ref VERSION_FILE_REGEX: Regex = Regex::new(r"^version$").unwrap();
3839
static ref BANK_FIELDS_FILE_REGEX: Regex = Regex::new(r"^[0-9]+(\.pre)?$").unwrap();
39-
static ref STORAGE_FILE_REGEX: Regex =
40-
Regex::new(r"^(?P<slot>[0-9]+)\.(?P<id>[0-9]+)$").unwrap();
4140
}
4241

4342
/// Convenient wrapper for snapshot version and rebuilt storages
@@ -268,8 +267,7 @@ impl SnapshotStorageRebuilder {
268267
/// Process an append_vec_file
269268
fn process_append_vec_file(&self, path: PathBuf) -> Result<(), SnapshotError> {
270269
let filename = path.file_name().unwrap().to_str().unwrap().to_owned();
271-
if let Some(SnapshotFileKind::Storage) = get_snapshot_file_kind(&filename) {
272-
let (slot, append_vec_id) = get_slot_and_append_vec_id(&filename);
270+
if let Ok((slot, append_vec_id)) = get_slot_and_append_vec_id(&filename) {
273271
if self.snapshot_from == SnapshotFrom::Dir {
274272
// Keep track of the highest append_vec_id in the system, so the future append_vecs
275273
// can be assigned to unique IDs. This is only needed when loading from a snapshot
@@ -305,7 +303,7 @@ impl SnapshotStorageRebuilder {
305303
.iter()
306304
.map(|path| {
307305
let filename = path.file_name().unwrap().to_str().unwrap();
308-
let (_, old_append_vec_id) = get_slot_and_append_vec_id(filename);
306+
let (_, old_append_vec_id) = get_slot_and_append_vec_id(filename)?;
309307
let current_len = *self
310308
.snapshot_storage_lengths
311309
.get(&slot)
@@ -439,25 +437,21 @@ fn get_snapshot_file_kind(filename: &str) -> Option<SnapshotFileKind> {
439437
Some(SnapshotFileKind::Version)
440438
} else if BANK_FIELDS_FILE_REGEX.is_match(filename) {
441439
Some(SnapshotFileKind::BankFields)
442-
} else if STORAGE_FILE_REGEX.is_match(filename) {
440+
} else if get_slot_and_append_vec_id(filename).is_ok() {
443441
Some(SnapshotFileKind::Storage)
444442
} else {
445443
None
446444
}
447445
}
448446

449447
/// Get the slot and append vec id from the filename
450-
pub(crate) fn get_slot_and_append_vec_id(filename: &str) -> (Slot, usize) {
451-
STORAGE_FILE_REGEX
452-
.captures(filename)
453-
.map(|cap| {
454-
let slot_str = cap.name("slot").map(|m| m.as_str()).unwrap();
455-
let id_str = cap.name("id").map(|m| m.as_str()).unwrap();
456-
let slot = slot_str.parse().unwrap();
457-
let id = id_str.parse().unwrap();
458-
(slot, id)
459-
})
460-
.unwrap()
448+
pub(crate) fn get_slot_and_append_vec_id(filename: &str) -> Result<(Slot, usize), SnapshotError> {
449+
let mut parts = filename.splitn(2, '.');
450+
let slot = parts.next().and_then(|s| Slot::from_str(s).ok());
451+
let id = parts.next().and_then(|s| usize::from_str(s).ok());
452+
453+
slot.zip(id)
454+
.ok_or_else(|| SnapshotError::InvalidAppendVecPath(PathBuf::from(filename)))
461455
}
462456

463457
#[cfg(test)]
@@ -510,7 +504,7 @@ mod tests {
510504
let expected_slot = 12345;
511505
let expected_id = 9987;
512506
let (slot, id) =
513-
get_slot_and_append_vec_id(&AppendVec::file_name(expected_slot, expected_id));
507+
get_slot_and_append_vec_id(&AppendVec::file_name(expected_slot, expected_id)).unwrap();
514508
assert_eq!(expected_slot, slot);
515509
assert_eq!(expected_id, id);
516510
}

0 commit comments

Comments
 (0)