cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From slebre...@apache.org
Subject [4/4] Improve picking of comparator for composite column with CQL3
Date Mon, 09 Apr 2012 17:03:31 GMT
http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/config/ColumnDefinition.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/config/ColumnDefinition.java b/src/java/org/apache/cassandra/config/ColumnDefinition.java
index 795f1d2..fed1d52 100644
--- a/src/java/org/apache/cassandra/config/ColumnDefinition.java
+++ b/src/java/org/apache/cassandra/config/ColumnDefinition.java
@@ -48,39 +48,51 @@ public class ColumnDefinition
     private Map<String,String> index_options;
     private String index_name;
 
-    public ColumnDefinition(ByteBuffer name, AbstractType<?> validator, IndexType index_type,
Map<String, String> index_options, String index_name)
+    /*
+     * If the column comparator is a composite type, indicates to which
+     * component this definition refers to. If null, the definition refers to
+     * the full column name.
+     */
+    public final Integer componentIndex;
+
+    public ColumnDefinition(ByteBuffer name, AbstractType<?> validator, IndexType index_type,
Map<String, String> index_options, String index_name, Integer componentIndex)
     {
         assert name != null && validator != null;
         this.name = name;
         this.index_name = index_name;
         this.validator = validator;
-
+        this.componentIndex = componentIndex;
         this.setIndexType(index_type, index_options);
     }
 
-    public static ColumnDefinition ascii(String name)
+    public static ColumnDefinition ascii(String name, Integer cidx)
     {
-        return new ColumnDefinition(ByteBufferUtil.bytes(name), AsciiType.instance, null,
null, null);
+        return new ColumnDefinition(ByteBufferUtil.bytes(name), AsciiType.instance, null,
null, null, cidx);
     }
 
-    public static ColumnDefinition bool(String name)
+    public static ColumnDefinition bool(String name, Integer cidx)
     {
-        return new ColumnDefinition(ByteBufferUtil.bytes(name), BooleanType.instance, null,
null, null);
+        return new ColumnDefinition(ByteBufferUtil.bytes(name), BooleanType.instance, null,
null, null, cidx);
     }
 
-    public static ColumnDefinition utf8(String name)
+    public static ColumnDefinition utf8(String name, Integer cidx)
     {
-        return new ColumnDefinition(ByteBufferUtil.bytes(name), UTF8Type.instance, null,
null, null);
+        return new ColumnDefinition(ByteBufferUtil.bytes(name), UTF8Type.instance, null,
null, null, cidx);
     }
 
-    public static ColumnDefinition int32(String name)
+    public static ColumnDefinition int32(String name, Integer cidx)
     {
-        return new ColumnDefinition(ByteBufferUtil.bytes(name), Int32Type.instance, null,
null, null);
+        return new ColumnDefinition(ByteBufferUtil.bytes(name), Int32Type.instance, null,
null, null, cidx);
     }
 
-    public static ColumnDefinition double_(String name)
+    public static ColumnDefinition double_(String name, Integer cidx)
     {
-        return new ColumnDefinition(ByteBufferUtil.bytes(name), DoubleType.instance, null,
null, null);
+        return new ColumnDefinition(ByteBufferUtil.bytes(name), DoubleType.instance, null,
null, null, cidx);
+    }
+
+    public ColumnDefinition clone()
+    {
+        return new ColumnDefinition(name, validator, index_type, index_options, index_name,
componentIndex);
     }
 
     public ColumnDefinition clone()
@@ -105,6 +117,8 @@ public class ColumnDefinition
             return false;
         if (!name.equals(that.name))
             return false;
+        if (componentIndex != null ? !componentIndex.equals(that.componentIndex) : that.componentIndex
!= null)
+            return false;
         return !(validator != null ? !validator.equals(that.validator) : that.validator !=
null);
     }
 
@@ -116,6 +130,7 @@ public class ColumnDefinition
         result = 31 * result + (index_type != null ? index_type.hashCode() : 0);
         result = 31 * result + (index_options != null ? index_options.hashCode() : 0);
         result = 31 * result + (index_name != null ? index_name.hashCode() : 0);
+        result = 31 * result + (componentIndex != null ? componentIndex.hashCode() : 0);
         return result;
     }
 
@@ -141,7 +156,8 @@ public class ColumnDefinition
                                     TypeParser.parse(thriftColumnDef.validation_class),
                                     thriftColumnDef.index_type,
                                     thriftColumnDef.index_options,
-                                    thriftColumnDef.index_name);
+                                    thriftColumnDef.index_name,
+                                    null);
     }
 
     public static Map<ByteBuffer, ColumnDefinition> fromThrift(List<ColumnDef>
thriftDefs) throws ConfigurationException
@@ -186,6 +202,8 @@ public class ColumnDefinition
                                            : Column.create(json(index_options), timestamp,
cfName, comparator.getString(name), "index_options"));
         cf.addColumn(index_name == null ? DeletedColumn.create(ldt, timestamp, cfName, comparator.getString(name),
"index_name")
                                         : Column.create(index_name, timestamp, cfName, comparator.getString(name),
"index_name"));
+        cf.addColumn(componentIndex == null ? DeletedColumn.create(ldt, timestamp, cfName,
comparator.getString(name), "component_index")
+                                            : Column.create(componentIndex, timestamp, cfName,
comparator.getString(name), "component_index"));
     }
 
     public void apply(ColumnDefinition def, AbstractType<?> comparator)  throws ConfigurationException
@@ -201,6 +219,10 @@ public class ColumnDefinition
                 throw new ConfigurationException("Cannot modify index name");
         }
 
+        if ((componentIndex != null && !componentIndex.equals(def.componentIndex))
+         || (componentIndex == null && def.componentIndex != null))
+            throw new ConfigurationException(String.format("Cannot modify component index
for column %s", comparator.getString(name)));
+
         setValidator(def.getValidator());
         setIndexType(def.getIndexType(), def.getIndexOptions());
         setIndexName(def.getIndexName());
@@ -212,7 +234,7 @@ public class ColumnDefinition
      * @return Thrift-based deserialized representation of the column
      * @param row
      */
-    public static List<ColumnDefinition> fromSchema(Row row, AbstractType<?>
comparator)
+    public static List<ColumnDefinition> fromSchema(Row row, CFMetaData cfm)
     {
         if (row.cf == null)
             return Collections.emptyList();
@@ -225,6 +247,7 @@ public class ColumnDefinition
                 IndexType index_type = null;
                 Map<String,String> index_options = null;
                 String index_name = null;
+                Integer componentIndex = null;
 
                 if (result.has("index_type"))
                     index_type = IndexType.valueOf(result.getString("index_type"));
@@ -232,12 +255,15 @@ public class ColumnDefinition
                     index_options = FBUtilities.fromJsonMap(result.getString("index_options"));
                 if (result.has("index_name"))
                     index_name = result.getString("index_name");
+                if (result.has("component_index"))
+                    componentIndex = result.getInt("component_index");
 
-                cds.add(new ColumnDefinition(comparator.fromString(result.getString("column")),
+                cds.add(new ColumnDefinition(cfm.getColumnDefinitionComparator(componentIndex).fromString(result.getString("column")),
                                              TypeParser.parse(result.getString("validator")),
                                              index_type,
                                              index_options,
-                                             index_name));
+                                             index_name,
+                                             componentIndex));
             }
             catch (ConfigurationException e)
             {
@@ -269,6 +295,7 @@ public class ColumnDefinition
                ", validator=" + validator +
                ", index_type=" + index_type +
                ", index_name='" + index_name + '\'' +
+               (componentIndex != null ? ", component_index=" + componentIndex : "") +
                '}';
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/config/KSMetaData.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/config/KSMetaData.java b/src/java/org/apache/cassandra/config/KSMetaData.java
index 0673115..df7f304 100644
--- a/src/java/org/apache/cassandra/config/KSMetaData.java
+++ b/src/java/org/apache/cassandra/config/KSMetaData.java
@@ -295,7 +295,7 @@ public final class KSMetaData
         for (CFMetaData cfm : cfms.values())
         {
             Row columnRow = ColumnDefinition.readSchema(cfm.ksName, cfm.cfName);
-            for (ColumnDefinition cd : ColumnDefinition.fromSchema(columnRow, cfm.getColumnDefinitionComparator()))
+            for (ColumnDefinition cd : ColumnDefinition.fromSchema(columnRow, cfm))
                 cfm.column_metadata.put(cd.name, cd);
         }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/cql/AlterTableStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql/AlterTableStatement.java b/src/java/org/apache/cassandra/cql/AlterTableStatement.java
index 46493b9..4267340 100644
--- a/src/java/org/apache/cassandra/cql/AlterTableStatement.java
+++ b/src/java/org/apache/cassandra/cql/AlterTableStatement.java
@@ -88,6 +88,7 @@ public class AlterTableStatement
                                                              TypeParser.parse(validator),
                                                              null,
                                                              null,
+                                                             null,
                                                              null));
                 break;
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/cql/CreateColumnFamilyStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql/CreateColumnFamilyStatement.java b/src/java/org/apache/cassandra/cql/CreateColumnFamilyStatement.java
index edeb260..0895e02 100644
--- a/src/java/org/apache/cassandra/cql/CreateColumnFamilyStatement.java
+++ b/src/java/org/apache/cassandra/cql/CreateColumnFamilyStatement.java
@@ -133,7 +133,7 @@ public class CreateColumnFamilyStatement
                                           ? CFPropDefs.comparators.get(col.getValue())
                                           : col.getValue();
                 AbstractType<?> validator = TypeParser.parse(validatorClassName);
-                columnDefs.put(columnName, new ColumnDefinition(columnName, validator, null,
null, null));
+                columnDefs.put(columnName, new ColumnDefinition(columnName, validator, null,
null, null, null));
             }
             catch (ConfigurationException e)
             {

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java b/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java
index 8dc19a8..1ecbd8a 100644
--- a/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java
@@ -24,6 +24,7 @@ import java.util.*;
 import org.apache.cassandra.cql3.*;
 import org.apache.cassandra.config.*;
 import org.apache.cassandra.io.compress.CompressionParameters;
+import org.apache.cassandra.db.marshal.CompositeType;
 import org.apache.cassandra.service.MigrationManager;
 import org.apache.cassandra.thrift.CfDef;
 import org.apache.cassandra.thrift.ColumnDef;
@@ -78,7 +79,8 @@ public class AlterTableStatement extends SchemaAlteringStatement
                                                              CFPropDefs.parseType(validator),
                                                              null,
                                                              null,
-                                                             null));
+                                                             null,
+                                                             cfDef.isComposite ? ((CompositeType)meta.comparator).types.size()
- 1 : null));
                 break;
 
             case ALTER:

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/src/java/org/apache/cassandra/cql3/statements/CreateColumnFamilyStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/CreateColumnFamilyStatement.java
b/src/java/org/apache/cassandra/cql3/statements/CreateColumnFamilyStatement.java
index 0eeee8e..c21a622 100644
--- a/src/java/org/apache/cassandra/cql3/statements/CreateColumnFamilyStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/CreateColumnFamilyStatement.java
@@ -66,12 +66,13 @@ public class CreateColumnFamilyStatement extends SchemaAlteringStatement
     private Map<ByteBuffer, ColumnDefinition> getColumns() throws InvalidRequestException
     {
         Map<ByteBuffer, ColumnDefinition> columnDefs = new HashMap<ByteBuffer, ColumnDefinition>();
+        Integer componentIndex = comparator instanceof CompositeType ? ((CompositeType)comparator).types.size()
- 1 : null;
 
         for (Map.Entry<ColumnIdentifier, String> col : columns.entrySet())
         {
             AbstractType<?> validator = CFPropDefs.parseType(col.getValue());
 
-            columnDefs.put(col.getKey().key, new ColumnDefinition(col.getKey().key, validator,
null, null, null));
+            columnDefs.put(col.getKey().key, new ColumnDefinition(col.getKey().key, validator,
null, null, null, componentIndex));
         }
 
         return columnDefs;

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/test/unit/org/apache/cassandra/SchemaLoader.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/SchemaLoader.java b/test/unit/org/apache/cassandra/SchemaLoader.java
index 4edd8b0..8087ea6 100644
--- a/test/unit/org/apache/cassandra/SchemaLoader.java
+++ b/test/unit/org/apache/cassandra/SchemaLoader.java
@@ -129,6 +129,7 @@ public class SchemaLoader
             UTF8Type.instance,
             null,
             null,
+            null,
             null));
         Map<ByteBuffer, ColumnDefinition> utf8Column = new HashMap<ByteBuffer, ColumnDefinition>();
         utf8Column.put(UTF8Type.instance.fromString("fortytwo"), new ColumnDefinition(
@@ -136,6 +137,7 @@ public class SchemaLoader
             IntegerType.instance,
             null,
             null,
+            null,
             null));
 
         // Keyspace 1
@@ -307,7 +309,7 @@ public class SchemaLoader
                    {{
                         ByteBuffer cName = ByteBuffer.wrap("birthdate".getBytes(Charsets.UTF_8));
                         IndexType keys = withIdxType ? IndexType.KEYS : null;
-                        put(cName, new ColumnDefinition(cName, LongType.instance, keys, null,
withIdxType ? ByteBufferUtil.bytesToHex(cName) : null));
+                        put(cName, new ColumnDefinition(cName, LongType.instance, keys, null,
withIdxType ? ByteBufferUtil.bytesToHex(cName) : null, null));
                     }});
     }
     private static CFMetaData jdbcCFMD(String ksName, String cfName, AbstractType comp)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/test/unit/org/apache/cassandra/config/ColumnDefinitionTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/config/ColumnDefinitionTest.java b/test/unit/org/apache/cassandra/config/ColumnDefinitionTest.java
index 7e3d698..c7ba1df 100644
--- a/test/unit/org/apache/cassandra/config/ColumnDefinitionTest.java
+++ b/test/unit/org/apache/cassandra/config/ColumnDefinitionTest.java
@@ -37,12 +37,14 @@ public class ColumnDefinitionTest
                                                     BytesType.instance,
                                                     IndexType.KEYS,
                                                     null,
-                                                    "random index name 0");
+                                                    "random index name 0",
+                                                    null);
 
         ColumnDefinition cd1 = new ColumnDefinition(ByteBufferUtil.bytes("TestColumnDefinition1"),
                                                     LongType.instance,
                                                     null,
                                                     null,
+                                                    null,
                                                     null);
 
         testSerializeDeserialize(cd0);

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/test/unit/org/apache/cassandra/config/DefsTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/config/DefsTest.java b/test/unit/org/apache/cassandra/config/DefsTest.java
index 862483f..69db1f6 100644
--- a/test/unit/org/apache/cassandra/config/DefsTest.java
+++ b/test/unit/org/apache/cassandra/config/DefsTest.java
@@ -64,7 +64,7 @@ public class DefsTest extends SchemaLoader
         for (int i = 0; i < 5; i++)
         {
             ByteBuffer name = ByteBuffer.wrap(new byte[] { (byte)i });
-            indexes.put(name, new ColumnDefinition(name, BytesType.instance, IndexType.KEYS,
null, Integer.toString(i)));
+            indexes.put(name, new ColumnDefinition(name, BytesType.instance, IndexType.KEYS,
null, Integer.toString(i), null));
         }
         CFMetaData cfm = new CFMetaData("Keyspace1",
                                         "TestApplyCFM_CF",
@@ -91,7 +91,8 @@ public class DefsTest extends SchemaLoader
                                                             BytesType.instance,
                                                             IndexType.KEYS,
                                                             null,
-                                                            "5");
+                                                            "5",
+                                                            null);
         cfNew.addColumnDefinition(addIndexDef);
 
         // remove one.
@@ -99,7 +100,8 @@ public class DefsTest extends SchemaLoader
                                                                BytesType.instance,
                                                                IndexType.KEYS,
                                                                null,
-                                                               "0");
+                                                               "0",
+                                                               null);
         assert cfNew.removeColumnDefinition(removeIndexDef);
 
         cfm.apply(cfNew);
@@ -531,7 +533,7 @@ public class DefsTest extends SchemaLoader
         // drop the index
         CFMetaData meta = cfs.metadata.clone();
         ColumnDefinition cdOld = meta.getColumn_metadata().values().iterator().next();
-        ColumnDefinition cdNew = new ColumnDefinition(cdOld.name, cdOld.getValidator(), null,
null, null);
+        ColumnDefinition cdNew = new ColumnDefinition(cdOld.name, cdOld.getValidator(), null,
null, null, null);
         meta.columnMetadata(Collections.singletonMap(cdOld.name, cdNew));
         MigrationManager.announceColumnFamilyUpdate(meta);
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java b/test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java
index 18dea98..e3fed41 100644
--- a/test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java
+++ b/test/unit/org/apache/cassandra/db/ColumnFamilyStoreTest.java
@@ -458,7 +458,7 @@ public class ColumnFamilyStoreTest extends SchemaLoader
 
         ColumnFamilyStore cfs = table.getColumnFamilyStore("Indexed2");
         ColumnDefinition old = cfs.metadata.getColumn_metadata().get(ByteBufferUtil.bytes("birthdate"));
-        ColumnDefinition cd = new ColumnDefinition(old.name, old.getValidator(), IndexType.KEYS,
null, "birthdate_index");
+        ColumnDefinition cd = new ColumnDefinition(old.name, old.getValidator(), IndexType.KEYS,
null, "birthdate_index", null);
         Future<?> future = cfs.indexManager.addIndexedColumn(cd);
         future.get();
         // we had a bug (CASSANDRA-2244) where index would get created but not flushed --
check for that

http://git-wip-us.apache.org/repos/asf/cassandra/blob/1ea2a60e/test/unit/org/apache/cassandra/thrift/ThriftValidationTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/thrift/ThriftValidationTest.java b/test/unit/org/apache/cassandra/thrift/ThriftValidationTest.java
index 0166e4f..9c131f9 100644
--- a/test/unit/org/apache/cassandra/thrift/ThriftValidationTest.java
+++ b/test/unit/org/apache/cassandra/thrift/ThriftValidationTest.java
@@ -124,7 +124,7 @@ public class ThriftValidationTest extends SchemaLoader
         assert !gotException : "got unexpected ConfigurationException";
 
         // add a column with name = "id"
-        newMetadata.addColumnDefinition(ColumnDefinition.utf8("id"));
+        newMetadata.addColumnDefinition(ColumnDefinition.utf8("id", null));
 
         gotException = false;
 


Mime
View raw message