mirror of https://github.com/apache/cassandra
fix bug in comparing-to-empty-array; realize that we need compare-to-empty-array to specify start/finish of slices.
patch by jbellis; reviewed by Stu Hood for CASSANDRA-119 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@798273 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
38d81587ac
commit
e3aef8e0ed
|
|
@ -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<byte[]>
|
||||
{
|
||||
/** get a string representation of the bytes suitable for log messages */
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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"));
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -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_));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue