kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ee708b32a78ba731ad100bf8e6c46dced5708439e07d1d6499fb160a1a6eae91 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-19 12:56 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-04 15:09 +01:00 |
| Parents | 6a663b7f2259 |
7 files changed
modified changelog/README.md +1 −0
| @@ -29,6 +29,7 @@ | ||
| 29 | 29 | - [improvement] JAVA-888: Add cluster-wide percentile tracker. |
| 30 | 30 | - [improvement] JAVA-963: Automatically register PercentileTracker from components that use it. |
| 31 | 31 | - [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent. |
| 32 | +- [improvement] JAVA-923: Position idempotent flag on object mapper queries. | |
| 32 | 33 | |
| 33 | 34 | Merged from 2.0 branch: |
| 34 | 35 |
modified clirr-ignores.xml +7 −0
| @@ -129,4 +129,11 @@ | ||
| 129 | 129 | <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification> |
| 130 | 130 | </difference> |
| 131 | 131 | |
| 132 | + <difference> | |
| 133 | + <differenceType>7012</differenceType> <!-- method added to interface --> | |
| 134 | + <className>com/datastax/driver/mapping/annotations/QueryParameters</className> | |
| 135 | + <method>boolean[] idempotent()</method> | |
| 136 | + <justification>False positive, it's an annotation and the new method has a default value</justification> | |
| 137 | + </difference> | |
| 138 | + | |
| 132 | 139 | </differences> |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +6 −1
| @@ -282,15 +282,20 @@ | ||
| 282 | 282 | ConsistencyLevel cl = null; |
| 283 | 283 | int fetchSize = -1; |
| 284 | 284 | boolean tracing = false; |
| 285 | + Boolean idempotent = null; | |
| 285 | 286 | |
| 286 | 287 | QueryParameters options = m.getAnnotation(QueryParameters.class); |
| 287 | 288 | if (options != null) { |
| 288 | 289 | cl = options.consistency().isEmpty() ? null : ConsistencyLevel.valueOf(options.consistency().toUpperCase()); |
| 289 | 290 | fetchSize = options.fetchSize(); |
| 290 | 291 | tracing = options.tracing(); |
| 292 | + if (options.idempotent().length > 1) { | |
| 293 | + throw new IllegalStateException("idemtpotence() attribute can only accept one value"); | |
| 294 | + } | |
| 295 | + idempotent = options.idempotent().length == 0 ? null : options.idempotent()[0]; | |
| 291 | 296 | } |
| 292 | 297 | |
| 293 | - methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing)); | |
| 298 | + methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing, idempotent)); | |
| 294 | 299 | } |
| 295 | 300 | |
| 296 | 301 | return factory.create(accClass, methods); |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +4 −1
| @@ -114,7 +114,10 @@ | ||
| 114 | 114 | if (stmt == null) { |
| 115 | 115 | String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, columns, options.values()); |
| 116 | 116 | logger.debug("Preparing query {}", queryString); |
| 117 | - stmt = session().prepare(queryString); | |
| 117 | + SimpleStatement s = new SimpleStatement(queryString); | |
| 118 | + // all queries generated by the mapper are idempotent | |
| 119 | + s.setIdempotent(true); | |
| 120 | + stmt = session().prepare(s); | |
| 118 | 121 | Map<MapperQueryKey, PreparedStatement> newQueries = new HashMap<MapperQueryKey, PreparedStatement>(preparedQueries); |
| 119 | 122 | newQueries.put(pqk, stmt); |
| 120 | 123 | preparedQueries = newQueries; |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +5 −1
| @@ -37,6 +37,7 @@ | ||
| 37 | 37 | private final ConsistencyLevel consistency; |
| 38 | 38 | private final int fetchSize; |
| 39 | 39 | private final boolean tracing; |
| 40 | + private final Boolean idempotent; | |
| 40 | 41 | |
| 41 | 42 | private Session session; |
| 42 | 43 | private PreparedStatement statement; |
| @@ -46,13 +47,14 @@ | ||
| 46 | 47 | private boolean mapOne; |
| 47 | 48 | private boolean async; |
| 48 | 49 | |
| 49 | - MethodMapper(Method method, String queryString, ParamMapper[] paramMappers, ConsistencyLevel consistency, int fetchSize, boolean enableTracing) { | |
| 50 | + MethodMapper(Method method, String queryString, ParamMapper[] paramMappers, ConsistencyLevel consistency, int fetchSize, boolean enableTracing, Boolean idempotent) { | |
| 50 | 51 | this.method = method; |
| 51 | 52 | this.queryString = queryString; |
| 52 | 53 | this.paramMappers = paramMappers; |
| 53 | 54 | this.consistency = consistency; |
| 54 | 55 | this.fetchSize = fetchSize; |
| 55 | 56 | this.tracing = enableTracing; |
| 57 | + this.idempotent = idempotent; | |
| 56 | 58 | } |
| 57 | 59 | |
| 58 | 60 | public void prepare(MappingManager manager, PreparedStatement ps) { |
| @@ -151,6 +153,8 @@ | ||
| 151 | 153 | bs.setFetchSize(fetchSize); |
| 152 | 154 | if (tracing) |
| 153 | 155 | bs.enableTracing(); |
| 156 | + if (idempotent != null) | |
| 157 | + bs.setIdempotent(idempotent); | |
| 154 | 158 | |
| 155 | 159 | if (returnStatement) |
| 156 | 160 | return bs; |
modified driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/QueryParameters.java +25 −0
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.mapping.annotations; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.Configuration; |
| 19 | +import com.datastax.driver.core.QueryOptions; | |
| 19 | 20 | |
| 20 | 21 | import java.lang.annotation.ElementType; |
| 21 | 22 | import java.lang.annotation.Retention; |
| @@ -53,4 +54,28 @@ | ||
| 53 | 54 | * @return whether tracing should be enabled for this operation. |
| 54 | 55 | */ |
| 55 | 56 | boolean tracing() default false; |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Whether the operation is idempotent or not. | |
| 60 | + * <p/> | |
| 61 | + * When this attribute is set to {@code true} the operation is assumed idempotent; | |
| 62 | + * when set to {@code false}, it is assumed that it is not. | |
| 63 | + * <p/> | |
| 64 | + * If a value of {@code true} or {@code false} is provided, | |
| 65 | + * the underlying {@link com.datastax.driver.core.Statement} | |
| 66 | + * will have its {@link com.datastax.driver.core.Statement#setIdempotent(boolean) idempotent flag} | |
| 67 | + * set accordingly. | |
| 68 | + * <p/> | |
| 69 | + * When this attribute is set to its default | |
| 70 | + * (an empty array), it means that the statement's idempotent flag | |
| 71 | + * will not be set, and its idempotence will be inferred | |
| 72 | + * from {@link QueryOptions#getDefaultIdempotence()}. | |
| 73 | + * <p/> | |
| 74 | + * This attribute is declared as a boolean array to allow | |
| 75 | + * for "unset" values, but it can only contain at most | |
| 76 | + * one element. | |
| 77 | + * | |
| 78 | + * @return {@code true} if the operation is idempotent, {@code false} otherwise. | |
| 79 | + */ | |
| 80 | + boolean[] idempotent() default {}; | |
| 56 | 81 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +80 −0
| @@ -291,6 +291,17 @@ | ||
| 291 | 291 | |
| 292 | 292 | @Query("SELECT * FROM posts") |
| 293 | 293 | Result<Post> getAll(); |
| 294 | + | |
| 295 | + @Query("SELECT * FROM posts") | |
| 296 | + @QueryParameters(idempotent = true) | |
| 297 | + Statement getAllAsStatementIdempotent(); | |
| 298 | + | |
| 299 | + @Query("SELECT * FROM posts") | |
| 300 | + @QueryParameters(idempotent = false) | |
| 301 | + Statement getAllAsStatementNonIdempotent(); | |
| 302 | + | |
| 303 | + @Query("SELECT * FROM posts") | |
| 304 | + Statement getAllAsStatement(); | |
| 294 | 305 | } |
| 295 | 306 | |
| 296 | 307 | @Accessor |
| @@ -447,4 +458,73 @@ | ||
| 447 | 458 | newSession.execute("USE " + keyspace); |
| 448 | 459 | assertThat(newSession.getState().getConnectedHosts()).hasSize(1); |
| 449 | 460 | } |
| 461 | + | |
| 462 | + /** | |
| 463 | + * Ensures that if an accessor method has a {@link QueryParameters} annotation with | |
| 464 | + * {@link QueryParameters#idempotent()} as {@code true} that the {@link Statement} it generates returns | |
| 465 | + * {@code true} for {@link Statement#isIdempotent()}. | |
| 466 | + * | |
| 467 | + * @jira_ticket JAVA-923 | |
| 468 | + * @test_category object_mapper | |
| 469 | + */ | |
| 470 | + @Test(groups = "short") | |
| 471 | + public void should_flag_statement_as_idempotent() { | |
| 472 | + MappingManager manager = new MappingManager(session()); | |
| 473 | + PostAccessor post = manager.createAccessor(PostAccessor.class); | |
| 474 | + Statement stmt = post.getAllAsStatementIdempotent(); | |
| 475 | + assertThat(stmt.isIdempotent()).isEqualTo(true); | |
| 476 | + } | |
| 477 | + | |
| 478 | + /** | |
| 479 | + * Ensures that if an accessor method has a {@link QueryParameters} annotation with | |
| 480 | + * {@link QueryParameters#idempotent()} as {@code false} that the {@link Statement} it generates returns | |
| 481 | + * {@code false} for {@link Statement#isIdempotent()}. | |
| 482 | + * | |
| 483 | + * @jira_ticket JAVA-923 | |
| 484 | + * @test_category object_mapper | |
| 485 | + */ | |
| 486 | + @Test(groups = "short") | |
| 487 | + public void should_flag_statement_as_non_idempotent() { | |
| 488 | + MappingManager manager = new MappingManager(session()); | |
| 489 | + PostAccessor post = manager.createAccessor(PostAccessor.class); | |
| 490 | + Statement stmt = post.getAllAsStatementNonIdempotent(); | |
| 491 | + assertThat(stmt.isIdempotent()).isEqualTo(false); | |
| 492 | + } | |
| 493 | + | |
| 494 | + /** | |
| 495 | + * Ensures that if an accessor method lacks a {@link QueryParameters} annotation with | |
| 496 | + * {@link QueryParameters#idempotent()} set that the {@link Statement} it generates returns | |
| 497 | + * {@code null} for {@link Statement#isIdempotent()}. | |
| 498 | + * | |
| 499 | + * @jira_ticket JAVA-923 | |
| 500 | + * @test_category object_mapper | |
| 501 | + */ | |
| 502 | + @Test(groups = "short") | |
| 503 | + public void should_flag_statement_with_null_idempotence() { | |
| 504 | + MappingManager manager = new MappingManager(session()); | |
| 505 | + PostAccessor post = manager.createAccessor(PostAccessor.class); | |
| 506 | + Statement stmt = post.getAllAsStatement(); | |
| 507 | + assertThat(stmt.isIdempotent()).isNull(); | |
| 508 | + } | |
| 509 | + | |
| 510 | + /** | |
| 511 | + * Ensures that all statements generated by the mapper are | |
| 512 | + * flagged as idempotent. | |
| 513 | + * | |
| 514 | + * @jira_ticket JAVA-923 | |
| 515 | + * @test_category object_mapper | |
| 516 | + */ | |
| 517 | + @Test(groups = "short") | |
| 518 | + public void should_flag_all_mapper_generated_statements_as_idempotent() { | |
| 519 | + MappingManager manager = new MappingManager(session()); | |
| 520 | + Mapper<User> mapper = manager.mapper(User.class); | |
| 521 | + User u = new User("Paul", "paul@yahoo.com", User.Gender.MALE); | |
| 522 | + Statement saveQuery = mapper.saveQuery(u); | |
| 523 | + assertThat(saveQuery.isIdempotent()).isTrue(); | |
| 524 | + Statement getQuery = mapper.getQuery(u.getUserId()); | |
| 525 | + assertThat(saveQuery.isIdempotent()).isTrue(); | |
| 526 | + Statement deleteQuery = mapper.deleteQuery(u.getUserId()); | |
| 527 | + assertThat(saveQuery.isIdempotent()).isTrue(); | |
| 528 | + } | |
| 529 | + | |
| 450 | 530 | } |