kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | fd4220a09b4ea156892ad64848e5353f85a1c6a8b06f527743616a15082dc3a6 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-24 17:24 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-24 17:24 +02:00 |
| Parents | e039b363026c, e82ad000a95f |
12 files changed
modified changelog/README.md +3 −0
| @@ -2,6 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | ### 3.0.0-alpha2 (in progress) |
| 4 | 4 | |
| 5 | +- [new feature] Move secondary index metadata out of column definitions | |
| 6 | + (JAVA-875, JAVA-882). | |
| 7 | + | |
| 5 | 8 | Merged from 2.2 branch: |
| 6 | 9 | |
| 7 | 10 | - [bug] Propagate CodecRegistry to nested UDTs (JAVA-847) |
modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +32 −202
| @@ -15,13 +15,13 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.util.Arrays; | |
| 19 | -import java.util.Collections; | |
| 20 | -import java.util.HashMap; | |
| 21 | -import java.util.Iterator; | |
| 22 | -import java.util.Map; | |
| 18 | +import java.util.*; | |
| 23 | 19 | import java.util.Map.Entry; |
| 24 | 20 | |
| 21 | +import com.google.common.base.Function; | |
| 22 | +import com.google.common.collect.ImmutableMap; | |
| 23 | +import com.google.common.collect.Maps; | |
| 24 | + | |
| 25 | 25 | /** |
| 26 | 26 | * Describes a Column. |
| 27 | 27 | */ |
| @@ -35,30 +35,29 @@ | ||
| 35 | 35 | static final String INDEX_TYPE = "index_type"; |
| 36 | 36 | static final String INDEX_OPTIONS = "index_options"; |
| 37 | 37 | static final String INDEX_NAME = "index_name"; |
| 38 | - private static final String CUSTOM_INDEX_CLASS = "class_name"; | |
| 39 | 38 | |
| 40 | - private static final String INDEX_MAP_KEYS = "index_keys"; | |
| 41 | - private static final String INDEX_MAP_ENTRIES = "index_keys_and_values"; | |
| 42 | 39 | private final TableMetadata table; |
| 43 | 40 | private final String name; |
| 44 | 41 | private final DataType type; |
| 45 | - private final IndexMetadata index; | |
| 46 | 42 | private final boolean isStatic; |
| 47 | 43 | |
| 48 | - private ColumnMetadata(TableMetadata table, String name, DataType type, boolean isStatic, Map<String, String> indexColumns) { | |
| 44 | + // this is the "reverse" side of the many-to-many relationship | |
| 45 | + // between columns and indexes, and is updated only after the column is created | |
| 46 | + final Map<String, IndexMetadata> indexes = new LinkedHashMap<String, IndexMetadata>(); | |
| 47 | + | |
| 48 | + private ColumnMetadata(TableMetadata table, String name, DataType type, boolean isStatic) { | |
| 49 | 49 | this.table = table; |
| 50 | 50 | this.name = name; |
| 51 | 51 | this.type = type; |
| 52 | 52 | this.isStatic = isStatic; |
| 53 | - this.index = IndexMetadata.build(this, indexColumns); | |
| 54 | 53 | } |
| 55 | 54 | |
| 56 | 55 | static ColumnMetadata fromRaw(TableMetadata tm, Raw raw) { |
| 57 | - return new ColumnMetadata(tm, raw.name, raw.dataType, raw.kind == Raw.Kind.STATIC, raw.indexColumns); | |
| 56 | + return new ColumnMetadata(tm, raw.name, raw.dataType, raw.kind == Raw.Kind.STATIC); | |
| 58 | 57 | } |
| 59 | 58 | |
| 60 | 59 | static ColumnMetadata forAlias(TableMetadata tm, String name, DataType type) { |
| 61 | - return new ColumnMetadata(tm, name, type, false, Collections.<String, String>emptyMap()); | |
| 60 | + return new ColumnMetadata(tm, name, type, false); | |
| 62 | 61 | } |
| 63 | 62 | |
| 64 | 63 | /** |
| @@ -88,16 +87,6 @@ | ||
| 88 | 87 | return type; |
| 89 | 88 | } |
| 90 | 89 | |
| 91 | - /** | |
| 92 | - * Returns the indexing metadata on this column if the column is indexed. | |
| 93 | - * | |
| 94 | - * @return the metadata on the column index if the column is indexed, | |
| 95 | - * {@code null} otherwise. | |
| 96 | - */ | |
| 97 | - public IndexMetadata getIndex() { | |
| 98 | - return index; | |
| 99 | - } | |
| 100 | - | |
| 101 | 90 | /** |
| 102 | 91 | * Whether this column is a static column. |
| 103 | 92 | * |
| @@ -108,186 +97,25 @@ | ||
| 108 | 97 | } |
| 109 | 98 | |
| 110 | 99 | /** |
| 111 | - * Metadata on a column index. | |
| 100 | + * Returns metadata on a index on this column. | |
| 101 | + * | |
| 102 | + * @param name the name of the index to retrieve ({@code name} will be | |
| 103 | + * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 104 | + * see {@link Metadata#quote}). | |
| 105 | + * @return the metadata for the {@code name} index if it exists, or | |
| 106 | + * {@code null} otherwise. | |
| 112 | 107 | */ |
| 113 | - public static class IndexMetadata { | |
| 114 | - | |
| 115 | - private final ColumnMetadata column; | |
| 116 | - private final String name; | |
| 117 | - private final Map<String, String> indexOptions; | |
| 118 | - | |
| 119 | - private IndexMetadata(ColumnMetadata column, String name) { | |
| 120 | - this(column, name, null); | |
| 121 | - } | |
| 122 | - | |
| 123 | - private IndexMetadata(ColumnMetadata column, String name, Map<String, String> indexOptions) { | |
| 124 | - this.column = column; | |
| 125 | - this.name = name; | |
| 126 | - this.indexOptions = indexOptions; | |
| 127 | - } | |
| 128 | - | |
| 129 | - /** | |
| 130 | - * Returns the column this index metadata refers to. | |
| 131 | - * | |
| 132 | - * @return the column this index metadata refers to. | |
| 133 | - */ | |
| 134 | - public ColumnMetadata getIndexedColumn() { | |
| 135 | - return column; | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Returns the index name. | |
| 140 | - * | |
| 141 | - * @return the index name. | |
| 142 | - */ | |
| 143 | - public String getName() { | |
| 144 | - return name; | |
| 145 | - } | |
| 146 | - | |
| 147 | - /** | |
| 148 | - * Returns whether this index is a custom one. | |
| 149 | - * <p> | |
| 150 | - * If it is indeed a custom index, {@link #getIndexClassName} will | |
| 151 | - * return the name of the class used in Cassandra to implement that | |
| 152 | - * index. | |
| 153 | - * | |
| 154 | - * @return {@code true} if this metadata represents a custom index. | |
| 155 | - */ | |
| 156 | - public boolean isCustomIndex() { | |
| 157 | - return getIndexClassName() != null; | |
| 158 | - } | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * The name of the class used to implement the custom index, if it is one. | |
| 162 | - * | |
| 163 | - * @return the name of the class used Cassandra side to implement this | |
| 164 | - * custom index if {@code isCustomIndex() == true}, {@code null} otherwise. | |
| 165 | - */ | |
| 166 | - public String getIndexClassName() { | |
| 167 | - return getOption(CUSTOM_INDEX_CLASS); | |
| 168 | - } | |
| 169 | - | |
| 170 | - /** | |
| 171 | - * Return whether this index is a 'KEYS' index on a map, e.g., | |
| 172 | - * CREATE INDEX ON mytable (KEYS(mymap)) | |
| 173 | - * | |
| 174 | - * @return {@code true} if this is a 'KEYS' index on a map. | |
| 175 | - */ | |
| 176 | - public boolean isKeys() { | |
| 177 | - return getOption(INDEX_MAP_KEYS) != null; | |
| 178 | - } | |
| 179 | - | |
| 180 | - /** | |
| 181 | - * Return whether this index is a 'FULL' index on a frozen collection, e.g., | |
| 182 | - * CREATE INDEX ON mytable (FULL(mymap)) | |
| 183 | - * | |
| 184 | - * @return {@code true} if this is a 'FULL' index on a frozen collection. | |
| 185 | - */ | |
| 186 | - public boolean isFull() { | |
| 187 | - /* | |
| 188 | - * This check is analogous to the Cassandra counterpart | |
| 189 | - * in IndexTarget#fromColumnDefinition. | |
| 190 | - */ | |
| 191 | - return !isKeys() | |
| 192 | - && !isEntries() | |
| 193 | - && column.getType() instanceof DataType.CollectionType | |
| 194 | - && column.getType().isFrozen(); | |
| 195 | - } | |
| 196 | - | |
| 197 | - /** | |
| 198 | - * Return whether this index is a 'ENTRIES' index on a map, e.g., | |
| 199 | - * CREATE INDEX ON mytable (ENTRIES(mymap)) | |
| 200 | - * | |
| 201 | - * @return {@code true} if this is an 'ENTRIES' index on a map. | |
| 202 | - */ | |
| 203 | - public boolean isEntries() { | |
| 204 | - return getOption(INDEX_MAP_ENTRIES) != null; | |
| 205 | - } | |
| 206 | - | |
| 207 | - /** | |
| 208 | - * Return the value for the given option name. | |
| 209 | - * | |
| 210 | - * @param name Option name | |
| 211 | - * @return Option value | |
| 212 | - */ | |
| 213 | - public String getOption(String name) { | |
| 214 | - return indexOptions != null ? indexOptions.get(name) : null; | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * Returns a CQL query representing this index. | |
| 219 | - * | |
| 220 | - * This method returns a single 'CREATE INDEX' query corresponding to | |
| 221 | - * this index definition. | |
| 222 | - * | |
| 223 | - * @return the 'CREATE INDEX' query corresponding to this index. | |
| 224 | - */ | |
| 225 | - public String asCQLQuery() { | |
| 226 | - TableMetadata table = column.getTable(); | |
| 227 | - String ksName = Metadata.escapeId(table.getKeyspace().getName()); | |
| 228 | - String cfName = Metadata.escapeId(table.getName()); | |
| 229 | - String colName = Metadata.escapeId(column.getName()); | |
| 230 | - return isCustomIndex() | |
| 231 | - ? String.format("CREATE CUSTOM INDEX %s ON %s.%s (%s) USING '%s' WITH OPTIONS = %s;", | |
| 232 | - name, ksName, cfName, colName, getIndexClassName(), getOptionsAsCql()) | |
| 233 | - : String.format("CREATE INDEX %s ON %s.%s (%s);", name, ksName, cfName, getIndexFunction(colName)); | |
| 234 | - } | |
| 235 | - | |
| 236 | - /** | |
| 237 | - * Builds a string representation of the custom index options. | |
| 238 | - * | |
| 239 | - * @return String representation of the custom index options, similar to what Cassandra stores in | |
| 240 | - * the 'index_options' column of the 'schema_columns' table in the 'system' keyspace. | |
| 241 | - */ | |
| 242 | - private String getOptionsAsCql() { | |
| 243 | - StringBuilder builder = new StringBuilder(); | |
| 244 | - builder.append("{"); | |
| 245 | - Iterator<Entry<String, String>> it = indexOptions.entrySet().iterator(); | |
| 246 | - while (it.hasNext()) { | |
| 247 | - Entry<String, String> option = it.next(); | |
| 248 | - builder.append(String.format("'%s' : '%s'", option.getKey(), option.getValue())); | |
| 249 | - if (it.hasNext()) | |
| 250 | - builder.append(", "); | |
| 251 | - } | |
| 252 | - builder.append("}"); | |
| 253 | - return builder.toString(); | |
| 254 | - } | |
| 255 | - | |
| 256 | - /** | |
| 257 | - * Wraps the column name with the appropriate index function (KEYS, FULL, ENTRIES), | |
| 258 | - * if necessary. | |
| 259 | - * | |
| 260 | - * @return Column name wrapped with the appropriate index function. | |
| 261 | - */ | |
| 262 | - private String getIndexFunction(String colName) { | |
| 263 | - if (isKeys()) | |
| 264 | - return String.format("KEYS(%s)", colName); | |
| 265 | - else if (isFull()) | |
| 266 | - return String.format("FULL(%s)", colName); | |
| 267 | - else if (isEntries()) | |
| 268 | - return String.format("ENTRIES(%s)", colName); | |
| 269 | - return colName; | |
| 270 | - } | |
| 271 | - | |
| 272 | - private static IndexMetadata build(ColumnMetadata column, Map<String, String> indexColumns) { | |
| 273 | - if (indexColumns.isEmpty()) | |
| 274 | - return null; | |
| 275 | - | |
| 276 | - String type = indexColumns.get(INDEX_TYPE); | |
| 277 | - if (type == null) | |
| 278 | - return null; | |
| 279 | - | |
| 280 | - // Special case check for the value of the index_options column being a string with value 'null' as this | |
| 281 | - // column appears to be set this way (JAVA-834). | |
| 282 | - String indexOptionsCol = indexColumns.get(INDEX_OPTIONS); | |
| 283 | - if (indexOptionsCol == null || indexOptionsCol.isEmpty() || indexOptionsCol.equals("null")) | |
| 284 | - return new IndexMetadata(column, indexColumns.get(INDEX_NAME)); | |
| 285 | - else { | |
| 286 | - Map<String, String> indexOptions = SimpleJSONParser.parseStringMap(indexOptionsCol); | |
| 287 | - return new IndexMetadata(column, indexColumns.get(INDEX_NAME), indexOptions); | |
| 288 | - } | |
| 108 | + public IndexMetadata getIndex(String name) { | |
| 109 | + return indexes.get(Metadata.handleId(name)); | |
| 110 | + } | |
| 289 | 111 | |
| 290 | - } | |
| 112 | + /** | |
| 113 | + * Returns a list containing all the indexes on this column. | |
| 114 | + * | |
| 115 | + * @return a list containing the metadata for the indexes on this column. | |
| 116 | + */ | |
| 117 | + public List<IndexMetadata> getIndexes() { | |
| 118 | + return new ArrayList<IndexMetadata>(indexes.values()); | |
| 291 | 119 | } |
| 292 | 120 | |
| 293 | 121 | @Override |
| @@ -365,8 +193,10 @@ | ||
| 365 | 193 | DataType dataType = CassandraTypeParser.parseOne(validatorStr, protocolVersion, codecRegistry); |
| 366 | 194 | Raw c = new Raw(name, kind, componentIndex, dataType, reversed); |
| 367 | 195 | |
| 196 | + // secondary indexes (C* < 3.0.0) | |
| 197 | + // from C* 3.0 onwards 2i are defined in a separate table | |
| 368 | 198 | for (String str : Arrays.asList(INDEX_TYPE, INDEX_NAME, INDEX_OPTIONS)) |
| 369 | - if (!row.isNull(str)) | |
| 199 | + if (row.getColumnDefinitions().contains(str) && !row.isNull(str)) | |
| 370 | 200 | c.indexColumns.put(str, row.getString(str)); |
| 371 | 201 | |
| 372 | 202 | return c; |
added driver-core/src/main/java/com/datastax/driver/core/IndexMetadata.java +342 −0
| @@ -0,0 +1,342 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.*; | |
| 19 | + | |
| 20 | +import com.google.common.base.Function; | |
| 21 | +import com.google.common.base.Objects; | |
| 22 | +import com.google.common.collect.ImmutableMap; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * An immutable representation of secondary index metadata. | |
| 26 | + */ | |
| 27 | +public class IndexMetadata { | |
| 28 | + | |
| 29 | + public enum IndexType { | |
| 30 | + KEYS, | |
| 31 | + CUSTOM, | |
| 32 | + COMPOSITES | |
| 33 | + } | |
| 34 | + | |
| 35 | + public enum TargetType { | |
| 36 | + COLUMN, ROW | |
| 37 | + } | |
| 38 | + | |
| 39 | + public static final Function<IndexMetadata, String> INDEX_NAME = new Function<IndexMetadata, String>() { | |
| 40 | + @Override | |
| 41 | + public String apply(IndexMetadata input) { | |
| 42 | + return input.getName(); | |
| 43 | + } | |
| 44 | + }; | |
| 45 | + | |
| 46 | + public static final String CUSTOM_INDEX_OPTION_NAME = "class_name"; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * The name of the option used to specify that the index is on the collection keys. | |
| 50 | + */ | |
| 51 | + public static final String INDEX_KEYS_OPTION_NAME = "index_keys"; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * The name of the option used to specify that the index is on the collection values. | |
| 55 | + */ | |
| 56 | + public static final String INDEX_VALUES_OPTION_NAME = "index_values"; | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * The name of the option used to specify that the index is on the collection (map) entries. | |
| 60 | + */ | |
| 61 | + public static final String INDEX_ENTRIES_OPTION_NAME = "index_keys_and_values"; | |
| 62 | + | |
| 63 | + | |
| 64 | + private final TableMetadata table; | |
| 65 | + private final String name; | |
| 66 | + private final Map<String, ColumnMetadata> columns; | |
| 67 | + private final IndexType indexType; | |
| 68 | + private final TargetType targetType; | |
| 69 | + private final Map<String, String> options; | |
| 70 | + | |
| 71 | + private IndexMetadata(TableMetadata table, String name, Map<String, ColumnMetadata> columns, IndexType indexType, TargetType targetType, Map<String, String> options) { | |
| 72 | + this.table = table; | |
| 73 | + this.name = name; | |
| 74 | + this.columns = columns; | |
| 75 | + this.indexType = indexType; | |
| 76 | + this.targetType = targetType; | |
| 77 | + this.options = options; | |
| 78 | + } | |
| 79 | + | |
| 80 | + /** | |
| 81 | + * Build an IndexMetadata from a system_schema.indexes row. | |
| 82 | + */ | |
| 83 | + static IndexMetadata fromRow(TableMetadata table, Row indexRow) { | |
| 84 | + String name = indexRow.getString("index_name"); | |
| 85 | + Set<String> targetColumnNames = indexRow.getSet("target_columns", String.class); | |
| 86 | + LinkedHashMap<String, ColumnMetadata> targetColumns = new LinkedHashMap<String, ColumnMetadata>(targetColumnNames.size()); | |
| 87 | + for (String targetColumnName : targetColumnNames) { | |
| 88 | + targetColumns.put(targetColumnName, table.getColumn(targetColumnName)); | |
| 89 | + } | |
| 90 | + IndexMetadata.IndexType indexType = IndexMetadata.IndexType.valueOf(indexRow.getString("index_type")); | |
| 91 | + IndexMetadata.TargetType targetType = IndexMetadata.TargetType.valueOf(indexRow.getString("target_type")); | |
| 92 | + Map<String, String> options = indexRow.getMap("options", String.class, String.class); | |
| 93 | + return new IndexMetadata(table, name, targetColumns, indexType, targetType, options); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Build an IndexMetadata from a legacy layout (index information is stored | |
| 98 | + * along with indexed column). | |
| 99 | + */ | |
| 100 | + static IndexMetadata fromLegacy(ColumnMetadata column, ColumnMetadata.Raw raw) { | |
| 101 | + if (raw.indexColumns.isEmpty()) | |
| 102 | + return null; | |
| 103 | + String type = raw.indexColumns.get(ColumnMetadata.INDEX_TYPE); | |
| 104 | + if (type == null) | |
| 105 | + return null; | |
| 106 | + String indexName = raw.indexColumns.get(ColumnMetadata.INDEX_NAME); | |
| 107 | + String indexTypeStr = raw.indexColumns.get(ColumnMetadata.INDEX_TYPE); | |
| 108 | + IndexType indexType = indexTypeStr == null ? null : IndexType.valueOf(indexTypeStr); | |
| 109 | + // Special case check for the value of the index_options column being a string with value 'null' as this | |
| 110 | + // column appears to be set this way (JAVA-834). | |
| 111 | + String indexOptionsCol = raw.indexColumns.get(ColumnMetadata.INDEX_OPTIONS); | |
| 112 | + ImmutableMap<String, ColumnMetadata> columns = ImmutableMap.of(column.getName(), column); | |
| 113 | + Map<String, String> options; | |
| 114 | + if (indexOptionsCol == null || indexOptionsCol.isEmpty() || indexOptionsCol.equals("null")) { | |
| 115 | + options = ImmutableMap.of(); | |
| 116 | + } else { | |
| 117 | + options = SimpleJSONParser.parseStringMap(indexOptionsCol); | |
| 118 | + } | |
| 119 | + return new IndexMetadata(column.getTable(), indexName, columns, indexType, TargetType.COLUMN, options); | |
| 120 | + } | |
| 121 | + | |
| 122 | + /** | |
| 123 | + * Returns the metadata of the table this column is part of. | |
| 124 | + * | |
| 125 | + * @return the {@code TableMetadata} for the table this column is part of. | |
| 126 | + */ | |
| 127 | + public TableMetadata getTable() { | |
| 128 | + return table; | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Returns the index name. | |
| 133 | + * | |
| 134 | + * @return the index name. | |
| 135 | + */ | |
| 136 | + public String getName() { | |
| 137 | + return name; | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Returns metadata on a column of this index. | |
| 142 | + * | |
| 143 | + * @param name the name of the column to retrieve ({@code name} will be | |
| 144 | + * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 145 | + * see {@link Metadata#quote}). | |
| 146 | + * @return the metadata for the column if it exists, or | |
| 147 | + * {@code null} otherwise. | |
| 148 | + */ | |
| 149 | + public ColumnMetadata getColumn(String name) { | |
| 150 | + return columns.get(Metadata.handleId(name)); | |
| 151 | + } | |
| 152 | + | |
| 153 | + /** | |
| 154 | + * Returns a list containing all the columns of this index. | |
| 155 | + * | |
| 156 | + * The order of the columns in the list is consistent with | |
| 157 | + * the order of the columns in the index. | |
| 158 | + * | |
| 159 | + * @return a list containing the metadata for the columns of this table. | |
| 160 | + */ | |
| 161 | + public List<ColumnMetadata> getColumns() { | |
| 162 | + return new ArrayList<ColumnMetadata>(columns.values()); | |
| 163 | + } | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * Returns the index type. | |
| 167 | + * | |
| 168 | + * @return the index type. | |
| 169 | + */ | |
| 170 | + public IndexType getIndexType() { | |
| 171 | + return indexType; | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Returns the index target type. | |
| 176 | + * Note: for legacy indexes, this is always {@link TargetType#COLUMN}. | |
| 177 | + * | |
| 178 | + * @return the index target type. | |
| 179 | + */ | |
| 180 | + public TargetType getTargetType() { | |
| 181 | + return targetType; | |
| 182 | + } | |
| 183 | + | |
| 184 | + /** | |
| 185 | + * Returns whether this index is a custom one. | |
| 186 | + * <p> | |
| 187 | + * If it is indeed a custom index, {@link #getIndexClassName} will | |
| 188 | + * return the name of the class used in Cassandra to implement that | |
| 189 | + * index. | |
| 190 | + * | |
| 191 | + * @return {@code true} if this metadata represents a custom index. | |
| 192 | + */ | |
| 193 | + public boolean isCustomIndex() { | |
| 194 | + return getIndexClassName() != null; | |
| 195 | + } | |
| 196 | + | |
| 197 | + /** | |
| 198 | + * The name of the class used to implement the custom index, if it is one. | |
| 199 | + * | |
| 200 | + * @return the name of the class used Cassandra side to implement this | |
| 201 | + * custom index if {@code isCustomIndex() == true}, {@code null} otherwise. | |
| 202 | + */ | |
| 203 | + public String getIndexClassName() { | |
| 204 | + return getOption(CUSTOM_INDEX_OPTION_NAME); | |
| 205 | + } | |
| 206 | + | |
| 207 | + /** | |
| 208 | + * Return whether this index is a 'KEYS' index on a map, e.g., | |
| 209 | + * CREATE INDEX ON mytable (KEYS(mymap)) | |
| 210 | + * | |
| 211 | + * @return {@code true} if this is a 'KEYS' index on a map. | |
| 212 | + */ | |
| 213 | + public boolean isKeys() { | |
| 214 | + return getOption(INDEX_KEYS_OPTION_NAME) != null; | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * Return whether this index is a 'VALUES' index on a map, e.g., | |
| 219 | + * CREATE INDEX ON mytable (VALUES(mymap)) | |
| 220 | + * | |
| 221 | + * @return {@code true} if this is an 'VALUES' index on a map. | |
| 222 | + */ | |
| 223 | + public boolean isValues() { | |
| 224 | + return getOption(INDEX_VALUES_OPTION_NAME) != null; | |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Return whether this index is a 'ENTRIES' index on a map, e.g., | |
| 229 | + * CREATE INDEX ON mytable (ENTRIES(mymap)) | |
| 230 | + * | |
| 231 | + * @return {@code true} if this is an 'ENTRIES' index on a map. | |
| 232 | + */ | |
| 233 | + public boolean isEntries() { | |
| 234 | + return getOption(INDEX_ENTRIES_OPTION_NAME) != null; | |
| 235 | + } | |
| 236 | + | |
| 237 | + /** | |
| 238 | + * Return whether this index is a 'FULL' index on a frozen collection, e.g., | |
| 239 | + * CREATE INDEX ON mytable (FULL(mymap)) | |
| 240 | + * | |
| 241 | + * @return {@code true} if this is a 'FULL' index on a frozen collection. | |
| 242 | + */ | |
| 243 | + public boolean isFull() { | |
| 244 | + /* | |
| 245 | + * This check is analogous to the Cassandra counterpart | |
| 246 | + * in IndexTarget. | |
| 247 | + */ | |
| 248 | + ColumnMetadata column = columns.values().iterator().next(); | |
| 249 | + return | |
| 250 | + !isKeys() | |
| 251 | + && !isValues() | |
| 252 | + && !isEntries() | |
| 253 | + && column.getType() instanceof DataType.CollectionType | |
| 254 | + && column.getType().isFrozen(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Return the value for the given option name. | |
| 259 | + * | |
| 260 | + * @param name Option name | |
| 261 | + * @return Option value | |
| 262 | + */ | |
| 263 | + public String getOption(String name) { | |
| 264 | + return options != null ? options.get(name) : null; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Returns a CQL query representing this index. | |
| 269 | + * | |
| 270 | + * This method returns a single 'CREATE INDEX' query corresponding to | |
| 271 | + * this index definition. | |
| 272 | + * | |
| 273 | + * @return the 'CREATE INDEX' query corresponding to this index. | |
| 274 | + */ | |
| 275 | + public String asCQLQuery() { | |
| 276 | + TableMetadata table = getTable(); | |
| 277 | + String ksName = Metadata.escapeId(table.getKeyspace().getName()); | |
| 278 | + String cfName = Metadata.escapeId(table.getName()); | |
| 279 | + // TODO indexes on multiple columns | |
| 280 | + String colName = Metadata.escapeId(columns.keySet().iterator().next()); | |
| 281 | + return isCustomIndex() | |
| 282 | + ? String.format("CREATE CUSTOM INDEX %s ON %s.%s (%s) USING '%s' WITH OPTIONS = %s;", name, ksName, cfName, colName, getIndexClassName(), getOptionsAsCql()) | |
| 283 | + : String.format("CREATE INDEX %s ON %s.%s (%s);", name, ksName, cfName, getIndexFunction(colName)); | |
| 284 | + } | |
| 285 | + | |
| 286 | + /** | |
| 287 | + * Builds a string representation of the custom index options. | |
| 288 | + * | |
| 289 | + * @return String representation of the custom index options, similar to what Cassandra stores in | |
| 290 | + * the 'index_options' column of the 'schema_columns' table in the 'system' keyspace. | |
| 291 | + */ | |
| 292 | + private String getOptionsAsCql() { | |
| 293 | + StringBuilder builder = new StringBuilder(); | |
| 294 | + builder.append("{"); | |
| 295 | + Iterator<Map.Entry<String, String>> it = options.entrySet().iterator(); | |
| 296 | + while (it.hasNext()) { | |
| 297 | + Map.Entry<String, String> option = it.next(); | |
| 298 | + builder.append(String.format("'%s' : '%s'", option.getKey(), option.getValue())); | |
| 299 | + if (it.hasNext()) | |
| 300 | + builder.append(", "); | |
| 301 | + } | |
| 302 | + builder.append("}"); | |
| 303 | + return builder.toString(); | |
| 304 | + } | |
| 305 | + | |
| 306 | + /** | |
| 307 | + * Wraps the column name with the appropriate index function (KEYS, FULL, ENTRIES), | |
| 308 | + * if necessary. | |
| 309 | + * | |
| 310 | + * @return Column name wrapped with the appropriate index function. | |
| 311 | + */ | |
| 312 | + private String getIndexFunction(String colName) { | |
| 313 | + if (isKeys()) | |
| 314 | + return String.format("KEYS(%s)", colName); | |
| 315 | + else if (isFull()) | |
| 316 | + return String.format("FULL(%s)", colName); | |
| 317 | + else if (isEntries()) | |
| 318 | + return String.format("ENTRIES(%s)", colName); | |
| 319 | + return colName; | |
| 320 | + } | |
| 321 | + | |
| 322 | + public int hashCode() { | |
| 323 | + return Objects.hashCode(name, columns, indexType, targetType, options); | |
| 324 | + } | |
| 325 | + | |
| 326 | + public boolean equals(Object obj) { | |
| 327 | + if (obj == this) | |
| 328 | + return true; | |
| 329 | + | |
| 330 | + if (!(obj instanceof IndexMetadata)) | |
| 331 | + return false; | |
| 332 | + | |
| 333 | + IndexMetadata other = (IndexMetadata)obj; | |
| 334 | + | |
| 335 | + return Objects.equal(name, other.name) | |
| 336 | + && Objects.equal(columns, other.columns) | |
| 337 | + && Objects.equal(indexType, other.indexType) | |
| 338 | + && Objects.equal(targetType, other.targetType) | |
| 339 | + && Objects.equal(options, other.options); | |
| 340 | + } | |
| 341 | + | |
| 342 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +37 −7
| @@ -18,6 +18,8 @@ | ||
| 18 | 18 | import java.util.*; |
| 19 | 19 | import java.util.concurrent.ExecutionException; |
| 20 | 20 | |
| 21 | +import com.google.common.collect.Lists; | |
| 22 | +import com.google.common.collect.Maps; | |
| 21 | 23 | import org.slf4j.Logger; |
| 22 | 24 | import org.slf4j.LoggerFactory; |
| 23 | 25 | |
| @@ -117,7 +119,7 @@ | ||
| 117 | 119 | buildUserTypes(ksm, udtDefs.get(ksName), protocolVersion, codecRegistry); |
| 118 | 120 | } |
| 119 | 121 | if (cfDefs.containsKey(ksName)) { |
| 120 | - buildTableMetadata(ksm, cfDefs.get(ksName), colsDefs.get(ksName), cassandraVersion, protocolVersion, codecRegistry, CF_NAME); | |
| 122 | + buildTableMetadata(ksm, cfDefs.get(ksName), colsDefs.get(ksName), null, cassandraVersion, protocolVersion, codecRegistry, CF_NAME); | |
| 121 | 123 | } |
| 122 | 124 | if (functionDefs.containsKey(ksName)) { |
| 123 | 125 | buildFunctionMetadata(ksm, functionDefs.get(ksName), protocolVersion, codecRegistry); |
| @@ -153,7 +155,7 @@ | ||
| 153 | 155 | switch (targetType) { |
| 154 | 156 | case TABLE: |
| 155 | 157 | if (cfDefs.containsKey(targetKeyspace)) |
| 156 | - buildTableMetadata(ksm, cfDefs.get(targetKeyspace), colsDefs.get(targetKeyspace), cassandraVersion, protocolVersion, codecRegistry, CF_NAME); | |
| 158 | + buildTableMetadata(ksm, cfDefs.get(targetKeyspace), colsDefs.get(targetKeyspace), null, cassandraVersion, protocolVersion, codecRegistry, CF_NAME); | |
| 157 | 159 | break; |
| 158 | 160 | case TYPE: |
| 159 | 161 | if (udtDefs.containsKey(targetKeyspace)) |
| @@ -194,6 +196,7 @@ | ||
| 194 | 196 | private static final String SELECT_USERTYPES = "SELECT * FROM system_schema.types"; |
| 195 | 197 | private static final String SELECT_FUNCTIONS = "SELECT * FROM system_schema.functions"; |
| 196 | 198 | private static final String SELECT_AGGREGATES = "SELECT * FROM system_schema.aggregates"; |
| 199 | + private static final String SELECT_INDEXES = "SELECT * FROM system_schema.indexes"; | |
| 197 | 200 | |
| 198 | 201 | private static final String TABLE_NAME = "table_name"; |
| 199 | 202 | |
| @@ -225,7 +228,8 @@ | ||
| 225 | 228 | cfFuture = null, |
| 226 | 229 | colsFuture = null, |
| 227 | 230 | functionsFuture = null, |
| 228 | - aggregatesFuture = null; | |
| 231 | + aggregatesFuture = null, | |
| 232 | + indexesFuture = null; | |
| 229 | 233 | |
| 230 | 234 | if (isSchemaOrKeyspace) |
| 231 | 235 | ksFuture = queryAsync(SELECT_KEYSPACES + whereClause, connection, protocolVersion); |
| @@ -236,6 +240,7 @@ | ||
| 236 | 240 | if (isSchemaOrKeyspace || targetType == TABLE) { |
| 237 | 241 | cfFuture = queryAsync(SELECT_TABLES + whereClause, connection, protocolVersion); |
| 238 | 242 | colsFuture = queryAsync(SELECT_COLUMNS + whereClause, connection, protocolVersion); |
| 243 | + indexesFuture = queryAsync(SELECT_INDEXES + whereClause, connection, protocolVersion); | |
| 239 | 244 | } |
| 240 | 245 | |
| 241 | 246 | if ((isSchemaOrKeyspace || targetType == FUNCTION)) |
| @@ -250,6 +255,7 @@ | ||
| 250 | 255 | Map<String, Map<String, Map<String, ColumnMetadata.Raw>>> colsDefs = groupByKeyspaceAndCf(get(colsFuture), cassandraVersion, protocolVersion, codecRegistry, TABLE_NAME); |
| 251 | 256 | Map<String, List<Row>> functionDefs = groupByKeyspace(get(functionsFuture)); |
| 252 | 257 | Map<String, List<Row>> aggregateDefs = groupByKeyspace(get(aggregatesFuture)); |
| 258 | + Map<String, Map<String, List<Row>>> indexDefs = groupByKeyspaceAndCf(get(indexesFuture), TABLE_NAME); | |
| 253 | 259 | |
| 254 | 260 | metadata.lock.lock(); |
| 255 | 261 | try { |
| @@ -264,7 +270,7 @@ | ||
| 264 | 270 | buildUserTypes(ksm, udtDefs.get(ksName), protocolVersion, codecRegistry); |
| 265 | 271 | } |
| 266 | 272 | if (cfDefs.containsKey(ksName)) { |
| 267 | - buildTableMetadata(ksm, cfDefs.get(ksName), colsDefs.get(ksName), cassandraVersion, protocolVersion, codecRegistry, TABLE_NAME); | |
| 273 | + buildTableMetadata(ksm, cfDefs.get(ksName), colsDefs.get(ksName), indexDefs.get(ksName), cassandraVersion, protocolVersion, codecRegistry, TABLE_NAME); | |
| 268 | 274 | } |
| 269 | 275 | if (functionDefs.containsKey(ksName)) { |
| 270 | 276 | buildFunctionMetadata(ksm, functionDefs.get(ksName), protocolVersion, codecRegistry); |
| @@ -300,7 +306,7 @@ | ||
| 300 | 306 | switch (targetType) { |
| 301 | 307 | case TABLE: |
| 302 | 308 | if (cfDefs.containsKey(targetKeyspace)) |
| 303 | - buildTableMetadata(ksm, cfDefs.get(targetKeyspace), colsDefs.get(targetKeyspace), cassandraVersion, protocolVersion, codecRegistry, TABLE_NAME); | |
| 309 | + buildTableMetadata(ksm, cfDefs.get(targetKeyspace), colsDefs.get(targetKeyspace), indexDefs.get(targetKeyspace), cassandraVersion, protocolVersion, codecRegistry, TABLE_NAME); | |
| 304 | 310 | break; |
| 305 | 311 | case TYPE: |
| 306 | 312 | if (udtDefs.containsKey(targetKeyspace)) |
| @@ -324,7 +330,7 @@ | ||
| 324 | 330 | } |
| 325 | 331 | }; |
| 326 | 332 | |
| 327 | - static void buildTableMetadata(KeyspaceMetadata ksm, List<Row> cfRows, Map<String, Map<String, ColumnMetadata.Raw>> colsDefs, VersionNumber cassandraVersion, ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String tableName) { | |
| 333 | + static void buildTableMetadata(KeyspaceMetadata ksm, List<Row> cfRows, Map<String, Map<String, ColumnMetadata.Raw>> colsDefs, Map<String, List<Row>> ksIndexes, VersionNumber cassandraVersion, ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String tableName) { | |
| 328 | 334 | for (Row cfRow : cfRows) { |
| 329 | 335 | String cfName = cfRow.getString(tableName); |
| 330 | 336 | try { |
| @@ -348,7 +354,8 @@ | ||
| 348 | 354 | cols = Collections.emptyMap(); |
| 349 | 355 | } |
| 350 | 356 | } |
| 351 | - ksm.add(TableMetadata.build(ksm, cfRow, cols, tableName, cassandraVersion, protocolVersion, codecRegistry)); | |
| 357 | + List<Row> cfIndexes = (ksIndexes == null) ? null : ksIndexes.get(cfName); | |
| 358 | + ksm.add(TableMetadata.build(ksm, cfRow, cols, cfIndexes, tableName, cassandraVersion, protocolVersion, codecRegistry)); | |
| 352 | 359 | } catch (RuntimeException e) { |
| 353 | 360 | // See ControlConnection#refreshSchema for why we'd rather not probably this further |
| 354 | 361 | logger.error(String.format("Error parsing schema for table %s.%s: " |
| @@ -390,6 +397,29 @@ | ||
| 390 | 397 | return result; |
| 391 | 398 | } |
| 392 | 399 | |
| 400 | + static Map<String, Map<String, List<Row>>> groupByKeyspaceAndCf(ResultSet rs, String tableName) { | |
| 401 | + if (rs == null) | |
| 402 | + return Collections.emptyMap(); | |
| 403 | + | |
| 404 | + Map<String, Map<String, List<Row>>> result = Maps.newHashMap(); | |
| 405 | + for (Row row : rs) { | |
| 406 | + String ksName = row.getString(KeyspaceMetadata.KS_NAME); | |
| 407 | + String cfName = row.getString(tableName); | |
| 408 | + Map<String, List<Row>> rowsByCf = result.get(ksName); | |
| 409 | + if (rowsByCf == null) { | |
| 410 | + rowsByCf = Maps.newHashMap(); | |
| 411 | + result.put(ksName, rowsByCf); | |
| 412 | + } | |
| 413 | + List<Row> l = rowsByCf.get(cfName); | |
| 414 | + if (l == null) { | |
| 415 | + l = Lists.newArrayList(); | |
| 416 | + rowsByCf.put(cfName, l); | |
| 417 | + } | |
| 418 | + l.add(row); | |
| 419 | + } | |
| 420 | + return result; | |
| 421 | + } | |
| 422 | + | |
| 393 | 423 | static Map<String, Map<String, Map<String, ColumnMetadata.Raw>>> groupByKeyspaceAndCf(ResultSet rs, VersionNumber cassandraVersion, ProtocolVersion protocolVersion, CodecRegistry codecRegistry, String tableName) { |
| 394 | 424 | if (rs == null) |
| 395 | 425 | return Collections.emptyMap(); |
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +85 −22
| @@ -64,9 +64,11 @@ | ||
| 64 | 64 | private final List<ColumnMetadata> partitionKey; |
| 65 | 65 | private final List<ColumnMetadata> clusteringColumns; |
| 66 | 66 | private final Map<String, ColumnMetadata> columns; |
| 67 | + private final Map<String, IndexMetadata> indexes; | |
| 67 | 68 | private final Options options; |
| 68 | 69 | private final List<Order> clusteringOrder; |
| 69 | 70 | |
| 71 | + | |
| 70 | 72 | private final VersionNumber cassandraVersion; |
| 71 | 73 | |
| 72 | 74 | /** |
| @@ -90,7 +92,8 @@ | ||
| 90 | 92 | UUID id, |
| 91 | 93 | List<ColumnMetadata> partitionKey, |
| 92 | 94 | List<ColumnMetadata> clusteringColumns, |
| 93 | - LinkedHashMap<String, ColumnMetadata> columns, | |
| 95 | + Map<String, ColumnMetadata> columns, | |
| 96 | + Map<String, IndexMetadata> indexes, | |
| 94 | 97 | Options options, |
| 95 | 98 | List<Order> clusteringOrder, |
| 96 | 99 | VersionNumber cassandraVersion) { |
| @@ -100,27 +103,28 @@ | ||
| 100 | 103 | this.partitionKey = partitionKey; |
| 101 | 104 | this.clusteringColumns = clusteringColumns; |
| 102 | 105 | this.columns = columns; |
| 106 | + this.indexes = indexes; | |
| 103 | 107 | this.options = options; |
| 104 | 108 | this.clusteringOrder = clusteringOrder; |
| 105 | 109 | this.cassandraVersion = cassandraVersion; |
| 106 | 110 | } |
| 107 | 111 | |
| 108 | - static TableMetadata build(KeyspaceMetadata ksm, Row row, Map<String, ColumnMetadata.Raw> rawCols, String nameColumn, VersionNumber cassandraVersion, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 112 | + static TableMetadata build(KeyspaceMetadata ksm, Row row, Map<String, ColumnMetadata.Raw> rawCols, List<Row> indexRows, String nameColumn, VersionNumber cassandraVersion, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 109 | 113 | |
| 110 | 114 | String name = row.getString(nameColumn); |
| 111 | 115 | |
| 112 | 116 | UUID id = null; |
| 113 | 117 | |
| 114 | - if(cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1) | |
| 118 | + if (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1) | |
| 115 | 119 | id = row.getUUID(CF_ID_V2); |
| 116 | - else if(cassandraVersion.getMajor() > 2) | |
| 120 | + else if (cassandraVersion.getMajor() > 2) | |
| 117 | 121 | id = row.getUUID(CF_ID_V3); |
| 118 | 122 | |
| 119 | 123 | CassandraTypeParser.ParseResult comparator = null; |
| 120 | 124 | CassandraTypeParser.ParseResult keyValidator = null; |
| 121 | 125 | List<String> columnAliases = null; |
| 122 | 126 | |
| 123 | - if(cassandraVersion.getMajor() <= 2) { | |
| 127 | + if (cassandraVersion.getMajor() <= 2) { | |
| 124 | 128 | comparator = CassandraTypeParser.parseWithComposite(row.getString(COMPARATOR), protocolVersion, codecRegistry); |
| 125 | 129 | keyValidator = CassandraTypeParser.parseWithComposite(row.getString(KEY_VALIDATOR), protocolVersion, codecRegistry); |
| 126 | 130 | columnAliases = cassandraVersion.getMajor() >= 2 || row.getString(COLUMN_ALIASES) == null |
| @@ -133,17 +137,17 @@ | ||
| 133 | 137 | |
| 134 | 138 | boolean isDense; |
| 135 | 139 | boolean isCompact; |
| 136 | - if(cassandraVersion.getMajor() > 2) { | |
| 140 | + if (cassandraVersion.getMajor() > 2) { | |
| 137 | 141 | Set<String> flags = row.getSet(FLAGS, String.class); |
| 138 | 142 | isDense = flags.contains(DENSE); |
| 139 | 143 | boolean isSuper = flags.contains(SUPER); |
| 140 | 144 | boolean isCompound = flags.contains(COMPOUND); |
| 141 | 145 | isCompact = isSuper || isDense || !isCompound; |
| 142 | 146 | boolean isStaticCompact = !isSuper && !isDense && !isCompound; |
| 143 | - if(isStaticCompact) { | |
| 147 | + if (isStaticCompact) { | |
| 144 | 148 | rawCols = pruneStaticCompactTableColumns(rawCols); |
| 145 | 149 | } |
| 146 | - if(isDense) { | |
| 150 | + if (isDense) { | |
| 147 | 151 | rawCols = pruneDenseTableColumnsV3(rawCols); |
| 148 | 152 | } |
| 149 | 153 | clusteringSize = findClusteringSize(comparator, rawCols.values(), columnAliases, cassandraVersion); |
| @@ -151,7 +155,7 @@ | ||
| 151 | 155 | assert comparator != null; |
| 152 | 156 | clusteringSize = findClusteringSize(comparator, rawCols.values(), columnAliases, cassandraVersion); |
| 153 | 157 | isDense = clusteringSize != comparator.types.size() - 1; |
| 154 | - if(isDense) { | |
| 158 | + if (isDense) { | |
| 155 | 159 | rawCols = pruneDenseTableColumnsV2(rawCols); |
| 156 | 160 | } |
| 157 | 161 | isCompact = isDense || !comparator.isComposite; |
| @@ -163,6 +167,7 @@ | ||
| 163 | 167 | |
| 164 | 168 | // We use a linked hashmap because we will keep this in the order of a 'SELECT * FROM ...'. |
| 165 | 169 | LinkedHashMap<String, ColumnMetadata> columns = new LinkedHashMap<String, ColumnMetadata>(); |
| 170 | + LinkedHashMap<String, IndexMetadata> indexes = new LinkedHashMap<String, IndexMetadata>(); | |
| 166 | 171 | |
| 167 | 172 | Options options = null; |
| 168 | 173 | try { |
| @@ -171,11 +176,11 @@ | ||
| 171 | 176 | // See ControlConnection#refreshSchema for why we'd rather not probably this further. Since table options is one thing |
| 172 | 177 | // that tends to change often in Cassandra, it's worth special casing this. |
| 173 | 178 | logger.error(String.format("Error parsing schema options for table %s.%s: " |
| 174 | - + "Cluster.getMetadata().getKeyspace(\"%s\").getTable(\"%s\").getOptions() will return null", | |
| 175 | - ksm.getName(), name, ksm.getName(), name), e); | |
| 179 | + + "Cluster.getMetadata().getKeyspace(\"%s\").getTable(\"%s\").getOptions() will return null", | |
| 180 | + ksm.getName(), name, ksm.getName(), name), e); | |
| 176 | 181 | } |
| 177 | 182 | |
| 178 | - TableMetadata tm = new TableMetadata(ksm, name, id, partitionKey, clusteringColumns, columns, options, clusteringOrder, cassandraVersion); | |
| 183 | + TableMetadata tm = new TableMetadata(ksm, name, id, partitionKey, clusteringColumns, columns, indexes, options, clusteringOrder, cassandraVersion); | |
| 179 | 184 | |
| 180 | 185 | // We use this temporary set just so non PK columns are added in lexicographical order, which is the one of a |
| 181 | 186 | // 'SELECT * FROM ...' |
| @@ -190,8 +195,8 @@ | ||
| 190 | 195 | // In C* 1.2, only the REGULAR columns are in the columns schema table, so we need to add the names from |
| 191 | 196 | // the aliases (and make sure we handle default aliases). |
| 192 | 197 | List<String> keyAliases = row.getString(KEY_ALIASES) == null |
| 193 | - ? Collections.<String>emptyList() | |
| 194 | - : SimpleJSONParser.parseStringList(row.getString(KEY_ALIASES)); | |
| 198 | + ? Collections.<String>emptyList() | |
| 199 | + : SimpleJSONParser.parseStringList(row.getString(KEY_ALIASES)); | |
| 195 | 200 | for (int i = 0; i < partitionKey.size(); i++) { |
| 196 | 201 | String alias = keyAliases.size() > i ? keyAliases.get(i) : (i == 0 ? DEFAULT_KEY_ALIAS : DEFAULT_KEY_ALIAS + (i + 1)); |
| 197 | 202 | partitionKey.set(i, ColumnMetadata.forAlias(tm, alias, keyValidator.types.get(i))); |
| @@ -207,13 +212,15 @@ | ||
| 207 | 212 | if (isDense) { |
| 208 | 213 | String alias = row.isNull(VALUE_ALIAS) ? DEFAULT_VALUE_ALIAS : row.getString(VALUE_ALIAS); |
| 209 | 214 | // ...unless the table does not have any regular column, only primary key columns (JAVA-873) |
| 210 | - if(!alias.isEmpty()) { | |
| 215 | + if (!alias.isEmpty()) { | |
| 211 | 216 | DataType type = CassandraTypeParser.parseOne(row.getString(VALIDATOR), protocolVersion, codecRegistry); |
| 212 | 217 | otherColumns.add(ColumnMetadata.forAlias(tm, alias, type)); |
| 213 | 218 | } |
| 214 | 219 | } |
| 215 | 220 | } |
| 216 | 221 | |
| 222 | + Map<ColumnMetadata, Set<IndexMetadata>> indexedColumns = new LinkedHashMap<ColumnMetadata, Set<IndexMetadata>>(); | |
| 223 | + | |
| 217 | 224 | for (ColumnMetadata.Raw rawCol : rawCols.values()) { |
| 218 | 225 | ColumnMetadata col = ColumnMetadata.fromRaw(tm, rawCol); |
| 219 | 226 | switch (rawCol.kind) { |
| @@ -228,8 +235,15 @@ | ||
| 228 | 235 | otherColumns.add(col); |
| 229 | 236 | break; |
| 230 | 237 | } |
| 238 | + // create legacy secondary indexes (C* < 3.0) | |
| 239 | + IndexMetadata index = IndexMetadata.fromLegacy(col, rawCol); | |
| 240 | + if(index != null) { | |
| 241 | + indexes.put(index.getName(), index); | |
| 242 | + addIndexToColumn(index, col, indexedColumns); | |
| 243 | + } | |
| 231 | 244 | } |
| 232 | 245 | |
| 246 | + | |
| 233 | 247 | for (ColumnMetadata c : partitionKey) |
| 234 | 248 | columns.put(c.getName(), c); |
| 235 | 249 | for (ColumnMetadata c : clusteringColumns) |
| @@ -237,9 +251,40 @@ | ||
| 237 | 251 | for (ColumnMetadata c : otherColumns) |
| 238 | 252 | columns.put(c.getName(), c); |
| 239 | 253 | |
| 254 | + // create secondary indexes (C* >= 3.0) | |
| 255 | + if (indexRows != null) | |
| 256 | + for (Row indexRow : indexRows) { | |
| 257 | + IndexMetadata index = IndexMetadata.fromRow(tm, indexRow); | |
| 258 | + indexes.put(index.getName(), index); | |
| 259 | + // update the many-to-many relationship between indexes and columns | |
| 260 | + for (ColumnMetadata column : index.getColumns()) { | |
| 261 | + addIndexToColumn(index, column, indexedColumns); | |
| 262 | + } | |
| 263 | + } | |
| 264 | + | |
| 265 | + // update indexed columns | |
| 266 | + for (Map.Entry<ColumnMetadata, Set<IndexMetadata>> entry : indexedColumns.entrySet()) { | |
| 267 | + ColumnMetadata column = entry.getKey(); | |
| 268 | + for (IndexMetadata index : entry.getValue()) { | |
| 269 | + // update the "reverse" side of the many-to-many relationship between indexes and columns | |
| 270 | + column.indexes.put(index.getName(), index); | |
| 271 | + } | |
| 272 | + } | |
| 240 | 273 | return tm; |
| 241 | 274 | } |
| 242 | 275 | |
| 276 | + /** | |
| 277 | + * Associate the given index with the given column and store it in the given map. | |
| 278 | + */ | |
| 279 | + private static void addIndexToColumn(IndexMetadata index, ColumnMetadata column, Map<ColumnMetadata, Set<IndexMetadata>> indexedColumns) { | |
| 280 | + Set<IndexMetadata> indexes = indexedColumns.get(column); | |
| 281 | + if(indexes == null) { | |
| 282 | + indexes = new LinkedHashSet<IndexMetadata>(); | |
| 283 | + indexedColumns.put(column, indexes); | |
| 284 | + } | |
| 285 | + indexes.add(index); | |
| 286 | + } | |
| 287 | + | |
| 243 | 288 | /** |
| 244 | 289 | * Upon migration from thrift to CQL, we internally create a pair of surrogate clustering/regular columns |
| 245 | 290 | * for compact static tables. These columns shouldn't be exposed to the user but are currently returned by C*. |
| @@ -372,7 +417,7 @@ | ||
| 372 | 417 | * @param name the name of the column to retrieve ({@code name} will be |
| 373 | 418 | * interpreted as a case-insensitive identifier unless enclosed in double-quotes, |
| 374 | 419 | * see {@link Metadata#quote}). |
| 375 | - * @return the metadata for the {@code name} column if it exists, or | |
| 420 | + * @return the metadata for the column if it exists, or | |
| 376 | 421 | * {@code null} otherwise. |
| 377 | 422 | */ |
| 378 | 423 | public ColumnMetadata getColumn(String name) { |
| @@ -432,6 +477,28 @@ | ||
| 432 | 477 | return Collections.unmodifiableList(clusteringColumns); |
| 433 | 478 | } |
| 434 | 479 | |
| 480 | + /** | |
| 481 | + * Returns metadata on a index of this table. | |
| 482 | + * | |
| 483 | + * @param name the name of the index to retrieve ({@code name} will be | |
| 484 | + * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 485 | + * see {@link Metadata#quote}). | |
| 486 | + * @return the metadata for the {@code name} index if it exists, or | |
| 487 | + * {@code null} otherwise. | |
| 488 | + */ | |
| 489 | + public IndexMetadata getIndex(String name) { | |
| 490 | + return indexes.get(Metadata.handleId(name)); | |
| 491 | + } | |
| 492 | + | |
| 493 | + /** | |
| 494 | + * Returns a list containing all the indexes of this table. | |
| 495 | + * | |
| 496 | + * @return a list containing the metadata for the indexes of this table. | |
| 497 | + */ | |
| 498 | + public List<IndexMetadata> getIndexes() { | |
| 499 | + return new ArrayList<IndexMetadata>(indexes.values()); | |
| 500 | + } | |
| 501 | + | |
| 435 | 502 | /** |
| 436 | 503 | * Returns the clustering order for this table. |
| 437 | 504 | * <p> |
| @@ -466,7 +533,7 @@ | ||
| 466 | 533 | * table and the index on it. |
| 467 | 534 | * <p> |
| 468 | 535 | * In other words, this method returns the queries that would allow you to |
| 469 | - * recreate the schema of this table, along with the index defined on | |
| 536 | + * recreate the schema of this table, along with the indexes defined on | |
| 470 | 537 | * columns of this table. |
| 471 | 538 | * <p> |
| 472 | 539 | * Note that the returned String is formatted to be human readable (for |
| @@ -480,11 +547,7 @@ | ||
| 480 | 547 | |
| 481 | 548 | sb.append(asCQLQuery(true)); |
| 482 | 549 | |
| 483 | - for (ColumnMetadata column : columns.values()) { | |
| 484 | - ColumnMetadata.IndexMetadata index = column.getIndex(); | |
| 485 | - if (index == null) | |
| 486 | - continue; | |
| 487 | - | |
| 550 | + for (IndexMetadata index : indexes.values()) { | |
| 488 | 551 | sb.append('\n').append(index.asCQLQuery()); |
| 489 | 552 | } |
| 490 | 553 | return sb.toString(); |
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +4 −6
| @@ -15,8 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.ColumnMetadata.IndexMetadata; | |
| 19 | - | |
| 20 | 18 | /** |
| 21 | 19 | * Augment AssertJ with custom assertions for the Java driver. |
| 22 | 20 | */ |
| @@ -37,10 +35,6 @@ | ||
| 37 | 35 | return new DataTypeAssert(type); |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | - public static IndexMetadataAssert assertThat(IndexMetadata indexMetadata) { | |
| 41 | - return new IndexMetadataAssert(indexMetadata); | |
| 42 | - } | |
| 43 | - | |
| 44 | 38 | public static LocalDateAssert assertThat(LocalDate localDate) { |
| 45 | 39 | return new LocalDateAssert(localDate); |
| 46 | 40 | } |
| @@ -53,6 +47,10 @@ | ||
| 53 | 47 | return new ColumnMetadataAssert(column); |
| 54 | 48 | } |
| 55 | 49 | |
| 50 | + public static IndexMetadataAssert assertThat(IndexMetadata index) { | |
| 51 | + return new IndexMetadataAssert(index); | |
| 52 | + } | |
| 53 | + | |
| 56 | 54 | public static TypeCodecAssert assertThat(TypeCodec codec) { |
| 57 | 55 | return new TypeCodecAssert(codec); |
| 58 | 56 | } |
modified driver-core/src/test/java/com/datastax/driver/core/ColumnMetadataAssert.java +10 −0
| @@ -70,4 +70,14 @@ | ||
| 70 | 70 | return this; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | + public ColumnMetadataAssert hasIndex(IndexMetadata index) { | |
| 74 | + assertThat(actual.getIndex(index.getName())).isEqualTo(index); | |
| 75 | + return this; | |
| 76 | + } | |
| 77 | + | |
| 78 | + public ColumnMetadataAssert hasSingleIndex(IndexMetadata index) { | |
| 79 | + assertThat(actual.getIndexes()).containsOnly(index); | |
| 80 | + return this; | |
| 81 | + } | |
| 82 | + | |
| 73 | 83 | } |
modified driver-core/src/test/java/com/datastax/driver/core/CustomPayloadTest.java +5 −8
| @@ -180,17 +180,14 @@ | ||
| 180 | 180 | */ |
| 181 | 181 | @Test(groups = "short") |
| 182 | 182 | public void should_echo_custom_payload_when_paginating() throws Exception { |
| 183 | - session.execute("INSERT INTO t1 (c1) VALUES (1)"); | |
| 184 | - session.execute("INSERT INTO t1 (c1) VALUES (2)"); | |
| 185 | - Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 IN (1,2)"); | |
| 183 | + session.execute("INSERT INTO t1 (c1, c2) VALUES (1, 'a')"); | |
| 184 | + session.execute("INSERT INTO t1 (c1, c2) VALUES (1, 'b')"); | |
| 185 | + Statement statement = session.newSimpleStatement("SELECT c2 FROM t1 where c1 = 1"); | |
| 186 | 186 | statement.setFetchSize(1); |
| 187 | 187 | statement.setOutgoingPayload(payload1); |
| 188 | 188 | ResultSet rows = session.execute(statement); |
| 189 | 189 | rows.all(); |
| 190 | - for (ExecutionInfo info : rows.getAllExecutionInfo()) { | |
| 191 | - Map<String, ByteBuffer> actual = info.getIncomingPayload(); | |
| 192 | - assertThat(actual).isEqualTo(payload1); | |
| 193 | - } | |
| 190 | + assertThat(rows.getAllExecutionInfo()).extracting("incomingPayload").containsOnly(payload1); | |
| 194 | 191 | } |
| 195 | 192 | |
| 196 | 193 | // TODO retries, spec execs |
| @@ -298,7 +295,7 @@ | ||
| 298 | 295 | |
| 299 | 296 | @Override |
| 300 | 297 | protected Collection<String> getTableDefinitions() { |
| 301 | - return Collections.singletonList("CREATE TABLE t1 (c1 int PRIMARY KEY, c2 text)"); | |
| 298 | + return Collections.singletonList("CREATE TABLE t1 (c1 int, c2 text, PRIMARY KEY (c1, c2))"); | |
| 302 | 299 | } |
| 303 | 300 | |
| 304 | 301 | } |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataAssert.java +11 −4
| @@ -15,11 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 19 | - | |
| 20 | 18 | import org.assertj.core.api.AbstractAssert; |
| 21 | 19 | |
| 22 | -import com.datastax.driver.core.ColumnMetadata.IndexMetadata; | |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 23 | 21 | |
| 24 | 22 | public class IndexMetadataAssert extends AbstractAssert<IndexMetadataAssert, IndexMetadata> { |
| 25 | 23 | |
| @@ -81,5 +79,14 @@ | ||
| 81 | 79 | assertThat(actual.isCustomIndex()).isFalse(); |
| 82 | 80 | return this; |
| 83 | 81 | } |
| 84 | - | |
| 82 | + | |
| 83 | + public IndexMetadataAssert hasColumn(ColumnMetadata column) { | |
| 84 | + assertThat(actual.getColumn(column.getName())).isEqualTo(column); | |
| 85 | + return this; | |
| 86 | + } | |
| 87 | + | |
| 88 | + public IndexMetadataAssert hasSingleColumn(ColumnMetadata column) { | |
| 89 | + assertThat(actual.getColumns()).containsOnly(column); | |
| 90 | + return this; | |
| 91 | + } | |
| 85 | 92 | } |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +102 −82
| @@ -32,23 +32,22 @@ | ||
| 32 | 32 | import static com.datastax.driver.core.DataType.cint; |
| 33 | 33 | import static com.datastax.driver.core.DataType.text; |
| 34 | 34 | |
| 35 | -@CassandraVersion( | |
| 36 | - major = 1.2) | |
| 35 | +@CassandraVersion(major = 1.2) | |
| 37 | 36 | public class IndexMetadataTest extends CCMBridge.PerClassSingleNodeCluster { |
| 38 | 37 | |
| 39 | 38 | /** |
| 40 | 39 | * Column definitions for schema_columns table. |
| 41 | 40 | */ |
| 42 | - private static final ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[] { | |
| 43 | - definition(COLUMN_NAME, text()), | |
| 44 | - definition(COMPONENT_INDEX, cint()), | |
| 45 | - definition(KIND, text()), | |
| 46 | - definition(INDEX_NAME, text()), | |
| 47 | - definition(INDEX_TYPE, text()), | |
| 48 | - definition(VALIDATOR, text()), | |
| 49 | - definition(INDEX_OPTIONS, text()) | |
| 41 | + private static final ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[]{ | |
| 42 | + definition(COLUMN_NAME, text()), | |
| 43 | + definition(COMPONENT_INDEX, cint()), | |
| 44 | + definition(KIND, text()), | |
| 45 | + definition(INDEX_NAME, text()), | |
| 46 | + definition(INDEX_TYPE, text()), | |
| 47 | + definition(VALIDATOR, text()), | |
| 48 | + definition(INDEX_OPTIONS, text()) | |
| 50 | 49 | }); |
| 51 | - | |
| 50 | + | |
| 52 | 51 | static { |
| 53 | 52 | defs.setCodecRegistry(new CodecRegistry()); |
| 54 | 53 | } |
| @@ -64,131 +63,150 @@ | ||
| 64 | 63 | + |
| 65 | 64 | // Frozen collections was introduced only in C* 2.1.3 |
| 66 | 65 | (cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) >= 0 |
| 67 | - ? | |
| 66 | + ? | |
| 68 | 67 | ", map_full frozen<map<text, int>>," |
| 69 | - + "set_full frozen<set<text>>," | |
| 70 | - + "list_full frozen<list<text>>);" | |
| 71 | - : | |
| 68 | + + "set_full frozen<set<text>>," | |
| 69 | + + "list_full frozen<list<text>>);" | |
| 70 | + : | |
| 72 | 71 | ")"); |
| 73 | 72 | return ImmutableList.of(createTable); |
| 74 | 73 | } |
| 75 | - | |
| 76 | - @Test( | |
| 77 | - groups = "short") | |
| 74 | + | |
| 75 | + @Test(groups = "short") | |
| 78 | 76 | public void should_not_flag_text_column_index_type() { |
| 79 | 77 | String createValuesIndex = String.format("CREATE INDEX text_column_index ON %s.indexing (text_column);", keyspace); |
| 80 | 78 | session.execute(createValuesIndex); |
| 81 | - IndexMetadata index = getIndexForColumn("text_column"); | |
| 79 | + ColumnMetadata column = getColumn("text_column"); | |
| 80 | + IndexMetadata index = getIndex("text_column_index"); | |
| 82 | 81 | assertThat(index).hasName("text_column_index") |
| 83 | 82 | .isNotKeys() |
| 84 | 83 | .isNotFull() |
| 85 | 84 | .isNotEntries() |
| 86 | 85 | .isNotCustomIndex() |
| 86 | + .hasSingleColumn(getColumn("text_column")) | |
| 87 | 87 | .asCqlQuery(createValuesIndex); |
| 88 | + assertThat(index).isSameAs(column.getIndex("text_column_index")); | |
| 89 | + assertThat(column).hasSingleIndex(index); | |
| 90 | + assertThat(column).isSameAs(index.getColumn("text_column")); | |
| 88 | 91 | } |
| 89 | 92 | |
| 90 | - @Test( | |
| 91 | - groups = "short") | |
| 92 | - @CassandraVersion( | |
| 93 | - major = 2.1) | |
| 93 | + @Test(groups = "short") | |
| 94 | + @CassandraVersion(major = 2.1) | |
| 94 | 95 | public void should_not_flag_map_index_type() { |
| 95 | 96 | String createValuesIndex = String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace); |
| 96 | 97 | session.execute(createValuesIndex); |
| 97 | - IndexMetadata index = getIndexForColumn("map_values"); | |
| 98 | + ColumnMetadata column = getColumn("map_values"); | |
| 99 | + IndexMetadata index = getIndex("map_values_index"); | |
| 98 | 100 | assertThat(index).hasName("map_values_index") |
| 99 | 101 | .isNotKeys() |
| 100 | 102 | .isNotFull() |
| 101 | 103 | .isNotEntries() |
| 102 | 104 | .isNotCustomIndex() |
| 105 | + .hasSingleColumn(getColumn("map_values")) | |
| 103 | 106 | .asCqlQuery(createValuesIndex); |
| 107 | + assertThat(index).isSameAs(column.getIndex("map_values_index")); | |
| 108 | + assertThat(column).hasSingleIndex(index); | |
| 109 | + assertThat(column).isSameAs(index.getColumn("map_values")); | |
| 104 | 110 | } |
| 105 | 111 | |
| 106 | - @Test( | |
| 107 | - groups = "short") | |
| 108 | - @CassandraVersion( | |
| 109 | - major = 2.1) | |
| 112 | + @Test(groups = "short") | |
| 113 | + @CassandraVersion(major = 2.1) | |
| 110 | 114 | public void should_flag_map_index_type_as_keys() { |
| 111 | 115 | String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (KEYS(map_keys));", keyspace); |
| 112 | 116 | session.execute(createKeysIndex); |
| 113 | - IndexMetadata index = getIndexForColumn("map_keys"); | |
| 117 | + ColumnMetadata column = getColumn("map_keys"); | |
| 118 | + IndexMetadata index = getIndex("map_keys_index"); | |
| 114 | 119 | assertThat(index).hasName("map_keys_index") |
| 115 | 120 | .isKeys() |
| 116 | 121 | .isNotFull() |
| 117 | 122 | .isNotEntries() |
| 118 | 123 | .isNotCustomIndex() |
| 124 | + .hasSingleColumn(getColumn("map_keys")) | |
| 119 | 125 | .asCqlQuery(createKeysIndex); |
| 126 | + assertThat(index).isSameAs(column.getIndex("map_keys_index")); | |
| 127 | + assertThat(column).hasSingleIndex(index); | |
| 128 | + assertThat(column).isSameAs(index.getColumn("map_keys")); | |
| 120 | 129 | } |
| 121 | 130 | |
| 122 | - @Test( | |
| 123 | - groups = "short") | |
| 124 | - @CassandraVersion( | |
| 125 | - major = 2.1, | |
| 126 | - minor = 3) | |
| 131 | + @Test(groups = "short") | |
| 132 | + @CassandraVersion(major = 2.1, minor = 3) | |
| 127 | 133 | public void should_flag_map_index_type_as_full() { |
| 128 | 134 | String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (FULL(map_full));", keyspace); |
| 129 | 135 | session.execute(createFullIndex); |
| 130 | - IndexMetadata index = getIndexForColumn("map_full"); | |
| 136 | + ColumnMetadata column = getColumn("map_full"); | |
| 137 | + IndexMetadata index = getIndex("map_full_index"); | |
| 131 | 138 | assertThat(index).hasName("map_full_index") |
| 132 | 139 | .isNotKeys() |
| 133 | 140 | .isFull() |
| 134 | 141 | .isNotEntries() |
| 135 | 142 | .isNotCustomIndex() |
| 143 | + .hasSingleColumn(getColumn("map_full")) | |
| 136 | 144 | .asCqlQuery(createFullIndex); |
| 145 | + assertThat(index).isSameAs(column.getIndex("map_full_index")); | |
| 146 | + assertThat(column).hasSingleIndex(index); | |
| 147 | + assertThat(column).isSameAs(index.getColumn("map_full")); | |
| 137 | 148 | } |
| 138 | 149 | |
| 139 | - @Test( | |
| 140 | - groups = "short") | |
| 141 | - @CassandraVersion( | |
| 142 | - major = 2.1, | |
| 143 | - minor = 3) | |
| 150 | + @Test(groups = "short") | |
| 151 | + @CassandraVersion(major = 2.1, minor = 3) | |
| 144 | 152 | public void should_flag_set_index_type_as_full() { |
| 145 | 153 | String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (FULL(set_full));", keyspace); |
| 146 | 154 | session.execute(createFullIndex); |
| 147 | - IndexMetadata index = getIndexForColumn("set_full"); | |
| 155 | + ColumnMetadata column = getColumn("set_full"); | |
| 156 | + IndexMetadata index = getIndex("set_full_index"); | |
| 148 | 157 | assertThat(index).hasName("set_full_index") |
| 149 | 158 | .isNotKeys() |
| 150 | 159 | .isFull() |
| 151 | 160 | .isNotEntries() |
| 152 | 161 | .isNotCustomIndex() |
| 162 | + .hasSingleColumn(getColumn("set_full")) | |
| 153 | 163 | .asCqlQuery(createFullIndex); |
| 164 | + assertThat(index).isSameAs(column.getIndex("set_full_index")); | |
| 165 | + assertThat(column).hasSingleIndex(index); | |
| 166 | + assertThat(column).isSameAs(index.getColumn("set_full")); | |
| 154 | 167 | } |
| 155 | 168 | |
| 156 | - @Test( | |
| 157 | - groups = "short") | |
| 158 | - @CassandraVersion( | |
| 159 | - major = 2.1, | |
| 160 | - minor = 3) | |
| 169 | + @Test(groups = "short") | |
| 170 | + @CassandraVersion(major = 2.1, minor = 3) | |
| 161 | 171 | public void should_flag_list_index_type_as_full() { |
| 162 | 172 | String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (FULL(list_full));", keyspace); |
| 163 | 173 | session.execute(createFullIndex); |
| 164 | - IndexMetadata index = getIndexForColumn("list_full"); | |
| 174 | + ColumnMetadata column = getColumn("list_full"); | |
| 175 | + IndexMetadata index = getIndex("list_full_index"); | |
| 165 | 176 | assertThat(index).hasName("list_full_index") |
| 166 | 177 | .isNotKeys() |
| 167 | 178 | .isFull() |
| 168 | 179 | .isNotEntries() |
| 169 | 180 | .isNotCustomIndex() |
| 181 | + .hasSingleColumn(getColumn("list_full")) | |
| 170 | 182 | .asCqlQuery(createFullIndex); |
| 183 | + assertThat(index).isSameAs(column.getIndex("list_full_index")); | |
| 184 | + assertThat(column).hasSingleIndex(index); | |
| 185 | + assertThat(column).isSameAs(index.getColumn("list_full")); | |
| 171 | 186 | } |
| 172 | 187 | |
| 173 | - @Test( | |
| 174 | - groups = "short") | |
| 175 | - @CassandraVersion( | |
| 176 | - major = 3.0) | |
| 188 | + @Test(groups = "short") | |
| 189 | + @CassandraVersion(major = 2.2) | |
| 177 | 190 | public void should_flag_map_index_type_as_entries() { |
| 178 | 191 | String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (ENTRIES(map_entries));", keyspace); |
| 179 | 192 | session.execute(createEntriesIndex); |
| 180 | - IndexMetadata index = getIndexForColumn("map_entries"); | |
| 193 | + ColumnMetadata column = getColumn("map_entries"); | |
| 194 | + IndexMetadata index = getIndex("map_entries_index"); | |
| 181 | 195 | assertThat(index).hasName("map_entries_index") |
| 182 | 196 | .isNotKeys() |
| 183 | 197 | .isNotFull() |
| 184 | 198 | .isEntries() |
| 185 | 199 | .isNotCustomIndex() |
| 200 | + .hasSingleColumn(getColumn("map_entries")) | |
| 186 | 201 | .asCqlQuery(createEntriesIndex); |
| 202 | + assertThat(index).isSameAs(column.getIndex("map_entries_index")); | |
| 203 | + assertThat(column).hasSingleIndex(index); | |
| 204 | + assertThat(column).isSameAs(index.getColumn("map_entries")); | |
| 187 | 205 | } |
| 188 | 206 | |
| 189 | 207 | @Test( |
| 190 | 208 | groups = "short", |
| 191 | - description = "This test case builds a ColumnMetadata object programatically to test custom indices," | |
| 209 | + description = "This test case builds a ColumnMetadata object programmatically to test custom indices," | |
| 192 | 210 | + "otherwise, it would require deploying an actual custom index class into the C* test cluster") |
| 193 | 211 | public void should_parse_custom_index_options() { |
| 194 | 212 | TableMetadata table = getTable("indexing"); |
| @@ -200,15 +218,19 @@ | ||
| 200 | 218 | wrap("CUSTOM"), // index type |
| 201 | 219 | wrap("org.apache.cassandra.db.marshal.UTF8Type"), // validator |
| 202 | 220 | wrap("{\"foo\" : \"bar\", \"class_name\" : \"dummy.DummyIndex\"}") // index options |
| 203 | - ); | |
| 204 | - Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, ProtocolVersion.V3, data); | |
| 205 | - ColumnMetadata column = ColumnMetadata.fromRaw(table, Raw.fromRow(row, VersionNumber.parse("2.1"), ProtocolVersion.V3, new CodecRegistry())); | |
| 206 | - IndexMetadata index = column.getIndex(); | |
| 221 | + ); | |
| 222 | + Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), data); | |
| 223 | + Raw raw = Raw.fromRow(row, VersionNumber.parse("2.1"), | |
| 224 | + cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), | |
| 225 | + cluster.getConfiguration().getCodecRegistry()); | |
| 226 | + ColumnMetadata column = ColumnMetadata.fromRaw(table, raw); | |
| 227 | + IndexMetadata index = IndexMetadata.fromLegacy(column, raw); | |
| 207 | 228 | assertThat(index).hasName("custom_index") |
| 208 | 229 | .isNotKeys() |
| 209 | 230 | .isNotFull() |
| 210 | 231 | .isNotEntries() |
| 211 | 232 | .isCustomIndex() |
| 233 | + .hasSingleColumn(column) | |
| 212 | 234 | .hasOption("foo", "bar") |
| 213 | 235 | .asCqlQuery(String.format("CREATE CUSTOM INDEX custom_index ON %s.indexing (text_column) " |
| 214 | 236 | + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar', 'class_name' : 'dummy.DummyIndex'};", keyspace)); |
| @@ -226,32 +248,30 @@ | ||
| 226 | 248 | @Test(groups = "short") |
| 227 | 249 | public void should_parse_with_null_string_index_options() { |
| 228 | 250 | TableMetadata table = getTable("indexing"); |
| 229 | - | |
| 230 | 251 | List<ByteBuffer> data = ImmutableList.of( |
| 231 | - wrap("b@706172656e745f70617468"), // column name 'parent_path' | |
| 232 | - ByteBuffer.allocate(0), // component index (null) | |
| 233 | - wrap("regular"), // kind | |
| 234 | - wrap("cfs_archive_parent_path"), // index name | |
| 235 | - wrap("KEYS"), // index type | |
| 236 | - wrap("org.apache.cassandra.db.marshal.BytesType"), // validator | |
| 237 | - wrap("null") // index options | |
| 252 | + wrap("b@706172656e745f70617468"), // column name 'parent_path' | |
| 253 | + ByteBuffer.allocate(0), // component index (null) | |
| 254 | + wrap("regular"), // kind | |
| 255 | + wrap("cfs_archive_parent_path"), // index name | |
| 256 | + wrap("KEYS"), // index type | |
| 257 | + wrap("org.apache.cassandra.db.marshal.BytesType"), // validator | |
| 258 | + wrap("null") // index options | |
| 238 | 259 | ); |
| 239 | - Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, ProtocolVersion.V3, data); | |
| 240 | - ColumnMetadata column = ColumnMetadata.fromRaw(table, | |
| 241 | - Raw.fromRow(row, VersionNumber.parse("2.1"), | |
| 242 | - cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), | |
| 243 | - cluster.getConfiguration().getCodecRegistry())); | |
| 244 | - IndexMetadata index = column.getIndex(); | |
| 245 | - | |
| 260 | + Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), data); | |
| 261 | + Raw raw = Raw.fromRow(row, VersionNumber.parse("2.1"), | |
| 262 | + cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), | |
| 263 | + cluster.getConfiguration().getCodecRegistry()); | |
| 264 | + ColumnMetadata column = ColumnMetadata.fromRaw(table, raw); | |
| 265 | + IndexMetadata index = IndexMetadata.fromLegacy(column, raw); | |
| 246 | 266 | assertThat(index).hasName("cfs_archive_parent_path") |
| 247 | - .isNotKeys() // While the index type is KEYS, since it lacks index_options it does not considered. | |
| 248 | - .isNotFull() | |
| 249 | - .isNotEntries() | |
| 250 | - .isNotCustomIndex() | |
| 251 | - .asCqlQuery(String.format("CREATE INDEX cfs_archive_parent_path ON %s.indexing (\"b@706172656e745f70617468\");", keyspace)); | |
| 267 | + .isNotKeys() // While the index type is KEYS, since it lacks index_options it does not considered. | |
| 268 | + .isNotFull() | |
| 269 | + .isNotEntries() | |
| 270 | + .isNotCustomIndex() | |
| 271 | + .hasSingleColumn(column) | |
| 272 | + .asCqlQuery(String.format("CREATE INDEX cfs_archive_parent_path ON %s.indexing (\"b@706172656e745f70617468\");", keyspace)); | |
| 252 | 273 | } |
| 253 | 274 | |
| 254 | - | |
| 255 | 275 | private static ColumnDefinitions.Definition definition(String name, DataType type) { |
| 256 | 276 | return new ColumnDefinitions.Definition("ks", "table", name, type); |
| 257 | 277 | } |
| @@ -264,14 +284,14 @@ | ||
| 264 | 284 | return ByteBuffer.wrap(Ints.toByteArray(number)); |
| 265 | 285 | } |
| 266 | 286 | |
| 267 | - private IndexMetadata getIndexForColumn(String columnName) { | |
| 268 | - return getColumn(columnName).getIndex(); | |
| 269 | - } | |
| 270 | - | |
| 271 | 287 | private ColumnMetadata getColumn(String name) { |
| 272 | 288 | return getTable("indexing").getColumn(name); |
| 273 | 289 | } |
| 274 | 290 | |
| 291 | + private IndexMetadata getIndex(String name) { | |
| 292 | + return getTable("indexing").getIndex(name); | |
| 293 | + } | |
| 294 | + | |
| 275 | 295 | private TableMetadata getTable(String name) { |
| 276 | 296 | return cluster.getMetadata().getKeyspace(keyspace).getTable(name); |
| 277 | 297 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +26 −1
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.util.Collection; |
| 19 | 19 | import java.util.Collections; |
| 20 | 20 | |
| 21 | +import org.testng.SkipException; | |
| 21 | 22 | import org.testng.annotations.Test; |
| 22 | 23 | |
| 23 | 24 | import static org.assertj.core.api.Assertions.entry; |
| @@ -172,6 +173,12 @@ | ||
| 172 | 173 | |
| 173 | 174 | @Test(groups = "short") |
| 174 | 175 | public void should_parse_table_options() { |
| 176 | + VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion(); | |
| 177 | + if (version.getMajor() > 2) { | |
| 178 | + // TODO adapt test for C* 3.0 | |
| 179 | + throw new SkipException("Needs adjustments to work with Cassandra 3.0"); | |
| 180 | + } | |
| 181 | + | |
| 175 | 182 | // given |
| 176 | 183 | String cql = String.format("CREATE TABLE %s.with_options (\n" |
| 177 | 184 | + " k text,\n" |
| @@ -200,7 +207,6 @@ | ||
| 200 | 207 | assertThat(table.getColumns().get(2)).isNotNull().hasName("c2").isClusteringColumn().hasClusteringOrder(ASC).hasType(cint()); |
| 201 | 208 | assertThat(table.getColumns().get(3)).isNotNull().hasName("i").isRegularColumn().hasType(cint()); |
| 202 | 209 | assertThat(table); |
| 203 | - VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion(); | |
| 204 | 210 | |
| 205 | 211 | // Cassandra 3.0 + |
| 206 | 212 | if (version.getMajor() > 2) { |
| @@ -381,4 +387,23 @@ | ||
| 381 | 387 | return Collections.emptyList(); |
| 382 | 388 | } |
| 383 | 389 | |
| 390 | + @Test(groups = "short") | |
| 391 | + public void should_not_mix_indexes_from_different_tables() { | |
| 392 | + String[] statements = { | |
| 393 | + "CREATE TABLE test_ab (a int PRIMARY KEY, b int);", | |
| 394 | + "CREATE INDEX test_b on test_ab (b);", | |
| 395 | + "CREATE TABLE test_cd (c int PRIMARY KEY, d int);", | |
| 396 | + "CREATE INDEX test_d on test_cd (d);", | |
| 397 | + }; | |
| 398 | + for (String statement: statements) | |
| 399 | + session.execute(statement); | |
| 400 | + | |
| 401 | + TableMetadata table_ab = cluster.getMetadata().getKeyspace(keyspace).getTable("test_ab"); | |
| 402 | + TableMetadata table_cd = cluster.getMetadata().getKeyspace(keyspace).getTable("test_cd"); | |
| 403 | + | |
| 404 | + assertThat(table_ab.getIndexes().size()).isEqualTo(1); | |
| 405 | + assertThat(table_ab.getIndexes().get(0).getName()).isEqualTo("test_b"); | |
| 406 | + assertThat(table_cd.getIndexes().size()).isEqualTo(1); | |
| 407 | + assertThat(table_cd.getIndexes().get(0).getName()).isEqualTo("test_d"); | |
| 408 | + } | |
| 384 | 409 | } |
modified pom.xml +1 −1
| @@ -43,7 +43,7 @@ | ||
| 43 | 43 | |
| 44 | 44 | <properties> |
| 45 | 45 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 46 | - <cassandra.version>3.0.0-alpha1</cassandra.version> | |
| 46 | + <cassandra.version>3.0.0-beta1</cassandra.version> | |
| 47 | 47 | <java.version>1.6</java.version> |
| 48 | 48 | <log4j.version>1.2.17</log4j.version> |
| 49 | 49 | <slf4j-log4j12.version>1.7.6</slf4j-log4j12.version> |