cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmcken...@apache.org
Subject [1/2] git commit: Optimize partitioner tokens
Date Wed, 05 Nov 2014 18:26:17 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 51bbec757 -> 70f1ac680


Optimize partitioner tokens

Patch by blambov, reviewed by jmckenzie for CASSANDRA-8230


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/6eef6f78
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/6eef6f78
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/6eef6f78

Branch: refs/heads/trunk
Commit: 6eef6f7856d1fe1bebb923362d730b9ef3c4bb6c
Parents: 82b91ab
Author: Joshua McKenzie <jmckenzie@apache.org>
Authored: Wed Nov 5 12:17:25 2014 -0600
Committer: Joshua McKenzie <jmckenzie@apache.org>
Committed: Wed Nov 5 12:17:25 2014 -0600

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../dht/AbstractByteOrderedPartitioner.java     | 45 ++++---------
 .../org/apache/cassandra/dht/AbstractToken.java | 53 ---------------
 .../apache/cassandra/dht/BigIntegerToken.java   |  7 +-
 .../org/apache/cassandra/dht/BytesToken.java    | 12 +++-
 .../cassandra/dht/ComparableObjectToken.java    | 69 ++++++++++++++++++++
 .../org/apache/cassandra/dht/LocalToken.java    | 21 +-----
 .../org/apache/cassandra/dht/LongToken.java     | 37 +++++++++--
 .../cassandra/dht/Murmur3Partitioner.java       |  3 +-
 .../org/apache/cassandra/dht/StringToken.java   |  7 +-
 src/java/org/apache/cassandra/dht/Token.java    |  2 +
 11 files changed, 132 insertions(+), 125 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 80f4c8f..a2bd3aa 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 2.1.2
+ * Optimize partitioner tokens (CASSANDRA-8230)
  * Improve compaction of repaired/unrepaired sstables (CASSANDRA-8004)
  * Make cache serializers pluggable (CASSANDRA-8096)
  * Fix issues with CONTAINS (KEY) queries on secondary indexes

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java b/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
index 88326a8..d3124ef 100644
--- a/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
+++ b/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
@@ -23,7 +23,6 @@ import java.util.*;
 
 import org.apache.cassandra.config.*;
 import org.apache.cassandra.db.BufferDecoratedKey;
-import org.apache.cassandra.utils.ByteBufferUtil;
 import org.apache.commons.lang3.ArrayUtils;
 
 import org.apache.cassandra.db.DecoratedKey;
@@ -48,35 +47,12 @@ public abstract class AbstractByteOrderedPartitioner extends AbstractPartitioner
 
     public BytesToken midpoint(Token lt, Token rt)
     {
-        AbstractToken<?> ltoken = (AbstractToken<?>) lt;
-        AbstractToken<?> rtoken = (AbstractToken<?>) rt;
-        int ll,rl;
-        ByteBuffer lb,rb;
+        BytesToken ltoken = (BytesToken) lt;
+        BytesToken rtoken = (BytesToken) rt;
 
-        if(ltoken.token instanceof byte[])
-        {
-            ll = ((byte[])ltoken.token).length;
-            lb = ByteBuffer.wrap(((byte[])ltoken.token));
-        }
-        else
-        {
-            ll = ((ByteBuffer)ltoken.token).remaining();
-            lb = (ByteBuffer)ltoken.token;
-        }
-
-        if(rtoken.token instanceof byte[])
-        {
-            rl = ((byte[])rtoken.token).length;
-            rb = ByteBuffer.wrap(((byte[])rtoken.token));
-        }
-        else
-        {
-            rl = ((ByteBuffer)rtoken.token).remaining();
-            rb = (ByteBuffer)rtoken.token;
-        }
-        int sigbytes = Math.max(ll, rl);
-        BigInteger left = bigForBytes(lb, sigbytes);
-        BigInteger right = bigForBytes(rb, sigbytes);
+        int sigbytes = Math.max(ltoken.token.length, rtoken.token.length);
+        BigInteger left = bigForBytes(ltoken.token, sigbytes);
+        BigInteger right = bigForBytes(rtoken.token, sigbytes);
 
         Pair<BigInteger,Boolean> midpair = FBUtilities.midpoint(left, right, 8*sigbytes);
         return new BytesToken(bytesForBig(midpair.left, sigbytes, midpair.right));
@@ -86,10 +62,15 @@ public abstract class AbstractByteOrderedPartitioner extends AbstractPartitioner
      * Convert a byte array containing the most significant of 'sigbytes' bytes
      * representing a big-endian magnitude into a BigInteger.
      */
-    private BigInteger bigForBytes(ByteBuffer bytes, int sigbytes)
+    private BigInteger bigForBytes(byte[] bytes, int sigbytes)
     {
-        byte[] b = new byte[sigbytes];
-        ByteBufferUtil.arrayCopy(bytes, bytes.position(), b, 0, bytes.remaining());
+        byte[] b;
+        if (sigbytes != bytes.length)
+        {
+            b = new byte[sigbytes];
+            System.arraycopy(bytes, 0, b, 0, bytes.length);
+        } else
+            b = bytes;
         return new BigInteger(1, b);
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/AbstractToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/AbstractToken.java b/src/java/org/apache/cassandra/dht/AbstractToken.java
deleted file mode 100644
index 9622d38..0000000
--- a/src/java/org/apache/cassandra/dht/AbstractToken.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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.dht;
-
-abstract class AbstractToken<C> extends Token
-{
-    private static final long serialVersionUID = 1L;
-
-    final C token;   // Package-private to allow access from subtypes, which should all reside
in the dht package.
-
-    protected AbstractToken(C token)
-    {
-        this.token = token;
-    }
-
-    @Override
-    public String toString()
-    {
-        return token.toString();
-    }
-
-    @Override
-    public boolean equals(Object obj)
-    {
-        if (this == obj)
-            return true;
-        if (obj == null || this.getClass() != obj.getClass())
-            return false;
-
-        return token.equals(((AbstractToken<?>)obj).token);
-    }
-
-    @Override
-    public int hashCode()
-    {
-        return token.hashCode();
-    }
-}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/BigIntegerToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/BigIntegerToken.java b/src/java/org/apache/cassandra/dht/BigIntegerToken.java
index 0c685db..f050519 100644
--- a/src/java/org/apache/cassandra/dht/BigIntegerToken.java
+++ b/src/java/org/apache/cassandra/dht/BigIntegerToken.java
@@ -19,7 +19,7 @@ package org.apache.cassandra.dht;
 
 import java.math.BigInteger;
 
-public class BigIntegerToken extends AbstractToken<BigInteger>
+public class BigIntegerToken extends ComparableObjectToken<BigInteger>
 {
     static final long serialVersionUID = -5833589141319293006L;
 
@@ -32,9 +32,4 @@ public class BigIntegerToken extends AbstractToken<BigInteger>
     public BigIntegerToken(String token) {
         this(new BigInteger(token));
     }
-
-    public int compareTo(Token o)
-    {
-        return token.compareTo(((BigIntegerToken) o).token);
-    }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/BytesToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/BytesToken.java b/src/java/org/apache/cassandra/dht/BytesToken.java
index f29af39..ad1fd10 100644
--- a/src/java/org/apache/cassandra/dht/BytesToken.java
+++ b/src/java/org/apache/cassandra/dht/BytesToken.java
@@ -24,10 +24,12 @@ import org.apache.cassandra.utils.ByteBufferUtil;
 import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.Hex;
 
-public class BytesToken extends AbstractToken<byte[]>
+public class BytesToken extends Token
 {
     static final long serialVersionUID = -2630749093733680626L;
 
+    final byte[] token;
+
     public BytesToken(ByteBuffer token)
     {
         this(ByteBufferUtil.getArray(token));
@@ -35,7 +37,7 @@ public class BytesToken extends AbstractToken<byte[]>
 
     public BytesToken(byte[] token)
     {
-        super(token);
+        this.token = token;
     }
 
     @Override
@@ -69,4 +71,10 @@ public class BytesToken extends AbstractToken<byte[]>
 
         return Arrays.equals(token, other.token);
     }
+
+    @Override
+    public byte[] getTokenValue()
+    {
+        return token;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/ComparableObjectToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/ComparableObjectToken.java b/src/java/org/apache/cassandra/dht/ComparableObjectToken.java
new file mode 100644
index 0000000..137f27c
--- /dev/null
+++ b/src/java/org/apache/cassandra/dht/ComparableObjectToken.java
@@ -0,0 +1,69 @@
+/*
+ * 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.dht;
+
+abstract class ComparableObjectToken<C extends Comparable<C>> extends Token
+{
+    private static final long serialVersionUID = 1L;
+
+    final C token;   // Package-private to allow access from subtypes, which should all reside
in the dht package.
+
+    protected ComparableObjectToken(C token)
+    {
+        this.token = token;
+    }
+
+    @Override
+    public C getTokenValue()
+    {
+        return token;
+    }
+
+    @Override
+    public String toString()
+    {
+        return token.toString();
+    }
+
+    @Override
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+            return true;
+        if (obj == null || this.getClass() != obj.getClass())
+            return false;
+
+        return token.equals(((ComparableObjectToken<?>)obj).token);
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return token.hashCode();
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public int compareTo(Token o)
+    {
+        if (o.getClass() != getClass())
+            throw new IllegalArgumentException("Invalid type of Token.compareTo() argument.");
+
+        return token.compareTo(((ComparableObjectToken<C>) o).token);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/LocalToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/LocalToken.java b/src/java/org/apache/cassandra/dht/LocalToken.java
index 6a0db2e..29e98eb 100644
--- a/src/java/org/apache/cassandra/dht/LocalToken.java
+++ b/src/java/org/apache/cassandra/dht/LocalToken.java
@@ -21,7 +21,7 @@ import java.nio.ByteBuffer;
 
 import org.apache.cassandra.db.marshal.AbstractType;
 
-public class LocalToken extends AbstractToken<ByteBuffer>
+public class LocalToken extends ComparableObjectToken<ByteBuffer>
 {
     static final long serialVersionUID = 8437543776403014875L;
 
@@ -43,23 +43,4 @@ public class LocalToken extends AbstractToken<ByteBuffer>
     {
         return comparator.compare(token, ((LocalToken) o).token);
     }
-
-    @Override
-    public int hashCode()
-    {
-        final int prime = 31;
-        return prime + token.hashCode();
-    }
-
-    @Override
-    public boolean equals(Object obj)
-    {
-        if (this == obj)
-            return true;
-        if (!(obj instanceof LocalToken))
-            return false;
-        LocalToken other = (LocalToken) obj;
-        return token.equals(other.token);
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/LongToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/LongToken.java b/src/java/org/apache/cassandra/dht/LongToken.java
index 8d4f721..8d60653 100644
--- a/src/java/org/apache/cassandra/dht/LongToken.java
+++ b/src/java/org/apache/cassandra/dht/LongToken.java
@@ -17,17 +17,46 @@
  */
 package org.apache.cassandra.dht;
 
-public class LongToken extends AbstractToken<Long>
+import com.google.common.primitives.Longs;
+
+public class LongToken extends Token
 {
     static final long serialVersionUID = -5833580143318243006L;
 
-    public LongToken(Long token)
+    final long token;
+
+    public LongToken(long token)
+    {
+        this.token = token;
+    }
+
+    public String toString()
     {
-        super(token);
+        return Long.toString(token);
+    }
+
+    public boolean equals(Object obj)
+    {
+        if (this == obj)
+            return true;
+        if (obj == null || this.getClass() != obj.getClass())
+            return false;
+
+        return token == (((LongToken)obj).token);
+    }
+
+    public int hashCode()
+    {
+        return Longs.hashCode(token);
     }
 
     public int compareTo(Token o)
     {
-        return token.compareTo(((LongToken) o).token);
+        return Long.compare(token, ((LongToken) o).token);
+    }
+
+    public Long getTokenValue()
+    {
+        return token;
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/Murmur3Partitioner.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/Murmur3Partitioner.java b/src/java/org/apache/cassandra/dht/Murmur3Partitioner.java
index 995a434..cf91c86 100644
--- a/src/java/org/apache/cassandra/dht/Murmur3Partitioner.java
+++ b/src/java/org/apache/cassandra/dht/Murmur3Partitioner.java
@@ -174,8 +174,7 @@ public class Murmur3Partitioner extends AbstractPartitioner
 
         public String toString(Token token)
         {
-            LongToken longToken = (LongToken) token;
-            return longToken.token.toString();
+            return token.toString();
         }
 
         public void validate(String token) throws ConfigurationException

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/StringToken.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/StringToken.java b/src/java/org/apache/cassandra/dht/StringToken.java
index 4615181..f541313 100644
--- a/src/java/org/apache/cassandra/dht/StringToken.java
+++ b/src/java/org/apache/cassandra/dht/StringToken.java
@@ -18,7 +18,7 @@
 package org.apache.cassandra.dht;
 
 
-public class StringToken extends AbstractToken<String>
+public class StringToken extends ComparableObjectToken<String>
 {
     static final long serialVersionUID = 5464084395277974963L;
 
@@ -26,9 +26,4 @@ public class StringToken extends AbstractToken<String>
     {
         super(token);
     }
-
-    public int compareTo(Token o)
-    {
-        return token.compareTo(((StringToken) o).token);
-    }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/6eef6f78/src/java/org/apache/cassandra/dht/Token.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/dht/Token.java b/src/java/org/apache/cassandra/dht/Token.java
index dbeee96..98babf5 100644
--- a/src/java/org/apache/cassandra/dht/Token.java
+++ b/src/java/org/apache/cassandra/dht/Token.java
@@ -72,6 +72,8 @@ public abstract class Token implements RingPosition<Token>, Serializable
         }
     }
 
+    abstract public Object getTokenValue();
+
     public Token getToken()
     {
         return this;


Mime
View raw message