Skip to content

Commit

Permalink
no regex parse snapshot (#1052)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinPostma authored Feb 20, 2024
1 parent a3648b5 commit bcfab3a
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions libsql-server/src/replication/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use anyhow::{bail, Context};
use futures::TryStreamExt;
use libsql_replication::frame::FrameMut;
use libsql_replication::snapshot::{SnapshotFile, SnapshotFileHeader};
use once_cell::sync::Lazy;
use regex::Regex;
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
use tokio::sync::mpsc;
use tokio_stream::{Stream, StreamExt};
Expand All @@ -34,30 +32,13 @@ const MAX_SNAPSHOT_NUMBER: usize = 32;

/// returns (db_id, start_frame_no, end_frame_no) for the given snapshot name
fn parse_snapshot_name(name: &str) -> Option<(Uuid, u64, u64)> {
static SNAPSHOT_FILE_MATCHER: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?x)
# match database id
(\w{8}-\w{4}-\w{4}-\w{4}-\w{12})-
# match start frame_no
(\d*)-
# match end frame_no
(\d*).snap",
)
.unwrap()
});
let Some(captures) = SNAPSHOT_FILE_MATCHER.captures(name) else {
return None;
};
let db_id = captures.get(1).unwrap();
let start_index: u64 = captures.get(2).unwrap().as_str().parse().unwrap();
let end_index: u64 = captures.get(3).unwrap().as_str().parse().unwrap();

Some((
Uuid::from_str(db_id.as_str()).unwrap(),
start_index,
end_index,
))
let (db_id_str, remaining) = name.split_at(36);
let mut split = remaining.split("-");
split.next()?;
let start_index: u64 = split.next()?.parse().ok()?;
let end_index: u64 = split.next()?.trim_end_matches(".snap").parse().ok()?;

Some((Uuid::from_str(db_id_str).ok()?, start_index, end_index))
}

fn snapshot_list(db_path: &Path) -> impl Stream<Item = anyhow::Result<String>> + '_ {
Expand Down

0 comments on commit bcfab3a

Please sign in to comment.