You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by jb...@apache.org on 2009/03/27 17:36:37 UTC
svn commit: r759214 -
/incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java
Author: jbellis
Date: Fri Mar 27 16:36:36 2009
New Revision: 759214
URL: http://svn.apache.org/viewvc?rev=759214&view=rev
Log:
fix crash when comparing supercolumns (since they cannot be sorted by time)
Modified:
incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java
Modified: incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java?rev=759214&r1=759213&r2=759214&view=diff
==============================================================================
--- incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java (original)
+++ incubator/cassandra/trunk/src/org/apache/cassandra/db/ColumnComparatorFactory.java Fri Mar 27 16:36:36 2009
@@ -28,127 +28,123 @@
public class ColumnComparatorFactory
{
- public static enum ComparatorType
- {
- NAME,
- TIMESTAMP
- }
-
- 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_;
-
- switch(comparatorType)
- {
- case NAME:
- columnComparator = nameComparator_;
- break;
-
- case TIMESTAMP:
-
- default:
- columnComparator = timestampComparator_;
- break;
- }
-
- return columnComparator;
- }
-
- 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);
- }
-
- 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));
- }
+ public static enum ComparatorType
+ {
+ NAME,
+ TIMESTAMP
+ }
+
+ 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_;
+
+ switch (comparatorType)
+ {
+ case NAME:
+ columnComparator = nameComparator_;
+ break;
+
+ case TIMESTAMP:
+
+ default:
+ columnComparator = timestampComparator_;
+ break;
+ }
+
+ return columnComparator;
+ }
+
+ 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);
+ }
+
}
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;
}
}