jwtryg commented on code in PR #146: URL: https://github.com/apache/iceberg-go/pull/146#discussion_r1754951127
########## table/metadata.go: ########## @@ -80,20 +86,512 @@ type Metadata interface { SnapshotByName(name string) *Snapshot // CurrentSnapshot returns the table's current snapshot. CurrentSnapshot() *Snapshot + // Ref returns the snapshot ref for the main branch. + Ref() SnapshotRef + // Refs returns a map of snapshot refs by name. + Refs() map[string]SnapshotRef + // SnapshotLogs returns the list of snapshot logs for the table. + SnapshotLogs() []SnapshotLogEntry // SortOrder returns the table's current sort order, ie: the one with the // ID that matches the default-sort-order-id. SortOrder() SortOrder // SortOrders returns the list of sort orders in the table. SortOrders() []SortOrder + // DefaultSortOrder returns the ID of the current sort order that writers + // should use by default. + DefaultSortOrder() int // Properties is a string to string map of table properties. This is used // to control settings that affect reading and writing and is not intended // to be used for arbitrary metadata. For example, commit.retry.num-retries // is used to control the number of commit retries. Properties() iceberg.Properties + // PreviousFiles returns the list of metadata log entries for the table. + PreviousFiles() []MetadataLogEntry Equals(Metadata) bool } +type MetadataBuilder struct { + base Metadata + updates []Update + + // common fields + formatVersion int + uuid uuid.UUID + loc string + lastUpdatedMS int64 + lastColumnId int + schemaList []*iceberg.Schema + currentSchemaID int + specs []iceberg.PartitionSpec + defaultSpecID int + lastPartitionID *int + props iceberg.Properties + snapshotList []Snapshot + currentSnapshotID *int64 + snapshotLog []SnapshotLogEntry + metadataLog []MetadataLogEntry + sortOrderList []SortOrder + defaultSortOrderID int + refs map[string]SnapshotRef + + // V2 specific + lastSequenceNumber *int64 +} + +func NewMetadataBuilder() (*MetadataBuilder, error) { + return &MetadataBuilder{ + updates: make([]Update, 0), + schemaList: make([]*iceberg.Schema, 0), + specs: make([]iceberg.PartitionSpec, 0), + props: make(iceberg.Properties), + snapshotList: make([]Snapshot, 0), + snapshotLog: make([]SnapshotLogEntry, 0), + metadataLog: make([]MetadataLogEntry, 0), + sortOrderList: make([]SortOrder, 0), + refs: make(map[string]SnapshotRef), + }, nil +} + +func MetadataBuilderFromBase(metadata Metadata) (*MetadataBuilder, error) { + b := &MetadataBuilder{} + b.base = metadata + + b.formatVersion = metadata.Version() + b.uuid = metadata.TableUUID() + b.loc = metadata.Location() + b.lastUpdatedMS = metadata.LastUpdatedMillis() + b.lastColumnId = metadata.LastColumnID() + b.schemaList = metadata.Schemas() + b.currentSchemaID = metadata.CurrentSchema().ID + b.specs = metadata.PartitionSpecs() + b.defaultSpecID = metadata.DefaultPartitionSpec() + b.lastPartitionID = metadata.LastPartitionSpecID() + b.props = metadata.Properties() + b.snapshotList = metadata.Snapshots() + b.currentSnapshotID = &metadata.CurrentSnapshot().SnapshotID + b.sortOrderList = metadata.SortOrders() + b.defaultSortOrderID = metadata.DefaultSortOrder() + b.refs = metadata.Refs() + b.snapshotLog = metadata.SnapshotLogs() + b.metadataLog = metadata.PreviousFiles() + + return b, nil +} + +func (b *MetadataBuilder) AddSchema(schema *iceberg.Schema, newLastColumnID int) (*MetadataBuilder, error) { + if newLastColumnID < b.lastColumnId { + return nil, fmt.Errorf("invalid last column id %d, must be >= %d", + newLastColumnID, b.lastColumnId) Review Comment: Agreed, I have applied the update. -- 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