Elffuss BraidHub

kikocis/java-driver public

JAVA-1065: Add method to create token from partition key values.
Changeset065e7cdfa26040d02ec57979c9458b9784e36a52724163127cd49bac76a9efc3
Authorolim7t <omichallat+github@gmail.com> · 2016-04-04 18:21 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-19 12:15 +02:00
Parents2539c251781f

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -8,6 +8,7 @@
88 - [improvement] JAVA-1154: Allow individual Statement to cancel the read timeout.
99 - [bug] JAVA-1074: Fix documentation around default timestamp generator.
1010 - [improvement] JAVA-1109: Document SSLOptions changes in upgrade guide.
11+- [improvement] JAVA-1065: Add method to create token from partition key values.
1112
1213 Merged from 2.1 branch:
1314
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +18 −0
@@ -460,6 +460,24 @@
460460 return current.factory.fromString(tokenStr);
461461 }
462462
463+ /**
464+ * Builds a new {@link Token} from a partition key.
465+ *
466+ * @param components the components of the partition key, in their serialized form (obtained with
467+ * {@link TypeCodec#serialize(Object, ProtocolVersion)}).
468+ * @return the token.
469+ * @throws IllegalStateException if the token factory was not initialized. This would typically
470+ * happen if metadata was explicitly disabled with {@link QueryOptions#setMetadataEnabled(boolean)}
471+ * before startup.
472+ */
473+ public Token newToken(ByteBuffer... components) {
474+ TokenMap current = tokenMap;
475+ if (current == null)
476+ throw new IllegalStateException("Token factory not set. This should only happen if metadata was explicitly disabled");
477+ return current.factory.hash(
478+ SimpleStatement.compose(components));
479+ }
480+
463481 /**
464482 * Builds a new {@link TokenRange}.
465483 *
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +13 −0
@@ -379,5 +379,18 @@
379379 .isEqualTo(range);
380380 }
381381
382+ @Test(groups = "short")
383+ public void should_create_token_from_partition_key() {
384+ Metadata metadata = cluster().getMetadata();
385+
386+ Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one();
387+ Token expected = row.getToken(0);
388+
389+ ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion();
390+ assertThat(
391+ metadata.newToken(TypeCodec.cint().serialize(1, protocolVersion))
392+ ).isEqualTo(expected);
393+ }
394+
382395 protected abstract Token.Factory tokenFactory();
383396 }