kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 065e7cdfa26040d02ec57979c9458b9784e36a52724163127cd49bac76a9efc3 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-04-04 18:21 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-04-19 12:15 +02:00 |
| Parents | 2539c251781f |
3 files changed
modified changelog/README.md +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [improvement] JAVA-1154: Allow individual Statement to cancel the read timeout. |
| 9 | 9 | - [bug] JAVA-1074: Fix documentation around default timestamp generator. |
| 10 | 10 | - [improvement] JAVA-1109: Document SSLOptions changes in upgrade guide. |
| 11 | +- [improvement] JAVA-1065: Add method to create token from partition key values. | |
| 11 | 12 | |
| 12 | 13 | Merged from 2.1 branch: |
| 13 | 14 |
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +18 −0
| @@ -460,6 +460,24 @@ | ||
| 460 | 460 | return current.factory.fromString(tokenStr); |
| 461 | 461 | } |
| 462 | 462 | |
| 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 | + | |
| 463 | 481 | /** |
| 464 | 482 | * Builds a new {@link TokenRange}. |
| 465 | 483 | * |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +13 −0
| @@ -379,5 +379,18 @@ | ||
| 379 | 379 | .isEqualTo(range); |
| 380 | 380 | } |
| 381 | 381 | |
| 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 | + | |
| 382 | 395 | protected abstract Token.Factory tokenFactory(); |
| 383 | 396 | } |