kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e5ab27d8c3580901836a72e4d22a5f59cb75adaed7626a60c49a06ac06de5ed9 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-15 12:37 +02:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-17 18:20 +02:00 |
| Parents | d4513b28760b |
11 files changed
modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +3 −27
| @@ -15,7 +15,9 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.util.*; | |
| 18 | +import java.util.Arrays; | |
| 19 | +import java.util.HashMap; | |
| 20 | +import java.util.Map; | |
| 19 | 21 | |
| 20 | 22 | /** |
| 21 | 23 | * Describes a Column. |
| @@ -42,10 +44,6 @@ | ||
| 42 | 44 | private final DataType type; |
| 43 | 45 | private final boolean isStatic; |
| 44 | 46 | |
| 45 | - // this is the "reverse" side of the many-to-many relationship | |
| 46 | - // between columns and indexes, and is updated only after the column is created | |
| 47 | - final Map<String, IndexMetadata> indexes = new LinkedHashMap<String, IndexMetadata>(); | |
| 48 | - | |
| 49 | 47 | private ColumnMetadata(TableOrView parent, String name, DataType type, boolean isStatic) { |
| 50 | 48 | this.parent = parent; |
| 51 | 49 | this.name = name; |
| @@ -98,28 +96,6 @@ | ||
| 98 | 96 | return isStatic; |
| 99 | 97 | } |
| 100 | 98 | |
| 101 | - /** | |
| 102 | - * Returns metadata on a index on this column. | |
| 103 | - * | |
| 104 | - * @param name the name of the index to retrieve ({@code name} will be | |
| 105 | - * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 106 | - * see {@link Metadata#quote}). | |
| 107 | - * @return the metadata for the {@code name} index if it exists, or | |
| 108 | - * {@code null} otherwise. | |
| 109 | - */ | |
| 110 | - public IndexMetadata getIndex(String name) { | |
| 111 | - return indexes.get(Metadata.handleId(name)); | |
| 112 | - } | |
| 113 | - | |
| 114 | - /** | |
| 115 | - * Returns a list containing all the indexes on this column. | |
| 116 | - * | |
| 117 | - * @return a list containing the metadata for the indexes on this column. | |
| 118 | - */ | |
| 119 | - public List<IndexMetadata> getIndexes() { | |
| 120 | - return new ArrayList<IndexMetadata>(indexes.values()); | |
| 121 | - } | |
| 122 | - | |
| 123 | 99 | @Override |
| 124 | 100 | public String toString() { |
| 125 | 101 | String str = Metadata.escapeId(name) + ' ' + type; |
modified driver-core/src/main/java/com/datastax/driver/core/IndexMetadata.java +77 −158
| @@ -15,65 +15,62 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.util.*; | |
| 18 | +import java.util.Iterator; | |
| 19 | +import java.util.Map; | |
| 19 | 20 | |
| 20 | -import com.google.common.base.Function; | |
| 21 | 21 | import com.google.common.base.Objects; |
| 22 | +import com.google.common.base.Predicate; | |
| 22 | 23 | import com.google.common.collect.ImmutableMap; |
| 24 | +import com.google.common.collect.Iterables; | |
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * An immutable representation of secondary index metadata. |
| 26 | 28 | */ |
| 27 | 29 | public class IndexMetadata { |
| 28 | 30 | |
| 29 | - public enum IndexType { | |
| 31 | + public enum Kind { | |
| 30 | 32 | KEYS, |
| 31 | 33 | CUSTOM, |
| 32 | 34 | COMPOSITES |
| 33 | 35 | } |
| 34 | 36 | |
| 35 | - public enum TargetType { | |
| 36 | - COLUMN, ROW | |
| 37 | - } | |
| 37 | + static final String NAME = "index_name"; | |
| 38 | 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 | - }; | |
| 39 | + static final String KIND = "kind"; | |
| 45 | 40 | |
| 46 | - public static final String CUSTOM_INDEX_OPTION_NAME = "class_name"; | |
| 41 | + static final String OPTIONS = "options"; | |
| 47 | 42 | |
| 48 | 43 | /** |
| 49 | - * The name of the option used to specify that the index is on the collection keys. | |
| 44 | + * The name of the option used to specify the index target (Cassandra 3.0 onwards). | |
| 50 | 45 | */ |
| 51 | - public static final String INDEX_KEYS_OPTION_NAME = "index_keys"; | |
| 46 | + public static final String TARGET_OPTION_NAME = "target"; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * The name of the option used to specify a custom index class name. | |
| 50 | + */ | |
| 51 | + public static final String CUSTOM_INDEX_OPTION_NAME = "class_name"; | |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | - * The name of the option used to specify that the index is on the collection values. | |
| 54 | + * The name of the option used to specify that the index is on the collection (map) keys. | |
| 55 | 55 | */ |
| 56 | - public static final String INDEX_VALUES_OPTION_NAME = "index_values"; | |
| 56 | + public static final String INDEX_KEYS_OPTION_NAME = "index_keys"; | |
| 57 | 57 | |
| 58 | 58 | /** |
| 59 | 59 | * The name of the option used to specify that the index is on the collection (map) entries. |
| 60 | 60 | */ |
| 61 | 61 | public static final String INDEX_ENTRIES_OPTION_NAME = "index_keys_and_values"; |
| 62 | 62 | |
| 63 | - | |
| 64 | 63 | private final TableMetadata table; |
| 65 | 64 | private final String name; |
| 66 | - private final Map<String, ColumnMetadata> columns; | |
| 67 | - private final IndexType indexType; | |
| 68 | - private final TargetType targetType; | |
| 65 | + private final Kind kind; | |
| 66 | + private final String target; | |
| 69 | 67 | private final Map<String, String> options; |
| 70 | 68 | |
| 71 | - private IndexMetadata(TableMetadata table, String name, Map<String, ColumnMetadata> columns, IndexType indexType, TargetType targetType, Map<String, String> options) { | |
| 69 | + private IndexMetadata(TableMetadata table, String name, Kind kind, String target, Map<String, String> options) { | |
| 72 | 70 | this.table = table; |
| 73 | 71 | this.name = name; |
| 74 | - this.columns = columns; | |
| 75 | - this.indexType = indexType; | |
| 76 | - this.targetType = targetType; | |
| 72 | + this.kind = kind; | |
| 73 | + this.target = target; | |
| 77 | 74 | this.options = options; |
| 78 | 75 | } |
| 79 | 76 | |
| @@ -81,16 +78,11 @@ | ||
| 81 | 78 | * Build an IndexMetadata from a system_schema.indexes row. |
| 82 | 79 | */ |
| 83 | 80 | 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); | |
| 81 | + String name = indexRow.getString(NAME); | |
| 82 | + Kind kind = Kind.valueOf(indexRow.getString(KIND)); | |
| 83 | + Map<String, String> options = indexRow.getMap(OPTIONS, String.class, String.class); | |
| 84 | + String target = options.get(TARGET_OPTION_NAME); | |
| 85 | + return new IndexMetadata(table, name, kind, target, options); | |
| 94 | 86 | } |
| 95 | 87 | |
| 96 | 88 | /** |
| @@ -98,31 +90,44 @@ | ||
| 98 | 90 | * along with indexed column). |
| 99 | 91 | */ |
| 100 | 92 | static IndexMetadata fromLegacy(ColumnMetadata column, ColumnMetadata.Raw raw) { |
| 101 | - if (raw.indexColumns.isEmpty()) | |
| 93 | + Map<String, String> indexColumns = raw.indexColumns; | |
| 94 | + if (indexColumns.isEmpty()) | |
| 102 | 95 | return null; |
| 103 | - String type = raw.indexColumns.get(ColumnMetadata.INDEX_TYPE); | |
| 96 | + String type = indexColumns.get(ColumnMetadata.INDEX_TYPE); | |
| 104 | 97 | if (type == null) |
| 105 | 98 | 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); | |
| 99 | + String indexName = indexColumns.get(ColumnMetadata.INDEX_NAME); | |
| 100 | + String kindStr = indexColumns.get(ColumnMetadata.INDEX_TYPE); | |
| 101 | + Kind kind = kindStr == null ? null : Kind.valueOf(kindStr); | |
| 109 | 102 | // Special case check for the value of the index_options column being a string with value 'null' as this |
| 110 | 103 | // 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); | |
| 104 | + String indexOptionsCol = indexColumns.get(ColumnMetadata.INDEX_OPTIONS); | |
| 113 | 105 | Map<String, String> options; |
| 114 | 106 | if (indexOptionsCol == null || indexOptionsCol.isEmpty() || indexOptionsCol.equals("null")) { |
| 115 | 107 | options = ImmutableMap.of(); |
| 116 | 108 | } else { |
| 117 | 109 | options = SimpleJSONParser.parseStringMap(indexOptionsCol); |
| 118 | 110 | } |
| 119 | - return new IndexMetadata((TableMetadata)column.getParent(), indexName, columns, indexType, TargetType.COLUMN, options); | |
| 111 | + String target = targetFromLegacyOptions(column, options); | |
| 112 | + return new IndexMetadata((TableMetadata)column.getParent(), indexName, kind, target, options); | |
| 113 | + } | |
| 114 | + | |
| 115 | + private static String targetFromLegacyOptions(ColumnMetadata column, Map<String, String> options) { | |
| 116 | + String columnName = Metadata.escapeId(column.getName()); | |
| 117 | + if(options.containsKey(INDEX_KEYS_OPTION_NAME)) | |
| 118 | + return String.format("keys(%s)", columnName); | |
| 119 | + if(options.containsKey(INDEX_ENTRIES_OPTION_NAME)) | |
| 120 | + return String.format("entries(%s)", columnName); | |
| 121 | + if(column.getType() instanceof DataType.CollectionType && column.getType().isFrozen()) | |
| 122 | + return String.format("full(%s)", columnName); | |
| 123 | + // Note: the keyword 'values' is not accepted as a valid index target function until 3.0 | |
| 124 | + return columnName; | |
| 120 | 125 | } |
| 121 | 126 | |
| 122 | 127 | /** |
| 123 | - * Returns the metadata of the table this column is part of. | |
| 128 | + * Returns the metadata of the table this index is part of. | |
| 124 | 129 | * |
| 125 | - * @return the {@code TableMetadata} for the table this column is part of. | |
| 130 | + * @return the table this index is part of. | |
| 126 | 131 | */ |
| 127 | 132 | public TableMetadata getTable() { |
| 128 | 133 | return table; |
| @@ -138,47 +143,21 @@ | ||
| 138 | 143 | } |
| 139 | 144 | |
| 140 | 145 | /** |
| 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. | |
| 146 | + * Returns the index kind. | |
| 167 | 147 | * |
| 168 | - * @return the index type. | |
| 148 | + * @return the index kind. | |
| 169 | 149 | */ |
| 170 | - public IndexType getIndexType() { | |
| 171 | - return indexType; | |
| 150 | + public Kind getKind() { | |
| 151 | + return kind; | |
| 172 | 152 | } |
| 173 | 153 | |
| 174 | 154 | /** |
| 175 | - * Returns the index target type. | |
| 176 | - * Note: for legacy indexes, this is always {@link TargetType#COLUMN}. | |
| 155 | + * Returns the index target. | |
| 177 | 156 | * |
| 178 | - * @return the index target type. | |
| 157 | + * @return the index target. | |
| 179 | 158 | */ |
| 180 | - public TargetType getTargetType() { | |
| 181 | - return targetType; | |
| 159 | + public String getTarget() { | |
| 160 | + return target; | |
| 182 | 161 | } |
| 183 | 162 | |
| 184 | 163 | /** |
| @@ -204,56 +183,6 @@ | ||
| 204 | 183 | return getOption(CUSTOM_INDEX_OPTION_NAME); |
| 205 | 184 | } |
| 206 | 185 | |
| 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 | 186 | /** |
| 258 | 187 | * Return the value for the given option name. |
| 259 | 188 | * |
| @@ -273,14 +202,12 @@ | ||
| 273 | 202 | * @return the 'CREATE INDEX' query corresponding to this index. |
| 274 | 203 | */ |
| 275 | 204 | 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()); | |
| 205 | + String keyspaceName = Metadata.escapeId(table.getKeyspace().getName()); | |
| 206 | + String tableName = Metadata.escapeId(table.getName()); | |
| 207 | + String indexName = Metadata.escapeId(this.name); | |
| 281 | 208 | 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)); | |
| 209 | + ? String.format("CREATE CUSTOM INDEX %s ON %s.%s (%s) USING '%s' %s;", indexName, keyspaceName, tableName, getTarget(), getIndexClassName(), getOptionsAsCql()) | |
| 210 | + : String.format("CREATE INDEX %s ON %s.%s (%s);", indexName, keyspaceName, tableName, getTarget()); | |
| 284 | 211 | } |
| 285 | 212 | |
| 286 | 213 | /** |
| @@ -290,9 +217,18 @@ | ||
| 290 | 217 | * the 'index_options' column of the 'schema_columns' table in the 'system' keyspace. |
| 291 | 218 | */ |
| 292 | 219 | private String getOptionsAsCql() { |
| 220 | + Iterable<Map.Entry<String, String>> filtered = Iterables.filter(options.entrySet(), new Predicate<Map.Entry<String, String>>() { | |
| 221 | + @Override | |
| 222 | + public boolean apply(Map.Entry<String, String> input) { | |
| 223 | + return | |
| 224 | + !input.getKey().equals(TARGET_OPTION_NAME) && | |
| 225 | + !input.getKey().equals(CUSTOM_INDEX_OPTION_NAME); | |
| 226 | + } | |
| 227 | + }); | |
| 228 | + if(Iterables.isEmpty(filtered)) return ""; | |
| 293 | 229 | StringBuilder builder = new StringBuilder(); |
| 294 | - builder.append("{"); | |
| 295 | - Iterator<Map.Entry<String, String>> it = options.entrySet().iterator(); | |
| 230 | + builder.append("WITH OPTIONS = {"); | |
| 231 | + Iterator<Map.Entry<String, String>> it = filtered.iterator(); | |
| 296 | 232 | while (it.hasNext()) { |
| 297 | 233 | Map.Entry<String, String> option = it.next(); |
| 298 | 234 | builder.append(String.format("'%s' : '%s'", option.getKey(), option.getValue())); |
| @@ -303,24 +239,8 @@ | ||
| 303 | 239 | return builder.toString(); |
| 304 | 240 | } |
| 305 | 241 | |
| 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 | 242 | public int hashCode() { |
| 323 | - return Objects.hashCode(name, columns, indexType, targetType, options); | |
| 243 | + return Objects.hashCode(name, kind, target, options); | |
| 324 | 244 | } |
| 325 | 245 | |
| 326 | 246 | public boolean equals(Object obj) { |
| @@ -333,9 +253,8 @@ | ||
| 333 | 253 | IndexMetadata other = (IndexMetadata)obj; |
| 334 | 254 | |
| 335 | 255 | return Objects.equal(name, other.name) |
| 336 | - && Objects.equal(columns, other.columns) | |
| 337 | - && Objects.equal(indexType, other.indexType) | |
| 338 | - && Objects.equal(targetType, other.targetType) | |
| 256 | + && Objects.equal(kind, other.kind) | |
| 257 | + && Objects.equal(target, other.target) | |
| 339 | 258 | && Objects.equal(options, other.options); |
| 340 | 259 | } |
| 341 | 260 |
modified driver-core/src/main/java/com/datastax/driver/core/MaterializedViewMetadata.java +2 −2
| @@ -72,12 +72,12 @@ | ||
| 72 | 72 | int clusteringSize = findCollectionSize(rawCols.values(), ColumnMetadata.Raw.Kind.CLUSTERING_COLUMN); |
| 73 | 73 | List<ColumnMetadata> partitionKey = nullInitializedList(partitionKeySize); |
| 74 | 74 | List<ColumnMetadata> clusteringColumns = nullInitializedList(clusteringSize); |
| 75 | - List<TableMetadata.Order> clusteringOrder = nullInitializedList(clusteringSize); | |
| 75 | + List<Order> clusteringOrder = nullInitializedList(clusteringSize); | |
| 76 | 76 | |
| 77 | 77 | // We use a linked hashmap because we will keep this in the order of a 'SELECT * FROM ...'. |
| 78 | 78 | LinkedHashMap<String, ColumnMetadata> columns = new LinkedHashMap<String, ColumnMetadata>(); |
| 79 | 79 | |
| 80 | - TableMetadata.Options options = null; | |
| 80 | + Options options = null; | |
| 81 | 81 | try { |
| 82 | 82 | options = new Options(row, false, cassandraVersion); |
| 83 | 83 | } catch (RuntimeException e) { |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +1 −0
| @@ -387,6 +387,7 @@ | ||
| 387 | 387 | Map<String, ColumnMetadata.Raw> cols = colsDefs.get(viewName); |
| 388 | 388 | if (cols == null || cols.isEmpty()) |
| 389 | 389 | continue; // we probably raced, we will update the metadata next time |
| 390 | + | |
| 390 | 391 | MaterializedViewMetadata view = MaterializedViewMetadata.build(ksm, viewRow, cols, cassandraVersion); |
| 391 | 392 | if(view != null) |
| 392 | 393 | ksm.add(view); |
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +6 −34
| @@ -50,6 +50,7 @@ | ||
| 50 | 50 | private static final String EMPTY_TYPE = "org.apache.cassandra.db.marshal.EmptyType"; |
| 51 | 51 | |
| 52 | 52 | private final Map<String, IndexMetadata> indexes; |
| 53 | + | |
| 53 | 54 | private final Map<String, MaterializedViewMetadata> views; |
| 54 | 55 | |
| 55 | 56 | private TableMetadata(KeyspaceMetadata keyspace, |
| @@ -177,8 +178,6 @@ | ||
| 177 | 178 | } |
| 178 | 179 | } |
| 179 | 180 | |
| 180 | - Map<ColumnMetadata, Set<IndexMetadata>> indexedColumns = new LinkedHashMap<ColumnMetadata, Set<IndexMetadata>>(); | |
| 181 | - | |
| 182 | 181 | for (ColumnMetadata.Raw rawCol : rawCols.values()) { |
| 183 | 182 | ColumnMetadata col = ColumnMetadata.fromRaw(tm, rawCol); |
| 184 | 183 | switch (rawCol.kind) { |
| @@ -193,15 +192,13 @@ | ||
| 193 | 192 | otherColumns.add(col); |
| 194 | 193 | break; |
| 195 | 194 | } |
| 196 | - // create legacy secondary indexes (C* < 3.0) | |
| 195 | + | |
| 196 | + // legacy secondary indexes (C* < 3.0) | |
| 197 | 197 | IndexMetadata index = IndexMetadata.fromLegacy(col, rawCol); |
| 198 | - if(index != null) { | |
| 198 | + if(index != null) | |
| 199 | 199 | indexes.put(index.getName(), index); |
| 200 | - addIndexToColumn(index, col, indexedColumns); | |
| 201 | - } | |
| 202 | 200 | } |
| 203 | 201 | |
| 204 | - | |
| 205 | 202 | for (ColumnMetadata c : partitionKey) |
| 206 | 203 | columns.put(c.getName(), c); |
| 207 | 204 | for (ColumnMetadata c : clusteringColumns) |
| @@ -214,36 +211,11 @@ | ||
| 214 | 211 | for (Row indexRow : indexRows) { |
| 215 | 212 | IndexMetadata index = IndexMetadata.fromRow(tm, indexRow); |
| 216 | 213 | indexes.put(index.getName(), index); |
| 217 | - // update the many-to-many relationship between indexes and columns | |
| 218 | - for (ColumnMetadata column : index.getColumns()) { | |
| 219 | - addIndexToColumn(index, column, indexedColumns); | |
| 220 | - } | |
| 221 | - } | |
| 222 | - | |
| 223 | - // update indexed columns | |
| 224 | - for (Map.Entry<ColumnMetadata, Set<IndexMetadata>> entry : indexedColumns.entrySet()) { | |
| 225 | - ColumnMetadata column = entry.getKey(); | |
| 226 | - for (IndexMetadata index : entry.getValue()) { | |
| 227 | - // update the "reverse" side of the many-to-many relationship between indexes and columns | |
| 228 | - column.indexes.put(index.getName(), index); | |
| 229 | 214 | } |
| 230 | - } | |
| 231 | 215 | |
| 232 | 216 | return tm; |
| 233 | 217 | } |
| 234 | 218 | |
| 235 | - /** | |
| 236 | - * Associate the given index with the given column and store it in the given map. | |
| 237 | - */ | |
| 238 | - private static void addIndexToColumn(IndexMetadata index, ColumnMetadata column, Map<ColumnMetadata, Set<IndexMetadata>> indexedColumns) { | |
| 239 | - Set<IndexMetadata> indexes = indexedColumns.get(column); | |
| 240 | - if(indexes == null) { | |
| 241 | - indexes = new LinkedHashSet<IndexMetadata>(); | |
| 242 | - indexedColumns.put(column, indexes); | |
| 243 | - } | |
| 244 | - indexes.add(index); | |
| 245 | - } | |
| 246 | - | |
| 247 | 219 | /** |
| 248 | 220 | * Upon migration from thrift to CQL, we internally create a pair of surrogate clustering/regular columns |
| 249 | 221 | * for compact static tables. These columns shouldn't be exposed to the user but are currently returned by C*. |
| @@ -405,8 +377,8 @@ | ||
| 405 | 377 | sb.append('\n').append(index.asCQLQuery()); |
| 406 | 378 | } |
| 407 | 379 | |
| 408 | - for (MaterializedViewMetadata index : views.values()) { | |
| 409 | - sb.append('\n').append(index.asCQLQuery()); | |
| 380 | + for (MaterializedViewMetadata view : views.values()) { | |
| 381 | + sb.append('\n').append(view.asCQLQuery()); | |
| 410 | 382 | } |
| 411 | 383 | |
| 412 | 384 | return sb.toString(); |
modified driver-core/src/main/java/com/datastax/driver/core/TableOrView.java +5 −1
| @@ -225,7 +225,11 @@ | ||
| 225 | 225 | * String}. |
| 226 | 226 | */ |
| 227 | 227 | public String exportAsString() { |
| 228 | - return asCQLQuery(true); | |
| 228 | + StringBuilder sb = new StringBuilder(); | |
| 229 | + | |
| 230 | + sb.append(asCQLQuery(true)); | |
| 231 | + | |
| 232 | + return sb.toString(); | |
| 229 | 233 | } |
| 230 | 234 | |
| 231 | 235 | /** |
modified driver-core/src/test/java/com/datastax/driver/core/ColumnMetadataAssert.java +0 −10
| @@ -70,14 +70,4 @@ | ||
| 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 | - | |
| 83 | 73 | } |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataAssert.java +13 −36
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | |
| 20 | 20 | import static org.assertj.core.api.Assertions.assertThat; |
| 21 | 21 | |
| 22 | +import com.datastax.driver.core.IndexMetadata.Kind; | |
| 23 | + | |
| 22 | 24 | public class IndexMetadataAssert extends AbstractAssert<IndexMetadataAssert, IndexMetadata> { |
| 23 | 25 | |
| 24 | 26 | public IndexMetadataAssert(IndexMetadata actual) { |
| @@ -29,7 +31,12 @@ | ||
| 29 | 31 | assertThat(actual.getName()).isEqualTo(name); |
| 30 | 32 | return this; |
| 31 | 33 | } |
| 32 | - | |
| 34 | + | |
| 35 | + public IndexMetadataAssert hasParent(TableMetadata parent) { | |
| 36 | + assertThat(actual.getTable()).isEqualTo(parent); | |
| 37 | + return this; | |
| 38 | + } | |
| 39 | + | |
| 33 | 40 | public IndexMetadataAssert hasOption(String name, String value) { |
| 34 | 41 | assertThat(actual.getOption(name)).isEqualTo(value); |
| 35 | 42 | return this; |
| @@ -39,37 +46,7 @@ | ||
| 39 | 46 | assertThat(actual.asCQLQuery()).isEqualTo(cqlQuery); |
| 40 | 47 | return this; |
| 41 | 48 | } |
| 42 | - | |
| 43 | - public IndexMetadataAssert isKeys(){ | |
| 44 | - assertThat(actual.isKeys()).isTrue(); | |
| 45 | - return this; | |
| 46 | - } | |
| 47 | - | |
| 48 | - public IndexMetadataAssert isNotKeys(){ | |
| 49 | - assertThat(actual.isKeys()).isFalse(); | |
| 50 | - return this; | |
| 51 | - } | |
| 52 | - | |
| 53 | - public IndexMetadataAssert isFull() { | |
| 54 | - assertThat(actual.isFull()).isTrue(); | |
| 55 | - return this; | |
| 56 | - } | |
| 57 | - | |
| 58 | - public IndexMetadataAssert isNotFull() { | |
| 59 | - assertThat(actual.isFull()).isFalse(); | |
| 60 | - return this; | |
| 61 | - } | |
| 62 | - | |
| 63 | - public IndexMetadataAssert isEntries() { | |
| 64 | - assertThat(actual.isEntries()).isTrue(); | |
| 65 | - return this; | |
| 66 | - } | |
| 67 | - | |
| 68 | - public IndexMetadataAssert isNotEntries() { | |
| 69 | - assertThat(actual.isEntries()).isFalse(); | |
| 70 | - return this; | |
| 71 | - } | |
| 72 | - | |
| 49 | + | |
| 73 | 50 | public IndexMetadataAssert isCustomIndex() { |
| 74 | 51 | assertThat(actual.isCustomIndex()).isTrue(); |
| 75 | 52 | return this; |
| @@ -80,13 +57,13 @@ | ||
| 80 | 57 | return this; |
| 81 | 58 | } |
| 82 | 59 | |
| 83 | - public IndexMetadataAssert hasColumn(ColumnMetadata column) { | |
| 84 | - assertThat(actual.getColumn(column.getName())).isEqualTo(column); | |
| 60 | + public IndexMetadataAssert hasTarget(String target) { | |
| 61 | + assertThat(actual.getTarget()).isEqualTo(target); | |
| 85 | 62 | return this; |
| 86 | 63 | } |
| 87 | 64 | |
| 88 | - public IndexMetadataAssert hasSingleColumn(ColumnMetadata column) { | |
| 89 | - assertThat(actual.getColumns()).containsOnly(column); | |
| 65 | + public IndexMetadataAssert hasKind(Kind kind) { | |
| 66 | + assertThat(actual.getKind()).isEqualTo(kind); | |
| 90 | 67 | return this; |
| 91 | 68 | } |
| 92 | 69 | } |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +210 −112
| @@ -20,7 +20,9 @@ | ||
| 20 | 20 | import java.util.List; |
| 21 | 21 | |
| 22 | 22 | import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.collect.ImmutableMap; | |
| 23 | 24 | import com.google.common.primitives.Ints; |
| 25 | +import org.testng.annotations.BeforeClass; | |
| 24 | 26 | import org.testng.annotations.Test; |
| 25 | 27 | |
| 26 | 28 | import com.datastax.driver.core.ColumnMetadata.*; |
| @@ -30,15 +32,17 @@ | ||
| 30 | 32 | import static com.datastax.driver.core.Assertions.assertThat; |
| 31 | 33 | import static com.datastax.driver.core.ColumnMetadata.*; |
| 32 | 34 | import static com.datastax.driver.core.DataType.cint; |
| 35 | +import static com.datastax.driver.core.DataType.map; | |
| 33 | 36 | import static com.datastax.driver.core.DataType.text; |
| 37 | +import static com.datastax.driver.core.IndexMetadata.Kind.*; | |
| 34 | 38 | |
| 35 | 39 | @CassandraVersion(major = 1.2) |
| 36 | 40 | public class IndexMetadataTest extends CCMBridge.PerClassSingleNodeCluster { |
| 37 | 41 | |
| 38 | 42 | /** |
| 39 | - * Column definitions for schema_columns table. | |
| 43 | + * Column definitions for schema_columns table (legacy pre-3.0 layout). | |
| 40 | 44 | */ |
| 41 | - private static final ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[]{ | |
| 45 | + private static final ColumnDefinitions legacyColumnDefs = new ColumnDefinitions(new ColumnDefinitions.Definition[]{ | |
| 42 | 46 | definition(COLUMN_NAME, text()), |
| 43 | 47 | definition(COMPONENT_INDEX, cint()), |
| 44 | 48 | definition(KIND_V2, text()), |
| @@ -48,192 +52,277 @@ | ||
| 48 | 52 | definition(INDEX_OPTIONS, text()) |
| 49 | 53 | }); |
| 50 | 54 | |
| 51 | - static { | |
| 52 | - defs.setCodecRegistry(new CodecRegistry()); | |
| 55 | + /** | |
| 56 | + * Column definitions for indexes table (post-3.0 layout). | |
| 57 | + */ | |
| 58 | + private static final ColumnDefinitions indexColumnDefs = new ColumnDefinitions(new ColumnDefinitions.Definition[]{ | |
| 59 | + definition(IndexMetadata.NAME, text()), | |
| 60 | + definition(IndexMetadata.KIND, text()), | |
| 61 | + definition(IndexMetadata.OPTIONS, map(text(), text())) | |
| 62 | + }); | |
| 63 | + public static final TypeCodec.MapCodec<String, String> MAP_CODEC = new TypeCodec.MapCodec<String, String>(TypeCodec.VarcharCodec.instance, TypeCodec.VarcharCodec.instance); | |
| 64 | + | |
| 65 | + private static ProtocolVersion protocolVersion; | |
| 66 | + private static CodecRegistry codecRegistry; | |
| 67 | + | |
| 68 | + @BeforeClass(groups = { "short", "long" }) | |
| 69 | + public void beforeClass() { | |
| 70 | + super.beforeClass(); | |
| 71 | + protocolVersion = cluster.getConfiguration().getProtocolOptions().getProtocolVersion(); | |
| 72 | + codecRegistry = cluster.getConfiguration().getCodecRegistry(); | |
| 73 | + legacyColumnDefs.setCodecRegistry(codecRegistry); | |
| 74 | + indexColumnDefs.setCodecRegistry(codecRegistry); | |
| 53 | 75 | } |
| 54 | 76 | |
| 55 | 77 | @Override |
| 56 | 78 | protected Collection<String> getTableDefinitions() { |
| 57 | - String createTable = "CREATE TABLE indexing (" | |
| 58 | - + "id int primary key," | |
| 59 | - + "map_values map<text, int>," | |
| 60 | - + "map_keys map<text, int>," | |
| 61 | - + "map_entries map<text, int>," | |
| 62 | - + "text_column text" | |
| 63 | - + | |
| 64 | - // Frozen collections was introduced only in C* 2.1.3 | |
| 65 | - (cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) >= 0 | |
| 66 | - ? | |
| 67 | - ", map_full frozen<map<text, int>>," | |
| 68 | - + "set_full frozen<set<text>>," | |
| 69 | - + "list_full frozen<list<text>>);" | |
| 70 | - : | |
| 71 | - ")"); | |
| 72 | - return ImmutableList.of(createTable); | |
| 79 | + ImmutableList.Builder<String> builder = ImmutableList.builder(); | |
| 80 | + builder.add("CREATE TABLE indexing (" | |
| 81 | + + "id int," | |
| 82 | + + "id2 int," | |
| 83 | + + "map_values map<text, int>," | |
| 84 | + + "map_keys map<text, int>," | |
| 85 | + + "map_entries map<text, int>," | |
| 86 | + + "map_all map<text, int>," | |
| 87 | + + "text_column text, " | |
| 88 | + + "\"MixedCaseColumn\" list<text>," | |
| 89 | + + | |
| 90 | + // Frozen collections was introduced only in C* 2.1.3 | |
| 91 | + (cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) >= 0 | |
| 92 | + ? | |
| 93 | + ", map_full frozen<map<text, int>>," | |
| 94 | + + "set_full frozen<set<text>>," | |
| 95 | + + "list_full frozen<list<text>>," | |
| 96 | + : | |
| 97 | + "") | |
| 98 | + + "PRIMARY KEY (id, id2));" | |
| 99 | + ); | |
| 100 | + | |
| 101 | + return builder.build(); | |
| 73 | 102 | } |
| 74 | 103 | |
| 75 | 104 | @Test(groups = "short") |
| 76 | - public void should_not_flag_text_column_index_type() { | |
| 105 | + public void should_create_metadata_for_simple_index() { | |
| 77 | 106 | String createValuesIndex = String.format("CREATE INDEX text_column_index ON %s.indexing (text_column);", keyspace); |
| 78 | 107 | session.execute(createValuesIndex); |
| 79 | 108 | ColumnMetadata column = getColumn("text_column"); |
| 80 | 109 | IndexMetadata index = getIndex("text_column_index"); |
| 81 | - assertThat(index).hasName("text_column_index") | |
| 82 | - .isNotKeys() | |
| 83 | - .isNotFull() | |
| 84 | - .isNotEntries() | |
| 110 | + assertThat(index) | |
| 111 | + .hasName("text_column_index") | |
| 112 | + .hasParent((TableMetadata)column.getParent()) | |
| 113 | + .isNotCustomIndex() | |
| 114 | + .hasTarget("text_column") | |
| 115 | + .hasKind(COMPOSITES) | |
| 116 | + .asCqlQuery(createValuesIndex); | |
| 117 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 118 | + } | |
| 119 | + | |
| 120 | + @Test(groups = "short") | |
| 121 | + @CassandraVersion(major = 2.1, description = "index names with quoted identifiers and collection indexes not supported until 2.1") | |
| 122 | + public void should_create_metadata_for_values_index_on_mixed_case_column() { | |
| 123 | + // 3.0 assumes the 'values' keyword if index on a collection | |
| 124 | + String createValuesIndex = cassandraVersion.getMajor() > 2 ? | |
| 125 | + String.format("CREATE INDEX \"MixedCaseIndex\" ON %s.indexing (values(\"MixedCaseColumn\"));", keyspace) : | |
| 126 | + String.format("CREATE INDEX \"MixedCaseIndex\" ON %s.indexing (\"MixedCaseColumn\");", keyspace); | |
| 127 | + session.execute(createValuesIndex); | |
| 128 | + ColumnMetadata column = getColumn("\"MixedCaseColumn\""); | |
| 129 | + IndexMetadata index = getIndex("\"MixedCaseIndex\""); | |
| 130 | + assertThat(index) | |
| 131 | + .hasName("MixedCaseIndex") | |
| 132 | + .hasParent((TableMetadata)column.getParent()) | |
| 85 | 133 | .isNotCustomIndex() |
| 86 | - .hasSingleColumn(getColumn("text_column")) | |
| 134 | + .hasTarget(cassandraVersion.getMajor() > 2 ? "values(\"MixedCaseColumn\")" : "\"MixedCaseColumn\"") | |
| 135 | + .hasKind(COMPOSITES) | |
| 87 | 136 | .asCqlQuery(createValuesIndex); |
| 88 | - assertThat(index).isSameAs(column.getIndex("text_column_index")); | |
| 89 | - assertThat(column).hasSingleIndex(index); | |
| 90 | - assertThat(column).isSameAs(index.getColumn("text_column")); | |
| 137 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 91 | 138 | } |
| 92 | 139 | |
| 93 | 140 | @Test(groups = "short") |
| 94 | 141 | @CassandraVersion(major = 2.1) |
| 95 | - public void should_not_flag_map_index_type() { | |
| 96 | - String createValuesIndex = String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace); | |
| 142 | + public void should_create_metadata_for_index_on_map_values() { | |
| 143 | + // 3.0 assumes the 'values' keyword if index on a collection | |
| 144 | + String createValuesIndex = cassandraVersion.getMajor() > 2 ? | |
| 145 | + String.format("CREATE INDEX map_values_index ON %s.indexing (values(map_values));", keyspace) : | |
| 146 | + String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace); | |
| 97 | 147 | session.execute(createValuesIndex); |
| 98 | 148 | ColumnMetadata column = getColumn("map_values"); |
| 99 | 149 | IndexMetadata index = getIndex("map_values_index"); |
| 100 | - assertThat(index).hasName("map_values_index") | |
| 101 | - .isNotKeys() | |
| 102 | - .isNotFull() | |
| 103 | - .isNotEntries() | |
| 150 | + assertThat(index) | |
| 151 | + .hasName("map_values_index") | |
| 152 | + .hasParent((TableMetadata)column.getParent()) | |
| 104 | 153 | .isNotCustomIndex() |
| 105 | - .hasSingleColumn(getColumn("map_values")) | |
| 154 | + .hasTarget(cassandraVersion.getMajor() > 2 ? "values(map_values)" : "map_values") | |
| 155 | + .hasKind(COMPOSITES) | |
| 106 | 156 | .asCqlQuery(createValuesIndex); |
| 107 | - assertThat(index).isSameAs(column.getIndex("map_values_index")); | |
| 108 | - assertThat(column).hasSingleIndex(index); | |
| 109 | - assertThat(column).isSameAs(index.getColumn("map_values")); | |
| 157 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 110 | 158 | } |
| 111 | 159 | |
| 112 | 160 | @Test(groups = "short") |
| 113 | 161 | @CassandraVersion(major = 2.1) |
| 114 | - public void should_flag_map_index_type_as_keys() { | |
| 115 | - String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (KEYS(map_keys));", keyspace); | |
| 162 | + public void should_create_metadata_for_index_on_map_keys() { | |
| 163 | + String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (keys(map_keys));", keyspace); | |
| 116 | 164 | session.execute(createKeysIndex); |
| 117 | 165 | ColumnMetadata column = getColumn("map_keys"); |
| 118 | 166 | IndexMetadata index = getIndex("map_keys_index"); |
| 119 | - assertThat(index).hasName("map_keys_index") | |
| 120 | - .isKeys() | |
| 121 | - .isNotFull() | |
| 122 | - .isNotEntries() | |
| 167 | + assertThat(index) | |
| 168 | + .hasName("map_keys_index") | |
| 169 | + .hasParent((TableMetadata)column.getParent()) | |
| 123 | 170 | .isNotCustomIndex() |
| 124 | - .hasSingleColumn(getColumn("map_keys")) | |
| 171 | + .hasTarget("keys(map_keys)") | |
| 172 | + .hasKind(COMPOSITES) | |
| 125 | 173 | .asCqlQuery(createKeysIndex); |
| 126 | - assertThat(index).isSameAs(column.getIndex("map_keys_index")); | |
| 127 | - assertThat(column).hasSingleIndex(index); | |
| 128 | - assertThat(column).isSameAs(index.getColumn("map_keys")); | |
| 174 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 129 | 175 | } |
| 130 | 176 | |
| 131 | 177 | @Test(groups = "short") |
| 132 | 178 | @CassandraVersion(major = 2.1, minor = 3) |
| 133 | - public void should_flag_map_index_type_as_full() { | |
| 134 | - String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (FULL(map_full));", keyspace); | |
| 179 | + public void should_create_metadata_for_full_index_on_map() { | |
| 180 | + String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (full(map_full));", keyspace); | |
| 135 | 181 | session.execute(createFullIndex); |
| 136 | 182 | ColumnMetadata column = getColumn("map_full"); |
| 137 | 183 | IndexMetadata index = getIndex("map_full_index"); |
| 138 | - assertThat(index).hasName("map_full_index") | |
| 139 | - .isNotKeys() | |
| 140 | - .isFull() | |
| 141 | - .isNotEntries() | |
| 184 | + assertThat(index) | |
| 185 | + .hasName("map_full_index") | |
| 186 | + .hasParent((TableMetadata)column.getParent()) | |
| 142 | 187 | .isNotCustomIndex() |
| 143 | - .hasSingleColumn(getColumn("map_full")) | |
| 188 | + .hasTarget("full(map_full)") | |
| 189 | + .hasKind(COMPOSITES) | |
| 144 | 190 | .asCqlQuery(createFullIndex); |
| 145 | - assertThat(index).isSameAs(column.getIndex("map_full_index")); | |
| 146 | - assertThat(column).hasSingleIndex(index); | |
| 147 | - assertThat(column).isSameAs(index.getColumn("map_full")); | |
| 191 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 148 | 192 | } |
| 149 | 193 | |
| 150 | 194 | @Test(groups = "short") |
| 151 | 195 | @CassandraVersion(major = 2.1, minor = 3) |
| 152 | - public void should_flag_set_index_type_as_full() { | |
| 153 | - String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (FULL(set_full));", keyspace); | |
| 196 | + public void should_create_metadata_for_full_index_on_set() { | |
| 197 | + String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (full(set_full));", keyspace); | |
| 154 | 198 | session.execute(createFullIndex); |
| 155 | 199 | ColumnMetadata column = getColumn("set_full"); |
| 156 | 200 | IndexMetadata index = getIndex("set_full_index"); |
| 157 | - assertThat(index).hasName("set_full_index") | |
| 158 | - .isNotKeys() | |
| 159 | - .isFull() | |
| 160 | - .isNotEntries() | |
| 201 | + assertThat(index) | |
| 202 | + .hasName("set_full_index") | |
| 203 | + .hasParent((TableMetadata)column.getParent()) | |
| 161 | 204 | .isNotCustomIndex() |
| 162 | - .hasSingleColumn(getColumn("set_full")) | |
| 205 | + .hasTarget("full(set_full)") | |
| 206 | + .hasKind(COMPOSITES) | |
| 163 | 207 | .asCqlQuery(createFullIndex); |
| 164 | - assertThat(index).isSameAs(column.getIndex("set_full_index")); | |
| 165 | - assertThat(column).hasSingleIndex(index); | |
| 166 | - assertThat(column).isSameAs(index.getColumn("set_full")); | |
| 208 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 167 | 209 | } |
| 168 | 210 | |
| 169 | 211 | @Test(groups = "short") |
| 170 | 212 | @CassandraVersion(major = 2.1, minor = 3) |
| 171 | - public void should_flag_list_index_type_as_full() { | |
| 172 | - String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (FULL(list_full));", keyspace); | |
| 213 | + public void should_create_metadata_for_full_index_on_list() { | |
| 214 | + String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (full(list_full));", keyspace); | |
| 173 | 215 | session.execute(createFullIndex); |
| 174 | 216 | ColumnMetadata column = getColumn("list_full"); |
| 175 | 217 | IndexMetadata index = getIndex("list_full_index"); |
| 176 | - assertThat(index).hasName("list_full_index") | |
| 177 | - .isNotKeys() | |
| 178 | - .isFull() | |
| 179 | - .isNotEntries() | |
| 218 | + assertThat(index) | |
| 219 | + .hasName("list_full_index") | |
| 220 | + .hasParent((TableMetadata)column.getParent()) | |
| 180 | 221 | .isNotCustomIndex() |
| 181 | - .hasSingleColumn(getColumn("list_full")) | |
| 222 | + .hasTarget("full(list_full)") | |
| 223 | + .hasKind(COMPOSITES) | |
| 182 | 224 | .asCqlQuery(createFullIndex); |
| 183 | - assertThat(index).isSameAs(column.getIndex("list_full_index")); | |
| 184 | - assertThat(column).hasSingleIndex(index); | |
| 185 | - assertThat(column).isSameAs(index.getColumn("list_full")); | |
| 225 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 186 | 226 | } |
| 187 | 227 | |
| 188 | 228 | @Test(groups = "short") |
| 189 | 229 | @CassandraVersion(major = 2.2) |
| 190 | - public void should_flag_map_index_type_as_entries() { | |
| 191 | - String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (ENTRIES(map_entries));", keyspace); | |
| 230 | + public void should_create_metadata_for_index_on_map_entries() { | |
| 231 | + String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (entries(map_entries));", keyspace); | |
| 192 | 232 | session.execute(createEntriesIndex); |
| 193 | 233 | ColumnMetadata column = getColumn("map_entries"); |
| 194 | 234 | IndexMetadata index = getIndex("map_entries_index"); |
| 195 | - assertThat(index).hasName("map_entries_index") | |
| 196 | - .isNotKeys() | |
| 197 | - .isNotFull() | |
| 198 | - .isEntries() | |
| 235 | + assertThat(index) | |
| 236 | + .hasName("map_entries_index") | |
| 237 | + .hasParent((TableMetadata)column.getParent()) | |
| 199 | 238 | .isNotCustomIndex() |
| 200 | - .hasSingleColumn(getColumn("map_entries")) | |
| 239 | + .hasTarget("entries(map_entries)") | |
| 240 | + .hasKind(COMPOSITES) | |
| 241 | + .asCqlQuery(createEntriesIndex); | |
| 242 | + assertThat((TableMetadata) column.getParent()).hasIndex(index); | |
| 243 | + } | |
| 244 | + | |
| 245 | + @Test(groups = "short") | |
| 246 | + @CassandraVersion(major = 3.0) | |
| 247 | + public void should_allow_multiple_indexes_on_map_column() { | |
| 248 | + String createEntriesIndex = String.format("CREATE INDEX map_all_entries_index ON %s.indexing (entries(map_all));", keyspace); | |
| 249 | + session.execute(createEntriesIndex); | |
| 250 | + String createKeysIndex = String.format("CREATE INDEX map_all_keys_index ON %s.indexing (keys(map_all));", keyspace); | |
| 251 | + session.execute(createKeysIndex); | |
| 252 | + String createValuesIndex = String.format("CREATE INDEX map_all_values_index ON %s.indexing (values(map_all));", keyspace); | |
| 253 | + session.execute(createValuesIndex); | |
| 254 | + | |
| 255 | + ColumnMetadata column = getColumn("map_all"); | |
| 256 | + TableMetadata table = (TableMetadata)column.getParent(); | |
| 257 | + | |
| 258 | + assertThat(getIndex("map_all_entries_index")) | |
| 259 | + .hasParent(table) | |
| 201 | 260 | .asCqlQuery(createEntriesIndex); |
| 202 | - assertThat(index).isSameAs(column.getIndex("map_entries_index")); | |
| 203 | - assertThat(column).hasSingleIndex(index); | |
| 204 | - assertThat(column).isSameAs(index.getColumn("map_entries")); | |
| 261 | + | |
| 262 | + assertThat(getIndex("map_all_keys_index")) | |
| 263 | + .hasParent(table) | |
| 264 | + .asCqlQuery(createKeysIndex); | |
| 265 | + | |
| 266 | + assertThat(getIndex("map_all_values_index")) | |
| 267 | + .hasParent(table) | |
| 268 | + .asCqlQuery(createValuesIndex); | |
| 205 | 269 | } |
| 206 | 270 | |
| 207 | 271 | @Test( |
| 208 | 272 | groups = "short", |
| 209 | - description = "This test case builds a ColumnMetadata object programmatically to test custom indices," | |
| 273 | + description = "This test case builds a ColumnMetadata object programmatically to test custom indices with pre-3.0 layout," | |
| 210 | 274 | + "otherwise, it would require deploying an actual custom index class into the C* test cluster") |
| 211 | - public void should_parse_custom_index_options() { | |
| 275 | + public void should_parse_legacy_custom_index_options() { | |
| 212 | 276 | TableMetadata table = getTable("indexing"); |
| 213 | - List<ByteBuffer> data = ImmutableList.of( | |
| 277 | + List<ByteBuffer> columnData = ImmutableList.of( | |
| 214 | 278 | wrap("text_column"), // column name |
| 215 | 279 | wrap(0), // component index |
| 216 | - wrap("regular"), // kind | |
| 280 | + wrap("regular"), // column kind | |
| 217 | 281 | wrap("custom_index"), // index name |
| 218 | 282 | wrap("CUSTOM"), // index type |
| 219 | 283 | wrap("org.apache.cassandra.db.marshal.UTF8Type"), // validator |
| 220 | 284 | wrap("{\"foo\" : \"bar\", \"class_name\" : \"dummy.DummyIndex\"}") // index options |
| 221 | 285 | ); |
| 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); | |
| 228 | - assertThat(index).hasName("custom_index") | |
| 229 | - .isNotKeys() | |
| 230 | - .isNotFull() | |
| 231 | - .isNotEntries() | |
| 286 | + Row columnRow = ArrayBackedRow.fromData(legacyColumnDefs, M3PToken.FACTORY, protocolVersion, columnData); | |
| 287 | + Raw columnRaw = Raw.fromRow(columnRow, VersionNumber.parse("2.1"), protocolVersion, codecRegistry); | |
| 288 | + ColumnMetadata column = ColumnMetadata.fromRaw(table, columnRaw); | |
| 289 | + IndexMetadata index = IndexMetadata.fromLegacy(column, columnRaw); | |
| 290 | + assertThat(index) | |
| 291 | + .isNotNull() | |
| 292 | + .hasName("custom_index") | |
| 232 | 293 | .isCustomIndex() |
| 233 | - .hasSingleColumn(column) | |
| 234 | 294 | .hasOption("foo", "bar") |
| 295 | + .hasKind(CUSTOM) | |
| 235 | 296 | .asCqlQuery(String.format("CREATE CUSTOM INDEX custom_index ON %s.indexing (text_column) " |
| 236 | - + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar', 'class_name' : 'dummy.DummyIndex'};", keyspace)); | |
| 297 | + + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar'};", keyspace)); | |
| 298 | + } | |
| 299 | + | |
| 300 | + @Test( | |
| 301 | + groups = "short", | |
| 302 | + description = "This test case builds a ColumnMetadata object programmatically to test custom indices with post-3.0 layout," | |
| 303 | + + "otherwise, it would require deploying an actual custom index class into the C* test cluster") | |
| 304 | + public void should_parse_custom_index_options() { | |
| 305 | + TableMetadata table = getTable("indexing"); | |
| 306 | + List<ByteBuffer> indexData = ImmutableList.of( | |
| 307 | + wrap("custom_index"), // index name | |
| 308 | + wrap("CUSTOM"), // kind | |
| 309 | + MAP_CODEC.serialize(ImmutableMap.of( | |
| 310 | + "foo", "bar", | |
| 311 | + IndexMetadata.CUSTOM_INDEX_OPTION_NAME, "dummy.DummyIndex", | |
| 312 | + IndexMetadata.TARGET_OPTION_NAME, "a, b, keys(c)" | |
| 313 | + ), protocolVersion) // options | |
| 314 | + ); | |
| 315 | + Row indexRow = ArrayBackedRow.fromData(indexColumnDefs, M3PToken.FACTORY, protocolVersion, indexData); | |
| 316 | + IndexMetadata index = IndexMetadata.fromRow(table, indexRow); | |
| 317 | + assertThat(index) | |
| 318 | + .isNotNull() | |
| 319 | + .hasName("custom_index") | |
| 320 | + .isCustomIndex() | |
| 321 | + .hasOption("foo", "bar") | |
| 322 | + .hasTarget("a, b, keys(c)") | |
| 323 | + .hasKind(CUSTOM) | |
| 324 | + .asCqlQuery(String.format("CREATE CUSTOM INDEX custom_index ON %s.indexing (a, b, keys(c)) " | |
| 325 | + + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar'};", keyspace)); | |
| 237 | 326 | } |
| 238 | 327 | |
| 239 | 328 | /** |
| @@ -257,19 +346,20 @@ | ||
| 257 | 346 | wrap("org.apache.cassandra.db.marshal.BytesType"), // validator |
| 258 | 347 | wrap("null") // index options |
| 259 | 348 | ); |
| 260 | - Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), data); | |
| 349 | + Row row = ArrayBackedRow.fromData(legacyColumnDefs, M3PToken.FACTORY, cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), data); | |
| 261 | 350 | Raw raw = Raw.fromRow(row, VersionNumber.parse("2.1"), |
| 262 | 351 | cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), |
| 263 | 352 | cluster.getConfiguration().getCodecRegistry()); |
| 264 | 353 | ColumnMetadata column = ColumnMetadata.fromRaw(table, raw); |
| 265 | 354 | IndexMetadata index = IndexMetadata.fromLegacy(column, raw); |
| 266 | - assertThat(index).hasName("cfs_archive_parent_path") | |
| 267 | - .isNotKeys() // While the index type is KEYS, since it lacks index_options it does not considered. | |
| 268 | - .isNotFull() | |
| 269 | - .isNotEntries() | |
| 355 | + assertThat(index) | |
| 356 | + .isNotNull() | |
| 357 | + .hasName("cfs_archive_parent_path") | |
| 270 | 358 | .isNotCustomIndex() |
| 271 | - .hasSingleColumn(column) | |
| 359 | + .hasTarget("\"b@706172656e745f70617468\"") | |
| 360 | + .hasKind(KEYS) | |
| 272 | 361 | .asCqlQuery(String.format("CREATE INDEX cfs_archive_parent_path ON %s.indexing (\"b@706172656e745f70617468\");", keyspace)); |
| 362 | + assertThat(index.getOption(IndexMetadata.INDEX_KEYS_OPTION_NAME)).isNull(); // While the index type is KEYS, since it lacks index_options it does not get considered. | |
| 273 | 363 | } |
| 274 | 364 | |
| 275 | 365 | private static ColumnDefinitions.Definition definition(String name, DataType type) { |
| @@ -285,7 +375,12 @@ | ||
| 285 | 375 | } |
| 286 | 376 | |
| 287 | 377 | private ColumnMetadata getColumn(String name) { |
| 288 | - return getTable("indexing").getColumn(name); | |
| 378 | + return getColumn(name, true); | |
| 379 | + } | |
| 380 | + | |
| 381 | + private ColumnMetadata getColumn(String name, boolean fromTable) { | |
| 382 | + TableOrView target = fromTable ? getTable("indexing") : getMaterializedView("mv1"); | |
| 383 | + return target.getColumn(name); | |
| 289 | 384 | } |
| 290 | 385 | |
| 291 | 386 | private IndexMetadata getIndex(String name) { |
| @@ -296,4 +391,7 @@ | ||
| 296 | 391 | return cluster.getMetadata().getKeyspace(keyspace).getTable(name); |
| 297 | 392 | } |
| 298 | 393 | |
| 394 | + private MaterializedViewMetadata getMaterializedView(String name) { | |
| 395 | + return cluster.getMetadata().getKeyspace(keyspace).getMaterializedView(name); | |
| 396 | + } | |
| 299 | 397 | } |
modified driver-core/src/test/java/com/datastax/driver/core/MaterializedViewMetadataTest.java +2 −2
| @@ -46,10 +46,10 @@ | ||
| 46 | 46 | keyspace); |
| 47 | 47 | String createMV = String.format( |
| 48 | 48 | "CREATE MATERIALIZED VIEW %s.monthlyhigh AS " |
| 49 | - + "SELECT user FROM %s.scores " | |
| 49 | + + "SELECT game, year, month, score, user, day FROM %s.scores " | |
| 50 | 50 | + "WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND day IS NOT NULL " |
| 51 | 51 | + "PRIMARY KEY ((game, year, month), score, user, day) " |
| 52 | - + "WITH CLUSTERING ORDER BY (score DESC)", | |
| 52 | + + "WITH CLUSTERING ORDER BY (score DESC, user ASC, day ASC)", | |
| 53 | 53 | keyspace, keyspace); |
| 54 | 54 | |
| 55 | 55 | // when |
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataAssert.java +5 −0
| @@ -49,4 +49,9 @@ | ||
| 49 | 49 | assertThat(actual.getView(expected.getName())).isNotNull().isEqualTo(expected); |
| 50 | 50 | return this; |
| 51 | 51 | } |
| 52 | + | |
| 53 | + public TableMetadataAssert hasIndex(IndexMetadata index) { | |
| 54 | + assertThat(actual.getIndexes()).contains(index); | |
| 55 | + return this; | |
| 56 | + } | |
| 52 | 57 | } |