Skip to content

Commit aabed46

Browse files
Li0kxxchan
authored andcommitted
feat(iceberg): introduce remove snapshot action (#21)
* feat(iceberg): basic remove snapshot * feat(iceberg): introduce new properties for remove snapshots * feat(iceberg): support remove schemas * refactor(iceberg): refactor file org * address comments * refactor(iceberg): refactor and ut * fix(iceberg): fix integration-test
1 parent 52e09d7 commit aabed46

File tree

9 files changed

+870
-1
lines changed

9 files changed

+870
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/iceberg/src/spec/snapshot.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ impl Snapshot {
175175
}
176176

177177
/// Get parent snapshot.
178-
#[cfg(test)]
179178
pub(crate) fn parent_snapshot(&self, table_metadata: &TableMetadata) -> Option<SnapshotRef> {
180179
match self.parent_snapshot_id {
181180
Some(id) => table_metadata.snapshot_by_id(id).cloned(),
@@ -401,6 +400,33 @@ impl SnapshotRetention {
401400
}
402401
}
403402

403+
/// An iterator over the ancestors of a snapshot.
404+
pub struct AncestorIterator<'a> {
405+
current: Option<SnapshotRef>,
406+
table_metadata: &'a TableMetadata,
407+
}
408+
409+
impl<'a> Iterator for AncestorIterator<'a> {
410+
type Item = SnapshotRef;
411+
412+
fn next(&mut self) -> Option<Self::Item> {
413+
let current = self.current.take()?;
414+
415+
let next = current.parent_snapshot(self.table_metadata);
416+
self.current = next;
417+
418+
Some(current)
419+
}
420+
}
421+
422+
/// Returns an iterator over the ancestors of a snapshot.
423+
pub fn ancestors_of(snapshot: SnapshotRef, table_metadata: &TableMetadata) -> AncestorIterator<'_> {
424+
AncestorIterator {
425+
current: Some(snapshot),
426+
table_metadata,
427+
}
428+
}
429+
404430
#[cfg(test)]
405431
mod tests {
406432
use std::collections::HashMap;

crates/iceberg/src/spec/table_metadata.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ pub const PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX: &str = "write.metadata.previo
7878
/// Default value for max number of previous versions to keep.
7979
pub const PROPERTY_METADATA_PREVIOUS_VERSIONS_MAX_DEFAULT: usize = 100;
8080

81+
/// Property key for max snapshot age in milliseconds.
82+
pub const MAX_SNAPSHOT_AGE_MS: &str = "history.expire.max-snapshot-age-ms";
83+
/// Default value for max snapshot age in milliseconds.
84+
pub const MAX_SNAPSHOT_AGE_MS_DEFAULT: i64 = 5 * 24 * 60 * 60 * 1000; // 5 days
85+
/// Property key for min snapshots to keep.
86+
pub const MIN_SNAPSHOTS_TO_KEEP: &str = "history.expire.min-snapshots-to-keep";
87+
/// Default value for min snapshots to keep.
88+
pub const MIN_SNAPSHOTS_TO_KEEP_DEFAULT: i32 = 1;
89+
/// Property key for max reference age in milliseconds.
90+
pub const MAX_REF_AGE_MS: &str = "history.expire.max-ref-age-ms";
91+
/// Default value for max reference age in milliseconds.
92+
pub const MAX_REF_AGE_MS_DEFAULT: i64 = i64::MAX;
93+
8194
/// Reserved Iceberg table properties list.
8295
///
8396
/// Reserved table properties are only used to control behaviors when creating or updating a

crates/iceberg/src/transaction/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! This module contains transaction api.
1919
2020
mod append;
21+
pub mod remove_snapshots;
2122
mod snapshot;
2223
mod sort_order;
2324

@@ -26,6 +27,7 @@ use std::collections::HashMap;
2627
use std::mem::discriminant;
2728
use std::sync::Arc;
2829

30+
use remove_snapshots::RemoveSnapshotAction;
2931
use uuid::Uuid;
3032

3133
use crate::error::Result;
@@ -190,6 +192,11 @@ impl<'a> Transaction<'a> {
190192
}
191193
}
192194

195+
/// Creates remove snapshot action.
196+
pub fn expire_snapshot(self) -> RemoveSnapshotAction<'a> {
197+
RemoveSnapshotAction::new(self)
198+
}
199+
193200
/// Remove properties in table.
194201
pub fn remove_properties(mut self, keys: Vec<String>) -> Result<Self> {
195202
self.apply(

0 commit comments

Comments
 (0)