badalprasadsingh commented on code in PR #524: URL: https://github.com/apache/iceberg-go/pull/524#discussion_r2288216632
########## table/rolling_data_writer.go: ########## @@ -0,0 +1,199 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package table + +import ( + "context" + "fmt" + "iter" + "net/url" + "sync" + + "github.com/apache/arrow-go/v18/arrow" + "github.com/apache/iceberg-go" +) + +// WriterFactory manages the creation and lifecycle of RollingDataWriter instances +// for different partitions, providing shared configuration and coordination +// across all writers in a partitioned write operation. +type WriterFactory struct { + rootLocation string + args recordWritingArgs + meta *MetadataBuilder + taskSchema *iceberg.Schema + targetFileSize int64 + writers sync.Map + nextCount func() (int, bool) + stopCount func() + mu sync.Mutex +} + +// NewWriterFactory creates a new WriterFactory with the specified configuration +// for managing rolling data writers across partitions. +func NewWriterFactory(rootLocation string, args recordWritingArgs, meta *MetadataBuilder, taskSchema *iceberg.Schema, targetFileSize int64) WriterFactory { + return WriterFactory{ + rootLocation: rootLocation, + args: args, + meta: meta, + taskSchema: taskSchema, + targetFileSize: targetFileSize, + } +} + +// RollingDataWriter accumulates Arrow records for a specific partition and flushes +// them to data files when the target file size is reached, implementing a rolling +// file strategy to manage file sizes. +type RollingDataWriter struct { + partitionKey string + data []arrow.Record + currentSize int64 + factory *WriterFactory + mu sync.Mutex + partitionValues map[int]any +} + +// NewRollingDataWriter creates a new RollingDataWriter for the specified partition +// with the given partition values. +func (w *WriterFactory) NewRollingDataWriter(partition string, partitionValues map[int]any) *RollingDataWriter { + return &RollingDataWriter{ + partitionKey: partition, + data: make([]arrow.Record, 0), + currentSize: 0, + factory: w, + partitionValues: partitionValues, + } +} + +func (w *WriterFactory) getOrCreateRollingDataWriter(partition string, partitionValues map[int]any) (*RollingDataWriter, error) { Review Comment: Yes this can be done. We can use `channels` for every partition. Each of the fanout threads can dump the partitioned `arrow.Record` data into the partition specific channel. Finally when we are finished partitioning, we can generate an `iter.Seq2[arrow.Record]` for every partition from its respective channel and pass it to `recordsToDataFiles` function. But, the only problem here would be to wait until we have finished partitioning the whole incoming data. This would eradicate the whole concept of `Rolling Data Writer`. Would this be fine? -- 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