fix crash when comparing supercolumns (since they cannot be sorted by time)

git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@758975 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Ellis 2009-03-27 02:19:24 +00:00
parent b6ae87ba00
commit abc75388a3
1 changed files with 85 additions and 89 deletions

View File

@ -28,127 +28,123 @@ import java.util.Comparator;
public class ColumnComparatorFactory
{
public static enum ComparatorType
{
NAME,
TIMESTAMP
}
public static enum ComparatorType
{
NAME,
TIMESTAMP
}
private static Comparator<IColumn> nameComparator_ = new ColumnNameComparator();
private static Comparator<IColumn> timestampComparator_ = new ColumnTimestampComparator();
private static Comparator<IColumn> nameComparator_ = new ColumnNameComparator();
private static Comparator<IColumn> timestampComparator_ = new ColumnTimestampComparator();
public static Comparator<IColumn> getComparator(ComparatorType comparatorType)
{
Comparator<IColumn> columnComparator = timestampComparator_;
public static Comparator<IColumn> getComparator(ComparatorType comparatorType)
{
Comparator<IColumn> columnComparator = timestampComparator_;
switch(comparatorType)
{
case NAME:
columnComparator = nameComparator_;
break;
switch (comparatorType)
{
case NAME:
columnComparator = nameComparator_;
break;
case TIMESTAMP:
case TIMESTAMP:
default:
columnComparator = timestampComparator_;
break;
}
default:
columnComparator = timestampComparator_;
break;
}
return columnComparator;
}
return columnComparator;
}
public static Comparator<IColumn> getComparator(int comparatorTypeInt)
{
ComparatorType comparatorType = ComparatorType.NAME;
public static Comparator<IColumn> getComparator(int comparatorTypeInt)
{
ComparatorType comparatorType = ComparatorType.NAME;
if(comparatorTypeInt == ComparatorType.NAME.ordinal())
{
comparatorType = ComparatorType.NAME;
}
else if(comparatorTypeInt == ComparatorType.TIMESTAMP.ordinal())
{
comparatorType = ComparatorType.TIMESTAMP;
}
return getComparator(comparatorType);
}
if (comparatorTypeInt == ComparatorType.NAME.ordinal())
{
comparatorType = ComparatorType.NAME;
}
else if (comparatorTypeInt == ComparatorType.TIMESTAMP.ordinal())
{
comparatorType = ComparatorType.TIMESTAMP;
}
return getComparator(comparatorType);
}
public static void main(String[] args)
{
IColumn col1 = new Column("Column-9");
IColumn col2 = new Column("Column-10");
System.out.println("Result of compare: " + getComparator(ComparatorType.NAME).compare(col1, col2));
}
}
abstract class AbstractColumnComparator implements Comparator<IColumn>, Serializable
{
protected ColumnComparatorFactory.ComparatorType comparatorType_;
protected ColumnComparatorFactory.ComparatorType comparatorType_;
public AbstractColumnComparator(ColumnComparatorFactory.ComparatorType comparatorType)
{
comparatorType_ = comparatorType;
}
public AbstractColumnComparator(ColumnComparatorFactory.ComparatorType comparatorType)
{
comparatorType_ = comparatorType;
}
ColumnComparatorFactory.ComparatorType getComparatorType()
{
return comparatorType_;
}
ColumnComparatorFactory.ComparatorType getComparatorType()
{
return comparatorType_;
}
}
class ColumnTimestampComparator extends AbstractColumnComparator
{
ColumnTimestampComparator()
{
super(ColumnComparatorFactory.ComparatorType.TIMESTAMP);
}
ColumnTimestampComparator()
{
super(ColumnComparatorFactory.ComparatorType.TIMESTAMP);
}
/* if the time-stamps are the same then sort by names */
/* if the time-stamps are the same then sort by names */
public int compare(IColumn column1, IColumn column2)
{
/* inverse sort by time to get hte latest first */
long result = column2.timestamp() - column1.timestamp();
int finalResult = 0;
if(result == 0)
{
result = column1.name().compareTo(column2.name());
}
if(result > 0)
{
finalResult = 1;
}
if( result < 0 )
{
finalResult = -1;
}
assert column1.getClass() == column2.getClass();
/* inverse sort by time to get hte latest first */
long result = column2.timestamp() - column1.timestamp();
int finalResult = 0;
if (result == 0)
{
result = column1.name().compareTo(column2.name());
}
if (result > 0)
{
finalResult = 1;
}
if (result < 0)
{
finalResult = -1;
}
return finalResult;
}
}
class ColumnNameComparator extends AbstractColumnComparator
{
ColumnNameComparator()
{
super(ColumnComparatorFactory.ComparatorType.NAME);
}
ColumnNameComparator()
{
super(ColumnComparatorFactory.ComparatorType.NAME);
}
/* if the names are the same then sort by time-stamps */
public int compare(IColumn column1, IColumn column2)
{
long result = column1.name().compareTo(column2.name());
int finalResult = 0;
if(result == 0)
{
/* inverse sort by time to get hte latest first */
result = column2.timestamp() - column1.timestamp();
}
if(result > 0)
{
finalResult = 1;
}
if( result < 0 )
{
finalResult = -1;
}
assert column1.getClass() == column2.getClass();
long result = column1.name().compareTo(column2.name());
int finalResult = 0;
if (result == 0 && (column1 instanceof Column))
{
/* inverse sort by time to get the latest first */
result = column2.timestamp() - column1.timestamp();
}
if (result > 0)
{
finalResult = 1;
}
if (result < 0)
{
finalResult = -1;
}
return finalResult;
}
}