Merge branch 'cassandra-1.2' into trunk Conflicts: bin/cqlsh src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/59172810 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/59172810 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/59172810 Branch: refs/heads/trunk Commit: 5917281000bf51d10ffade5ac357f9cbf81daee6 Parents: 5dad160 24f6387 Author: Aleksey Yeschenko <alek...@apache.org> Authored: Wed May 1 20:03:26 2013 +0300 Committer: Aleksey Yeschenko <alek...@apache.org> Committed: Wed May 1 20:03:26 2013 +0300 ---------------------------------------------------------------------- CHANGES.txt | 3 + bin/cqlsh | 41 +++++++++++++-- pylib/cqlshlib/cql3handling.py | 6 ++- .../cql3/statements/AlterTableStatement.java | 27 ++++++++++ 4 files changed, 71 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/59172810/CHANGES.txt ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/59172810/bin/cqlsh ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/59172810/pylib/cqlshlib/cql3handling.py ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/59172810/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java ---------------------------------------------------------------------- diff --cc src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java index 945d202,c6af2a0..03e1b4b --- a/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java +++ b/src/java/org/apache/cassandra/cql3/statements/AlterTableStatement.java @@@ -186,18 -191,81 +190,41 @@@ public class AlterTableStatement extend cfProps.applyToCFMetadata(cfm); break; case RENAME: - - if (cfm.getKeyAliases().size() < cfDef.keys.size() && !renamesAllAliases(cfDef, renames.keySet(), CFDefinition.Name.Kind.KEY_ALIAS, cfDef.keys.size())) ++ if (cfm.partitionKeyColumns().size() < cfDef.keys.size() && !renamesAllAliases(cfDef, renames.keySet(), CFDefinition.Name.Kind.KEY_ALIAS, cfDef.keys.size())) + throw new InvalidRequestException("When upgrading from Thrift, all the columns of the (composite) partition key must be renamed together."); - if (cfm.getColumnAliases().size() < cfDef.columns.size() && !renamesAllAliases(cfDef, renames.keySet(), CFDefinition.Name.Kind.COLUMN_ALIAS, cfDef.columns.size())) ++ if (cfm.clusteringKeyColumns().size() < cfDef.columns.size() && !renamesAllAliases(cfDef, renames.keySet(), CFDefinition.Name.Kind.COLUMN_ALIAS, cfDef.columns.size())) + throw new InvalidRequestException("When upgrading from Thrift, all the columns of the (composite) clustering key must be renamed together."); + for (Map.Entry<ColumnIdentifier, ColumnIdentifier> entry : renames.entrySet()) { - CFDefinition.Name from = cfDef.get(entry.getKey()); + ColumnIdentifier from = entry.getKey(); ColumnIdentifier to = entry.getValue(); - if (from == null) - throw new InvalidRequestException(String.format("Column %s was not found in table %s", entry.getKey(), columnFamily())); - - CFDefinition.Name exists = cfDef.get(to); - if (exists != null) - throw new InvalidRequestException(String.format("Cannot rename column %s in table %s to %s; another column of that name already exist", from, columnFamily(), to)); - - switch (from.kind) - { - case KEY_ALIAS: - cfm.keyAliases(rename(from.position, to, cfm.getKeyAliases())); - break; - case COLUMN_ALIAS: - cfm.columnAliases(rename(from.position, to, cfm.getColumnAliases())); - break; - case VALUE_ALIAS: - cfm.valueAlias(to.key); - break; - case COLUMN_METADATA: - throw new InvalidRequestException(String.format("Cannot rename non PRIMARY KEY part %s", from)); - } + cfm.renameColumn(from.key, from.toString(), to.key, to.toString()); } break; } - MigrationManager.announceColumnFamilyUpdate(cfm); + MigrationManager.announceColumnFamilyUpdate(cfm, false); } + private static boolean renamesAllAliases(CFDefinition cfDef, Set<ColumnIdentifier> names, CFDefinition.Name.Kind kind, int expected) + { + int renamed = Sets.filter(names, isA(cfDef, kind)).size(); + return renamed == 0 || renamed == expected; + } + + private static Predicate<ColumnIdentifier> isA(final CFDefinition cfDef, final CFDefinition.Name.Kind kind) + { + return new Predicate<ColumnIdentifier>() + { + public boolean apply(ColumnIdentifier input) + { + CFDefinition.Name name = cfDef.get(input); + return name != null && name.kind == kind; + } + }; + } + - private static List<ByteBuffer> rename(int pos, ColumnIdentifier newName, List<ByteBuffer> aliases) - { - if (pos < aliases.size()) - { - List<ByteBuffer> newList = new ArrayList<ByteBuffer>(aliases); - newList.set(pos, newName.key); - return newList; - } - else - { - // We insert nulls temporarly, but have checked that all the aliases are renamed - List<ByteBuffer> newList = new ArrayList<ByteBuffer>(pos + 1); - for (int i = 0; i < pos; ++i) - newList.add(i < aliases.size() ? aliases.get(i) : null); - newList.add(newName.key); - return newList; - } - } - public String toString() { return String.format("AlterTableStatement(name=%s, type=%s, column=%s, validator=%s)",