Elffuss BraidHub

kikocis/java-driver public

Merge pull request #528 from datastax/java863

JAVA-863: Idempotence propagation in PreparedStatements.
Changeseta58b761fd5cc233c6066e33a859f1d670fecd602fb50e373b01ba3e73240a8a3
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2015-11-26 14:45 +01:00
CommitterAlexandre Dutra <adutra@users.noreply.github.com> · 2015-11-26 14:45 +01:00
Parents694c94a19612, f82f76e59d60

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -4,6 +4,7 @@
44
55 - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes.
66 - [bug] JAVA-983: QueryBuilder cannot handle collections containing function calls.
7+- [improvement] JAVA-863: Idempotence propagation in PreparedStatements.
78
89
910 ### 2.1.9
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +4 −1
@@ -111,7 +111,10 @@
111111 if (statement.isTracing())
112112 prepared.enableTracing();
113113 prepared.setRetryPolicy(statement.getRetryPolicy());
114-
114+ if (prepared instanceof IdempotenceAwarePreparedStatement) {
115+ IdempotenceAwarePreparedStatement idempotenceAwarePS = (IdempotenceAwarePreparedStatement) prepared;
116+ idempotenceAwarePS.setIdempotent(statement.isIdempotent());
117+ }
115118 return prepared;
116119 }
117120 });
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +6 −0
@@ -74,6 +74,12 @@
7474 this.enableTracing();
7575 if (statement.getRetryPolicy() != null)
7676 this.setRetryPolicy(statement.getRetryPolicy());
77+ if (statement instanceof IdempotenceAwarePreparedStatement) {
78+ IdempotenceAwarePreparedStatement idempotenceAwarePS = (IdempotenceAwarePreparedStatement) statement;
79+ if (idempotenceAwarePS.isIdempotent() != null) {
80+ this.setIdempotent(idempotenceAwarePS.isIdempotent());
81+ }
82+ }
7783 }
7884
7985 /**
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +11 −1
@@ -20,7 +20,7 @@
2020
2121 import com.datastax.driver.core.policies.RetryPolicy;
2222
23-public class DefaultPreparedStatement implements PreparedStatement{
23+public class DefaultPreparedStatement implements IdempotenceAwarePreparedStatement{
2424
2525 final PreparedId preparedId;
2626
@@ -33,6 +33,7 @@
3333 volatile ConsistencyLevel serialConsistency;
3434 volatile boolean traceQuery;
3535 volatile RetryPolicy retryPolicy;
36+ volatile Boolean idempotent;
3637
3738 private DefaultPreparedStatement(PreparedId id, String query, String queryKeyspace) {
3839 this.preparedId = id;
@@ -175,4 +176,13 @@
175176 public PreparedId getPreparedId() {
176177 return preparedId;
177178 }
179+
180+ public PreparedStatement setIdempotent(Boolean idempotent) {
181+ this.idempotent = idempotent;
182+ return this;
183+ }
184+
185+ public Boolean isIdempotent() {
186+ return this.idempotent;
187+ }
178188 }
added driver-core/src/main/java/com/datastax/driver/core/IdempotenceAwarePreparedStatement.java +47 −0
@@ -0,0 +1,47 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+/**
19+ * Interface that extends the {@link PreparedStatement} interface in order to prevent breaking
20+ * binary compatibility. This interface is removed in versions 3.x of the driver and the methods
21+ * are directly available from the {@link PreparedStatement} interface.
22+ */
23+public interface IdempotenceAwarePreparedStatement extends PreparedStatement {
24+ /**
25+ * Sets whether this statement is idempotent.
26+ * <p/>
27+ * See {@link com.datastax.driver.core.Statement#isIdempotent} for more explanations about this property.
28+ *
29+ * @param idempotent the new value.
30+ * @return this {@code IdempotenceAwarePreparedStatement} object.
31+ */
32+ public PreparedStatement setIdempotent(Boolean idempotent);
33+
34+ /**
35+ * Whether this statement is idempotent, i.e. whether it can be applied multiple times
36+ * without changing the result beyond the initial application.
37+ * <p/>
38+ * See {@link com.datastax.driver.core.Statement#isIdempotent} for more explanations about this property.
39+ *
40+ * Please note that idempotence will be propagated to all {@link BoundStatement}s created from this prepared statement.
41+ *
42+ * @return whether this statement is idempotent, or {@code null} to use
43+ * {@link QueryOptions#getDefaultIdempotence()}.
44+ *
45+ */
46+ public Boolean isIdempotent();
47+}
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +3 −0
@@ -431,6 +431,9 @@
431431 * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value,
432432 * the result will be {@code false}; otherwise it will be {@code true}.
433433 * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides every other mechanism.
434+ * <p>
435+ * Note that when a statement is prepared ({@link Session#prepare(String)}), its idempotence flag will be propagated
436+ * to all {@link PreparedStatement}s created from it.
434437 *
435438 * @return whether this statement is idempotent, or {@code null} to use
436439 * {@link QueryOptions#getDefaultIdempotence()}.
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +50 −13
@@ -79,7 +79,7 @@
7979 sb = new StringBuilder();
8080 sb.append("CREATE TABLE ").append(ALL_SET_TABLE).append(" (k text PRIMARY KEY");
8181 for (DataType type : DataType.allPrimitiveTypes()) {
82- // This must be handled separatly
82+ // This must be handled separately
8383 if (exclude(type))
8484 continue;
8585 sb.append(", c_set_").append(type).append(" set<").append(type).append('>');
@@ -90,12 +90,12 @@
9090 sb = new StringBuilder();
9191 sb.append("CREATE TABLE ").append(ALL_MAP_TABLE).append(" (k text PRIMARY KEY");
9292 for (DataType keyType : DataType.allPrimitiveTypes()) {
93- // This must be handled separatly
93+ // This must be handled separately
9494 if (exclude(keyType))
9595 continue;
9696
9797 for (DataType valueType : DataType.allPrimitiveTypes()) {
98- // This must be handled separatly
98+ // This must be handled separately
9999 if (exclude(valueType))
100100 continue;
101101 sb.append(", c_map_").append(keyType).append('_').append(valueType).append(" map<").append(keyType).append(',').append(valueType).append('>');
@@ -119,7 +119,7 @@
119119 public void preparedNativeTest() {
120120 // Test preparing/bounding for all native types
121121 for (DataType type : DataType.allPrimitiveTypes()) {
122- // This must be handled separatly
122+ // This must be handled separately
123123 if (exclude(type))
124124 continue;
125125
@@ -140,7 +140,7 @@
140140 public void preparedNativeTest2() {
141141 // Test preparing/bounding for all native types
142142 for (DataType type : DataType.allPrimitiveTypes()) {
143- // This must be handled separatly
143+ // This must be handled separately
144144 if (exclude(type))
145145 continue;
146146
@@ -159,7 +159,7 @@
159159 public void prepareListTest() {
160160 // Test preparing/bounding for all possible list types
161161 for (DataType rawType : DataType.allPrimitiveTypes()) {
162- // This must be handled separatly
162+ // This must be handled separately
163163 if (exclude(rawType))
164164 continue;
165165
@@ -183,7 +183,7 @@
183183 public void prepareListTest2() {
184184 // Test preparing/bounding for all possible list types
185185 for (DataType rawType : DataType.allPrimitiveTypes()) {
186- // This must be handled separatly
186+ // This must be handled separately
187187 if (exclude(rawType))
188188 continue;
189189
@@ -204,7 +204,7 @@
204204 public void prepareSetTest() {
205205 // Test preparing/bounding for all possible set types
206206 for (DataType rawType : DataType.allPrimitiveTypes()) {
207- // This must be handled separatly
207+ // This must be handled separately
208208 if (exclude(rawType))
209209 continue;
210210
@@ -228,7 +228,7 @@
228228 public void prepareSetTest2() {
229229 // Test preparing/bounding for all possible set types
230230 for (DataType rawType : DataType.allPrimitiveTypes()) {
231- // This must be handled separatly
231+ // This must be handled separately
232232 if (exclude(rawType))
233233 continue;
234234
@@ -249,12 +249,12 @@
249249 public void prepareMapTest() {
250250 // Test preparing/bounding for all possible map types
251251 for (DataType rawKeyType : DataType.allPrimitiveTypes()) {
252- // This must be handled separatly
252+ // This must be handled separately
253253 if (exclude(rawKeyType))
254254 continue;
255255
256256 for (DataType rawValueType : DataType.allPrimitiveTypes()) {
257- // This must be handled separatly
257+ // This must be handled separately
258258 if (exclude(rawValueType))
259259 continue;
260260
@@ -279,12 +279,12 @@
279279 public void prepareMapTest2() {
280280 // Test preparing/bounding for all possible map types
281281 for (DataType rawKeyType : DataType.allPrimitiveTypes()) {
282- // This must be handled separatly
282+ // This must be handled separately
283283 if (exclude(rawKeyType))
284284 continue;
285285
286286 for (DataType rawValueType : DataType.allPrimitiveTypes()) {
287- // This must be handled separatly
287+ // This must be handled separately
288288 if (exclude(rawValueType))
289289 continue;
290290
@@ -435,4 +435,41 @@
435435 otherCluster.close();
436436 }
437437 }
438+
439+ @Test(groups = "short")
440+ public void should_propagate_idempotence_in_statements() {
441+ session.execute(String.format("CREATE TABLE %s.idempotencetest (i int PRIMARY KEY)", keyspace));
442+
443+ SimpleStatement statement;
444+ IdempotenceAwarePreparedStatement prepared;
445+ BoundStatement bound;
446+
447+ statement = new SimpleStatement(String.format("SELECT * FROM %s.idempotencetest WHERE i = ?", keyspace));
448+
449+ prepared = (IdempotenceAwarePreparedStatement) session.prepare(statement);
450+ bound = prepared.bind(1);
451+
452+ assertThat(prepared.isIdempotent()).isNull();
453+ assertThat(bound.isIdempotent()).isNull();
454+
455+ statement.setIdempotent(true);
456+ prepared = (IdempotenceAwarePreparedStatement) session.prepare(statement);
457+ bound = prepared.bind(1);
458+
459+ assertThat(prepared.isIdempotent()).isTrue();
460+ assertThat(bound.isIdempotent()).isTrue();
461+
462+ statement.setIdempotent(false);
463+ prepared = (IdempotenceAwarePreparedStatement)session.prepare(statement);
464+ bound = prepared.bind(1);
465+
466+ assertThat(prepared.isIdempotent()).isFalse();
467+ assertThat(bound.isIdempotent()).isFalse();
468+
469+ prepared.setIdempotent(true);
470+ bound = prepared.bind(1);
471+
472+ assertThat(bound.isIdempotent()).isTrue();
473+ }
474+
438475 }