twuebi commented on code in PR #585:
URL: https://github.com/apache/iceberg-go/pull/585#discussion_r2413735972


##########
table/metadata.go:
##########
@@ -1670,6 +1711,106 @@ func (m *metadataV2) checkLastSequenceNumber() error {
        return nil
 }
 
+// metadataV3 represents table metadata for format version 3
+type metadataV3 struct {
+       LastSeqNum     int64 `json:"last-sequence-number"`
+       NextRowIDValue int64 `json:"next-row-id"`
+
+       commonMetadata
+}
+
+func initMetadataV3Deser() *metadataV3 {
+       return &metadataV3{
+               LastSeqNum:     -1,
+               NextRowIDValue: -1,
+               commonMetadata: initCommonMetadataForDeserialization(),
+       }
+}
+
+func (m *metadataV3) LastSequenceNumber() int64 { return m.LastSeqNum }
+func (m *metadataV3) NextRowID() int64 {
+       if m.NextRowIDValue == -1 {
+               return 0 // RFor v1/v2 compatibility when field is missing
+       }
+       return m.NextRowIDValue
+}
+
+func (m *metadataV3) Equals(other Metadata) bool {
+       rhs, ok := other.(*metadataV3)
+       if !ok {
+               return false
+       }
+
+       if m == rhs {
+               return true
+       }
+
+       return m.LastSeqNum == rhs.LastSeqNum &&
+               m.NextRowIDValue == rhs.NextRowIDValue &&
+               m.commonMetadata.Equals(&rhs.commonMetadata)
+}
+
+func (m *metadataV3) UnmarshalJSON(b []byte) error {
+       type Alias metadataV3
+       aux := (*Alias)(m)
+
+       // Set LastColumnId to -1 to indicate that it is not set as 
LastColumnId = 0 is a valid value for when no schema is present
+       aux.LastColumnId = -1
+
+       if err := json.Unmarshal(b, aux); err != nil {
+               return err
+       }
+
+       m.preValidate()
+
+       return m.validate()
+}
+
+func (m *metadataV3) validate() error {
+       if err := m.checkLastSequenceNumber(); err != nil {
+               return err
+       }
+
+       if err := m.checkNextRowID(); err != nil {
+               return err
+       }
+
+       if err := m.commonMetadata.validate(); err != nil {
+               return err
+       }
+
+       return nil
+}
+
+func (m *metadataV3) checkLastSequenceNumber() error {
+       for _, snap := range m.SnapshotList {
+               if snap.SequenceNumber > m.LastSequenceNumber() {
+                       return fmt.Errorf("%w: snapshot %d has sequence number 
%d which is greater than last-sequence-number %d",
+                               ErrInvalidMetadata, snap.SnapshotID, 
snap.SequenceNumber, m.LastSequenceNumber())
+               }
+       }
+
+       return nil
+}

Review Comment:
   Is there a way to not have this function twice? maybe by defining another 
interface which defines a func LastSequenceNumber() and implements this which 
would be part of both metadata v2 & v3?



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