|
| 1 | +//! Public-facing [`LogPath`] type for representing paths to delta log files. |
| 2 | +
|
| 3 | +use crate::path::ParsedLogPath; |
| 4 | +use crate::utils::require; |
| 5 | +use crate::{DeltaResult, Error, FileMeta, FileSize}; |
| 6 | + |
| 7 | +use url::Url; |
| 8 | + |
| 9 | +/// A path to a valid delta log file. You can parse a given `FileMeta` into a `LogPath` using |
| 10 | +/// [`LogPath::try_new`]. |
| 11 | +/// |
| 12 | +/// Today, a `LogPath` is a file in the `_delta_log` directory of a Delta table; in the future, |
| 13 | +/// this will expand to support providing inline data in the log path itself. |
| 14 | +#[derive(Debug, Clone, PartialEq)] |
| 15 | +pub struct LogPath(ParsedLogPath); |
| 16 | + |
| 17 | +impl From<LogPath> for ParsedLogPath { |
| 18 | + fn from(p: LogPath) -> Self { |
| 19 | + p.0 |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl LogPath { |
| 24 | + /// Attempt to create a `LogPath` from `FileMeta`. This returns an error if the path isn't a |
| 25 | + /// valid log path. |
| 26 | + pub fn try_new(file_meta: FileMeta) -> DeltaResult<Self> { |
| 27 | + // TODO: we should avoid the clone |
| 28 | + let parsed = ParsedLogPath::try_from(file_meta.clone())? |
| 29 | + .ok_or_else(|| Error::invalid_log_path(&file_meta.location))?; |
| 30 | + |
| 31 | + require!( |
| 32 | + !parsed.is_unknown(), |
| 33 | + Error::invalid_log_path(&file_meta.location) |
| 34 | + ); |
| 35 | + |
| 36 | + Ok(Self(parsed)) |
| 37 | + } |
| 38 | + |
| 39 | + /// Create a new staged commit log path given the table root and filename and metadata. |
| 40 | + pub fn staged_commit( |
| 41 | + table_root: Url, |
| 42 | + filename: &str, |
| 43 | + last_modified: i64, |
| 44 | + size: FileSize, |
| 45 | + ) -> DeltaResult<LogPath> { |
| 46 | + // TODO: we should introduce TablePath/LogPath types which enforce checks like ending '/' |
| 47 | + |
| 48 | + // require table_root ends with '/' |
| 49 | + require!( |
| 50 | + table_root.path().ends_with('/'), |
| 51 | + Error::generic("table root must be a directory-like URL ending with '/'") |
| 52 | + ); |
| 53 | + let location = table_root |
| 54 | + .join("_delta_log/")? |
| 55 | + .join("_staged_commits/")? |
| 56 | + .join(filename)?; |
| 57 | + let file_meta = FileMeta { |
| 58 | + location, |
| 59 | + last_modified, |
| 60 | + size, |
| 61 | + }; |
| 62 | + LogPath::try_new(file_meta) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(test)] |
| 67 | +mod test { |
| 68 | + use super::*; |
| 69 | + |
| 70 | + use std::str::FromStr; |
| 71 | + |
| 72 | + #[test] |
| 73 | + fn test_staged_commit_path_creation() { |
| 74 | + let table_root = Url::from_str("s3://my-bucket/my-table/").unwrap(); |
| 75 | + let filename = "00000000000000000010.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.json"; |
| 76 | + let last_modified = 1234567890i64; |
| 77 | + let size = 1024u64; |
| 78 | + |
| 79 | + let log_path = LogPath::staged_commit(table_root.clone(), filename, last_modified, size) |
| 80 | + .expect("Failed to create staged commit log path"); |
| 81 | + |
| 82 | + let expected_path = |
| 83 | + Url::from_str("s3://my-bucket/my-table/_delta_log/_staged_commits/00000000000000000010.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.json") |
| 84 | + .unwrap(); |
| 85 | + let expected = FileMeta { |
| 86 | + location: expected_path, |
| 87 | + last_modified, |
| 88 | + size, |
| 89 | + }; |
| 90 | + |
| 91 | + let path = log_path.0; |
| 92 | + assert_eq!(path.location, expected); |
| 93 | + } |
| 94 | + |
| 95 | + #[test] |
| 96 | + fn test_staged_commit_path_creation_failures() { |
| 97 | + let last_modified = 1234567890i64; |
| 98 | + let size = 1024u64; |
| 99 | + |
| 100 | + // table root not ending with '/' |
| 101 | + let table_root = Url::from_str("s3://my-bucket/my-table").unwrap(); |
| 102 | + let filename = "00000000000000000010.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.json"; |
| 103 | + LogPath::staged_commit(table_root.clone(), filename, last_modified, size).unwrap_err(); |
| 104 | + |
| 105 | + // filename with path separators |
| 106 | + let table_root = Url::from_str("s3://my-bucket/my-table/").unwrap(); |
| 107 | + let filename = "subdir/00000000000000000010.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.json"; |
| 108 | + LogPath::staged_commit(table_root.clone(), filename, last_modified, size).unwrap_err(); |
| 109 | + |
| 110 | + // incorrect filenames |
| 111 | + let table_root = Url::from_str("s3://my-bucket/my-table/").unwrap(); |
| 112 | + let filename = "00000000000000000010.not-a-uuid.json"; |
| 113 | + LogPath::staged_commit(table_root.clone(), filename, last_modified, size).unwrap_err(); |
| 114 | + let filename = "000000000000000000aa.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.json"; |
| 115 | + LogPath::staged_commit(table_root.clone(), filename, last_modified, size).unwrap_err(); |
| 116 | + let filename = "00000000000000000010.3a0d65cd-4a56-49a8-937b-95f9e3ee90e5.parquet"; |
| 117 | + LogPath::staged_commit(table_root.clone(), filename, last_modified, size).unwrap_err(); |
| 118 | + } |
| 119 | +} |
0 commit comments