Skip to content

Commit 5d32171

Browse files
committed
Add with_length
1 parent 31faaee commit 5d32171

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

crates/iceberg/src/encryption/key_metadata.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ static AVRO_SCHEMA_V1: LazyLock<AvroSchema> = LazyLock::new(|| {
7272
pub struct StandardKeyMetadata {
7373
encryption_key: SensitiveBytes,
7474
aad_prefix: Box<[u8]>,
75-
file_length: Option<i64>,
75+
file_length: Option<u64>,
7676
}
7777

7878
impl fmt::Debug for StandardKeyMetadata {
@@ -95,6 +95,12 @@ impl StandardKeyMetadata {
9595
}
9696
}
9797

98+
/// Adds a file length
99+
pub fn with_file_length(mut self, length: u64) -> Self {
100+
self.file_length = Some(length);
101+
self
102+
}
103+
98104
/// Returns the plaintext Data Encryption Key.
99105
pub fn encryption_key(&self) -> &[u8] {
100106
self.encryption_key.as_bytes()
@@ -106,7 +112,7 @@ impl StandardKeyMetadata {
106112
}
107113

108114
/// Returns the optional file length.
109-
pub fn file_length(&self) -> Option<i64> {
115+
pub fn file_length(&self) -> Option<u64> {
110116
self.file_length
111117
}
112118

@@ -181,7 +187,7 @@ impl StandardKeyMetadata {
181187
struct StandardKeyMetadataV1 {
182188
encryption_key: serde_bytes::ByteBuf,
183189
aad_prefix: Option<serde_bytes::ByteBuf>,
184-
file_length: Option<i64>,
190+
file_length: Option<u64>,
185191
}
186192

187193
#[cfg(test)]
@@ -202,6 +208,21 @@ mod tests {
202208
assert_eq!(parsed.file_length(), None);
203209
}
204210

211+
#[test]
212+
fn test_roundtrip_with_length() {
213+
let key = b"0123456789012345";
214+
let aad = b"1234567890123456";
215+
216+
let file_length = 100_000;
217+
let metadata = StandardKeyMetadata::new(key, aad).with_file_length(file_length);
218+
let serialized = metadata.serialize().unwrap();
219+
let parsed = StandardKeyMetadata::deserialize(&serialized).unwrap();
220+
221+
assert_eq!(parsed.encryption_key(), key);
222+
assert_eq!(parsed.aad_prefix(), aad);
223+
assert_eq!(parsed.file_length(), Some(file_length));
224+
}
225+
205226
#[test]
206227
fn test_unsupported_version() {
207228
let result = StandardKeyMetadata::deserialize(&[0x02]);

0 commit comments

Comments
 (0)