Skip to content

Commit

Permalink
fix: return empty path when extract function fails to extract
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Jul 10, 2024
1 parent 52a9a74 commit a1ba546
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/object-store/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,20 @@ pub fn normalize_path(path: &str) -> String {
p
}

// This logical tries to extract parent path from the object storage operation
// the function also relies on assumption that the region path is built from
// pattern `<data|index>/catalog/schema/table_id/....`
//
// this implementation tries to extract at most 3 levels of parent path
/// This logical tries to extract parent path from the object storage operation
/// the function also relies on assumption that the region path is built from
/// pattern `<data|index>/catalog/schema/table_id/....`
///
/// this implementation tries to extract at most 3 levels of parent path
/// it returns empty string if there are less than 3 levels of path, to avoid
/// invalid path extracted
pub(crate) fn extract_parent_path(path: &str) -> &str {
// split the path into `catalog`, `schema` and others
path.char_indices()
.filter(|&(_, c)| c == '/')
// we get the data/catalog/schema from path, split at the 3rd /
.nth(2)
.map_or(path, |(i, _)| &path[..i])
.map_or("", |(i, _)| &path[..i])
}

/// Attaches instrument layers to the object store.
Expand Down Expand Up @@ -208,13 +210,13 @@ mod tests {

assert_eq!(
"data/greptime/public",
extract_parent_path("data/greptime/public")
extract_parent_path("data/greptime/public/")
);

assert_eq!("data/greptime/", extract_parent_path("data/greptime/"));
assert_eq!("", extract_parent_path("data/greptime/"));

assert_eq!("data/", extract_parent_path("data/"));
assert_eq!("", extract_parent_path("data/"));

assert_eq!("/", extract_parent_path("/"));
assert_eq!("", extract_parent_path("/"));
}
}

0 comments on commit a1ba546

Please sign in to comment.