From abc75388a3df8de72b28a5edb830e1c19ed9ec80 Mon Sep 17 00:00:00 2001 From: Jonathan Ellis Date: Fri, 27 Mar 2009 02:19:24 +0000 Subject: [PATCH] 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 --- .../cassandra/db/ColumnComparatorFactory.java | 174 +++++++++--------- 1 file changed, 85 insertions(+), 89 deletions(-) diff --git a/src/org/apache/cassandra/db/ColumnComparatorFactory.java b/src/org/apache/cassandra/db/ColumnComparatorFactory.java index 1a43151b58..62dae8d86a 100644 --- a/src/org/apache/cassandra/db/ColumnComparatorFactory.java +++ b/src/org/apache/cassandra/db/ColumnComparatorFactory.java @@ -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 nameComparator_ = new ColumnNameComparator(); - private static Comparator timestampComparator_ = new ColumnTimestampComparator(); + private static Comparator nameComparator_ = new ColumnNameComparator(); + private static Comparator timestampComparator_ = new ColumnTimestampComparator(); - public static Comparator getComparator(ComparatorType comparatorType) - { - Comparator columnComparator = timestampComparator_; + public static Comparator getComparator(ComparatorType comparatorType) + { + Comparator 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 getComparator(int comparatorTypeInt) - { - ComparatorType comparatorType = ComparatorType.NAME; + public static Comparator 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, 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; } }