openinx commented on code in PR #99:
URL: https://github.com/apache/flink-table-store/pull/99#discussion_r865678648


##########
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/utils/AppendOnlyWriter.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * 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.utils;
+
+import org.apache.flink.api.common.serialization.BulkWriter;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.binary.BinaryRowData;
+import org.apache.flink.table.runtime.typeutils.RowDataSerializer;
+import org.apache.flink.table.store.file.ValueKind;
+import org.apache.flink.table.store.file.format.FileFormat;
+import org.apache.flink.table.store.file.mergetree.Increment;
+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.types.logical.RowType;
+
+import org.apache.flink.shaded.guava30.com.google.common.collect.Lists;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.Supplier;
+
+/** A {@link RecordWriter} implementation that only accepts append-only 
records. */
+public class AppendOnlyWriter implements RecordWriter {
+    private final BulkWriter.Factory<RowData> writerFactory;
+    private RollingFileWriter<RowData, List<Path>> writer;
+    private final long targetFileSize;
+    private final Comparator<RowData> keyComparator;
+    private final RowDataSerializer keySerializer;
+    private final List<DataFileMeta> existingFiles;
+    private final List<DataFileMeta> newFiles;
+    private final Supplier<Path> pathCreator;
+    private final FileStatsExtractor fileStatsExtractor;
+    private final FieldStatsCollector fieldStatsCollector;
+
+    private final AtomicLong startingSeqNum;
+    private final AtomicLong nextSeqNum;
+    private final AtomicInteger rowCount;
+
+    private final AtomicReference<BinaryRowData> minKeyRef = new 
AtomicReference<>(null);
+    private final AtomicReference<BinaryRowData> maxKeyRef = new 
AtomicReference<>(null);
+
+    public AppendOnlyWriter(
+            FileFormat fileFormat,
+            long targetFileSize,
+            RowType writeSchema,
+            Comparator<RowData> keyComparator,
+            List<DataFileMeta> existingFiles,
+            Supplier<Path> pathCreator,
+            FileStatsExtractor fileStatsExtractor) {
+
+        this.writerFactory = fileFormat.createWriterFactory(writeSchema);
+
+        this.writer = new BaseRollingFileWriter(writerFactory, targetFileSize, 
pathCreator);
+        this.writer.register(new CloseWriterListener());
+
+        this.targetFileSize = targetFileSize;
+
+        this.keyComparator = keyComparator;
+        this.keySerializer = new RowDataSerializer(writeSchema);
+
+        this.existingFiles = existingFiles;
+        this.newFiles = new ArrayList<>();
+        this.pathCreator = pathCreator;
+        this.fileStatsExtractor = fileStatsExtractor;
+        this.fieldStatsCollector =
+                fileStatsExtractor == null ? new 
FieldStatsCollector(writeSchema) : null;
+
+        this.startingSeqNum = new AtomicLong(0L);
+        this.nextSeqNum = new AtomicLong(maxSequenceNumber() + 1);
+        this.rowCount = new AtomicInteger(0);
+    }
+
+    private long maxSequenceNumber() {
+        return existingFiles.stream()
+                .map(DataFileMeta::maxSequenceNumber)
+                .max(Long::compare)
+                .orElse(-1L);
+    }
+
+    @Override
+    public void append(RowData row) throws Exception {
+        writer.write(row);
+        if (fieldStatsCollector != null) {
+            fieldStatsCollector.collect(row);
+        }
+        updateMinKey(row);
+        updateMaxKey(row);
+
+        rowCount.incrementAndGet();
+        nextSeqNum.incrementAndGet();
+    }
+
+    private void updateMinKey(RowData row) {
+        BinaryRowData minKey = minKeyRef.get();
+        if ((minKey == null) || (keyComparator.compare(minKey, row) > 0)) {
+            minKey = keySerializer.toBinaryRow(row);
+        }
+        minKeyRef.set(minKey);
+    }
+
+    private void updateMaxKey(RowData row) {
+        BinaryRowData maxKey = maxKeyRef.get();
+        if ((maxKey == null) || (keyComparator.compare(maxKey, row) < 0)) {
+            maxKey = keySerializer.toBinaryRow(row);
+        }
+        maxKeyRef.set(maxKey);

Review Comment:
   That's quite time-consuming for the append-only table, because we will need 
to compare the whole row each time to get the max key and min key. 
   
   It's better to set the dummy constant key and set the whole row as the 
value, then we can skip to compare the whole row columns to get the max-min 
keys ( Because the key is always a constant).



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

Reply via email to