Elffuss BraidHub

kikocis/java-driver public

JAVA-912: Integrate changes to 'columns' table.
Changeset99740bf2fccd98e0b859e36686877eecaddde1b336e39a338b31fe2c47d087fc
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-11 13:14 +02:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-09-11 13:14 +02:00
Parentsa3d96ee58d97

Browse the files at this changeset

5 files changed

modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +35 −19
@@ -16,11 +16,6 @@
1616 package com.datastax.driver.core;
1717
1818 import java.util.*;
19-import java.util.Map.Entry;
20-
21-import com.google.common.base.Function;
22-import com.google.common.collect.ImmutableMap;
23-import com.google.common.collect.Maps;
2419
2520 /**
2621 * Describes a Column.
@@ -28,9 +23,15 @@
2823 public class ColumnMetadata {
2924
3025 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";
26+
27+ static final String VALIDATOR = "validator"; // v2 only
28+ static final String TYPE = "type"; // replaces validator, v3 onwards
29+
30+ static final String COMPONENT_INDEX = "component_index"; // v2 only
31+ static final String POSITION = "position"; // replaces component_index, v3 onwards
32+
33+ static final String KIND_V2 = "type"; // v2 only
34+ static final String KIND_V3 = "kind"; // replaces type, v3 onwards
3435
3536 static final String INDEX_TYPE = "index_type";
3637 static final String INDEX_OPTIONS = "index_options";
@@ -163,35 +164,50 @@
163164
164165 public final String name;
165166 public Kind kind;
166- public final int componentIndex;
167+ public final int position;
167168 public final DataType dataType;
168169 public final boolean isReversed;
169170
170171 public final Map<String, String> indexColumns = new HashMap<String, String>();
171172
172- Raw(String name, Kind kind, int componentIndex, DataType dataType, boolean isReversed) {
173+ Raw(String name, Kind kind, int position, DataType dataType, boolean isReversed) {
173174 this.name = name;
174175 this.kind = kind;
175- this.componentIndex = componentIndex;
176+ this.position = position;
176177 this.dataType = dataType;
177178 this.isReversed = isReversed;
178179 }
179180
180181 static Raw fromRow(Row row, VersionNumber version, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
181182 String name = row.getString(COLUMN_NAME);
183+
182184 Kind kind;
183- if(version.getMajor() < 2 || row.isNull(KIND)) {
185+ if(version.getMajor() < 2) {
184186 kind = Kind.REGULAR;
185187 } else if (version.getMajor() < 3) {
186- kind = Kind.fromStringV2(row.getString(KIND));
188+ kind = row.isNull(KIND_V2) ? Kind.REGULAR : Kind.fromStringV2(row.getString(KIND_V2));
189+ } else {
190+ kind = row.isNull(KIND_V3) ? Kind.REGULAR : Kind.fromStringV3(row.getString(KIND_V3));
191+ }
192+
193+ int position;
194+ if(version.getMajor() >= 3) {
195+ position = row.getInt(POSITION); // cannot be null, -1 is used as a special value instead of null to avoid tombstones
196+ if(position == -1) position = 0;
197+ } else {
198+ position = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX);
199+ }
200+
201+ String dataTypeStr;
202+ if(version.getMajor() >= 3) {
203+ dataTypeStr = row.getString(TYPE);
187204 } else {
188- kind = Kind.fromStringV3(row.getString(KIND));
205+ dataTypeStr = row.getString(VALIDATOR);
189206 }
190- int componentIndex = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX);
191- String validatorStr = row.getString(VALIDATOR);
192- boolean reversed = CassandraTypeParser.isReversed(validatorStr);
193- DataType dataType = CassandraTypeParser.parseOne(validatorStr, protocolVersion, codecRegistry);
194- Raw c = new Raw(name, kind, componentIndex, dataType, reversed);
207+ DataType dataType = CassandraTypeParser.parseOne(dataTypeStr, protocolVersion, codecRegistry);
208+ boolean reversed = CassandraTypeParser.isReversed(dataTypeStr);
209+
210+ Raw c = new Raw(name, kind, position, dataType, reversed);
195211
196212 // secondary indexes (C* < 3.0.0)
197213 // from C* 3.0 onwards 2i are defined in a separate table
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +6 −6
@@ -225,11 +225,11 @@
225225 ColumnMetadata col = ColumnMetadata.fromRaw(tm, rawCol);
226226 switch (rawCol.kind) {
227227 case PARTITION_KEY:
228- partitionKey.set(rawCol.componentIndex, col);
228+ partitionKey.set(rawCol.position, col);
229229 break;
230230 case CLUSTERING_COLUMN:
231- clusteringColumns.set(rawCol.componentIndex, col);
232- clusteringOrder.set(rawCol.componentIndex, rawCol.isReversed ? Order.DESC : Order.ASC);
231+ clusteringColumns.set(rawCol.position, col);
232+ clusteringOrder.set(rawCol.position, rawCol.isReversed ? Order.DESC : Order.ASC);
233233 break;
234234 default:
235235 otherColumns.add(col);
@@ -347,7 +347,7 @@
347347 int maxId = -1;
348348 for (ColumnMetadata.Raw col : cols)
349349 if (col.kind == ColumnMetadata.Raw.Kind.PARTITION_KEY)
350- maxId = Math.max(maxId, col.componentIndex);
350+ maxId = Math.max(maxId, col.position);
351351 return maxId + 1;
352352 }
353353
@@ -355,14 +355,14 @@
355355 Collection<ColumnMetadata.Raw> cols,
356356 List<String> columnAliases,
357357 VersionNumber cassandraVersion) {
358- // In 2.0 onwards, this is relatively easy, we just find the biggest 'componentIndex' amongst the clustering columns.
358+ // In 2.0 onwards, this is relatively easy, we just find the biggest 'position' amongst the clustering columns.
359359 // For 1.2 however, this is slightly more subtle: we need to infer it based on whether the comparator is composite or not, and whether we have
360360 // regular columns or not.
361361 if (cassandraVersion.getMajor() >= 2) {
362362 int maxId = -1;
363363 for (ColumnMetadata.Raw col : cols)
364364 if (col.kind == ColumnMetadata.Raw.Kind.CLUSTERING_COLUMN)
365- maxId = Math.max(maxId, col.componentIndex);
365+ maxId = Math.max(maxId, col.position);
366366 return maxId + 1;
367367 } else {
368368 int size = comparator.types.size();
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +1 −1
@@ -41,7 +41,7 @@
4141 private static final ColumnDefinitions defs = new ColumnDefinitions(new ColumnDefinitions.Definition[]{
4242 definition(COLUMN_NAME, text()),
4343 definition(COMPONENT_INDEX, cint()),
44- definition(KIND, text()),
44+ definition(KIND_V2, text()),
4545 definition(INDEX_NAME, text()),
4646 definition(INDEX_TYPE, text()),
4747 definition(VALIDATOR, text()),
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +49 −24
@@ -174,32 +174,57 @@
174174 @Test(groups = "short")
175175 public void should_parse_table_options() {
176176 VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion();
177+
178+ // given
179+ String cql;
180+
181+ // Cassandra 3.0 +
177182 if (version.getMajor() > 2) {
178- // TODO adapt test for C* 3.0
179- throw new SkipException("Needs adjustments to work with Cassandra 3.0");
183+ cql = String.format("CREATE TABLE %s.with_options (\n"
184+ + " k text,\n"
185+ + " c1 int,\n"
186+ + " c2 int,\n"
187+ + " i int,\n"
188+ + " PRIMARY KEY (k, c1, c2)\n"
189+ + ") WITH CLUSTERING ORDER BY (c1 DESC, c2 ASC)\n"
190+ + " AND read_repair_chance = 0.5\n"
191+ + " AND dclocal_read_repair_chance = 0.6\n"
192+ + " AND speculative_retry = '99.9PERCENTILE'\n"
193+ // replicate_on_write not supported anymore in 3.0
194+ + " AND gc_grace_seconds = 42\n"
195+ + " AND bloom_filter_fp_chance = 0.01\n"
196+ // older caching formats not supported anymore in 3.0
197+ + " AND caching = { 'keys' : 'ALL', 'rows_per_partition' : 10 }\n"
198+ + " AND comment = 'My awesome table'\n"
199+ + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n"
200+ + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };",
201+ keyspace);
202+
203+ // older versions
204+ } else {
205+ cql = String.format("CREATE TABLE %s.with_options (\n"
206+ + " k text,\n"
207+ + " c1 int,\n"
208+ + " c2 int,\n"
209+ + " i int,\n"
210+ + " PRIMARY KEY (k, c1, c2)\n"
211+ + ") WITH CLUSTERING ORDER BY (c1 DESC, c2 ASC)\n"
212+ + " AND read_repair_chance = 0.5\n"
213+ + " AND dclocal_read_repair_chance = 0.6\n"
214+ + " AND replicate_on_write = true\n"
215+ + " AND gc_grace_seconds = 42\n"
216+ + " AND bloom_filter_fp_chance = 0.01\n"
217+ + " AND caching = 'ALL'\n"
218+ + " AND comment = 'My awesome table'\n"
219+ + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n"
220+ + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };",
221+ keyspace);
180222 }
181223
182- // given
183- String cql = String.format("CREATE TABLE %s.with_options (\n"
184- + " k text,\n"
185- + " c1 int,\n"
186- + " c2 int,\n"
187- + " i int,\n"
188- + " PRIMARY KEY (k, c1, c2)\n"
189- + ") WITH CLUSTERING ORDER BY (c1 DESC, c2 ASC)\n"
190- + " AND read_repair_chance = 0.5\n"
191- + " AND dclocal_read_repair_chance = 0.6\n"
192- + " AND replicate_on_write = true\n"
193- + " AND gc_grace_seconds = 42\n"
194- + " AND bloom_filter_fp_chance = 0.01\n"
195- + " AND caching = 'ALL'\n"
196- + " AND comment = 'My awesome table'\n"
197- + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n"
198- + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };",
199- keyspace);
200224 // when
201225 session.execute(cql);
202226 TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("with_options");
227+
203228 // then
204229 assertThat(table).isNotNull().hasName("with_options").hasNumberOfColumns(4).isNotCompactStorage();
205230 assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text());
@@ -217,13 +242,13 @@
217242 assertThat(table.getOptions().getBloomFilterFalsePositiveChance()).isEqualTo(0.01);
218243 assertThat(table.getOptions().getComment()).isEqualTo("My awesome table");
219244 assertThat(table.getOptions().getCaching()).contains(entry("keys", "ALL" ));
220- assertThat(table.getOptions().getCaching()).contains(entry("rows_per_partition", "ALL"));
245+ assertThat(table.getOptions().getCaching()).contains(entry("rows_per_partition", "10"));
221246 assertThat(table.getOptions().getCompaction()).contains(entry("class", "org.apache.cassandra.db.compaction.LeveledCompactionStrategy"));
222247 assertThat(table.getOptions().getCompaction()).contains(entry("sstable_size_in_mb", "15"));
223248 assertThat(table.getOptions().getCompression()).contains(entry("class", "org.apache.cassandra.io.compress.SnappyCompressor")); // sstable_compression becomes class
224249 assertThat(table.getOptions().getCompression()).contains(entry("chunk_length_in_kb", "128")); // note the "in" prefix
225250 assertThat(table.getOptions().getDefaultTimeToLive()).isEqualTo(0);
226- assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("99.0PERCENTILE");
251+ assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("99.9PERCENTILE");
227252 assertThat(table.getOptions().getIndexInterval()).isNull();
228253 assertThat(table.getOptions().getMinIndexInterval()).isEqualTo(128);
229254 assertThat(table.getOptions().getMaxIndexInterval()).isEqualTo(2048);
@@ -235,13 +260,13 @@
235260 .contains("bloom_filter_fp_chance = 0.01")
236261 .contains("comment = 'My awesome table'")
237262 .contains("'keys' : 'ALL'")
238- .contains("'rows_per_partition' : 'ALL'")
263+ .contains("'rows_per_partition' : 10")
239264 .contains("'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'")
240265 .contains("'sstable_size_in_mb' : 15")
241266 .contains("'class' : 'org.apache.cassandra.io.compress.SnappyCompressor'") // sstable_compression becomes class
242267 .contains("'chunk_length_in_kb' : 128") // note the "in" prefix
243268 .contains("default_time_to_live = 0")
244- .contains("speculative_retry = '99.0PERCENTILE'")
269+ .contains("speculative_retry = '99.9PERCENTILE'")
245270 .contains("min_index_interval = 128")
246271 .contains("max_index_interval = 2048")
247272 .doesNotContain(" index_interval")
modified pom.xml +1 −1
@@ -43,7 +43,7 @@
4343
4444 <properties>
4545 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46- <cassandra.version>3.0.0-beta1</cassandra.version>
46+ <cassandra.version>3.0.0-rc1</cassandra.version>
4747 <java.version>1.6</java.version>
4848 <log4j.version>1.2.17</log4j.version>
4949 <slf4j-log4j12.version>1.7.6</slf4j-log4j12.version>