azagrebin commented on a change in pull request #6501: [FLINK-10041] Extract 
iterators from RocksDBKeyedStateBackend (inner …
URL: https://github.com/apache/flink/pull/6501#discussion_r209287458
 
 

 ##########
 File path: 
flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/iterator/RocksStatesPerKeyGroupMergeIterator.java
 ##########
 @@ -0,0 +1,217 @@
+/*
+ * 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.contrib.streaming.state.iterator;
+
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.contrib.streaming.state.RocksIteratorWrapper;
+import org.apache.flink.util.IOUtils;
+import org.apache.flink.util.Preconditions;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.PriorityQueue;
+
+/**
+ * Iterator that merges multiple RocksDB iterators to partition all states 
into contiguous key-groups.
+ * The resulting iteration sequence is ordered by (key-group, kv-state).
+ */
+public class RocksStatesPerKeyGroupMergeIterator implements AutoCloseable {
+
+       private final PriorityQueue<RocksSingleStateIterator> heap;
+       private final int keyGroupPrefixByteCount;
+       private boolean newKeyGroup;
+       private boolean newKVState;
+       private boolean valid;
+       private RocksSingleStateIterator currentSubIterator;
+
+       private static final List<Comparator<RocksSingleStateIterator>> 
COMPARATORS;
+
+       static {
+               int maxBytes = 2;
+               COMPARATORS = new ArrayList<>(maxBytes);
+               for (int i = 0; i < maxBytes; ++i) {
+                       final int currentBytes = i + 1;
+                       COMPARATORS.add((o1, o2) -> {
+                               int arrayCmpRes = compareKeyGroupsForByteArrays(
+                                       o1.getCurrentKey(), o2.getCurrentKey(), 
currentBytes);
+                               return arrayCmpRes == 0 ? o1.getKvStateId() - 
o2.getKvStateId() : arrayCmpRes;
+                       });
+               }
+       }
+
+       public RocksStatesPerKeyGroupMergeIterator(
+               List<Tuple2<RocksIteratorWrapper, Integer>> kvStateIterators,
+               final int keyGroupPrefixByteCount) {
+               Preconditions.checkNotNull(kvStateIterators);
+               Preconditions.checkArgument(keyGroupPrefixByteCount >= 1);
+
+               this.keyGroupPrefixByteCount = keyGroupPrefixByteCount;
+
+               Comparator<RocksSingleStateIterator> iteratorComparator = 
COMPARATORS.get(keyGroupPrefixByteCount - 1);
+
+               if (kvStateIterators.size() > 0) {
+                       PriorityQueue<RocksSingleStateIterator> 
iteratorPriorityQueue =
 
 Review comment:
   could be also separate method like:
   `PriorityQueue<RocksSingleStateIterator> fillQueue(... kvStateIterators)`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to