Skip to content

Commit 58cb6e2

Browse files
committed
Add with_length
1 parent 31faaee commit 58cb6e2

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

crates/iceberg/src/encryption/key_metadata.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use std::fmt;
2222
use std::io::Cursor;
2323
use std::sync::LazyLock;
24-
2524
use apache_avro::{Schema as AvroSchema, from_avro_datum, from_value, to_avro_datum, to_value};
2625
use serde::{Deserialize, Serialize};
2726

@@ -72,7 +71,7 @@ static AVRO_SCHEMA_V1: LazyLock<AvroSchema> = LazyLock::new(|| {
7271
pub struct StandardKeyMetadata {
7372
encryption_key: SensitiveBytes,
7473
aad_prefix: Box<[u8]>,
75-
file_length: Option<i64>,
74+
file_length: Option<u64>,
7675
}
7776

7877
impl fmt::Debug for StandardKeyMetadata {
@@ -95,6 +94,12 @@ impl StandardKeyMetadata {
9594
}
9695
}
9796

97+
/// Adds a file length
98+
pub fn with_file_length(mut self, length: u64) -> Self {
99+
self.file_length = Some(length);
100+
self
101+
}
102+
98103
/// Returns the plaintext Data Encryption Key.
99104
pub fn encryption_key(&self) -> &[u8] {
100105
self.encryption_key.as_bytes()
@@ -106,7 +111,7 @@ impl StandardKeyMetadata {
106111
}
107112

108113
/// Returns the optional file length.
109-
pub fn file_length(&self) -> Option<i64> {
114+
pub fn file_length(&self) -> Option<u64> {
110115
self.file_length
111116
}
112117

@@ -181,7 +186,7 @@ impl StandardKeyMetadata {
181186
struct StandardKeyMetadataV1 {
182187
encryption_key: serde_bytes::ByteBuf,
183188
aad_prefix: Option<serde_bytes::ByteBuf>,
184-
file_length: Option<i64>,
189+
file_length: Option<u64>,
185190
}
186191

187192
#[cfg(test)]
@@ -202,6 +207,22 @@ mod tests {
202207
assert_eq!(parsed.file_length(), None);
203208
}
204209

210+
#[test]
211+
fn test_roundtrip_with_length() {
212+
let key = b"0123456789012345";
213+
let aad = b"1234567890123456";
214+
215+
let file_length = 100_000;
216+
let metadata = StandardKeyMetadata::new(key, aad)
217+
.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)