cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r825684 - in /incubator/cassandra/trunk: src/java/org/apache/cassandra/dht/Range.java test/unit/org/apache/cassandra/dht/RangeTest.java
Date Thu, 15 Oct 2009 22:21:16 GMT
Author: jbellis
Date: Thu Oct 15 22:21:15 2009
New Revision: 825684

URL: http://svn.apache.org/viewvc?rev=825684&view=rev
Log:
fix token to cover (left, right] like the rest of Cassandra (and Range.toString).  patch by
Stu Hood; reviewed by jbellis for CASSANDRA-495

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/Range.java
    incubator/cassandra/trunk/test/unit/org/apache/cassandra/dht/RangeTest.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/Range.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/Range.java?rev=825684&r1=825683&r2=825684&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/Range.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/Range.java Thu Oct 15 22:21:15
2009
@@ -30,9 +30,8 @@
 /**
  * A representation of the range that a node is responsible for on the DHT ring.
  *
- * A Range is responsible for the tokens between [left, right).
+ * A Range is responsible for the tokens between (left, right].
  */
-
 public class Range implements Comparable<Range>, Serializable
 {
     private static ICompactSerializer<Range> serializer_;
@@ -81,31 +80,27 @@
      */
     public boolean contains(Token bi)
     {
-        if ( left_.compareTo(right_) > 0 )
+        if ( isWrapAround(this) )
         {
             /* 
-             * left is greater than right we are wrapping around.
-             * So if the interval is [a,b) where a > b then we have
-             * 3 cases one of which holds for any given token k.
-             * (1) k > a -- return true
-             * (2) k < b -- return true
-             * (3) b < k < a -- return false
-            */
-            if ( bi.compareTo(left_) >= 0 )
+             * We are wrapping around, so the interval is (a,b] where a >= b,
+             * then we have 3 cases which hold for any given token k:
+             * (1) a < k -- return true
+             * (2) k <= b -- return true
+             * (3) b < k <= a -- return false
+             */
+            if ( bi.compareTo(left_) > 0 )
                 return true;
-            else return right_.compareTo(bi) > 0;
+            else
+                return right_.compareTo(bi) >= 0;
         }
-        else if ( left_.compareTo(right_) < 0 )
+        else
         {
             /*
-             * This is the range [a, b) where a < b. 
-            */
-            return ( bi.compareTo(left_) >= 0 && right_.compareTo(bi) > 0 );
+             * This is the range (a, b] where a < b. 
+             */
+            return ( bi.compareTo(left_) > 0 && right_.compareTo(bi) >= 0 );
         }        
-        else
-    	{
-    		return true;
-    	}    	
     }
 
     /**
@@ -115,7 +110,7 @@
      */
     private static boolean isWrapAround(Range range)
     {
-        return range.left_.compareTo(range.right_) > 0;
+        return range.left_.compareTo(range.right_) >= 0;
     }
     
     public int compareTo(Range rhs)
@@ -123,7 +118,7 @@
         /* 
          * If the range represented by the "this" pointer
          * is a wrap around then it is the smaller one.
-        */
+         */
         if ( isWrapAround(this) )
             return -1;
         

Modified: incubator/cassandra/trunk/test/unit/org/apache/cassandra/dht/RangeTest.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/test/unit/org/apache/cassandra/dht/RangeTest.java?rev=825684&r1=825683&r2=825684&view=diff
==============================================================================
--- incubator/cassandra/trunk/test/unit/org/apache/cassandra/dht/RangeTest.java (original)
+++ incubator/cassandra/trunk/test/unit/org/apache/cassandra/dht/RangeTest.java Thu Oct 15
22:21:15 2009
@@ -24,17 +24,24 @@
     @Test
     public void testRange() {
         Range left = new Range(new BigIntegerToken("0"), new BigIntegerToken("100"));
-		assert left.contains(new BigIntegerToken("0"));
+        assert !left.contains(new BigIntegerToken("0"));
         assert left.contains(new BigIntegerToken("10"));
-        assert !left.contains(new BigIntegerToken("-1"));
-        assert !left.contains(new BigIntegerToken("100"));
+        assert left.contains(new BigIntegerToken("100"));
         assert !left.contains(new BigIntegerToken("101"));
+    }
+
+    @Test
+    public void testWrappingRange() {
+        Range range = new Range(new BigIntegerToken("0"), new BigIntegerToken("0"));
+        assert range.contains(new BigIntegerToken("0"));
+        assert range.contains(new BigIntegerToken("10"));
+        assert range.contains(new BigIntegerToken("100"));
+        assert range.contains(new BigIntegerToken("101"));
 
-        Range right = new Range(new BigIntegerToken("100"), new BigIntegerToken("0"));
-        assert right.contains(new BigIntegerToken("100"));
-        assert right.contains(new BigIntegerToken("200"));
-        assert right.contains(new BigIntegerToken("-10"));
-        assert !right.contains(new BigIntegerToken("0"));
-        assert !right.contains(new BigIntegerToken("1"));
+        range = new Range(new BigIntegerToken("100"), new BigIntegerToken("0"));
+        assert range.contains(new BigIntegerToken("0"));
+        assert !range.contains(new BigIntegerToken("1"));
+        assert !range.contains(new BigIntegerToken("100"));
+        assert range.contains(new BigIntegerToken("200"));
     }
 }



Mime
View raw message