Elffuss BraidHub

kikocis/java-driver public

JAVA-863: Idempotent Prepared Statements
Changeset231c837ee5421f1977503897165ca5a12bbdc83150eee8320666b713ca566d72
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2015-11-16 19:42 UTC
Committerolim7t <omichallat+github@gmail.com> · 2015-12-03 18:18 +01:00
Parents2541974f8581

Browse the files at this changeset

8 files changed

modified changelog/README.md +1 −0
@@ -8,6 +8,7 @@
88 - [bug] JAVA-1009: use CL ONE to compute query plan when reconnecting
99 control connection.
1010 - [improvement] JAVA-1003: Change default consistency level to LOCAL_ONE (amends JAVA-926).
11+- [improvement] JAVA-863: Idempotence propagation in prepared statements.
1112
1213 Merged from 2.1 branch:
1314
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +2 −0
@@ -140,6 +140,8 @@
140140 prepared.enableTracing();
141141 prepared.setRetryPolicy(statement.getRetryPolicy());
142142 prepared.setOutgoingPayload(statement.getOutgoingPayload());
143+ prepared.setIdempotent(statement.isIdempotent());
144+
143145 return prepared;
144146 }
145147 });
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +3 −0
@@ -87,6 +87,9 @@
8787 // propagate incoming payload as outgoing payload, if no outgoing payload has been explicitly set
8888 this.setOutgoingPayload(statement.getIncomingPayload());
8989 this.codecRegistry = statement.getCodecRegistry();
90+ if (statement.isIdempotent() != null) {
91+ this.setIdempotent(statement.isIdempotent());
92+ }
9093 }
9194
9295 /**
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +18 −0
@@ -41,6 +41,7 @@
4141 volatile boolean traceQuery;
4242 volatile RetryPolicy retryPolicy;
4343 volatile ImmutableMap<String, ByteBuffer> outgoingPayload;
44+ volatile Boolean idempotent;
4445
4546 private DefaultPreparedStatement(PreparedId id, String query, String queryKeyspace, Map<String, ByteBuffer> incomingPayload, Cluster cluster) {
4647 this.preparedId = id;
@@ -219,4 +220,21 @@
219220 public CodecRegistry getCodecRegistry() {
220221 return cluster.getConfiguration().getCodecRegistry();
221222 }
223+
224+ /**
225+ * {@inheritDoc}
226+ */
227+ @Override
228+ public PreparedStatement setIdempotent(Boolean idempotent) {
229+ this.idempotent = idempotent;
230+ return this;
231+ }
232+
233+ /**
234+ * {@inheritDoc}
235+ */
236+ @Override
237+ public Boolean isIdempotent() {
238+ return this.idempotent;
239+ }
222240 }
modified driver-core/src/main/java/com/datastax/driver/core/PreparedStatement.java +23 −0
@@ -347,4 +347,27 @@
347347 */
348348 public CodecRegistry getCodecRegistry();
349349
350+ /**
351+ * Sets whether this statement is idempotent.
352+ * <p/>
353+ * See {@link com.datastax.driver.core.Statement#isIdempotent} for more explanations about this property.
354+ *
355+ * @param idempotent the new value.
356+ * @return this {@code IdempotenceAwarePreparedStatement} object.
357+ */
358+ public PreparedStatement setIdempotent(Boolean idempotent);
359+
360+ /**
361+ * Whether this statement is idempotent, i.e. whether it can be applied multiple times
362+ * without changing the result beyond the initial application.
363+ * <p/>
364+ * See {@link com.datastax.driver.core.Statement#isIdempotent} for more explanations about this property.
365+ * <p/>
366+ * Please note that idempotence will be propagated to all {@link BoundStatement}s created from this prepared statement.
367+ *
368+ * @return whether this statement is idempotent, or {@code null} to use
369+ * {@link QueryOptions#getDefaultIdempotence()}.
370+ */
371+ public Boolean isIdempotent();
372+
350373 }
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +3 −0
@@ -446,6 +446,9 @@
446446 * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value,
447447 * the result will be {@code false}; otherwise it will be {@code true}.
448448 * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides every other mechanism.
449+ * <p>
450+ * Note that when a statement is prepared ({@link Session#prepare(String)}), its idempotence flag will be propagated
451+ * to all {@link PreparedStatement}s created from it.
449452 *
450453 * @return whether this statement is idempotent, or {@code null} to use
451454 * {@link QueryOptions#getDefaultIdempotence()}.
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +49 −13
@@ -84,7 +84,7 @@
8484 sb = new StringBuilder();
8585 sb.append("CREATE TABLE ").append(ALL_SET_TABLE).append(" (k text PRIMARY KEY");
8686 for (DataType type : primitiveTypes) {
87- // This must be handled separatly
87+ // This must be handled separately
8888 if (exclude(type))
8989 continue;
9090 sb.append(", c_set_").append(type).append(" set<").append(type).append('>');
@@ -95,12 +95,12 @@
9595 sb = new StringBuilder();
9696 sb.append("CREATE TABLE ").append(ALL_MAP_TABLE).append(" (k text PRIMARY KEY");
9797 for (DataType keyType : primitiveTypes) {
98- // This must be handled separatly
98+ // This must be handled separately
9999 if (exclude(keyType))
100100 continue;
101101
102102 for (DataType valueType : primitiveTypes) {
103- // This must be handled separatly
103+ // This must be handled separately
104104 if (exclude(valueType))
105105 continue;
106106 sb.append(", c_map_").append(keyType).append('_').append(valueType).append(" map<").append(keyType).append(',').append(valueType).append('>');
@@ -124,7 +124,7 @@
124124 public void preparedNativeTest() {
125125 // Test preparing/bounding for all native types
126126 for (DataType type : primitiveTypes) {
127- // This must be handled separatly
127+ // This must be handled separately
128128 if (exclude(type))
129129 continue;
130130
@@ -146,7 +146,7 @@
146146 public void preparedNativeTest2() {
147147 // Test preparing/bounding for all native types
148148 for (DataType type : primitiveTypes) {
149- // This must be handled separatly
149+ // This must be handled separately
150150 if (exclude(type))
151151 continue;
152152
@@ -166,7 +166,7 @@
166166 public void prepareListTest() {
167167 // Test preparing/bounding for all possible list types
168168 for (DataType rawType : primitiveTypes) {
169- // This must be handled separatly
169+ // This must be handled separately
170170 if (exclude(rawType))
171171 continue;
172172
@@ -191,7 +191,7 @@
191191 public void prepareListTest2() {
192192 // Test preparing/bounding for all possible list types
193193 for (DataType rawType : primitiveTypes) {
194- // This must be handled separatly
194+ // This must be handled separately
195195 if (exclude(rawType))
196196 continue;
197197
@@ -213,7 +213,7 @@
213213 public void prepareSetTest() {
214214 // Test preparing/bounding for all possible set types
215215 for (DataType rawType : primitiveTypes) {
216- // This must be handled separatly
216+ // This must be handled separately
217217 if (exclude(rawType))
218218 continue;
219219
@@ -238,7 +238,7 @@
238238 public void prepareSetTest2() {
239239 // Test preparing/bounding for all possible set types
240240 for (DataType rawType : primitiveTypes) {
241- // This must be handled separatly
241+ // This must be handled separately
242242 if (exclude(rawType))
243243 continue;
244244
@@ -260,12 +260,12 @@
260260 public void prepareMapTest() {
261261 // Test preparing/bounding for all possible map types
262262 for (DataType rawKeyType : primitiveTypes) {
263- // This must be handled separatly
263+ // This must be handled separately
264264 if (exclude(rawKeyType))
265265 continue;
266266
267267 for (DataType rawValueType : primitiveTypes) {
268- // This must be handled separatly
268+ // This must be handled separately
269269 if (exclude(rawValueType))
270270 continue;
271271
@@ -291,12 +291,12 @@
291291 public void prepareMapTest2() {
292292 // Test preparing/bounding for all possible map types
293293 for (DataType rawKeyType : primitiveTypes) {
294- // This must be handled separatly
294+ // This must be handled separately
295295 if (exclude(rawKeyType))
296296 continue;
297297
298298 for (DataType rawValueType : primitiveTypes) {
299- // This must be handled separatly
299+ // This must be handled separately
300300 if (exclude(rawValueType))
301301 continue;
302302
@@ -664,4 +664,40 @@
664664 }
665665 fail("Did not find '" + toFind + "' in trace");
666666 }
667+
668+ @Test(groups = "short")
669+ public void should_propagate_idempotence_in_statements() {
670+ session.execute(String.format("CREATE TABLE %s.idempotencetest (i int PRIMARY KEY)", keyspace));
671+
672+ SimpleStatement statement;
673+ PreparedStatement prepared;
674+ BoundStatement bound;
675+
676+ statement = session.newSimpleStatement(String.format("SELECT * FROM %s.idempotencetest WHERE i = ?", keyspace));
677+
678+ prepared = session.prepare(statement);
679+ bound = prepared.bind(1);
680+
681+ assertThat(prepared.isIdempotent()).isNull();
682+ assertThat(bound.isIdempotent()).isNull();
683+
684+ statement.setIdempotent(true);
685+ prepared = session.prepare(statement);
686+ bound = prepared.bind(1);
687+
688+ assertThat(prepared.isIdempotent()).isTrue();
689+ assertThat(bound.isIdempotent()).isTrue();
690+
691+ statement.setIdempotent(false);
692+ prepared = session.prepare(statement);
693+ bound = prepared.bind(1);
694+
695+ assertThat(prepared.isIdempotent()).isFalse();
696+ assertThat(bound.isIdempotent()).isFalse();
697+
698+ prepared.setIdempotent(true);
699+ bound = prepared.bind(1);
700+
701+ assertThat(bound.isIdempotent()).isTrue();
702+ }
667703 }
modified upgrade_guide/README.md +4 −0
@@ -230,6 +230,10 @@
230230 ...
231231 }
232232 ```
233+
234+22. The interface `IdempotenceAwarePreparedStatement` has been removed
235+ and now the `PreparedStatement` interface exposes 2 new methods,
236+ `setIdempotent(Boolean)` and `isIdempotent()`.
233237
234238 ### 2.1.8
235239