yifan-c commented on code in PR #45:
URL: https://github.com/apache/cassandra-sidecar/pull/45#discussion_r1192824961


##########
cassandra40/src/main/java/org/apache/cassandra/sidecar/cassandra40/TokenRangeReplicas.java:
##########
@@ -0,0 +1,417 @@
+/*
+ * 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.cassandra.sidecar.cassandra40;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import com.google.common.base.Objects;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.jetbrains.annotations.NotNull;
+
+
+/**
+ * Representation of a token range and the corresponding mapping to 
replica-set hosts
+ */
+public class TokenRangeReplicas implements Comparable<TokenRangeReplicas>
+{
+    private final BigInteger start;
+    private final BigInteger end;
+
+    private final Partitioner partitioner;
+
+    private final Set<String> replicaSet;
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(TokenRangeReplicas.class);
+
+    public TokenRangeReplicas(BigInteger start, BigInteger end, Partitioner 
partitioner, Set<String> replicaSet)
+    {
+        this.start = start;
+        this.end = end;
+        this.partitioner = partitioner;
+        this.replicaSet = replicaSet;
+    }
+
+
+    public BigInteger start()
+    {
+        return start;
+    }
+
+    public BigInteger end()
+    {
+        return end;
+    }
+
+    public Set<String> replicaSet()
+    {
+        return replicaSet;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int compareTo(@NotNull TokenRangeReplicas other)
+    {
+        if (this.partitioner != other.partitioner)
+            throw new IllegalStateException("Token ranges being compared do 
not have the same partitioner");
+
+        // TODO
+        BigInteger maxValue = this.partitioner.maxToken;
+        if (this.start.compareTo(other.start) == 0)
+        {
+            if (this.end.equals(maxValue)) return 1;
+            else if (other.end.equals(maxValue)) return -1;
+            else return this.end.compareTo(other.end);
+        }
+        else return this.start.compareTo(other.start);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean equals(Object o)
+    {
+        if (!(o instanceof TokenRangeReplicas))
+        {
+            return false;
+        }
+        TokenRangeReplicas that = (TokenRangeReplicas) o;
+        return (this.start.equals(that.start) && this.end.equals(that.end) && 
this.partitioner == that.partitioner);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int hashCode()
+    {
+        return Objects.hashCode(start, end, partitioner);
+    }
+
+    private boolean isWrapAround()
+    {
+        return start.compareTo(end) >= 0;
+    }
+
+    private boolean isSubsetOf(TokenRangeReplicas other)
+    {
+        if (this.partitioner != other.partitioner)
+            throw new IllegalStateException("Token ranges being compared do 
not have the same partitioner");
+
+        BigInteger maxValue = this.partitioner.maxToken;
+        if (this.start.compareTo(other.start) >= 0)
+        {
+            // TODO:
+            if (other.end.equals(maxValue)) return true;
+            if (this.end.equals(maxValue)) return false;
+            if (this.end.compareTo(other.end) <= 0) return true;
+        }
+        return false;
+    }
+
+    /**
+     * For subset ranges, this is used to determine if a range is larger than 
the other by comparing start-end lengths
+     * If both ranges end at the min, we compare starting points to determine 
the result.
+     * When the left range is the only one ending at min, it is always the 
larger one since all subsequent ranges
+     * in the sorted range list have to be smaller.
+     *
+     * @param other the next range in the range list to compare
+     * @return true if "this" range is larger than the other
+     */
+    private boolean isLarger(TokenRangeReplicas other)
+    {
+        if (this.partitioner != other.partitioner)
+            throw new IllegalStateException("Token ranges being compared do 
not have the same partitioner");
+
+        // If both ranges end at min, we compare start of ranges
+        if (this.end.equals(partitioner.maxToken) && 
other.end.equals(partitioner.maxToken))
+        {
+            return this.start.compareTo(other.start) < 0;
+        }
+
+        if (this.end.equals(partitioner.maxToken)) return true;
+        if (other.end.equals(partitioner.maxToken)) return false;
+
+        return 
this.end.subtract(this.start).compareTo(other.end.subtract(other.start)) > 0;
+    }
+
+    /**
+     * Determines intersection if the next range starts before the current 
range ends.
+     * When the current range ending at min, we determine intersection merely 
if the next range starts after the current
+     * since all subsequent ranges have to be subsets.
+     *
+     * @param other the range we are currently processing to check if "this" 
intersects it
+     * @return true if "this" range intersects the other
+     */
+    private boolean intersects(TokenRangeReplicas other)
+    {
+        return (other.end.compareTo(partitioner.maxToken) == 0 && 
this.start.compareTo(other.start) > 0) ||
+               this.start.compareTo(other.end) < 0;
+    }

Review Comment:
   I think this implementation is wrong. You want to compare 
`this.end.compareTo(other.start) < 0;`. Before the comparison, it needs to sort 
the 2 ranges by `start`, and make sure to compare left range with right range.
   ~Beside that, the first condition that checks with the maxToken of the 
partitioner seems not necessary. It only wants to compare 2 ranges.~
   (Edit: looks like wrap around is considered. In that case, either this or 
that range needs to unwrap to check intersection.)
   
   The below test fails 
   
   ```java
       @Test
       void testInterests()
       {
           TokenRangeReplicas range1 = new 
TokenRangeReplicas(BigInteger.valueOf(1), BigInteger.valueOf(10), 
Partitioner.Murmur3, new HashSet<>());
           TokenRangeReplicas range2 = new 
TokenRangeReplicas(BigInteger.valueOf(9), BigInteger.valueOf(12), 
Partitioner.Murmur3, new HashSet<>());
           assertThat(range1.intersects(range2)).isTrue();
           assertThat(range2.intersects(range1)).isTrue();
   
           TokenRangeReplicas range3 = new 
TokenRangeReplicas(BigInteger.valueOf(1), BigInteger.valueOf(10), 
Partitioner.Murmur3, new HashSet<>());
           TokenRangeReplicas range4 = new 
TokenRangeReplicas(BigInteger.valueOf(11), BigInteger.valueOf(20), 
Partitioner.Murmur3, new HashSet<>());
           assertThat(range3.intersects(range4)).isFalse();
           assertThat(range4.intersects(range3)).isFalse();
       }
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to