zeroshade commented on code in PR #401:
URL: https://github.com/apache/iceberg-go/pull/401#discussion_r2070861384


##########
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
+               }
+
+               snap, err := t.meta.SnapshotByID(ref.SnapshotID)
+               if err != nil {
+                       return err
+               }
+
+               maxRefAgeMs := internal.Coalesce(ref.MaxRefAgeMs, 
cfg.maxSnapshotAgeMs)
+               if maxRefAgeMs == nil {
+                       return errors.New("cannot find a valid value for 
maxRefAgeMs")
+               }
+
+               refAge := time.Now().UnixMilli() - snap.TimestampMs
+               if refAge > *maxRefAgeMs {
+                       updates = append(updates, 
NewRemoveSnapshotRefUpdate(refName))
+                       refsToDelete[refName] = struct{}{}
+               }
+       }
+
+       for refName, ref := range t.meta.refs {

Review Comment:
   can we combine these loops and just put the continue after 
`refsToDelete[refName] = struct{}{}`?



-- 
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

Reply via email to