Author: gdusbabek
Date: Wed Apr 14 19:53:33 2010
New Revision: 934156
URL: http://svn.apache.org/viewvc?rev=934156&view=rev
Log:
add ByteOrderedPartitioner. Patch by Stu Hood, reviewed by Gary Dusbabek.
CASSANDRA-767
Added:
cassandra/trunk/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
- copied, changed from r934155,
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
cassandra/trunk/src/java/org/apache/cassandra/dht/ByteOrderedPartitioner.java
cassandra/trunk/test/unit/org/apache/cassandra/dht/ByteOrderedPartitionerTest.java
Modified:
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
cassandra/trunk/test/unit/org/apache/cassandra/dht/CollatingOrderPreservingPartitionerTest.java
cassandra/trunk/test/unit/org/apache/cassandra/dht/PartitionerTestCase.java
Copied:
cassandra/trunk/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
(from r934155,
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java)
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java?p2=cassandra/trunk/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java&p1=cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java&r1=934155&r2=934156&rev=934156&view=diff
==============================================================================
---
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
(original)
+++
cassandra/trunk/src/java/org/apache/cassandra/dht/AbstractByteOrderedPartitioner.java
Wed Apr 14 19:53:33 2010
@@ -31,10 +31,8 @@ import org.apache.cassandra.db.Decorated
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.Pair;
-public class CollatingOrderPreservingPartitioner implements
IPartitioner<BytesToken>
+public abstract class AbstractByteOrderedPartitioner implements
IPartitioner<BytesToken>
{
- static final Collator collator = Collator.getInstance(new Locale("en",
"US"));
-
public static final BytesToken MINIMUM = new
BytesToken(ArrayUtils.EMPTY_BYTE_ARRAY);
public static final BigInteger BYTE_MASK = new BigInteger("255");
@@ -146,11 +144,5 @@ public class CollatingOrderPreservingPar
return true;
}
- public BytesToken getToken(byte[] key)
- {
- if (key.length == 0)
- return MINIMUM;
- String skey = new String(key, FBUtilities.UTF8);
- return new BytesToken(collator.getCollationKey(skey).toByteArray());
- }
+ public abstract BytesToken getToken(byte[] key);
}
Added:
cassandra/trunk/src/java/org/apache/cassandra/dht/ByteOrderedPartitioner.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/dht/ByteOrderedPartitioner.java?rev=934156&view=auto
==============================================================================
---
cassandra/trunk/src/java/org/apache/cassandra/dht/ByteOrderedPartitioner.java
(added)
+++
cassandra/trunk/src/java/org/apache/cassandra/dht/ByteOrderedPartitioner.java
Wed Apr 14 19:53:33 2010
@@ -0,0 +1,42 @@
+/**
+ * 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;
+
+import java.math.BigInteger;
+import java.text.Collator;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.Locale;
+import java.util.Random;
+
+import org.apache.commons.lang.ArrayUtils;
+
+import org.apache.cassandra.db.DecoratedKey;
+import org.apache.cassandra.utils.FBUtilities;
+import org.apache.cassandra.utils.Pair;
+
+public class ByteOrderedPartitioner extends AbstractByteOrderedPartitioner
+{
+ public BytesToken getToken(byte[] key)
+ {
+ if (key.length == 0)
+ return MINIMUM;
+ return new BytesToken(key);
+ }
+}
Modified:
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java?rev=934156&r1=934155&r2=934156&view=diff
==============================================================================
---
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
(original)
+++
cassandra/trunk/src/java/org/apache/cassandra/dht/CollatingOrderPreservingPartitioner.java
Wed Apr 14 19:53:33 2010
@@ -31,121 +31,10 @@ import org.apache.cassandra.db.Decorated
import org.apache.cassandra.utils.FBUtilities;
import org.apache.cassandra.utils.Pair;
-public class CollatingOrderPreservingPartitioner implements
IPartitioner<BytesToken>
+public class CollatingOrderPreservingPartitioner extends
AbstractByteOrderedPartitioner
{
static final Collator collator = Collator.getInstance(new Locale("en",
"US"));
- public static final BytesToken MINIMUM = new
BytesToken(ArrayUtils.EMPTY_BYTE_ARRAY);
-
- public static final BigInteger BYTE_MASK = new BigInteger("255");
-
- public DecoratedKey<BytesToken> decorateKey(byte[] key)
- {
- return new DecoratedKey<BytesToken>(getToken(key), key);
- }
-
- public DecoratedKey<BytesToken> convertFromDiskFormat(byte[] key)
- {
- return new DecoratedKey<BytesToken>(getToken(key), key);
- }
-
- public byte[] convertToDiskFormat(DecoratedKey<BytesToken> key)
- {
- return key.key;
- }
-
- public BytesToken midpoint(BytesToken ltoken, BytesToken rtoken)
- {
- 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));
- }
-
- /**
- * Convert a byte array containing the most significant of 'sigbytes' bytes
- * representing a big-endian magnitude into a BigInteger.
- */
- private BigInteger bigForBytes(byte[] bytes, int sigbytes)
- {
- if (bytes.length != sigbytes)
- {
- // append zeros
- bytes = Arrays.copyOf(bytes, sigbytes);
- }
- return new BigInteger(1, bytes);
- }
-
- /**
- * Convert a (positive) BigInteger into a byte array representing its
magnitude.
- * If remainder is true, an additional byte with the high order bit enabled
- * will be added to the end of the array
- */
- private byte[] bytesForBig(BigInteger big, int sigbytes, boolean remainder)
- {
- byte[] bytes = new byte[sigbytes + (remainder ? 1 : 0)];
- if (remainder)
- {
- // remaining bit is the most significant in the last byte
- bytes[sigbytes] |= 0x80;
- }
- // bitmask for a single byte
- for (int i = 0; i < sigbytes; i++)
- {
- int maskpos = 8 * (sigbytes - (i + 1));
- // apply bitmask and get byte value
- bytes[i] =
(byte)(big.and(BYTE_MASK.shiftLeft(maskpos)).shiftRight(maskpos).intValue() &
0xFF);
- }
- return bytes;
- }
-
- public BytesToken getMinimumToken()
- {
- return MINIMUM;
- }
-
- public BytesToken getRandomToken()
- {
- Random r = new Random();
- byte[] buffer = new byte[16];
- r.nextBytes(buffer);
- return new BytesToken(buffer);
- }
-
- private final Token.TokenFactory<byte[]> tokenFactory = new
Token.TokenFactory<byte[]>() {
- public byte[] toByteArray(Token<byte[]> bytesToken)
- {
- return bytesToken.token;
- }
-
- public Token<byte[]> fromByteArray(byte[] bytes)
- {
- return new BytesToken(bytes);
- }
-
- public String toString(Token<byte[]> bytesToken)
- {
- return FBUtilities.bytesToHex(bytesToken.token);
- }
-
- public Token<byte[]> fromString(String string)
- {
- return new BytesToken(FBUtilities.hexToBytes(string));
- }
- };
-
- public Token.TokenFactory<byte[]> getTokenFactory()
- {
- return tokenFactory;
- }
-
- public boolean preservesOrder()
- {
- return true;
- }
-
public BytesToken getToken(byte[] key)
{
if (key.length == 0)
Added:
cassandra/trunk/test/unit/org/apache/cassandra/dht/ByteOrderedPartitionerTest.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/dht/ByteOrderedPartitionerTest.java?rev=934156&view=auto
==============================================================================
---
cassandra/trunk/test/unit/org/apache/cassandra/dht/ByteOrderedPartitionerTest.java
(added)
+++
cassandra/trunk/test/unit/org/apache/cassandra/dht/ByteOrderedPartitionerTest.java
Wed Apr 14 19:53:33 2010
@@ -0,0 +1,32 @@
+/*
+* 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;
+
+import org.junit.Test;
+
+import org.apache.cassandra.utils.FBUtilities;
+
+public class ByteOrderedPartitionerTest extends PartitionerTestCase<BytesToken>
+{
+ @Override
+ public void initPartitioner()
+ {
+ partitioner = new ByteOrderedPartitioner();
+ }
+}
Modified:
cassandra/trunk/test/unit/org/apache/cassandra/dht/CollatingOrderPreservingPartitionerTest.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/dht/CollatingOrderPreservingPartitionerTest.java?rev=934156&r1=934155&r2=934156&view=diff
==============================================================================
---
cassandra/trunk/test/unit/org/apache/cassandra/dht/CollatingOrderPreservingPartitionerTest.java
(original)
+++
cassandra/trunk/test/unit/org/apache/cassandra/dht/CollatingOrderPreservingPartitionerTest.java
Wed Apr 14 19:53:33 2010
@@ -22,7 +22,8 @@ import org.junit.Test;
import org.apache.cassandra.utils.FBUtilities;
-public class CollatingOrderPreservingPartitionerTest extends
PartitionerTestCase<BytesToken> {
+public class CollatingOrderPreservingPartitionerTest extends
PartitionerTestCase<BytesToken>
+{
@Override
public void initPartitioner()
{
Modified:
cassandra/trunk/test/unit/org/apache/cassandra/dht/PartitionerTestCase.java
URL:
http://svn.apache.org/viewvc/cassandra/trunk/test/unit/org/apache/cassandra/dht/PartitionerTestCase.java?rev=934156&r1=934155&r2=934156&view=diff
==============================================================================
--- cassandra/trunk/test/unit/org/apache/cassandra/dht/PartitionerTestCase.java
(original)
+++ cassandra/trunk/test/unit/org/apache/cassandra/dht/PartitionerTestCase.java
Wed Apr 14 19:53:33 2010
@@ -27,7 +27,8 @@ import org.junit.Test;
import org.apache.cassandra.db.DecoratedKey;
-public abstract class PartitionerTestCase<T extends Token> {
+public abstract class PartitionerTestCase<T extends Token>
+{
protected IPartitioner<T> partitioner;
public abstract void initPartitioner();
@@ -38,9 +39,14 @@ public abstract class PartitionerTestCas
initPartitioner();
}
- public T tok(String string)
+ public T tok(byte... key)
{
- return partitioner.getToken(string.getBytes());
+ return partitioner.getToken(key);
+ }
+
+ public T tok(String key)
+ {
+ return tok(key.getBytes());
}
/**