JingsongLi commented on code in PR #117: URL: https://github.com/apache/flink-table-store/pull/117#discussion_r874898190
########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/writer/MetricFileWriter.java: ########## @@ -0,0 +1,194 @@ +/* + * 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 org.apache.flink.table.store.file.writer; + +import org.apache.flink.api.common.serialization.BulkWriter; +import org.apache.flink.core.fs.FSDataOutputStream; +import org.apache.flink.core.fs.FileSystem; +import org.apache.flink.core.fs.Path; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.store.file.stats.FieldStats; +import org.apache.flink.table.store.file.stats.FieldStatsCollector; +import org.apache.flink.table.store.file.stats.FileStatsExtractor; +import org.apache.flink.table.store.file.utils.FileUtils; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.util.IOUtils; +import org.apache.flink.util.Preconditions; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.function.Function; + +/** + * An {@link FileWriter} to write generic record and generate {@link Metric} once closing it. + * + * @param <T> generic record type. + */ +public class MetricFileWriter<T> implements FileWriter<T, Metric> { + private static final Logger LOG = LoggerFactory.getLogger(MetricFileWriter.class); + + private final BulkWriter<RowData> writer; + private final Function<T, RowData> converter; + private final FSDataOutputStream out; + private final Path path; + private final FileStatsExtractor fileStatsExtractor; + + private FieldStatsCollector fieldStatsCollector = null; + + private long recordCount; + private boolean closed = false; + + private MetricFileWriter( + BulkWriter<RowData> writer, + Function<T, RowData> converter, + FSDataOutputStream out, + Path path, + RowType writeSchema, + FileStatsExtractor fileStatsExtractor) { + this.writer = writer; + this.converter = converter; + this.out = out; + this.path = path; + + this.fileStatsExtractor = fileStatsExtractor; + if (this.fileStatsExtractor == null) { + this.fieldStatsCollector = new FieldStatsCollector(writeSchema); + } + + this.recordCount = 0L; + } + + @Override + public void write(T record) throws IOException { + RowData rowData = converter.apply(record); + writer.addElement(rowData); + + if (fieldStatsCollector != null) { + fieldStatsCollector.collect(rowData); + } + + recordCount += 1; + } + + @Override + public long recordCount() { + return recordCount; + } + + @Override + public long length() { + try { + return out.getPos(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + @Override + public void abort() { + try { + close(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + FileUtils.deleteOrWarn(path); + } + + @Override + public Metric result() throws IOException { + Preconditions.checkState(closed, "Cannot access metric unless the writer is closed."); + + FieldStats[] stats; + if (fileStatsExtractor != null) { + stats = fileStatsExtractor.extract(path); + } else { + stats = fieldStatsCollector.extractFieldStats(); + } + + return new Metric(stats, recordCount); + } + + @Override + public void close() throws IOException { + if (!closed) { + if (writer != null) { + writer.flush(); + writer.finish(); + } + + if (out != null) { + out.flush(); + out.close(); + } + + closed = true; + } + } + + public static <T> FileWriter.Factory<T, Metric> createFactory( + BulkWriter.Factory<RowData> factory, + Function<T, RowData> converter, + RowType writeSchema, + FileStatsExtractor fileStatsExtractor) { + return new Factory<>(factory, converter, writeSchema, fileStatsExtractor); + } + + private static class Factory<T> implements FileWriter.Factory<T, Metric> { + private final BulkWriter.Factory<RowData> factory; + private final Function<T, RowData> converter; + private final RowType writeSchema; + private final FileStatsExtractor fileStatsExtractor; + + public Factory( + BulkWriter.Factory<RowData> factory, + Function<T, RowData> converter, + RowType writeSchema, + FileStatsExtractor fileStatsExtractor) { Review Comment: Nullable annotation. like `DataFileWriter` need to add annotation too. ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/writer/AppendOnlyWriter.java: ########## @@ -116,9 +117,15 @@ public List<DataFileMeta> close() throws Exception { return result; } + private FileWriter.Factory<RowData, Metric> createFileWriterFactory() { Review Comment: It is already a factory, we can just init the factory in the constructor of this class. ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/writer/AppendOnlyWriter.java: ########## @@ -143,19 +150,11 @@ public void write(RowData row) throws IOException { super.write(row); nextSeqNum += 1; - if (fileStatsExtractor == null) { - fieldStatsCollector.collect(row); - } } @Override - protected DataFileMeta createFileMeta(Path path) throws IOException { - BinaryTableStats stats; - if (fileStatsExtractor != null) { - stats = fieldStatsCollector.toBinary(fileStatsExtractor.extract(path)); - } else { - stats = fieldStatsCollector.extract(); - } + protected DataFileMeta createResult(Path path, Metric metric) throws IOException { + BinaryTableStats stats = fieldStatsCollector.toBinary(metric.fieldStats()); Review Comment: Just create a `FieldStatsArraySerializer` in the member of `AppendOnlyWriter`? ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/manifest/ManifestFile.java: ########## @@ -112,26 +124,22 @@ public void delete(String fileName) { FileUtils.deleteOrWarn(pathFactory.toManifestFilePath(fileName)); } - private class ManifestEntryBulkWriterFactory implements BulkWriter.Factory<ManifestEntry> { - - @Override - public BulkWriter<ManifestEntry> create(FSDataOutputStream out) throws IOException { - return new BaseBulkWriter<>(writerFactory.create(out), serializer::toRow); - } + private FileWriter.Factory<ManifestEntry, Metric> createManifestEntryWriterFactory() { Review Comment: Init Factory in the constructor? ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/manifest/ManifestFile.java: ########## @@ -52,23 +55,32 @@ public class ManifestFile { private static final Logger LOG = LoggerFactory.getLogger(ManifestFile.class); private final RowType partitionType; + private final RowType entryType; private final ManifestEntrySerializer serializer; + private final FieldStatsArraySerializer statsSerializer; private final BulkFormat<RowData, FileSourceSplit> readerFactory; private final BulkWriter.Factory<RowData> writerFactory; + private final FileStatsExtractor fileStatsExtractor; private final FileStorePathFactory pathFactory; private final long suggestedFileSize; private ManifestFile( RowType partitionType, + RowType entryType, Review Comment: Can we just keep `ManifestFile` as it is? I don't feel we need to introduce MetricFileWriter to it. ########## flink-table-store-core/src/main/java/org/apache/flink/table/store/file/data/DataFileWriter.java: ########## @@ -237,7 +210,14 @@ public RollingKvWriter(Supplier<KvFileWriter> writerFactory, long targetFileSize private Supplier<KvFileWriter> createWriterFactory(int level) { return () -> { try { - return new KvFileWriter(new KvBulkWriterFactory(), pathFactory.newPath(), level); + FileWriter.Factory<KeyValue, Metric> factory = Review Comment: Factory can be init in the class constructor? -- 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]
