|
25 | 25 | fs::File,
|
26 | 26 | io::{BufReader, Error as IoError},
|
27 | 27 | path::{Path, PathBuf},
|
| 28 | + str::FromStr as _, |
28 | 29 | sync::{
|
29 | 30 | atomic::{AtomicUsize, Ordering},
|
30 | 31 | Arc, Mutex,
|
|
36 | 37 | lazy_static! {
|
37 | 38 | static ref VERSION_FILE_REGEX: Regex = Regex::new(r"^version$").unwrap();
|
38 | 39 | 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(); |
41 | 40 | }
|
42 | 41 |
|
43 | 42 | /// Convenient wrapper for snapshot version and rebuilt storages
|
@@ -268,8 +267,7 @@ impl SnapshotStorageRebuilder {
|
268 | 267 | /// Process an append_vec_file
|
269 | 268 | fn process_append_vec_file(&self, path: PathBuf) -> Result<(), SnapshotError> {
|
270 | 269 | 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) { |
273 | 271 | if self.snapshot_from == SnapshotFrom::Dir {
|
274 | 272 | // Keep track of the highest append_vec_id in the system, so the future append_vecs
|
275 | 273 | // can be assigned to unique IDs. This is only needed when loading from a snapshot
|
@@ -305,7 +303,7 @@ impl SnapshotStorageRebuilder {
|
305 | 303 | .iter()
|
306 | 304 | .map(|path| {
|
307 | 305 | 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)?; |
309 | 307 | let current_len = *self
|
310 | 308 | .snapshot_storage_lengths
|
311 | 309 | .get(&slot)
|
@@ -439,25 +437,21 @@ fn get_snapshot_file_kind(filename: &str) -> Option<SnapshotFileKind> {
|
439 | 437 | Some(SnapshotFileKind::Version)
|
440 | 438 | } else if BANK_FIELDS_FILE_REGEX.is_match(filename) {
|
441 | 439 | Some(SnapshotFileKind::BankFields)
|
442 |
| - } else if STORAGE_FILE_REGEX.is_match(filename) { |
| 440 | + } else if get_slot_and_append_vec_id(filename).is_ok() { |
443 | 441 | Some(SnapshotFileKind::Storage)
|
444 | 442 | } else {
|
445 | 443 | None
|
446 | 444 | }
|
447 | 445 | }
|
448 | 446 |
|
449 | 447 | /// 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))) |
461 | 455 | }
|
462 | 456 |
|
463 | 457 | #[cfg(test)]
|
@@ -510,7 +504,7 @@ mod tests {
|
510 | 504 | let expected_slot = 12345;
|
511 | 505 | let expected_id = 9987;
|
512 | 506 | 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(); |
514 | 508 | assert_eq!(expected_slot, slot);
|
515 | 509 | assert_eq!(expected_id, id);
|
516 | 510 | }
|
|
0 commit comments