zeroshade commented on code in PR #401: URL: https://github.com/apache/iceberg-go/pull/401#discussion_r2076149034
########## table/transaction.go: ########## @@ -142,6 +143,120 @@ func (t *Transaction) SetProperties(props iceberg.Properties) error { return nil } +type expireSnapshotsCfg struct { + minSnapshotsToKeep *int + maxSnapshotAgeMs *int64 +} + +type ExpireSnapshotsOpt func(*expireSnapshotsCfg) + +func WithRetainLast(n int) ExpireSnapshotsOpt { + return func(cfg *expireSnapshotsCfg) { + cfg.minSnapshotsToKeep = &n + } +} + +func WithOlderThan(t time.Duration) ExpireSnapshotsOpt { + return func(cfg *expireSnapshotsCfg) { + n := t.Milliseconds() + cfg.maxSnapshotAgeMs = &n + } +} + +func (t *Transaction) ExpireSnapshots(opts ...ExpireSnapshotsOpt) error { + var ( + cfg expireSnapshotsCfg + updates []Update + snapsToKeep = make(map[int64]struct{}) + refsToDelete = make(map[string]struct{}) + ) + + for _, opt := range opts { + opt(&cfg) + } + + for refName, ref := range t.meta.refs { + if refName == MainBranch { + continue + } Review Comment: we can't remove snapshots from the main branch? ########## table/metadata.go: ########## @@ -335,6 +335,36 @@ func (b *MetadataBuilder) AddSnapshot(snapshot *Snapshot) (*MetadataBuilder, err return b, nil } +func (b *MetadataBuilder) RemoveSnapshots(snapshotIds []int64) (*MetadataBuilder, error) { + var snapshotsToKeep []Snapshot + + for _, snapshot := range b.snapshotList { + if slices.Contains(snapshotIds, snapshot.SnapshotID) { + if snapshot.SnapshotID == *b.currentSnapshotID { + return nil, errors.New("current snapshot cannot be removed") + } + + continue + } + + snapshotsToKeep = append(snapshotsToKeep, snapshot) + } + + var prunedSnapshotLog []SnapshotLogEntry + + for _, entry := range b.snapshotLog { + if !slices.Contains(snapshotIds, entry.SnapshotID) { + prunedSnapshotLog = append(prunedSnapshotLog, entry) + } + } Review Comment: Maybe we can simplify this a bit? ```go if slices.Contains(snapshotIds, *b.currentSnapshotID) { return nil, errors.New("current snapshot cannot be removed") } b.snapshotList = slices.DeleteFunc(b.snapshotList, func(e Snapshot) bool { return slices.Contains(snapshotIds, e.SnapshotID) }) b.snapshotLog = slices.DeleteFunc(b.snapshotLog, func(e SnapshotLogEntry) bool { return slices.Contains(snapshotIds, e.SnapshotID) }) b.updates = append(b.updates, NewRemoveSnapshotsUpdate(snapshotIds)) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org