diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractType.java b/src/java/org/apache/cassandra/db/marshal/AbstractType.java index fd9bb6f5df..f7624b105e 100644 --- a/src/java/org/apache/cassandra/db/marshal/AbstractType.java +++ b/src/java/org/apache/cassandra/db/marshal/AbstractType.java @@ -4,8 +4,15 @@ import java.util.Comparator; import java.util.Collection; import org.apache.cassandra.db.IColumn; -import org.apache.cassandra.db.SuperColumn; +/** + * Specifies a Comparator for a specific type of byte[]. + * + * Note that empty byte[] are used to represent "start at the beginning" + * or "stop at the end" arguments to get_slice, so the Comparator + * should always handle those values even if they normally do not + * represent a valid byte[] for the type being compared. + */ public abstract class AbstractType implements Comparator { /** get a string representation of the bytes suitable for log messages */ diff --git a/src/java/org/apache/cassandra/db/marshal/LongType.java b/src/java/org/apache/cassandra/db/marshal/LongType.java index fcb1726b23..72f6c5990f 100644 --- a/src/java/org/apache/cassandra/db/marshal/LongType.java +++ b/src/java/org/apache/cassandra/db/marshal/LongType.java @@ -7,14 +7,13 @@ public class LongType extends AbstractType { public int compare(byte[] o1, byte[] o2) { - // TODO rm hack to support the "i'm going to pretend [] is an index entry if I didn't actually index anything" hack if (o1.length == 0) { return o2.length == 0 ? 0 : -1; } if (o2.length == 0) { - return -1; + return 1; } long L1 = ByteBuffer.wrap(o1).order(ByteOrder.LITTLE_ENDIAN).getLong(); diff --git a/src/java/org/apache/cassandra/db/marshal/UTF8Type.java b/src/java/org/apache/cassandra/db/marshal/UTF8Type.java index e4fb257d5c..4f39665e13 100644 --- a/src/java/org/apache/cassandra/db/marshal/UTF8Type.java +++ b/src/java/org/apache/cassandra/db/marshal/UTF8Type.java @@ -6,16 +6,6 @@ public class UTF8Type extends AbstractType { public int compare(byte[] o1, byte[] o2) { - // TODO rm hack to support the "i'm going to pretend [] is an index entry if I didn't actually index anything" hack - if (o1.length == 0) - { - return o2.length == 0 ? 0 : -1; - } - if (o2.length == 0) - { - return -1; - } - try { return new String(o1, "UTF-8").compareTo(new String(o2, "UTF-8")); diff --git a/src/java/org/apache/cassandra/db/marshal/UUIDType.java b/src/java/org/apache/cassandra/db/marshal/UUIDType.java index 87718e7742..3a0b702dbb 100644 --- a/src/java/org/apache/cassandra/db/marshal/UUIDType.java +++ b/src/java/org/apache/cassandra/db/marshal/UUIDType.java @@ -13,14 +13,13 @@ public class UUIDType extends AbstractType public int compare(byte[] o1, byte[] o2) { - // TODO rm hack to support the "i'm going to pretend [] is an index entry if I didn't actually index anything" hack if (o1.length == 0) { return o2.length == 0 ? 0 : -1; } if (o2.length == 0) { - return -1; + return 1; } return getUUID(o1).compareTo(getUUID(o2)); diff --git a/src/java/org/apache/cassandra/io/SequenceFile.java b/src/java/org/apache/cassandra/io/SequenceFile.java index 7f21616e24..8c23da1dac 100644 --- a/src/java/org/apache/cassandra/io/SequenceFile.java +++ b/src/java/org/apache/cassandra/io/SequenceFile.java @@ -258,7 +258,6 @@ public class SequenceFile if (columnIndexList.size() == 0) { /* if there is no column index, add an index entry that covers the full space. */ - // TODO can we remove this? it causes a lot of ugliness in everything that touches Marshal return Arrays.asList(new IndexHelper.ColumnIndexInfo(ArrayUtils.EMPTY_BYTE_ARRAY, 0, totalNumCols, comparator_)); } diff --git a/test/unit/org/apache/cassandra/db/marshal/AsciiTypeTest.java b/test/unit/org/apache/cassandra/db/marshal/AsciiTypeTest.java index e3aafabb2c..4b9ea6ce1c 100644 --- a/test/unit/org/apache/cassandra/db/marshal/AsciiTypeTest.java +++ b/test/unit/org/apache/cassandra/db/marshal/AsciiTypeTest.java @@ -4,8 +4,6 @@ import org.apache.commons.lang.ArrayUtils; import org.junit.Test; -import junit.framework.TestCase; - public class AsciiTypeTest { @Test @@ -13,7 +11,10 @@ public class AsciiTypeTest { AsciiType comparator = new AsciiType(); assert comparator.compare(ArrayUtils.EMPTY_BYTE_ARRAY, "asdf".getBytes()) < 0; + assert comparator.compare("asdf".getBytes(), ArrayUtils.EMPTY_BYTE_ARRAY) > 0; + assert comparator.compare(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.EMPTY_BYTE_ARRAY) == 0; assert comparator.compare("z".getBytes(), "a".getBytes()) > 0; + assert comparator.compare("a".getBytes(), "z".getBytes()) < 0; assert comparator.compare("asdf".getBytes(), "asdf".getBytes()) == 0; assert comparator.compare("asdz".getBytes(), "asdf".getBytes()) > 0; } diff --git a/test/unit/org/apache/cassandra/db/marshal/UTF8TypeTest.java b/test/unit/org/apache/cassandra/db/marshal/UTF8TypeTest.java new file mode 100644 index 0000000000..8ad26feaa2 --- /dev/null +++ b/test/unit/org/apache/cassandra/db/marshal/UTF8TypeTest.java @@ -0,0 +1,22 @@ +package org.apache.cassandra.db.marshal; + +import java.io.UnsupportedEncodingException; + +import org.apache.commons.lang.ArrayUtils; + +import org.junit.Test; + +public class UTF8TypeTest +{ + @Test + public void testCompare() throws UnsupportedEncodingException + { + UTF8Type comparator = new UTF8Type(); + assert comparator.compare(ArrayUtils.EMPTY_BYTE_ARRAY, "asdf".getBytes()) < 0; + assert comparator.compare("asdf".getBytes(), ArrayUtils.EMPTY_BYTE_ARRAY) > 0; + assert comparator.compare(ArrayUtils.EMPTY_BYTE_ARRAY, ArrayUtils.EMPTY_BYTE_ARRAY) == 0; + assert comparator.compare("z".getBytes("UTF-8"), "a".getBytes("UTF-8")) > 0; + assert comparator.compare("z".getBytes("UTF-8"), "z".getBytes("UTF-8")) == 0; + assert comparator.compare("a".getBytes("UTF-8"), "z".getBytes("UTF-8")) < 0; + } +}