kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f82f76e59d60beb902963098cae0513ff3b992989d2ec02b18ae10a2fb3aab56 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-11-16 19:42 UTC |
| Committer | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-11-26 12:56 UTC |
| Parents | 694c94a19612 |
7 files changed
modified changelog/README.md +1 −0
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | |
| 5 | 5 | - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes. |
| 6 | 6 | - [bug] JAVA-983: QueryBuilder cannot handle collections containing function calls. |
| 7 | +- [improvement] JAVA-863: Idempotence propagation in PreparedStatements. | |
| 7 | 8 | |
| 8 | 9 | |
| 9 | 10 | ### 2.1.9 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +4 −1
| @@ -111,7 +111,10 @@ | ||
| 111 | 111 | if (statement.isTracing()) |
| 112 | 112 | prepared.enableTracing(); |
| 113 | 113 | prepared.setRetryPolicy(statement.getRetryPolicy()); |
| 114 | - | |
| 114 | + if (prepared instanceof IdempotenceAwarePreparedStatement) { | |
| 115 | + IdempotenceAwarePreparedStatement idempotenceAwarePS = (IdempotenceAwarePreparedStatement) prepared; | |
| 116 | + idempotenceAwarePS.setIdempotent(statement.isIdempotent()); | |
| 117 | + } | |
| 115 | 118 | return prepared; |
| 116 | 119 | } |
| 117 | 120 | }); |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +6 −0
| @@ -74,6 +74,12 @@ | ||
| 74 | 74 | this.enableTracing(); |
| 75 | 75 | if (statement.getRetryPolicy() != null) |
| 76 | 76 | 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 | + } | |
| 77 | 83 | } |
| 78 | 84 | |
| 79 | 85 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +11 −1
| @@ -20,7 +20,7 @@ | ||
| 20 | 20 | |
| 21 | 21 | import com.datastax.driver.core.policies.RetryPolicy; |
| 22 | 22 | |
| 23 | -public class DefaultPreparedStatement implements PreparedStatement{ | |
| 23 | +public class DefaultPreparedStatement implements IdempotenceAwarePreparedStatement{ | |
| 24 | 24 | |
| 25 | 25 | final PreparedId preparedId; |
| 26 | 26 | |
| @@ -33,6 +33,7 @@ | ||
| 33 | 33 | volatile ConsistencyLevel serialConsistency; |
| 34 | 34 | volatile boolean traceQuery; |
| 35 | 35 | volatile RetryPolicy retryPolicy; |
| 36 | + volatile Boolean idempotent; | |
| 36 | 37 | |
| 37 | 38 | private DefaultPreparedStatement(PreparedId id, String query, String queryKeyspace) { |
| 38 | 39 | this.preparedId = id; |
| @@ -175,4 +176,13 @@ | ||
| 175 | 176 | public PreparedId getPreparedId() { |
| 176 | 177 | return preparedId; |
| 177 | 178 | } |
| 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 | + } | |
| 178 | 188 | } |
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 @@ | ||
| 431 | 431 | * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value, |
| 432 | 432 | * the result will be {@code false}; otherwise it will be {@code true}. |
| 433 | 433 | * 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. | |
| 434 | 437 | * |
| 435 | 438 | * @return whether this statement is idempotent, or {@code null} to use |
| 436 | 439 | * {@link QueryOptions#getDefaultIdempotence()}. |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +50 −13
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | sb = new StringBuilder(); |
| 80 | 80 | sb.append("CREATE TABLE ").append(ALL_SET_TABLE).append(" (k text PRIMARY KEY"); |
| 81 | 81 | for (DataType type : DataType.allPrimitiveTypes()) { |
| 82 | - // This must be handled separatly | |
| 82 | + // This must be handled separately | |
| 83 | 83 | if (exclude(type)) |
| 84 | 84 | continue; |
| 85 | 85 | sb.append(", c_set_").append(type).append(" set<").append(type).append('>'); |
| @@ -90,12 +90,12 @@ | ||
| 90 | 90 | sb = new StringBuilder(); |
| 91 | 91 | sb.append("CREATE TABLE ").append(ALL_MAP_TABLE).append(" (k text PRIMARY KEY"); |
| 92 | 92 | for (DataType keyType : DataType.allPrimitiveTypes()) { |
| 93 | - // This must be handled separatly | |
| 93 | + // This must be handled separately | |
| 94 | 94 | if (exclude(keyType)) |
| 95 | 95 | continue; |
| 96 | 96 | |
| 97 | 97 | for (DataType valueType : DataType.allPrimitiveTypes()) { |
| 98 | - // This must be handled separatly | |
| 98 | + // This must be handled separately | |
| 99 | 99 | if (exclude(valueType)) |
| 100 | 100 | continue; |
| 101 | 101 | sb.append(", c_map_").append(keyType).append('_').append(valueType).append(" map<").append(keyType).append(',').append(valueType).append('>'); |
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | public void preparedNativeTest() { |
| 120 | 120 | // Test preparing/bounding for all native types |
| 121 | 121 | for (DataType type : DataType.allPrimitiveTypes()) { |
| 122 | - // This must be handled separatly | |
| 122 | + // This must be handled separately | |
| 123 | 123 | if (exclude(type)) |
| 124 | 124 | continue; |
| 125 | 125 | |
| @@ -140,7 +140,7 @@ | ||
| 140 | 140 | public void preparedNativeTest2() { |
| 141 | 141 | // Test preparing/bounding for all native types |
| 142 | 142 | for (DataType type : DataType.allPrimitiveTypes()) { |
| 143 | - // This must be handled separatly | |
| 143 | + // This must be handled separately | |
| 144 | 144 | if (exclude(type)) |
| 145 | 145 | continue; |
| 146 | 146 | |
| @@ -159,7 +159,7 @@ | ||
| 159 | 159 | public void prepareListTest() { |
| 160 | 160 | // Test preparing/bounding for all possible list types |
| 161 | 161 | for (DataType rawType : DataType.allPrimitiveTypes()) { |
| 162 | - // This must be handled separatly | |
| 162 | + // This must be handled separately | |
| 163 | 163 | if (exclude(rawType)) |
| 164 | 164 | continue; |
| 165 | 165 | |
| @@ -183,7 +183,7 @@ | ||
| 183 | 183 | public void prepareListTest2() { |
| 184 | 184 | // Test preparing/bounding for all possible list types |
| 185 | 185 | for (DataType rawType : DataType.allPrimitiveTypes()) { |
| 186 | - // This must be handled separatly | |
| 186 | + // This must be handled separately | |
| 187 | 187 | if (exclude(rawType)) |
| 188 | 188 | continue; |
| 189 | 189 | |
| @@ -204,7 +204,7 @@ | ||
| 204 | 204 | public void prepareSetTest() { |
| 205 | 205 | // Test preparing/bounding for all possible set types |
| 206 | 206 | for (DataType rawType : DataType.allPrimitiveTypes()) { |
| 207 | - // This must be handled separatly | |
| 207 | + // This must be handled separately | |
| 208 | 208 | if (exclude(rawType)) |
| 209 | 209 | continue; |
| 210 | 210 | |
| @@ -228,7 +228,7 @@ | ||
| 228 | 228 | public void prepareSetTest2() { |
| 229 | 229 | // Test preparing/bounding for all possible set types |
| 230 | 230 | for (DataType rawType : DataType.allPrimitiveTypes()) { |
| 231 | - // This must be handled separatly | |
| 231 | + // This must be handled separately | |
| 232 | 232 | if (exclude(rawType)) |
| 233 | 233 | continue; |
| 234 | 234 | |
| @@ -249,12 +249,12 @@ | ||
| 249 | 249 | public void prepareMapTest() { |
| 250 | 250 | // Test preparing/bounding for all possible map types |
| 251 | 251 | for (DataType rawKeyType : DataType.allPrimitiveTypes()) { |
| 252 | - // This must be handled separatly | |
| 252 | + // This must be handled separately | |
| 253 | 253 | if (exclude(rawKeyType)) |
| 254 | 254 | continue; |
| 255 | 255 | |
| 256 | 256 | for (DataType rawValueType : DataType.allPrimitiveTypes()) { |
| 257 | - // This must be handled separatly | |
| 257 | + // This must be handled separately | |
| 258 | 258 | if (exclude(rawValueType)) |
| 259 | 259 | continue; |
| 260 | 260 | |
| @@ -279,12 +279,12 @@ | ||
| 279 | 279 | public void prepareMapTest2() { |
| 280 | 280 | // Test preparing/bounding for all possible map types |
| 281 | 281 | for (DataType rawKeyType : DataType.allPrimitiveTypes()) { |
| 282 | - // This must be handled separatly | |
| 282 | + // This must be handled separately | |
| 283 | 283 | if (exclude(rawKeyType)) |
| 284 | 284 | continue; |
| 285 | 285 | |
| 286 | 286 | for (DataType rawValueType : DataType.allPrimitiveTypes()) { |
| 287 | - // This must be handled separatly | |
| 287 | + // This must be handled separately | |
| 288 | 288 | if (exclude(rawValueType)) |
| 289 | 289 | continue; |
| 290 | 290 | |
| @@ -435,4 +435,41 @@ | ||
| 435 | 435 | otherCluster.close(); |
| 436 | 436 | } |
| 437 | 437 | } |
| 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 | + | |
| 438 | 475 | } |