Elffuss BraidHub

kikocis/java-driver public

Merge pull request #478 from datastax/java938

JAVA-938 Parse extensions into TableOrView Options.
Changeset9cf06281cd9f1e1be29e0ac1b02d4c66282bc253951801b16469a9ae5165f064
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-12 12:06 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-10-12 12:06 +02:00
Parentsccdf5a93b3d2, d9c6eca56604

Browse the files at this changeset

3 files changed

modified changelog/README.md +2 −1
@@ -1,10 +1,11 @@
11 ## Changelog
22
3-### 3.0.0-beta1
3+### 3.0.0-beta1 (in progress)
44
55 - [improvement] Change default consistency level to LOCAL_QUORUM (JAVA-926)
66 - [bug] Fix implementation of UserType.hashCode() (JAVA-942)
77 - [improvement] Don't delay UP/ADDED notifications if protocol version = V4 (JAVA-877)
8+- [improvement] Parse 'extensions' column in table metadata (JAVA-938)
89
910
1011 ### 3.0.0-alpha3
modified driver-core/src/main/java/com/datastax/driver/core/TableOrView.java +22 −2
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818
19+import java.nio.ByteBuffer;
1920 import java.util.*;
2021
2122 import com.google.common.base.Objects;
@@ -371,6 +372,7 @@
371372 private static final String MIN_INDEX_INTERVAL = "min_index_interval";
372373 private static final String MAX_INDEX_INTERVAL = "max_index_interval";
373374 private static final String CRC_CHECK_CHANCE = "crc_check_chance";
375+ private static final String EXTENSIONS = "extensions";
374376
375377 private static final boolean DEFAULT_REPLICATE_ON_WRITE = true;
376378 private static final double DEFAULT_BF_FP_CHANCE = 0.01;
@@ -402,6 +404,7 @@
402404 private final Map<String, String> compaction;
403405 private final Map<String, String> compression;
404406 private final Double crcCheckChance;
407+ private final Map<String, ByteBuffer> extensions;
405408
406409 Options(Row row, boolean isCompactStorage, VersionNumber version) {
407410
@@ -474,6 +477,11 @@
474477 : row.getDouble(CRC_CHECK_CHANCE);
475478 else
476479 this.crcCheckChance = null;
480+
481+ if(is300OrHigher)
482+ this.extensions = ImmutableMap.copyOf(row.getMap(EXTENSIONS, String.class, ByteBuffer.class));
483+ else
484+ this.extensions = ImmutableMap.of();
477485 }
478486
479487 private static boolean isNullOrAbsent(Row row, String name) {
@@ -675,6 +683,17 @@
675683 return compression;
676684 }
677685
686+ /**
687+ * Returns the extension options for this table.
688+ * <p>
689+ * For Cassandra versions prior to 3.0.0, this method always returns an empty map.
690+ *
691+ * @return an immutable map containing the extension options for this table.
692+ */
693+ public Map<String, ByteBuffer> getExtensions() {
694+ return extensions;
695+ }
696+
678697 @Override
679698 public boolean equals(Object other) {
680699 if (other == this)
@@ -699,14 +718,15 @@
699718 Objects.equal(this.minIndexInterval, that.minIndexInterval) &&
700719 Objects.equal(this.maxIndexInterval, that.maxIndexInterval) &&
701720 Objects.equal(this.compaction, that.compaction) &&
702- Objects.equal(this.compression, that.compression);
721+ Objects.equal(this.compression, that.compression) &&
722+ Objects.equal(this.extensions, that.extensions);
703723 }
704724
705725 @Override
706726 public int hashCode() {
707727 return Objects.hashCode(isCompactStorage, comment, readRepair, localReadRepair, replicateOnWrite, gcGrace,
708728 bfFpChance, caching, populateCacheOnFlush, memtableFlushPeriodMs, defaultTTL, speculativeRetry,
709- indexInterval, minIndexInterval, maxIndexInterval, compaction, compression);
729+ indexInterval, minIndexInterval, maxIndexInterval, compaction, compression, extensions);
710730 }
711731 }
712732 }
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +39 −0
@@ -15,9 +15,11 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.nio.ByteBuffer;
1819 import java.util.Collection;
1920 import java.util.Collections;
2021
22+import com.google.common.collect.ImmutableMap;
2123 import org.testng.annotations.Test;
2224
2325 import static org.assertj.core.api.Assertions.entry;
@@ -269,6 +271,7 @@
269271 assertThat(table.getOptions().getMaxIndexInterval()).isEqualTo(2048);
270272 assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // default
271273 assertThat(table.getOptions().getCrcCheckChance()).isEqualTo(0.5);
274+ assertThat(table.getOptions().getExtensions()).isEmpty(); // default
272275 assertThat(table.asCQLQuery())
273276 .contains("read_repair_chance = 0.5")
274277 .contains("dclocal_read_repair_chance = 0.6")
@@ -310,6 +313,7 @@
310313 assertThat(table.getOptions().getMinIndexInterval()).isEqualTo(128);
311314 assertThat(table.getOptions().getMaxIndexInterval()).isEqualTo(2048);
312315 assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // default
316+ assertThat(table.getOptions().getExtensions()).isEmpty();
313317 assertThat(table.asCQLQuery())
314318 .contains("read_repair_chance = 0.5")
315319 .contains("dclocal_read_repair_chance = 0.6")
@@ -349,6 +353,7 @@
349353 assertThat(table.getOptions().getMinIndexInterval()).isNull();
350354 assertThat(table.getOptions().getMaxIndexInterval()).isNull();
351355 assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // explicitly set
356+ assertThat(table.getOptions().getExtensions()).isEmpty();
352357 assertThat(table.asCQLQuery())
353358 .contains("read_repair_chance = 0.5")
354359 .contains("dclocal_read_repair_chance = 0.6")
@@ -387,6 +392,7 @@
387392 assertThat(table.getOptions().getMinIndexInterval()).isNull();
388393 assertThat(table.getOptions().getMaxIndexInterval()).isNull();
389394 assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // explicitly set
395+ assertThat(table.getOptions().getExtensions()).isEmpty();
390396 assertThat(table.asCQLQuery())
391397 .contains("read_repair_chance = 0.5")
392398 .contains("dclocal_read_repair_chance = 0.6")
@@ -474,4 +480,37 @@
474480 assertThat(table_cd.getIndexes().size()).isEqualTo(1);
475481 assertThat(table_cd.getIndexes().get(0).getName()).isEqualTo("test_d");
476482 }
483+
484+ /**
485+ * Validates that the 'extensions' option is properly parsed when set on a table.
486+ * This value is currently not modifiable via CQL so we fake out a table containing
487+ * populated extensions by updating the extensions column in system_schema.tables
488+ * and forcing a schema refresh on it.
489+ *
490+ * @jira_ticket JAVA-938, CASSANDRA-9426
491+ * @test_category metadata
492+ */
493+ @Test(groups = "short")
494+ @CassandraVersion(major=3.0)
495+ public void should_parse_extensions_from_table_options() throws Exception {
496+ // given
497+ // create a simple table and retrieve it's metadata from system_schema.tables.
498+ String cql = String.format("CREATE TABLE %s.table_with_extensions (\n"
499+ + " k text,\n"
500+ + " c int,\n"
501+ + " v timeuuid,\n"
502+ + " PRIMARY KEY (k, c)\n"
503+ + ");", keyspace);
504+ session.execute(cql);
505+
506+ // Manually change column value in system_schema.tables and force a schema refresh on that table.
507+ ImmutableMap<String, ByteBuffer> extensions = ImmutableMap.of("Hello", ByteBuffer.wrap("World".getBytes("UTF-8")));
508+ session.execute("update system_schema.tables set extensions=? where keyspace_name=? and table_name=?", extensions, keyspace, "table_with_extensions");
509+ cluster.manager.controlConnection.refreshSchema(SchemaElement.TABLE, keyspace, "table_with_extensions", null);
510+
511+ // when retrieving the table's metadata.
512+ TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("table_with_extensions");
513+ // then the table's options should contain populated extensions.
514+ assertThat(table.getOptions().getExtensions()).isEqualTo(extensions);
515+ }
477516 }