kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 742f9af567f07289aab9d7fd7f578ab8e55fc480679d2fe6c78ddc4af391ed7c |
|---|---|
| Author | Pierre Laporte <pierre@pingtimeout.fr> · 2014-11-17 18:17 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-11-24 10:43 +01:00 |
| Parents | 2c8f595e4806 |
2 files changed
added driver-core/src/test/java/com/datastax/driver/core/schemabuilder/SchemaBuilderIT.java +310 −0
| @@ -0,0 +1,310 @@ | ||
| 1 | +package com.datastax.driver.core.schemabuilder; | |
| 2 | + | |
| 3 | +import java.util.ArrayList; | |
| 4 | +import java.util.Collection; | |
| 5 | +import java.util.Iterator; | |
| 6 | + | |
| 7 | +import org.testng.annotations.Test; | |
| 8 | + | |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 10 | +import static org.assertj.core.api.Assertions.fail; | |
| 11 | + | |
| 12 | +import com.datastax.driver.core.*; | |
| 13 | +import com.datastax.driver.core.schemabuilder.TableOptions.CompactionOptions.DateTieredCompactionStrategyOptions.TimeStampResolution; | |
| 14 | + | |
| 15 | +import static com.datastax.driver.core.schemabuilder.SchemaBuilder.dateTieredStrategy; | |
| 16 | +import static com.datastax.driver.core.schemabuilder.SchemaBuilder.percentile; | |
| 17 | +import static com.datastax.driver.core.schemabuilder.SchemaBuilder.rows; | |
| 18 | +import static com.datastax.driver.core.schemabuilder.SchemaBuilder.snappy; | |
| 19 | + | |
| 20 | +public class SchemaBuilderIT extends CCMBridge.PerClassSingleNodeCluster{ | |
| 21 | + | |
| 22 | + @Override | |
| 23 | + protected Collection<String> getTableDefinitions() { | |
| 24 | + return new ArrayList<String>(); | |
| 25 | + } | |
| 26 | + | |
| 27 | + @Test(groups = "short") | |
| 28 | + public void should_modify_table_metadata() { | |
| 29 | + TestUtils.versionCheck(2.1, 2, "This test requires Cassandra 2.1.2"); | |
| 30 | + | |
| 31 | + // Create a table | |
| 32 | + session.execute(SchemaBuilder.createTable("ks", "TableMetadata") | |
| 33 | + .addPartitionKey("a", DataType.cint()) | |
| 34 | + .addPartitionKey("b", DataType.cint()) | |
| 35 | + .addClusteringColumn("c", DataType.cint()) | |
| 36 | + .addClusteringColumn("d", DataType.cint()) | |
| 37 | + .withOptions() | |
| 38 | + .compactStorage() | |
| 39 | + ); | |
| 40 | + | |
| 41 | + // Modify the table metadata | |
| 42 | + session.execute(SchemaBuilder.alterTable("TableMetadata") | |
| 43 | + .withOptions() | |
| 44 | + .defaultTimeToLive(1337) | |
| 45 | + .bloomFilterFPChance(0.42) | |
| 46 | + .caching(SchemaBuilder.KeyCaching.ALL, rows(1)) | |
| 47 | + .gcGraceSeconds(1234567890) | |
| 48 | + .minIndexInterval(6) | |
| 49 | + .indexInterval(7) | |
| 50 | + .maxIndexInterval(8) | |
| 51 | + .comment("Useful comment") | |
| 52 | + .readRepairChance(0.123456) | |
| 53 | + .speculativeRetry(percentile(50)) | |
| 54 | + .dcLocalReadRepairChance(0.84) | |
| 55 | + .memtableFlushPeriodInMillis(1234567890) | |
| 56 | + .compactionOptions(dateTieredStrategy() | |
| 57 | + .baseTimeSeconds(1) | |
| 58 | + .minThreshold(2) | |
| 59 | + .maxThreshold(3) | |
| 60 | + .maxSSTableAgeDays(4) | |
| 61 | + .timestampResolution(TimeStampResolution.MILLISECONDS)) | |
| 62 | + .compressionOptions(snappy())); | |
| 63 | + | |
| 64 | + // Retrieve the metadata from Cassandra | |
| 65 | + ResultSet rows = session.execute("SELECT " | |
| 66 | + + "bloom_filter_fp_chance, " | |
| 67 | + + "caching, " | |
| 68 | + + "cf_id, " | |
| 69 | + + "column_aliases, " | |
| 70 | + + "comment, " | |
| 71 | + + "compaction_strategy_class, " | |
| 72 | + + "compaction_strategy_options, " | |
| 73 | + + "comparator, " | |
| 74 | + + "compression_parameters, " | |
| 75 | + + "default_time_to_live, " | |
| 76 | + + "default_validator, " | |
| 77 | + + "dropped_columns, " | |
| 78 | + + "gc_grace_seconds, " | |
| 79 | + + "index_interval, " | |
| 80 | + + "is_dense, " | |
| 81 | + + "key_aliases, " | |
| 82 | + + "key_validator, " | |
| 83 | + + "local_read_repair_chance, " | |
| 84 | + + "max_compaction_threshold, " | |
| 85 | + + "max_index_interval, " | |
| 86 | + + "memtable_flush_period_in_ms, " | |
| 87 | + + "min_compaction_threshold, " | |
| 88 | + + "min_index_interval, " | |
| 89 | + + "read_repair_chance, " | |
| 90 | + + "speculative_retry, " | |
| 91 | + + "subcomparator, " | |
| 92 | + + "type, " | |
| 93 | + + "value_alias " | |
| 94 | + + "FROM system.schema_columnfamilies " | |
| 95 | + + "WHERE keyspace_name='ks' AND columnfamily_name='tablemetadata'"); | |
| 96 | + for (Row row : rows) { | |
| 97 | + // There should be only one row | |
| 98 | + // Verify that every property we modified is correctly set | |
| 99 | + assertThat(row.getDouble("bloom_filter_fp_chance")).isEqualTo(0.42); | |
| 100 | + assertThat(row.getString("caching")).isEqualTo("{\"keys\":\"ALL\", \"rows_per_partition\":\"1\"}"); | |
| 101 | + assertThat(row.getUUID("cf_id")).isNotNull(); | |
| 102 | + assertThat(row.getString("column_aliases")).isEqualTo("[\"c\",\"d\"]"); | |
| 103 | + assertThat(row.getString("comment")).isEqualTo("Useful comment"); | |
| 104 | + assertThat(row.getString("compaction_strategy_class")) | |
| 105 | + .isEqualTo("org.apache.cassandra.db.compaction.DateTieredCompactionStrategy"); | |
| 106 | + assertThat(row.getString("compaction_strategy_options")).isEqualTo( | |
| 107 | + "{\"base_time_seconds\":\"1\",\"timestamp_resolution\":\"MILLISECONDS\",\"max_sstable_age_days\":\"4\",\"min_threshold\":\"2\",\"max_threshold\":\"3\"}"); | |
| 108 | + assertThat(row.getString("compression_parameters")) | |
| 109 | + .isEqualTo("{\"sstable_compression\":\"org.apache.cassandra.io.compress.SnappyCompressor\"}"); | |
| 110 | + assertThat(row.getInt("default_time_to_live")).isEqualTo(1337); | |
| 111 | + assertThat(row.getInt("gc_grace_seconds")).isEqualTo(1234567890); | |
| 112 | + assertThat(row.getInt("min_index_interval")).isEqualTo(6); | |
| 113 | + assertThat(row.getInt("max_index_interval")).isEqualTo(8); | |
| 114 | + assertThat(row.getString("key_aliases")).isEqualTo("[\"a\",\"b\"]"); | |
| 115 | + assertThat(row.getDouble("local_read_repair_chance")).isEqualTo(0.84); | |
| 116 | + assertThat(row.getInt("max_compaction_threshold")).isEqualTo(3); | |
| 117 | + assertThat(row.getInt("memtable_flush_period_in_ms")).isEqualTo(1234567890); | |
| 118 | + assertThat(row.getInt("min_compaction_threshold")).isEqualTo(2); | |
| 119 | + assertThat(row.getDouble("read_repair_chance")).isEqualTo(0.123456); | |
| 120 | + assertThat(row.getString("speculative_retry")).isEqualTo("50.0PERCENTILE"); | |
| 121 | + } | |
| 122 | + } | |
| 123 | + | |
| 124 | + @Test(groups = "short") | |
| 125 | + public void should_create_a_table_and_a_udt() { | |
| 126 | + TestUtils.versionCheck(2.1, 0, "This test requires Cassandra 2.1.0"); | |
| 127 | + | |
| 128 | + // Create a UDT and a table | |
| 129 | + session.execute(SchemaBuilder.createType("MyUDT") | |
| 130 | + .ifNotExists() | |
| 131 | + .addColumn("x", DataType.cint()) | |
| 132 | + ); | |
| 133 | + UDTType myUDT = UDTType.frozen("MyUDT"); | |
| 134 | + session.execute(SchemaBuilder.createTable("ks", "CreateTable") | |
| 135 | + .ifNotExists() | |
| 136 | + .addPartitionKey("a", DataType.cint()) | |
| 137 | + .addUDTPartitionKey("b", myUDT) | |
| 138 | + .addClusteringColumn("c", DataType.ascii()) | |
| 139 | + .addUDTClusteringColumn("d", myUDT) | |
| 140 | + .addUDTColumn("e", myUDT) | |
| 141 | + .addStaticColumn("f", DataType.bigint()) | |
| 142 | + .addUDTStaticColumn("g", myUDT) | |
| 143 | + .addUDTListColumn("h", myUDT) | |
| 144 | + .addUDTMapColumn("i", DataType.cboolean(), myUDT) | |
| 145 | + .addUDTMapColumn("j", myUDT, DataType.cboolean()) | |
| 146 | + .addUDTSetColumn("k", myUDT) | |
| 147 | + ); | |
| 148 | + | |
| 149 | + // Check columns a to k | |
| 150 | + ResultSet rows = session.execute( | |
| 151 | + "SELECT column_name, type, validator " | |
| 152 | + + "FROM system.schema_columns " | |
| 153 | + + "WHERE keyspace_name='ks' AND columnfamily_name='createtable'"); | |
| 154 | + Iterator<Row> iterator = rows.iterator(); | |
| 155 | + verifyNextColumnDefinition(iterator, "a", "partition_key", "org.apache.cassandra.db.marshal.Int32Type"); | |
| 156 | + verifyNextColumnDefinition(iterator, "b", "partition_key", "org.apache.cassandra.db.marshal.UserType"); | |
| 157 | + verifyNextColumnDefinition(iterator, "c", "clustering_key", "org.apache.cassandra.db.marshal.AsciiType"); | |
| 158 | + verifyNextColumnDefinition(iterator, "d", "clustering_key", "org.apache.cassandra.db.marshal.UserType"); | |
| 159 | + verifyNextColumnDefinition(iterator, "e", "regular", "org.apache.cassandra.db.marshal.UserType"); | |
| 160 | + verifyNextColumnDefinition(iterator, "f", "static", "org.apache.cassandra.db.marshal.LongType"); | |
| 161 | + verifyNextColumnDefinition(iterator, "g", "static", "org.apache.cassandra.db.marshal.UserType"); | |
| 162 | + verifyNextColumnDefinition(iterator, "h", "regular", "org.apache.cassandra.db.marshal.ListType" | |
| 163 | + , "org.apache.cassandra.db.marshal.UserType"); | |
| 164 | + verifyNextColumnDefinition(iterator, "i", "regular", "org.apache.cassandra.db.marshal.MapType" | |
| 165 | + , "org.apache.cassandra.db.marshal.BooleanType", "org.apache.cassandra.db.marshal.UserType"); | |
| 166 | + verifyNextColumnDefinition(iterator, "j", "regular", "org.apache.cassandra.db.marshal.MapType" | |
| 167 | + , "org.apache.cassandra.db.marshal.UserType", "org.apache.cassandra.db.marshal.BooleanType"); | |
| 168 | + verifyNextColumnDefinition(iterator, "k", "regular", "org.apache.cassandra.db.marshal.SetType" | |
| 169 | + , "org.apache.cassandra.db.marshal.UserType"); | |
| 170 | + } | |
| 171 | + | |
| 172 | + @Test(groups = "short") | |
| 173 | + public void should_add_and_drop_a_column() { | |
| 174 | + // Create a table, add a column to it with an alter table statement and delete that column | |
| 175 | + session.execute(SchemaBuilder.createTable("ks", "DropColumn") | |
| 176 | + .ifNotExists() | |
| 177 | + .addPartitionKey("a", DataType.cint()) | |
| 178 | + ); | |
| 179 | + | |
| 180 | + // Add and then drop a column | |
| 181 | + session.execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 182 | + .addColumn("b") | |
| 183 | + .type(DataType.cint()) | |
| 184 | + ); | |
| 185 | + session.execute(SchemaBuilder.alterTable("ks", "DropColumn") | |
| 186 | + .dropColumn("b") | |
| 187 | + ); | |
| 188 | + | |
| 189 | + // Check that only column a exist | |
| 190 | + ResultSet rows = session.execute( | |
| 191 | + "SELECT column_name, type, validator " | |
| 192 | + + "FROM system.schema_columns " | |
| 193 | + + "WHERE keyspace_name='ks' AND columnfamily_name='dropcolumn'"); | |
| 194 | + Iterator<Row> iterator = rows.iterator(); | |
| 195 | + verifyNextColumnDefinition(iterator, "a", "partition_key", "org.apache.cassandra.db.marshal.Int32Type"); | |
| 196 | + assertThat(iterator.hasNext()).isFalse(); | |
| 197 | + } | |
| 198 | + | |
| 199 | + private void verifyNextColumnDefinition(Iterator<Row> rowIterator, String columnName, String type, | |
| 200 | + String... validatorFragments) { | |
| 201 | + Row rowA = rowIterator.next(); | |
| 202 | + assertThat(rowA.getString("column_name")).isEqualTo(columnName); | |
| 203 | + assertThat(rowA.getString("type")).isEqualTo(type); | |
| 204 | + for (String validatorFragment : validatorFragments) { | |
| 205 | + assertThat(rowA.getString("validator")).contains(validatorFragment); | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + @Test(groups = "short") | |
| 210 | + public void should_drop_a_table() { | |
| 211 | + // Create a table | |
| 212 | + session.execute(SchemaBuilder.createTable("ks", "DropTable") | |
| 213 | + .addPartitionKey("a", DataType.cint()) | |
| 214 | + ); | |
| 215 | + | |
| 216 | + // Drop the table | |
| 217 | + session.execute(SchemaBuilder.dropTable("ks", "DropTable")); | |
| 218 | + session.execute(SchemaBuilder.dropTable("DropTable").ifExists()); | |
| 219 | + | |
| 220 | + ResultSet rows = session.execute( | |
| 221 | + "SELECT columnfamily_name " | |
| 222 | + + "FROM system.schema_columnfamilies " | |
| 223 | + + "WHERE keyspace_name='ks' AND columnfamily_name='droptable'"); | |
| 224 | + if (rows.iterator().hasNext()) { | |
| 225 | + fail("This table should have been deleted"); | |
| 226 | + } | |
| 227 | + } | |
| 228 | + | |
| 229 | + @Test(groups = "short") | |
| 230 | + public void should_create_an_index() { | |
| 231 | + // Create a table | |
| 232 | + session.execute(SchemaBuilder.createTable("ks", "CreateIndex") | |
| 233 | + .addPartitionKey("a", DataType.cint()) | |
| 234 | + .addClusteringColumn("b", DataType.cint()) | |
| 235 | + .addColumn("c", DataType.map(DataType.cint(), DataType.cint())) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + // Create an index on a regular column of the table | |
| 239 | + session.execute(SchemaBuilder.createIndex("ks_Index") | |
| 240 | + .onTable("ks", "CreateIndex") | |
| 241 | + .andColumn("b") | |
| 242 | + ); | |
| 243 | + session.execute(SchemaBuilder.createIndex("ks_IndexOnMap") | |
| 244 | + .onTable("ks", "CreateIndex") | |
| 245 | + .andKeysOfColumn("c") | |
| 246 | + ); | |
| 247 | + | |
| 248 | + // Verify that the indexes exist on the right columns | |
| 249 | + ResultSet rows = session.execute( | |
| 250 | + "SELECT column_name, index_name, index_options, index_type, component_index " | |
| 251 | + + "FROM system.schema_columns " | |
| 252 | + + "WHERE keyspace_name='ks' " | |
| 253 | + + "AND columnfamily_name='createindex' " | |
| 254 | + + "AND column_name IN ('b', 'c')"); | |
| 255 | + Iterator<Row> iterator = rows.iterator(); | |
| 256 | + verifyNextIndexDefinition(iterator, "ks_Index", "{}", "COMPOSITES", 0); | |
| 257 | + verifyNextIndexDefinition(iterator, "ks_IndexOnMap", "{\"index_keys\":\"\"}", "COMPOSITES", 1); | |
| 258 | + assertThat(iterator.hasNext()).isFalse(); | |
| 259 | + } | |
| 260 | + | |
| 261 | + private void verifyNextIndexDefinition(Iterator<Row> iterator, String name, String options, String type, | |
| 262 | + int index) { | |
| 263 | + Row nextIndex = iterator.next(); | |
| 264 | + assertThat(nextIndex.getString("index_name")).isEqualTo(name); | |
| 265 | + assertThat(nextIndex.getString("index_options")).isEqualTo(options); | |
| 266 | + assertThat(nextIndex.getString("index_type")).isEqualTo(type); | |
| 267 | + assertThat(nextIndex.getInt("component_index")).isEqualTo(index); | |
| 268 | + } | |
| 269 | + | |
| 270 | + @Test(groups = "short") | |
| 271 | + public void should_drop_an_index() { | |
| 272 | + // Create a table | |
| 273 | + session.execute(SchemaBuilder.createTable("ks", "DropIndex") | |
| 274 | + .addPartitionKey("a", DataType.cint()) | |
| 275 | + .addClusteringColumn("b", DataType.cint()) | |
| 276 | + ); | |
| 277 | + | |
| 278 | + // Create an index | |
| 279 | + // Note: we have to pick a lower-case name because Cassandra uses the CamelCase index name at creation | |
| 280 | + // but a lowercase index name at deletion | |
| 281 | + // See : https://issues.apache.org/jira/browse/CASSANDRA-8365 | |
| 282 | + session.execute(SchemaBuilder.createIndex("ks_index") | |
| 283 | + .onTable("ks", "DropIndex") | |
| 284 | + .andColumn("b") | |
| 285 | + ); | |
| 286 | + | |
| 287 | + // Verify that the PK index and the secondary indexes both exist | |
| 288 | + assertThat(numberOfIndexedColumns("ks", "dropindex")).isEqualTo(1); | |
| 289 | + | |
| 290 | + // Delete the index | |
| 291 | + session.execute(SchemaBuilder.dropIndex("ks", "ks_index")); | |
| 292 | + | |
| 293 | + // Verify that only the PK index exists | |
| 294 | + assertThat(numberOfIndexedColumns("ks", "dropindex")).isEqualTo(0); | |
| 295 | + } | |
| 296 | + | |
| 297 | + private int numberOfIndexedColumns(String keyspace, String table) { | |
| 298 | + ResultSet columns = session.execute( | |
| 299 | + "SELECT * " | |
| 300 | + + "FROM system.schema_columns " | |
| 301 | + + "WHERE keyspace_name='ks' " | |
| 302 | + + "AND columnfamily_name='dropindex' "); | |
| 303 | + int count = 0; | |
| 304 | + for (Row column : columns) { | |
| 305 | + if (column.getString("index_name") != null) | |
| 306 | + count += 1; | |
| 307 | + } | |
| 308 | + return count; | |
| 309 | + } | |
| 310 | +} |
modified pom.xml +1 −1
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | |
| 39 | 39 | <properties> |
| 40 | 40 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 41 | - <cassandra.version>2.1.1</cassandra.version> | |
| 41 | + <cassandra.version>2.1.2</cassandra.version> | |
| 42 | 42 | </properties> |
| 43 | 43 | |
| 44 | 44 | <dependencies> |