kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 62321c99b59c79dab7ad4c59c53a14cb379113038c29dbb1907557687ad8102c |
|---|---|
| Author | Gianluca Righetto <gianluca@menttis.com> · 2015-05-04 05:09 -03:00 |
| Committer | Gianluca Righetto <gianluca@menttis.com> · 2015-05-04 05:09 -03:00 |
| Parents | f589d67997c1 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +113 −20
| @@ -18,23 +18,27 @@ | ||
| 18 | 18 | import java.util.Arrays; |
| 19 | 19 | import java.util.Collections; |
| 20 | 20 | import java.util.HashMap; |
| 21 | +import java.util.Iterator; | |
| 21 | 22 | import java.util.Map; |
| 23 | +import java.util.Map.Entry; | |
| 22 | 24 | |
| 23 | 25 | /** |
| 24 | 26 | * Describes a Column. |
| 25 | 27 | */ |
| 26 | 28 | public class ColumnMetadata { |
| 27 | 29 | |
| 28 | - private static final String COLUMN_NAME = "column_name"; | |
| 29 | - private static final String VALIDATOR = "validator"; | |
| 30 | - private static final String COMPONENT_INDEX = "component_index"; | |
| 31 | - private static final String KIND = "type"; | |
| 32 | - | |
| 33 | - private static final String INDEX_TYPE = "index_type"; | |
| 34 | - private static final String INDEX_OPTIONS = "index_options"; | |
| 35 | - private static final String INDEX_NAME = "index_name"; | |
| 30 | + static final String COLUMN_NAME = "column_name"; | |
| 31 | + static final String VALIDATOR = "validator"; | |
| 32 | + static final String COMPONENT_INDEX = "component_index"; | |
| 33 | + static final String KIND = "type"; | |
| 34 | + | |
| 35 | + static final String INDEX_TYPE = "index_type"; | |
| 36 | + static final String INDEX_OPTIONS = "index_options"; | |
| 37 | + static final String INDEX_NAME = "index_name"; | |
| 36 | 38 | private static final String CUSTOM_INDEX_CLASS = "class_name"; |
| 37 | 39 | |
| 40 | + private static final String INDEX_MAP_KEYS = "index_keys"; | |
| 41 | + private static final String INDEX_MAP_ENTRIES = "index_keys_and_values"; | |
| 38 | 42 | private final TableMetadata table; |
| 39 | 43 | private final String name; |
| 40 | 44 | private final DataType type; |
| @@ -110,12 +114,16 @@ | ||
| 110 | 114 | |
| 111 | 115 | private final ColumnMetadata column; |
| 112 | 116 | private final String name; |
| 113 | - private final String customClassName; // will be null, unless it's a custom index | |
| 117 | + private final Map<String, String> indexOptions; | |
| 114 | 118 | |
| 115 | - private IndexMetadata(ColumnMetadata column, String name, String customClassName) { | |
| 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) { | |
| 116 | 124 | this.column = column; |
| 117 | 125 | this.name = name; |
| 118 | - this.customClassName = customClassName; | |
| 126 | + this.indexOptions = indexOptions; | |
| 119 | 127 | } |
| 120 | 128 | |
| 121 | 129 | /** |
| @@ -146,7 +154,7 @@ | ||
| 146 | 154 | * @return {@code true} if this metadata represents a custom index. |
| 147 | 155 | */ |
| 148 | 156 | public boolean isCustomIndex() { |
| 149 | - return customClassName != null; | |
| 157 | + return getIndexClassName() != null; | |
| 150 | 158 | } |
| 151 | 159 | |
| 152 | 160 | /** |
| @@ -156,7 +164,54 @@ | ||
| 156 | 164 | * custom index if {@code isCustomIndex() == true}, {@code null} otherwise. |
| 157 | 165 | */ |
| 158 | 166 | public String getIndexClassName() { |
| 159 | - return customClassName; | |
| 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().isCollection() | |
| 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; | |
| 160 | 215 | } |
| 161 | 216 | |
| 162 | 217 | /** |
| @@ -173,8 +228,46 @@ | ||
| 173 | 228 | String cfName = Metadata.escapeId(table.getName()); |
| 174 | 229 | String colName = Metadata.escapeId(column.getName()); |
| 175 | 230 | return isCustomIndex() |
| 176 | - ? String.format("CREATE CUSTOM INDEX %s ON %s.%s (%s) USING '%s';", name, ksName, cfName, colName, customClassName) | |
| 177 | - : String.format("CREATE INDEX %s ON %s.%s (%s);", name, ksName, cfName, colName); | |
| 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 JSON object based on the index options map. | |
| 238 | + * | |
| 239 | + * @return String representation of the JSON object containing the index options, | |
| 240 | + * similar to what Cassandra stores in the 'index_options' column of the 'schema_columns' table | |
| 241 | + * in the 'system' keyspace. | |
| 242 | + */ | |
| 243 | + private String getOptionsAsCql() { | |
| 244 | + StringBuilder builder = new StringBuilder(); | |
| 245 | + builder.append("{"); | |
| 246 | + Iterator<Entry<String, String>> it = indexOptions.entrySet().iterator(); | |
| 247 | + while (it.hasNext()) { | |
| 248 | + Entry<String, String> option = it.next(); | |
| 249 | + builder.append(String.format("'%s' : '%s'", option.getKey(), option.getValue())); | |
| 250 | + if (it.hasNext()) | |
| 251 | + builder.append(", "); | |
| 252 | + } | |
| 253 | + builder.append("}"); | |
| 254 | + return builder.toString(); | |
| 255 | + } | |
| 256 | + | |
| 257 | + /** | |
| 258 | + * Wraps the column name with the appropriate index function (KEYS, FULL, ENTRIES), | |
| 259 | + * if necessary. | |
| 260 | + * | |
| 261 | + * @return Column name wrapped with the appropriate index function. | |
| 262 | + */ | |
| 263 | + private String getIndexFunction(String colName) { | |
| 264 | + if (isKeys()) | |
| 265 | + return String.format("KEYS(%s)", colName); | |
| 266 | + else if (isFull()) | |
| 267 | + return String.format("FULL(%s)", colName); | |
| 268 | + else if (isEntries()) | |
| 269 | + return String.format("ENTRIES(%s)", colName); | |
| 270 | + return colName; | |
| 178 | 271 | } |
| 179 | 272 | |
| 180 | 273 | private static IndexMetadata build(ColumnMetadata column, Map<String, String> indexColumns) { |
| @@ -185,11 +278,11 @@ | ||
| 185 | 278 | if (type == null) |
| 186 | 279 | return null; |
| 187 | 280 | |
| 188 | - if (!type.equalsIgnoreCase("CUSTOM") || !indexColumns.containsKey(INDEX_OPTIONS)) | |
| 189 | - return new IndexMetadata(column, indexColumns.get(INDEX_NAME), null); | |
| 281 | + if (!indexColumns.containsKey(INDEX_OPTIONS)) | |
| 282 | + return new IndexMetadata(column, indexColumns.get(INDEX_NAME)); | |
| 190 | 283 | |
| 191 | 284 | Map<String, String> indexOptions = SimpleJSONParser.parseStringMap(indexColumns.get(INDEX_OPTIONS)); |
| 192 | - return new IndexMetadata(column, indexColumns.get(INDEX_NAME), indexOptions.get(CUSTOM_INDEX_CLASS)); | |
| 285 | + return new IndexMetadata(column, indexColumns.get(INDEX_NAME), indexOptions); | |
| 193 | 286 | } |
| 194 | 287 | } |
| 195 | 288 | |
| @@ -224,8 +317,8 @@ | ||
| 224 | 317 | |
| 225 | 318 | String name = row.getString(COLUMN_NAME); |
| 226 | 319 | Kind kind = version.getMajor() < 2 || row.isNull(KIND) |
| 227 | - ? Kind.REGULAR | |
| 228 | - : Enum.valueOf(Kind.class, row.getString(KIND).toUpperCase()); | |
| 320 | + ? Kind.REGULAR | |
| 321 | + : Enum.valueOf(Kind.class, row.getString(KIND).toUpperCase()); | |
| 229 | 322 | int componentIndex = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX); |
| 230 | 323 | String validatorStr = row.getString(VALIDATOR); |
| 231 | 324 | boolean reversed = CassandraTypeParser.isReversed(validatorStr); |
added driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +200 −0
| @@ -0,0 +1,200 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import static com.datastax.driver.core.ColumnMetadata.COLUMN_NAME; | |
| 4 | +import static com.datastax.driver.core.ColumnMetadata.COMPONENT_INDEX; | |
| 5 | +import static com.datastax.driver.core.ColumnMetadata.INDEX_NAME; | |
| 6 | +import static com.datastax.driver.core.ColumnMetadata.INDEX_OPTIONS; | |
| 7 | +import static com.datastax.driver.core.ColumnMetadata.INDEX_TYPE; | |
| 8 | +import static com.datastax.driver.core.ColumnMetadata.KIND; | |
| 9 | +import static com.datastax.driver.core.ColumnMetadata.VALIDATOR; | |
| 10 | +import static com.datastax.driver.core.DataType.cint; | |
| 11 | +import static com.datastax.driver.core.DataType.text; | |
| 12 | + | |
| 13 | +import java.nio.ByteBuffer; | |
| 14 | +import java.util.Collection; | |
| 15 | +import java.util.List; | |
| 16 | + | |
| 17 | +import org.testng.Assert; | |
| 18 | +import org.testng.annotations.Test; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.ColumnMetadata.IndexMetadata; | |
| 21 | +import com.datastax.driver.core.ColumnMetadata.Raw; | |
| 22 | +import com.datastax.driver.core.Token.M3PToken; | |
| 23 | +import com.datastax.driver.core.utils.CassandraVersion; | |
| 24 | +import com.google.common.collect.ImmutableList; | |
| 25 | +import com.google.common.primitives.Ints; | |
| 26 | + | |
| 27 | +@CassandraVersion( | |
| 28 | + major = 2.1) | |
| 29 | +public class IndexMetadataTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + protected Collection<String> getTableDefinitions() { | |
| 33 | + String createTable = "CREATE TABLE indexing (" | |
| 34 | + + "id int primary key," | |
| 35 | + + "map_values map<text, int>," | |
| 36 | + + "map_keys map<text, int>," | |
| 37 | + + "map_entries map<text, int>," | |
| 38 | + + "map_full frozen<map<text, int>>," | |
| 39 | + + "set_full frozen<set<text>>," | |
| 40 | + + "list_full frozen<list<text>>," | |
| 41 | + + "custom text)"; | |
| 42 | + return ImmutableList.of(createTable); | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Test( | |
| 46 | + groups = "short") | |
| 47 | + public void should_not_flag_map_index_type() { | |
| 48 | + String createValuesIndex = String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace); | |
| 49 | + session.execute(createValuesIndex); | |
| 50 | + IndexMetadata index = getIndexForColumn("map_values"); | |
| 51 | + Assert.assertEquals(index.getName(), "map_values_index"); | |
| 52 | + Assert.assertFalse(index.isKeys()); | |
| 53 | + Assert.assertFalse(index.isFull()); | |
| 54 | + Assert.assertFalse(index.isEntries()); | |
| 55 | + Assert.assertFalse(index.isCustomIndex()); | |
| 56 | + Assert.assertEquals(index.asCQLQuery(), createValuesIndex); | |
| 57 | + } | |
| 58 | + | |
| 59 | + @Test( | |
| 60 | + groups = "short") | |
| 61 | + public void should_flag_map_index_type_as_keys() { | |
| 62 | + String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (KEYS(map_keys));", keyspace); | |
| 63 | + session.execute(createKeysIndex); | |
| 64 | + IndexMetadata index = getIndexForColumn("map_keys"); | |
| 65 | + Assert.assertEquals(index.getName(), "map_keys_index"); | |
| 66 | + Assert.assertTrue(index.isKeys()); | |
| 67 | + Assert.assertFalse(index.isFull()); | |
| 68 | + Assert.assertFalse(index.isEntries()); | |
| 69 | + Assert.assertFalse(index.isCustomIndex()); | |
| 70 | + Assert.assertEquals(index.asCQLQuery(), createKeysIndex); | |
| 71 | + } | |
| 72 | + | |
| 73 | + @Test( | |
| 74 | + groups = "short") | |
| 75 | + @CassandraVersion( | |
| 76 | + major = 2.1, | |
| 77 | + minor = 3) | |
| 78 | + public void should_flag_map_index_type_as_full() { | |
| 79 | + String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (FULL(map_full));", keyspace); | |
| 80 | + session.execute(createFullIndex); | |
| 81 | + IndexMetadata index = getIndexForColumn("map_full"); | |
| 82 | + Assert.assertEquals(index.getName(), "map_full_index"); | |
| 83 | + Assert.assertFalse(index.isKeys()); | |
| 84 | + Assert.assertTrue(index.isFull()); | |
| 85 | + Assert.assertFalse(index.isEntries()); | |
| 86 | + Assert.assertFalse(index.isCustomIndex()); | |
| 87 | + Assert.assertEquals(index.asCQLQuery(), createFullIndex); | |
| 88 | + } | |
| 89 | + | |
| 90 | + @Test( | |
| 91 | + groups = "short") | |
| 92 | + @CassandraVersion( | |
| 93 | + major = 2.1, | |
| 94 | + minor = 3) | |
| 95 | + public void should_flag_set_index_type_as_full() { | |
| 96 | + String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (FULL(set_full));", keyspace); | |
| 97 | + session.execute(createFullIndex); | |
| 98 | + IndexMetadata index = getIndexForColumn("set_full"); | |
| 99 | + Assert.assertEquals(index.getName(), "set_full_index"); | |
| 100 | + Assert.assertFalse(index.isKeys()); | |
| 101 | + Assert.assertTrue(index.isFull()); | |
| 102 | + Assert.assertFalse(index.isEntries()); | |
| 103 | + Assert.assertFalse(index.isCustomIndex()); | |
| 104 | + Assert.assertEquals(index.asCQLQuery(), createFullIndex); | |
| 105 | + } | |
| 106 | + | |
| 107 | + @Test( | |
| 108 | + groups = "short") | |
| 109 | + @CassandraVersion( | |
| 110 | + major = 2.1, | |
| 111 | + minor = 3) | |
| 112 | + public void should_flag_list_index_type_as_full() { | |
| 113 | + String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (FULL(list_full));", keyspace); | |
| 114 | + session.execute(createFullIndex); | |
| 115 | + IndexMetadata index = getIndexForColumn("list_full"); | |
| 116 | + Assert.assertEquals(index.getName(), "list_full_index"); | |
| 117 | + Assert.assertFalse(index.isKeys()); | |
| 118 | + Assert.assertTrue(index.isFull()); | |
| 119 | + Assert.assertFalse(index.isEntries()); | |
| 120 | + Assert.assertFalse(index.isCustomIndex()); | |
| 121 | + Assert.assertEquals(index.asCQLQuery(), createFullIndex); | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Test( | |
| 125 | + groups = "short") | |
| 126 | + @CassandraVersion( | |
| 127 | + major = 3.0) | |
| 128 | + public void should_flag_map_index_type_as_entries() { | |
| 129 | + String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (ENTRIES(map_entries));", keyspace); | |
| 130 | + session.execute(createEntriesIndex); | |
| 131 | + IndexMetadata index = getIndexForColumn("map_entries"); | |
| 132 | + Assert.assertEquals(index.getName(), "map_entries_index"); | |
| 133 | + Assert.assertFalse(index.isKeys()); | |
| 134 | + Assert.assertFalse(index.isFull()); | |
| 135 | + Assert.assertTrue(index.isEntries()); | |
| 136 | + Assert.assertFalse(index.isCustomIndex()); | |
| 137 | + Assert.assertEquals(index.asCQLQuery(), createEntriesIndex); | |
| 138 | + } | |
| 139 | + | |
| 140 | + @Test( | |
| 141 | + groups = "short", | |
| 142 | + description = "This test case builds a ColumnMetadata object programatically to test custom indices," | |
| 143 | + + "otherwise, it would require deploying an actual custom index class into the C* test cluster") | |
| 144 | + public void should_parse_custom_index_options() { | |
| 145 | + TableMetadata table = getTable("indexing"); | |
| 146 | + ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[] { | |
| 147 | + definition(COLUMN_NAME, text()), | |
| 148 | + definition(COMPONENT_INDEX, cint()), | |
| 149 | + definition(KIND, text()), | |
| 150 | + definition(INDEX_NAME, text()), | |
| 151 | + definition(INDEX_TYPE, text()), | |
| 152 | + definition(VALIDATOR, text()), | |
| 153 | + definition(INDEX_OPTIONS, text()) | |
| 154 | + }); | |
| 155 | + List<ByteBuffer> data = ImmutableList.of( | |
| 156 | + wrap("custom"), // column name | |
| 157 | + wrap(0), // component index | |
| 158 | + wrap("regular"), // kind | |
| 159 | + wrap("custom_index"), // index name | |
| 160 | + wrap("CUSTOM"), // index type | |
| 161 | + wrap("org.apache.cassandra.db.marshal.UTF8Type"), // validator | |
| 162 | + wrap("{\"foo\" : \"bar\", \"class_name\" : \"dummy.DummyIndex\"}") // index options | |
| 163 | + ); | |
| 164 | + Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, ProtocolVersion.V3, data); | |
| 165 | + ColumnMetadata column = ColumnMetadata.fromRaw(table, Raw.fromRow(row, VersionNumber.parse("2.1"))); | |
| 166 | + IndexMetadata index = column.getIndex(); | |
| 167 | + Assert.assertFalse(index.isKeys()); | |
| 168 | + Assert.assertFalse(index.isFull()); | |
| 169 | + Assert.assertFalse(index.isEntries()); | |
| 170 | + Assert.assertTrue(index.isCustomIndex()); | |
| 171 | + Assert.assertEquals(index.getOption("foo"), "bar"); | |
| 172 | + Assert.assertEquals(index.asCQLQuery(), "CREATE CUSTOM INDEX custom_index ON ks_1.indexing (custom) " | |
| 173 | + + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar', 'class_name' : 'dummy.DummyIndex'};"); | |
| 174 | + } | |
| 175 | + | |
| 176 | + private ColumnDefinitions.Definition definition(String name, DataType type) { | |
| 177 | + return new ColumnDefinitions.Definition("ks", "table", name, type); | |
| 178 | + } | |
| 179 | + | |
| 180 | + private ByteBuffer wrap(String value) { | |
| 181 | + return ByteBuffer.wrap(value.getBytes()); | |
| 182 | + } | |
| 183 | + | |
| 184 | + private ByteBuffer wrap(int number) { | |
| 185 | + return ByteBuffer.wrap(Ints.toByteArray(number)); | |
| 186 | + } | |
| 187 | + | |
| 188 | + private IndexMetadata getIndexForColumn(String columnName) { | |
| 189 | + return getColumn(columnName).getIndex(); | |
| 190 | + } | |
| 191 | + | |
| 192 | + private ColumnMetadata getColumn(String name) { | |
| 193 | + return getTable("indexing").getColumn(name); | |
| 194 | + } | |
| 195 | + | |
| 196 | + private TableMetadata getTable(String name) { | |
| 197 | + return cluster.getMetadata().getKeyspace(keyspace).getTable(name); | |
| 198 | + } | |
| 199 | + | |
| 200 | +} |