Elffuss BraidHub

kikocis/java-driver public

Expose public API to build tokens and ranges (JAVA-681).
Changeset78fd0af56d43592f3f00d546305aa84a7eb09b3083d1ec3f91511b08633f6d71
Authorolim7t <omichallat+github@gmail.com> · 2015-03-05 14:41 -08:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-05 15:50 -08:00
Parents65d9c6f82538

Browse the files at this changeset

4 files changed

modified driver-core/CHANGELOG.rst +1 −1
@@ -7,7 +7,7 @@
77 - [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518)
88 - [improvement] Add connection heartbeat (JAVA-533)
99 - [improvement] Reduce level of logs on missing rpc_address (JAVA-568)
10-- [improvement] Expose node token and range information (JAVA-312)
10+- [improvement] Expose node token and range information (JAVA-312, JAVA-681)
1111 - [bug] Fix cluster name mismatch check at startup (JAVA-595)
1212 - [bug] Fix guava dependency when using OSGI (JAVA-620)
1313 - [bug] Fix handling of DROP events when ks name is case-sensitive (JAVA-678)
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +32 −0
@@ -27,6 +27,8 @@
2727 import org.slf4j.Logger;
2828 import org.slf4j.LoggerFactory;
2929
30+import com.datastax.driver.core.exceptions.DriverInternalError;
31+
3032 /**
3133 * Keeps metadata on the connected cluster, including known nodes and schema definitions.
3234 */
@@ -407,6 +409,36 @@
407409 return sb.toString();
408410 }
409411
412+ /**
413+ * Builds a new {@link Token} from its string representation, according to the partitioner
414+ * reported by the Cassandra nodes.
415+ *
416+ * @param tokenStr the string representation.
417+ * @return the token.
418+ */
419+ public Token newToken(String tokenStr) {
420+ TokenMap current = tokenMap;
421+ if (current == null)
422+ throw new DriverInternalError("Token factory not set. This should only happen at initialization time");
423+
424+ return current.factory.fromString(tokenStr);
425+ }
426+
427+ /**
428+ * Builds a new {@link TokenRange}.
429+ *
430+ * @param start the start token.
431+ * @param end the end token.
432+ * @return the range.
433+ */
434+ public TokenRange newTokenRange(Token start, Token end) {
435+ TokenMap current = tokenMap;
436+ if (current == null)
437+ throw new DriverInternalError("Token factory not set. This should only happen at initialization time");
438+
439+ return new TokenRange(start, end, current.factory);
440+ }
441+
410442 Token.Factory tokenFactory() {
411443 TokenMap current = tokenMap;
412444 return (current == null) ? null : current.factory;
modified driver-core/src/main/java/com/datastax/driver/core/Token.java +9 −2
@@ -305,8 +305,15 @@
305305
306306 @Override
307307 public OPPToken fromString(String tokenStr) {
308- String prefix = (tokenStr.length() % 2 == 0) ? "0x" : "0x0";
309- ByteBuffer value = Bytes.fromHexString(prefix + tokenStr);
308+ // This method must be able to parse the contents of system.peers.tokens, which do not have the "0x" prefix.
309+ // On the other hand, OPPToken#toString has the "0x" because it should be usable in a CQL query, and it's
310+ // nice to have fromString and toString symetrical.
311+ // So handle both cases:
312+ if (!tokenStr.startsWith("0x")) {
313+ String prefix = (tokenStr.length() % 2 == 0) ? "0x" : "0x0";
314+ tokenStr = prefix + tokenStr;
315+ }
316+ ByteBuffer value = Bytes.fromHexString(tokenStr);
310317 return new OPPToken(value);
311318 }
312319
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +14 −1
@@ -76,7 +76,6 @@
7676
7777 @AfterClass(groups = "short", alwaysRun=true)
7878 public void teardown() {
79- System.out.println("Tearing down");
8079 if (cluster != null)
8180 cluster.close();
8281 if (ccm != null)
@@ -360,5 +359,19 @@
360359 }
361360 }
362361
362+ @Test(groups = "short")
363+ public void should_expose_token_and_range_creation_methods() {
364+ Metadata metadata = cluster.getMetadata();
365+
366+ // Pick a random range
367+ TokenRange range = metadata.getTokenRanges().iterator().next();
368+
369+ Token start = metadata.newToken(range.getStart().toString());
370+ Token end = metadata.newToken(range.getEnd().toString());
371+
372+ assertThat(metadata.newTokenRange(start, end))
373+ .isEqualTo(range);
374+ }
375+
363376 protected abstract Token.Factory tokenFactory();
364377 }