Skip to content

Commit

Permalink
simplify path parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wjones127 committed Feb 11, 2023
1 parent 4df29af commit 6b566ce
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions aws/delta-checkpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use log::*;
use regex::Regex;
use serde_json::Value;
use std::num::ParseIntError;
use std::path::PathBuf;

#[tokio::main]
async fn main() -> Result<(), Error> {
Expand All @@ -31,7 +30,7 @@ async fn main() -> Result<(), Error> {
async fn process_event(event: LambdaEvent<Value>) -> Result<(), CheckPointLambdaError> {
let (bucket, key) = bucket_and_key_from_event(&event.payload)?;
let (path, version) = table_path_and_version_from_key(key.as_str())?;
let table_uri = table_uri_from_parts(bucket.as_str(), path.as_str())?;
let table_uri = table_uri_from_parts(bucket.as_str(), path.as_str());

// Checkpoints are created for every 10th delta log commit.
// Follows the reference implementation described in the delta protocol doc.
Expand Down Expand Up @@ -106,23 +105,13 @@ fn table_path_and_version_from_key(
}
}

fn table_uri_from_parts(bucket: &str, path: &str) -> Result<String, CheckPointLambdaError> {
let mut table_uri = PathBuf::new();

table_uri.push(format!("s3://{bucket}"));
table_uri.push(path);

Ok(table_uri
.to_str()
.ok_or_else(|| CheckPointLambdaError::InvalidTableUri(table_uri.clone()))?
.to_string())
fn table_uri_from_parts(bucket: &str, path: &str) -> String {
let path = path.trim_start_matches('/');
format!("s3://{bucket}/{path}")
}

#[derive(thiserror::Error, Debug)]
enum CheckPointLambdaError {
#[error("Invalid table uri: {0}")]
InvalidTableUri(PathBuf),

#[error("Invalid event structure: {0}")]
InvalidEventStructure(String),

Expand Down Expand Up @@ -190,7 +179,7 @@ mod tests {

#[test]
fn checkpoint_table_uri_from_parts_test() {
let table_uri = table_uri_from_parts("my_bucket", "database_name/table_name").unwrap();
let table_uri = table_uri_from_parts("my_bucket", "database_name/table_name");

assert_eq!(
"s3://my_bucket/database_name/table_name",
Expand Down

0 comments on commit 6b566ce

Please sign in to comment.