From 6092b01e329246fc524400aaced63c82d55e017a Mon Sep 17 00:00:00 2001 From: Benedict Elliott Smith Date: Thu, 2 Jul 2015 08:52:03 +0100 Subject: [PATCH] Enforce simple << complex sort order more strictly and efficiently patch by benedict; reviewed by slebresne for CASSANDRA-9701 --- .../cassandra/config/ColumnDefinition.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/cassandra/config/ColumnDefinition.java b/src/java/org/apache/cassandra/config/ColumnDefinition.java index ea008168d2..d6605a72d8 100644 --- a/src/java/org/apache/cassandra/config/ColumnDefinition.java +++ b/src/java/org/apache/cassandra/config/ColumnDefinition.java @@ -54,6 +54,7 @@ public class ColumnDefinition extends ColumnSpecification implements Comparable< { return this == PARTITION_KEY || this == CLUSTERING_COLUMN; } + } public final Kind kind; @@ -72,6 +73,17 @@ public class ColumnDefinition extends ColumnSpecification implements Comparable< private final Comparator cellPathComparator; private final Comparator cellComparator; + /** + * These objects are compared frequently, so we encode several of their comparison components + * into a single int value so that this can be done efficiently + */ + private final int comparisonOrder; + + private static int comparisonOrder(Kind kind, boolean isComplex, int position) + { + return (kind.ordinal() << 28) | (isComplex ? 1 << 27 : 0) | position; + } + public static ColumnDefinition partitionKeyDef(CFMetaData cfm, ByteBuffer name, AbstractType validator, Integer componentIndex) { return new ColumnDefinition(cfm, name, validator, componentIndex, Kind.PARTITION_KEY); @@ -145,6 +157,7 @@ public class ColumnDefinition extends ColumnSpecification implements Comparable< this.setIndexType(indexType, indexOptions); this.cellPathComparator = makeCellPathComparator(kind, validator); this.cellComparator = makeCellComparator(cellPathComparator); + this.comparisonOrder = comparisonOrder(kind, isComplex(), position()); } private static Comparator makeCellPathComparator(Kind kind, AbstractType validator) @@ -399,15 +412,8 @@ public class ColumnDefinition extends ColumnSpecification implements Comparable< if (this == other) return 0; - if (kind != other.kind) - return kind.ordinal() < other.kind.ordinal() ? -1 : 1; - if (position() != other.position()) - return position() < other.position() ? -1 : 1; - - if (isStatic() != other.isStatic()) - return isStatic() ? -1 : 1; - if (isComplex() != other.isComplex()) - return isComplex() ? 1 : -1; + if (comparisonOrder != other.comparisonOrder) + return comparisonOrder - other.comparisonOrder; return ByteBufferUtil.compareUnsigned(name.bytes, other.name.bytes); }