You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2009/08/06 03:55:30 UTC

svn commit: r801493 - /incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java

Author: jbellis
Date: Thu Aug  6 01:55:29 2009
New Revision: 801493

URL: http://svn.apache.org/viewvc?rev=801493&view=rev
Log:
Improve the speed of RandomPartitioner comparator.  patch by Sammy Yu; reviewed by jbellis for CASSANDRA-346

Modified:
    incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java

Modified: incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java?rev=801493&r1=801492&r2=801493&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java (original)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/dht/RandomPartitioner.java Thu Aug  6 01:55:29 2009
@@ -20,6 +20,7 @@
 
 import java.math.BigInteger;
 import java.util.Comparator;
+import java.util.StringTokenizer;
 
 import org.apache.cassandra.config.DatabaseDescriptor;
 import org.apache.cassandra.utils.FBUtilities;
@@ -34,15 +35,20 @@
     {
         public int compare(String o1, String o2)
         {
-            String[] split1 = o1.split(":", 2);
-            String[] split2 = o2.split(":", 2);
-            BigInteger i1 = new BigInteger(split1[0]);
-            BigInteger i2 = new BigInteger(split2[0]);
+            // StringTokenizer is faster than String.split()
+            StringTokenizer st1 = new StringTokenizer(o1, ":");
+            StringTokenizer st2 = new StringTokenizer(o2, ":");
+
+            // first, compare on the bigint hash "decoration".  usually this will be enough.
+            BigInteger i1 = new BigInteger(st1.nextToken());
+            BigInteger i2 = new BigInteger(st2.nextToken());
             int v = i1.compareTo(i2);
             if (v != 0) {
                 return v;
             }
-            return split1[1].compareTo(split2[1]);
+
+            // if the hashes are equal, compare the strings
+            return st1.nextToken().compareTo(st2.nextToken());
         }
     };
     private static final Comparator<String> rcomparator = new Comparator<String>()
@@ -118,4 +124,4 @@
     {
         return new BigIntegerToken(FBUtilities.hash(key));
     }
-}
\ No newline at end of file
+}