Elffuss BraidHub

kikocis/java-driver public

Merge pull request #334 from grighetto/2.1

JAVA-732 Expose KEYS and FULL indexing options in IndexMetadata.
Thanks @grighetto !
Changeset9d1fa9824d128ded3f0ea852502dbcff42f5708293a22576e4c6af4844848c9b
AuthorAlexandre Dutra <alexdut@gmail.com> · 2015-05-28 14:58 +02:00
CommitterAlexandre Dutra <alexdut@gmail.com> · 2015-05-28 14:58 +02:00
Parents0d6180208ede, f22d1085a049

Browse the files at this changeset

5 files changed

modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +112 −20
@@ -18,23 +18,27 @@
1818 import java.util.Arrays;
1919 import java.util.Collections;
2020 import java.util.HashMap;
21+import java.util.Iterator;
2122 import java.util.Map;
23+import java.util.Map.Entry;
2224
2325 /**
2426 * Describes a Column.
2527 */
2628 public class ColumnMetadata {
2729
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";
3638 private static final String CUSTOM_INDEX_CLASS = "class_name";
3739
40+ private static final String INDEX_MAP_KEYS = "index_keys";
41+ private static final String INDEX_MAP_ENTRIES = "index_keys_and_values";
3842 private final TableMetadata table;
3943 private final String name;
4044 private final DataType type;
@@ -110,12 +114,16 @@
110114
111115 private final ColumnMetadata column;
112116 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;
114118
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) {
116124 this.column = column;
117125 this.name = name;
118- this.customClassName = customClassName;
126+ this.indexOptions = indexOptions;
119127 }
120128
121129 /**
@@ -146,7 +154,7 @@
146154 * @return {@code true} if this metadata represents a custom index.
147155 */
148156 public boolean isCustomIndex() {
149- return customClassName != null;
157+ return getIndexClassName() != null;
150158 }
151159
152160 /**
@@ -156,7 +164,54 @@
156164 * custom index if {@code isCustomIndex() == true}, {@code null} otherwise.
157165 */
158166 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;
160215 }
161216
162217 /**
@@ -173,8 +228,45 @@
173228 String cfName = Metadata.escapeId(table.getName());
174229 String colName = Metadata.escapeId(column.getName());
175230 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 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;
178270 }
179271
180272 private static IndexMetadata build(ColumnMetadata column, Map<String, String> indexColumns) {
@@ -185,11 +277,11 @@
185277 if (type == null)
186278 return null;
187279
188- if (!type.equalsIgnoreCase("CUSTOM") || !indexColumns.containsKey(INDEX_OPTIONS))
189- return new IndexMetadata(column, indexColumns.get(INDEX_NAME), null);
280+ if (!indexColumns.containsKey(INDEX_OPTIONS))
281+ return new IndexMetadata(column, indexColumns.get(INDEX_NAME));
190282
191283 Map<String, String> indexOptions = SimpleJSONParser.parseStringMap(indexColumns.get(INDEX_OPTIONS));
192- return new IndexMetadata(column, indexColumns.get(INDEX_NAME), indexOptions.get(CUSTOM_INDEX_CLASS));
284+ return new IndexMetadata(column, indexColumns.get(INDEX_NAME), indexOptions);
193285 }
194286 }
195287
@@ -224,8 +316,8 @@
224316
225317 String name = row.getString(COLUMN_NAME);
226318 Kind kind = version.getMajor() < 2 || row.isNull(KIND)
227- ? Kind.REGULAR
228- : Enum.valueOf(Kind.class, row.getString(KIND).toUpperCase());
319+ ? Kind.REGULAR
320+ : Enum.valueOf(Kind.class, row.getString(KIND).toUpperCase());
229321 int componentIndex = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX);
230322 String validatorStr = row.getString(VALIDATOR);
231323 boolean reversed = CassandraTypeParser.isReversed(validatorStr);
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +6 −0
@@ -15,6 +15,8 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import com.datastax.driver.core.ColumnMetadata.IndexMetadata;
19+
1820 /**
1921 * Augment AssertJ with custom assertions for the Java driver.
2022 */
@@ -34,4 +36,8 @@
3436 public static DataTypeAssert assertThat(DataType type) {
3537 return new DataTypeAssert(type);
3638 }
39+
40+ public static IndexMetadataAssert assertThat(IndexMetadata indexMetadata) {
41+ return new IndexMetadataAssert(indexMetadata);
42+ }
3743 }
modified driver-core/src/test/java/com/datastax/driver/core/CCMBridge.java +2 −0
@@ -458,6 +458,8 @@
458458
459459 protected static Cluster cluster;
460460 protected static Session session;
461+
462+ protected final VersionNumber cassandraVersion = VersionNumber.parse(System.getProperty("cassandra.version"));
461463
462464 protected abstract Collection<String> getTableDefinitions();
463465
added driver-core/src/test/java/com/datastax/driver/core/IndexMetadataAssert.java +70 −0
@@ -0,0 +1,70 @@
1+package com.datastax.driver.core;
2+
3+import static org.assertj.core.api.Assertions.assertThat;
4+
5+import org.assertj.core.api.AbstractAssert;
6+
7+import com.datastax.driver.core.ColumnMetadata.IndexMetadata;
8+
9+public class IndexMetadataAssert extends AbstractAssert<IndexMetadataAssert, IndexMetadata> {
10+
11+ public IndexMetadataAssert(IndexMetadata actual) {
12+ super(actual, IndexMetadataAssert.class);
13+ }
14+
15+ public IndexMetadataAssert hasName(String name) {
16+ assertThat(actual.getName()).isEqualTo(name);
17+ return this;
18+ }
19+
20+ public IndexMetadataAssert hasOption(String name, String value) {
21+ assertThat(actual.getOption(name)).isEqualTo(value);
22+ return this;
23+ }
24+
25+ public IndexMetadataAssert asCqlQuery(String cqlQuery) {
26+ assertThat(actual.asCQLQuery()).isEqualTo(cqlQuery);
27+ return this;
28+ }
29+
30+ public IndexMetadataAssert isKeys(){
31+ assertThat(actual.isKeys()).isTrue();
32+ return this;
33+ }
34+
35+ public IndexMetadataAssert isNotKeys(){
36+ assertThat(actual.isKeys()).isFalse();
37+ return this;
38+ }
39+
40+ public IndexMetadataAssert isFull() {
41+ assertThat(actual.isFull()).isTrue();
42+ return this;
43+ }
44+
45+ public IndexMetadataAssert isNotFull() {
46+ assertThat(actual.isFull()).isFalse();
47+ return this;
48+ }
49+
50+ public IndexMetadataAssert isEntries() {
51+ assertThat(actual.isEntries()).isTrue();
52+ return this;
53+ }
54+
55+ public IndexMetadataAssert isNotEntries() {
56+ assertThat(actual.isEntries()).isFalse();
57+ return this;
58+ }
59+
60+ public IndexMetadataAssert isCustomIndex() {
61+ assertThat(actual.isCustomIndex()).isTrue();
62+ return this;
63+ }
64+
65+ public IndexMetadataAssert isNotCustomIndex() {
66+ assertThat(actual.isCustomIndex()).isFalse();
67+ return this;
68+ }
69+
70+}
added driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +225 −0
@@ -0,0 +1,225 @@
1+package com.datastax.driver.core;
2+
3+import static com.datastax.driver.core.Assertions.assertThat;
4+import static com.datastax.driver.core.ColumnMetadata.COLUMN_NAME;
5+import static com.datastax.driver.core.ColumnMetadata.COMPONENT_INDEX;
6+import static com.datastax.driver.core.ColumnMetadata.INDEX_NAME;
7+import static com.datastax.driver.core.ColumnMetadata.INDEX_OPTIONS;
8+import static com.datastax.driver.core.ColumnMetadata.INDEX_TYPE;
9+import static com.datastax.driver.core.ColumnMetadata.KIND;
10+import static com.datastax.driver.core.ColumnMetadata.VALIDATOR;
11+import static com.datastax.driver.core.DataType.cint;
12+import static com.datastax.driver.core.DataType.text;
13+
14+import java.nio.ByteBuffer;
15+import java.util.Collection;
16+import java.util.List;
17+
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 = 1.2)
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+ + "text_column text"
39+ +
40+ // Frozen collections was introduced only in C* 2.1.3
41+ (cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) >= 0
42+ ?
43+ ", map_full frozen<map<text, int>>,"
44+ + "set_full frozen<set<text>>,"
45+ + "list_full frozen<list<text>>);"
46+ :
47+ ")");
48+ return ImmutableList.of(createTable);
49+ }
50+
51+ @Test(
52+ groups = "short")
53+ public void should_not_flag_text_column_index_type() {
54+ String createValuesIndex = String.format("CREATE INDEX text_column_index ON %s.indexing (text_column);", keyspace);
55+ session.execute(createValuesIndex);
56+ IndexMetadata index = getIndexForColumn("text_column");
57+ assertThat(index).hasName("text_column_index")
58+ .isNotKeys()
59+ .isNotFull()
60+ .isNotEntries()
61+ .isNotCustomIndex()
62+ .asCqlQuery(createValuesIndex);
63+ }
64+
65+ @Test(
66+ groups = "short")
67+ @CassandraVersion(
68+ major = 2.1)
69+ public void should_not_flag_map_index_type() {
70+ String createValuesIndex = String.format("CREATE INDEX map_values_index ON %s.indexing (map_values);", keyspace);
71+ session.execute(createValuesIndex);
72+ IndexMetadata index = getIndexForColumn("map_values");
73+ assertThat(index).hasName("map_values_index")
74+ .isNotKeys()
75+ .isNotFull()
76+ .isNotEntries()
77+ .isNotCustomIndex()
78+ .asCqlQuery(createValuesIndex);
79+ }
80+
81+ @Test(
82+ groups = "short")
83+ @CassandraVersion(
84+ major = 2.1)
85+ public void should_flag_map_index_type_as_keys() {
86+ String createKeysIndex = String.format("CREATE INDEX map_keys_index ON %s.indexing (KEYS(map_keys));", keyspace);
87+ session.execute(createKeysIndex);
88+ IndexMetadata index = getIndexForColumn("map_keys");
89+ assertThat(index).hasName("map_keys_index")
90+ .isKeys()
91+ .isNotFull()
92+ .isNotEntries()
93+ .isNotCustomIndex()
94+ .asCqlQuery(createKeysIndex);
95+ }
96+
97+ @Test(
98+ groups = "short")
99+ @CassandraVersion(
100+ major = 2.1,
101+ minor = 3)
102+ public void should_flag_map_index_type_as_full() {
103+ String createFullIndex = String.format("CREATE INDEX map_full_index ON %s.indexing (FULL(map_full));", keyspace);
104+ session.execute(createFullIndex);
105+ IndexMetadata index = getIndexForColumn("map_full");
106+ assertThat(index).hasName("map_full_index")
107+ .isNotKeys()
108+ .isFull()
109+ .isNotEntries()
110+ .isNotCustomIndex()
111+ .asCqlQuery(createFullIndex);
112+ }
113+
114+ @Test(
115+ groups = "short")
116+ @CassandraVersion(
117+ major = 2.1,
118+ minor = 3)
119+ public void should_flag_set_index_type_as_full() {
120+ String createFullIndex = String.format("CREATE INDEX set_full_index ON %s.indexing (FULL(set_full));", keyspace);
121+ session.execute(createFullIndex);
122+ IndexMetadata index = getIndexForColumn("set_full");
123+ assertThat(index).hasName("set_full_index")
124+ .isNotKeys()
125+ .isFull()
126+ .isNotEntries()
127+ .isNotCustomIndex()
128+ .asCqlQuery(createFullIndex);
129+ }
130+
131+ @Test(
132+ groups = "short")
133+ @CassandraVersion(
134+ major = 2.1,
135+ minor = 3)
136+ public void should_flag_list_index_type_as_full() {
137+ String createFullIndex = String.format("CREATE INDEX list_full_index ON %s.indexing (FULL(list_full));", keyspace);
138+ session.execute(createFullIndex);
139+ IndexMetadata index = getIndexForColumn("list_full");
140+ assertThat(index).hasName("list_full_index")
141+ .isNotKeys()
142+ .isFull()
143+ .isNotEntries()
144+ .isNotCustomIndex()
145+ .asCqlQuery(createFullIndex);
146+ }
147+
148+ @Test(
149+ groups = "short")
150+ @CassandraVersion(
151+ major = 3.0)
152+ public void should_flag_map_index_type_as_entries() {
153+ String createEntriesIndex = String.format("CREATE INDEX map_entries_index ON %s.indexing (ENTRIES(map_entries));", keyspace);
154+ session.execute(createEntriesIndex);
155+ IndexMetadata index = getIndexForColumn("map_entries");
156+ assertThat(index).hasName("map_entries_index")
157+ .isNotKeys()
158+ .isNotFull()
159+ .isEntries()
160+ .isNotCustomIndex()
161+ .asCqlQuery(createEntriesIndex);
162+ }
163+
164+ @Test(
165+ groups = "short",
166+ description = "This test case builds a ColumnMetadata object programatically to test custom indices,"
167+ + "otherwise, it would require deploying an actual custom index class into the C* test cluster")
168+ public void should_parse_custom_index_options() {
169+ TableMetadata table = getTable("indexing");
170+ ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[] {
171+ definition(COLUMN_NAME, text()),
172+ definition(COMPONENT_INDEX, cint()),
173+ definition(KIND, text()),
174+ definition(INDEX_NAME, text()),
175+ definition(INDEX_TYPE, text()),
176+ definition(VALIDATOR, text()),
177+ definition(INDEX_OPTIONS, text())
178+ });
179+ List<ByteBuffer> data = ImmutableList.of(
180+ wrap("text_column"), // column name
181+ wrap(0), // component index
182+ wrap("regular"), // kind
183+ wrap("custom_index"), // index name
184+ wrap("CUSTOM"), // index type
185+ wrap("org.apache.cassandra.db.marshal.UTF8Type"), // validator
186+ wrap("{\"foo\" : \"bar\", \"class_name\" : \"dummy.DummyIndex\"}") // index options
187+ );
188+ Row row = ArrayBackedRow.fromData(defs, M3PToken.FACTORY, ProtocolVersion.V3, data);
189+ ColumnMetadata column = ColumnMetadata.fromRaw(table, Raw.fromRow(row, VersionNumber.parse("2.1")));
190+ IndexMetadata index = column.getIndex();
191+ assertThat(index).hasName("custom_index")
192+ .isNotKeys()
193+ .isNotFull()
194+ .isNotEntries()
195+ .isCustomIndex()
196+ .hasOption("foo", "bar")
197+ .asCqlQuery(String.format("CREATE CUSTOM INDEX custom_index ON %s.indexing (text_column) "
198+ + "USING 'dummy.DummyIndex' WITH OPTIONS = {'foo' : 'bar', 'class_name' : 'dummy.DummyIndex'};", keyspace));
199+ }
200+
201+ private ColumnDefinitions.Definition definition(String name, DataType type) {
202+ return new ColumnDefinitions.Definition("ks", "table", name, type);
203+ }
204+
205+ private ByteBuffer wrap(String value) {
206+ return ByteBuffer.wrap(value.getBytes());
207+ }
208+
209+ private ByteBuffer wrap(int number) {
210+ return ByteBuffer.wrap(Ints.toByteArray(number));
211+ }
212+
213+ private IndexMetadata getIndexForColumn(String columnName) {
214+ return getColumn(columnName).getIndex();
215+ }
216+
217+ private ColumnMetadata getColumn(String name) {
218+ return getTable("indexing").getColumn(name);
219+ }
220+
221+ private TableMetadata getTable(String name) {
222+ return cluster.getMetadata().getKeyspace(keyspace).getTable(name);
223+ }
224+
225+}