echauchot commented on code in PR #3:
URL: 
https://github.com/apache/flink-connector-cassandra/pull/3#discussion_r1101601871


##########
flink-connector-cassandra/src/main/java/org/apache/flink/connector/cassandra/source/split/SplitsGenerator.java:
##########
@@ -0,0 +1,110 @@
+/*
+ * 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.connector.cassandra.source.split;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * This class generates {@link CassandraSplit}s based on Cassandra cluster 
partitioner and Flink
+ * source parallelism.
+ */
+public final class SplitsGenerator {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(SplitsGenerator.class);
+
+    private final CassandraPartitioner partitioner;
+
+    public SplitsGenerator(CassandraPartitioner partitioner) {
+        this.partitioner = partitioner;
+    }
+
+    /**
+     * Split Cassandra tokens ring into {@link CassandraSplit}s containing 
each a range of the ring.
+     *
+     * @param numSplits requested number of splits
+     * @return list containing {@code numSplits} CassandraSplits.
+     */
+    public List<CassandraSplit> generateSplits(long numSplits) {
+        if (numSplits == 1) {
+            return Collections.singletonList(
+                    new CassandraSplit(partitioner.minToken(), 
partitioner.maxToken()));
+        }
+        List<CassandraSplit> splits = new ArrayList<>();
+        BigInteger splitSize =
+                (partitioner.ringSize()).divide(new 
BigInteger(String.valueOf(numSplits)));
+
+        BigInteger startToken, endToken = partitioner.minToken();
+        for (int splitCount = 1; splitCount <= numSplits; splitCount++) {
+            startToken = endToken;
+            endToken = startToken.add(splitSize);
+            if (splitCount == numSplits) {
+                endToken = partitioner.maxToken();
+            }
+            splits.add(new CassandraSplit(startToken, endToken));
+        }

Review Comment:
   agree



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