tsreaper commented on a change in pull request #15:
URL: https://github.com/apache/flink-table-store/pull/15#discussion_r794979738



##########
File path: 
flink-table-store-connector/src/main/java/org/apache/flink/table/store/connector/source/FileStoreSourceSplitReader.java
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.connector.source;
+
+import org.apache.flink.connector.base.source.reader.RecordsWithSplitIds;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitReader;
+import 
org.apache.flink.connector.base.source.reader.splitreader.SplitsAddition;
+import org.apache.flink.connector.base.source.reader.splitreader.SplitsChange;
+import org.apache.flink.connector.file.src.impl.FileRecords;
+import org.apache.flink.connector.file.src.reader.BulkFormat;
+import org.apache.flink.connector.file.src.util.MutableRecordAndPosition;
+import org.apache.flink.connector.file.src.util.Pool;
+import org.apache.flink.connector.file.src.util.RecordAndPosition;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.store.file.KeyValue;
+import org.apache.flink.table.store.file.operation.FileStoreRead;
+import org.apache.flink.table.store.file.utils.RecordReader;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.Queue;
+
+/** The {@link SplitReader} implementation for the file store source. */
+public class FileStoreSourceSplitReader
+        implements SplitReader<RecordAndPosition<RowData>, 
FileStoreSourceSplit> {
+
+    private final FileStoreRead fileStoreRead;
+    private final boolean keyAsRecord;
+
+    private final Queue<FileStoreSourceSplit> splits;
+
+    private final Pool<FileStoreRecordIterator> pool;
+
+    @Nullable private RecordReader currentReader;
+    @Nullable private String currentSplitId;
+    private long currentNumRead;
+    private RecordReader.RecordIterator currentFirstBatch;
+
+    public FileStoreSourceSplitReader(FileStoreRead fileStoreRead, boolean 
keyAsRecord) {
+        this.fileStoreRead = fileStoreRead;
+        this.keyAsRecord = keyAsRecord;
+        this.splits = new LinkedList<>();
+        this.pool = new Pool<>(1);
+        this.pool.add(new FileStoreRecordIterator());
+    }
+
+    @Override
+    public RecordsWithSplitIds<RecordAndPosition<RowData>> fetch() throws 
IOException {
+        checkSplitOrStartNext();
+
+        // pool first, avoid thread safety issues

Review comment:
       Not thread safety. This is because batches can only be fetched one by 
one, so we use a pool to restrict the reader thread not to fetch too many 
batches at the same time.

##########
File path: 
flink-table-store-connector/src/main/java/org/apache/flink/table/store/connector/source/FileStoreSourceSplitGenerator.java
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.connector.source;
+
+import org.apache.flink.table.store.file.operation.FileStoreScan;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * The {@code FileStoreSplitGenerator}'s task is to plan all files to be read 
and to split them into
+ * a set of {@link FileStoreSourceSplit}.
+ */
+public class FileStoreSourceSplitGenerator {
+
+    /**
+     * The current Id as a mutable string representation. This covers more 
values than the integer
+     * value range, so we should never overflow.
+     */
+    private final char[] currentId = "0000000000".toCharArray();
+
+    public List<FileStoreSourceSplit> createSplits(FileStoreScan scan) {
+        return createSplits(scan.plan());
+    }
+
+    public List<FileStoreSourceSplit> createSplits(FileStoreScan.Plan plan) {
+        return plan.groupByPartFiles().entrySet().stream()
+                .flatMap(
+                        pe ->
+                                pe.getValue().entrySet().stream()
+                                        .map(
+                                                be ->
+                                                        new 
FileStoreSourceSplit(
+                                                                getNextId(),
+                                                                pe.getKey(),
+                                                                be.getKey(),
+                                                                
be.getValue())))
+                .collect(Collectors.toList());
+    }
+
+    protected final String getNextId() {
+        // because we just increment numbers, we increment the char 
representation directly,
+        // rather than incrementing an integer and converting it to a string 
representation
+        // every time again (requires quite some expensive conversion logic).
+        incrementCharArrayByOne(currentId, currentId.length - 1);
+        return new String(currentId);
+    }
+
+    private static void incrementCharArrayByOne(char[] array, int pos) {
+        char c = array[pos];
+        c++;
+
+        if (c > '9') {
+            c = '0';
+            incrementCharArrayByOne(array, pos - 1);
+        }
+        array[pos] = c;
+    }

Review comment:
       pos might be -1 if this method is called too many times. In this case we 
should recreate `array` and increment its length by 1.

##########
File path: 
flink-table-store-connector/src/test/java/org/apache/flink/table/store/connector/source/FileStoreSourceSplitGeneratorTest.java
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.connector.source;
+
+import org.apache.flink.table.store.file.ValueKind;
+import org.apache.flink.table.store.file.manifest.ManifestEntry;
+import org.apache.flink.table.store.file.mergetree.sst.SstFileMeta;
+import org.apache.flink.table.store.file.operation.FileStoreScan;
+import org.apache.flink.table.store.file.stats.FieldStats;
+
+import org.junit.jupiter.api.Test;
+
+import javax.annotation.Nullable;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static 
org.apache.flink.table.store.file.mergetree.compact.CompactManagerTest.row;
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Test for {@link FileStoreSourceSplitGenerator}. */
+public class FileStoreSourceSplitGeneratorTest {
+
+    @Test
+    public void test() {
+        FileStoreScan.Plan plan =
+                new FileStoreScan.Plan() {
+                    @Nullable
+                    @Override
+                    public Long snapshotId() {
+                        return null;
+                    }
+
+                    @Override
+                    public List<ManifestEntry> files() {
+                        return Arrays.asList(
+                                makeEntry(1, 0, "f0"),
+                                makeEntry(1, 0, "f1"),
+                                makeEntry(1, 1, "f2"),
+                                makeEntry(2, 0, "f3"),
+                                makeEntry(2, 0, "f4"),
+                                makeEntry(2, 0, "f5"),
+                                makeEntry(2, 1, "f6"));
+                    }
+                };
+        List<FileStoreSourceSplit> splits = new 
FileStoreSourceSplitGenerator().createSplits(plan);
+        assertThat(splits.size()).isEqualTo(4);
+        assertSplit(splits.get(0), "0000000001", 2, 0, Arrays.asList("f3", 
"f4", "f5"));
+        assertSplit(splits.get(1), "0000000002", 2, 1, 
Collections.singletonList("f6"));
+        assertSplit(splits.get(2), "0000000003", 1, 0, Arrays.asList("f0", 
"f1"));
+        assertSplit(splits.get(3), "0000000004", 1, 1, 
Collections.singletonList("f2"));

Review comment:
       Add cases to test carrying.




-- 
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...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to