Elffuss BraidHub

kikocis/java-driver public

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

- make all queries generated by Mapper idempotent;
- add flag to QueryParameters annotation to configure Accessor queries.
Changesetee708b32a78ba731ad100bf8e6c46dced5708439e07d1d6499fb160a1a6eae91
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-02-19 12:56 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-04 15:09 +01:00
Parents6a663b7f2259

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -29,6 +29,7 @@
2929 - [improvement] JAVA-888: Add cluster-wide percentile tracker.
3030 - [improvement] JAVA-963: Automatically register PercentileTracker from components that use it.
3131 - [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
32+- [improvement] JAVA-923: Position idempotent flag on object mapper queries.
3233
3334 Merged from 2.0 branch:
3435
modified clirr-ignores.xml +7 −0
@@ -129,4 +129,11 @@
129129 <justification>Introduced more generic parent type PercentileTracker (API was marked as beta and still subject to change)</justification>
130130 </difference>
131131
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+
132139 </differences>
modified driver-mapping/src/main/java/com/datastax/driver/mapping/AnnotationParser.java +6 −1
@@ -282,15 +282,20 @@
282282 ConsistencyLevel cl = null;
283283 int fetchSize = -1;
284284 boolean tracing = false;
285+ Boolean idempotent = null;
285286
286287 QueryParameters options = m.getAnnotation(QueryParameters.class);
287288 if (options != null) {
288289 cl = options.consistency().isEmpty() ? null : ConsistencyLevel.valueOf(options.consistency().toUpperCase());
289290 fetchSize = options.fetchSize();
290291 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];
291296 }
292297
293- methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing));
298+ methods.add(new MethodMapper(m, queryString, paramMappers, cl, fetchSize, tracing, idempotent));
294299 }
295300
296301 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
@@ -291,6 +291,17 @@
291291
292292 @Query("SELECT * FROM posts")
293293 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();
294305 }
295306
296307 @Accessor
@@ -447,4 +458,73 @@
447458 newSession.execute("USE " + keyspace);
448459 assertThat(newSession.getState().getConnectedHosts()).hasSize(1);
449460 }
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+
450530 }