Elffuss BraidHub

kikocis/java-driver public

JAVA-923: Position idempotent flag on object mapper queries.

(cherry picked from 2.1)
Changeset4a433a1ccc27e0aeb38e908c2bd7f76f08ad12d2bcc8639bc5fee50ce0c663d8
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-04 15:10 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-01 11:28 +02:00
Parentsf3037e709a8a

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -14,6 +14,7 @@
1414 - [bug] JAVA-1100: Exception when connecting with shaded java driver in OSGI
1515 - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
1616 - [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
17+- [improvement] JAVA-923: Position idempotent flag on object mapper queries.
1718
1819
1920 ### 3.0.0
modified clirr-ignores.xml +11 −1
@@ -7,6 +7,9 @@
77 * run `mvn clirr:clirr` at the root directory
88 * check the reports in `<module>/target/site/clirr-report.html`
99 * add new differences if needed. Difference types are explained at http://www.mojohaus.org/clirr-maven-plugin/examples/ignored-differences.html
10+
11+ The formatter introduces line breaks that make Clirr unhappy:
12+ @formatter:off
1013 -->
1114 <differences>
1215
@@ -15,5 +18,12 @@
1518 <className>com/datastax/driver/mapping/ColumnMapper$Kind</className>
1619 <justification>False positive, the enclosing class is package-private so this was never exposed</justification>
1720 </difference>
18-
21+
22+ <difference>
23+ <differenceType>7012</differenceType> <!-- method added to interface -->
24+ <className>com/datastax/driver/mapping/annotations/QueryParameters</className>
25+ <method>boolean[] idempotent()</method>
26+ <justification>False positive, it's an annotation and the new method has a default value</justification>
27+ </difference>
28+
1929 </differences>
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +6 −1
@@ -320,15 +320,20 @@
320320 ConsistencyLevel cl = null;
321321 int fetchSize = -1;
322322 boolean tracing = false;
323+ Boolean idempotent = null;
323324
324325 QueryParameters options = m.getAnnotation(QueryParameters.class);
325326 if (options != null) {
326327 cl = options.consistency().isEmpty() ? null : ConsistencyLevel.valueOf(options.consistency().toUpperCase());
327328 fetchSize = options.fetchSize();
328329 tracing = options.tracing();
330+ if (options.idempotent().length > 1) {
331+ throw new IllegalStateException("idemtpotence() attribute can only accept one value");
332+ }
333+ idempotent = options.idempotent().length == 0 ? null : options.idempotent()[0];
329334 }
330335
331- methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing));
336+ methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing, idempotent));
332337 }
333338
334339 return factory.create(accClass, methods);
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +4 −1
@@ -114,7 +114,10 @@
114114 if (stmt == null) {
115115 String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, columns, options.values());
116116 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);
118121 Map<MapperQueryKey, PreparedStatement> newQueries = new HashMap<MapperQueryKey, PreparedStatement>(preparedQueries);
119122 newQueries.put(pqk, stmt);
120123 preparedQueries = newQueries;
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +5 −1
@@ -37,6 +37,7 @@
3737 private final ConsistencyLevel consistency;
3838 private final int fetchSize;
3939 private final boolean tracing;
40+ private final Boolean idempotent;
4041
4142 private Session session;
4243 private PreparedStatement statement;
@@ -46,13 +47,14 @@
4647 private boolean mapOne;
4748 private boolean async;
4849
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) {
5051 this.method = method;
5152 this.queryString = queryString;
5253 this.paramMappers = paramMappers;
5354 this.consistency = consistency;
5455 this.fetchSize = fetchSize;
5556 this.tracing = enableTracing;
57+ this.idempotent = idempotent;
5658 }
5759
5860 public void prepare(MappingManager manager, PreparedStatement ps) {
@@ -151,6 +153,8 @@
151153 bs.setFetchSize(fetchSize);
152154 if (tracing)
153155 bs.enableTracing();
156+ if (idempotent != null)
157+ bs.setIdempotent(idempotent);
154158
155159 if (returnStatement)
156160 return bs;
modified driver-mapping/src/main/java/com/datastax/driver/mapping/annotations/QueryParameters.java +25 −0
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.mapping.annotations;
1717
1818 import com.datastax.driver.core.Configuration;
19+import com.datastax.driver.core.QueryOptions;
1920
2021 import java.lang.annotation.ElementType;
2122 import java.lang.annotation.Retention;
@@ -53,4 +54,28 @@
5354 * @return whether tracing should be enabled for this operation.
5455 */
5556 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 {};
5681 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +80 −0
@@ -279,6 +279,17 @@
279279
280280 @Query("SELECT * FROM posts")
281281 Result<Post> getAll();
282+
283+ @Query("SELECT * FROM posts")
284+ @QueryParameters(idempotent = true)
285+ Statement getAllAsStatementIdempotent();
286+
287+ @Query("SELECT * FROM posts")
288+ @QueryParameters(idempotent = false)
289+ Statement getAllAsStatementNonIdempotent();
290+
291+ @Query("SELECT * FROM posts")
292+ Statement getAllAsStatement();
282293 }
283294
284295 @Test(groups = "short")
@@ -419,4 +430,73 @@
419430 newSession.execute("USE " + keyspace);
420431 assertThat(newSession.getState().getConnectedHosts()).hasSize(1);
421432 }
433+
434+ /**
435+ * Ensures that if an accessor method has a {@link QueryParameters} annotation with
436+ * {@link QueryParameters#idempotent()} as {@code true} that the {@link Statement} it generates returns
437+ * {@code true} for {@link Statement#isIdempotent()}.
438+ *
439+ * @jira_ticket JAVA-923
440+ * @test_category object_mapper
441+ */
442+ @Test(groups = "short")
443+ public void should_flag_statement_as_idempotent() {
444+ MappingManager manager = new MappingManager(session());
445+ PostAccessor post = manager.createAccessor(PostAccessor.class);
446+ Statement stmt = post.getAllAsStatementIdempotent();
447+ assertThat(stmt.isIdempotent()).isEqualTo(true);
448+ }
449+
450+ /**
451+ * Ensures that if an accessor method has a {@link QueryParameters} annotation with
452+ * {@link QueryParameters#idempotent()} as {@code false} that the {@link Statement} it generates returns
453+ * {@code false} for {@link Statement#isIdempotent()}.
454+ *
455+ * @jira_ticket JAVA-923
456+ * @test_category object_mapper
457+ */
458+ @Test(groups = "short")
459+ public void should_flag_statement_as_non_idempotent() {
460+ MappingManager manager = new MappingManager(session());
461+ PostAccessor post = manager.createAccessor(PostAccessor.class);
462+ Statement stmt = post.getAllAsStatementNonIdempotent();
463+ assertThat(stmt.isIdempotent()).isEqualTo(false);
464+ }
465+
466+ /**
467+ * Ensures that if an accessor method lacks a {@link QueryParameters} annotation with
468+ * {@link QueryParameters#idempotent()} set that the {@link Statement} it generates returns
469+ * {@code null} for {@link Statement#isIdempotent()}.
470+ *
471+ * @jira_ticket JAVA-923
472+ * @test_category object_mapper
473+ */
474+ @Test(groups = "short")
475+ public void should_flag_statement_with_null_idempotence() {
476+ MappingManager manager = new MappingManager(session());
477+ PostAccessor post = manager.createAccessor(PostAccessor.class);
478+ Statement stmt = post.getAllAsStatement();
479+ assertThat(stmt.isIdempotent()).isNull();
480+ }
481+
482+ /**
483+ * Ensures that all statements generated by the mapper are
484+ * flagged as idempotent.
485+ *
486+ * @jira_ticket JAVA-923
487+ * @test_category object_mapper
488+ */
489+ @Test(groups = "short")
490+ public void should_flag_all_mapper_generated_statements_as_idempotent() {
491+ MappingManager manager = new MappingManager(session());
492+ Mapper<User> mapper = manager.mapper(User.class);
493+ User u = new User("Paul", "paul@yahoo.com");
494+ Statement saveQuery = mapper.saveQuery(u);
495+ assertThat(saveQuery.isIdempotent()).isTrue();
496+ Statement getQuery = mapper.getQuery(u.getUserId());
497+ assertThat(saveQuery.isIdempotent()).isTrue();
498+ Statement deleteQuery = mapper.deleteQuery(u.getUserId());
499+ assertThat(saveQuery.isIdempotent()).isTrue();
500+ }
501+
422502 }