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


##########
table/metadata.go:
##########
@@ -1444,31 +1444,42 @@ func (c *commonMetadata) checkRefsExist() error {
 }
 
 func (c *commonMetadata) validateChronologicalSnapshotLogs() error {
-       for i := 1; i < len(c.SnapshotLog); i++ {
-               prev, cur := c.SnapshotLog[i-1], c.SnapshotLog[i]
-               if (cur.TimestampMs - prev.TimestampMs) < -oneMinuteInMs {
-                       return fmt.Errorf("%w: expected sorted snapshot log 
entries", ErrInvalidMetadata)
-               }
-               if i == len(c.SnapshotLog)-1 {
-                       if c.LastUpdatedMS-cur.TimestampMs < -oneMinuteInMs {
-                               return fmt.Errorf("%w: invalid update timestamp 
%d: before last snapshot log entry at %d", ErrInvalidMetadata, c.LastUpdatedMS, 
cur.TimestampMs)
-                       }
+       if !slices.IsSortedFunc(c.SnapshotLog, func(cur, prev SnapshotLogEntry) 
int {
+               diff := cur.TimestampMs - prev.TimestampMs
+               if diff > -oneMinuteInMs {
+                       return 1
+               }
+
+               return -1
+       }) {
+               return fmt.Errorf("%w: expected sorted snapshot log entries", 
ErrInvalidMetadata)
+       }
+       if len(c.SnapshotLog) > 0 {
+               last := c.SnapshotLog[len(c.SnapshotLog)-1].TimestampMs
+               if c.LastUpdatedMS-last < -oneMinuteInMs {
+                       return fmt.Errorf("%w: invalid update timestamp %d: 
before last snapshot log entry at %d", ErrInvalidMetadata, c.LastUpdatedMS, 
last)
                }
        }
 
        return nil
 }
 
 func (c *commonMetadata) validateChronologicalMetadataLogs() error {
-       for i := 1; i < len(c.MetadataLog); i++ {
-               prev, cur := c.MetadataLog[i-1], c.MetadataLog[i]
-               if (cur.TimestampMs - prev.TimestampMs) < -oneMinuteInMs {
-                       return fmt.Errorf("%w: expected sorted metadata log 
entries", ErrInvalidMetadata)
-               }
-               if i == len(c.MetadataLog)-1 {
-                       if c.LastUpdatedMS-cur.TimestampMs < -oneMinuteInMs {
-                               return fmt.Errorf("%w: invalid update timestamp 
%d: before last metadata log entry at %d", ErrInvalidMetadata, c.LastUpdatedMS, 
cur.TimestampMs)
-                       }
+       if !slices.IsSortedFunc(c.MetadataLog, func(cur, prev MetadataLogEntry) 
int {
+               diff := cur.TimestampMs - prev.TimestampMs
+               if diff > -oneMinuteInMs {
+                       return 1
+               }
+
+               return -1

Review Comment:
   `return cmp.Compare(cur.TimestmapMs - prev.TimestampMs, -oneMinuteInMs)` ?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to