kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 231c837ee5421f1977503897165ca5a12bbdc83150eee8320666b713ca566d72 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-11-16 19:42 UTC |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-12-03 18:18 +01:00 |
| Parents | 2541974f8581 |
8 files changed
modified changelog/README.md +1 −0
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | - [bug] JAVA-1009: use CL ONE to compute query plan when reconnecting |
| 9 | 9 | control connection. |
| 10 | 10 | - [improvement] JAVA-1003: Change default consistency level to LOCAL_ONE (amends JAVA-926). |
| 11 | +- [improvement] JAVA-863: Idempotence propagation in prepared statements. | |
| 11 | 12 | |
| 12 | 13 | Merged from 2.1 branch: |
| 13 | 14 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractSession.java +2 −0
| @@ -140,6 +140,8 @@ | ||
| 140 | 140 | prepared.enableTracing(); |
| 141 | 141 | prepared.setRetryPolicy(statement.getRetryPolicy()); |
| 142 | 142 | prepared.setOutgoingPayload(statement.getOutgoingPayload()); |
| 143 | + prepared.setIdempotent(statement.isIdempotent()); | |
| 144 | + | |
| 143 | 145 | return prepared; |
| 144 | 146 | } |
| 145 | 147 | }); |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +3 −0
| @@ -87,6 +87,9 @@ | ||
| 87 | 87 | // propagate incoming payload as outgoing payload, if no outgoing payload has been explicitly set |
| 88 | 88 | this.setOutgoingPayload(statement.getIncomingPayload()); |
| 89 | 89 | this.codecRegistry = statement.getCodecRegistry(); |
| 90 | + if (statement.isIdempotent() != null) { | |
| 91 | + this.setIdempotent(statement.isIdempotent()); | |
| 92 | + } | |
| 90 | 93 | } |
| 91 | 94 | |
| 92 | 95 | /** |
modified driver-core/src/main/java/com/datastax/driver/core/DefaultPreparedStatement.java +18 −0
| @@ -41,6 +41,7 @@ | ||
| 41 | 41 | volatile boolean traceQuery; |
| 42 | 42 | volatile RetryPolicy retryPolicy; |
| 43 | 43 | volatile ImmutableMap<String, ByteBuffer> outgoingPayload; |
| 44 | + volatile Boolean idempotent; | |
| 44 | 45 | |
| 45 | 46 | private DefaultPreparedStatement(PreparedId id, String query, String queryKeyspace, Map<String, ByteBuffer> incomingPayload, Cluster cluster) { |
| 46 | 47 | this.preparedId = id; |
| @@ -219,4 +220,21 @@ | ||
| 219 | 220 | public CodecRegistry getCodecRegistry() { |
| 220 | 221 | return cluster.getConfiguration().getCodecRegistry(); |
| 221 | 222 | } |
| 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 | + } | |
| 222 | 240 | } |
modified driver-core/src/main/java/com/datastax/driver/core/PreparedStatement.java +23 −0
| @@ -347,4 +347,27 @@ | ||
| 347 | 347 | */ |
| 348 | 348 | public CodecRegistry getCodecRegistry(); |
| 349 | 349 | |
| 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 | + | |
| 350 | 373 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +3 −0
| @@ -446,6 +446,9 @@ | ||
| 446 | 446 | * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value, |
| 447 | 447 | * the result will be {@code false}; otherwise it will be {@code true}. |
| 448 | 448 | * 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. | |
| 449 | 452 | * |
| 450 | 453 | * @return whether this statement is idempotent, or {@code null} to use |
| 451 | 454 | * {@link QueryOptions#getDefaultIdempotence()}. |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +49 −13
| @@ -84,7 +84,7 @@ | ||
| 84 | 84 | sb = new StringBuilder(); |
| 85 | 85 | sb.append("CREATE TABLE ").append(ALL_SET_TABLE).append(" (k text PRIMARY KEY"); |
| 86 | 86 | for (DataType type : primitiveTypes) { |
| 87 | - // This must be handled separatly | |
| 87 | + // This must be handled separately | |
| 88 | 88 | if (exclude(type)) |
| 89 | 89 | continue; |
| 90 | 90 | sb.append(", c_set_").append(type).append(" set<").append(type).append('>'); |
| @@ -95,12 +95,12 @@ | ||
| 95 | 95 | sb = new StringBuilder(); |
| 96 | 96 | sb.append("CREATE TABLE ").append(ALL_MAP_TABLE).append(" (k text PRIMARY KEY"); |
| 97 | 97 | for (DataType keyType : primitiveTypes) { |
| 98 | - // This must be handled separatly | |
| 98 | + // This must be handled separately | |
| 99 | 99 | if (exclude(keyType)) |
| 100 | 100 | continue; |
| 101 | 101 | |
| 102 | 102 | for (DataType valueType : primitiveTypes) { |
| 103 | - // This must be handled separatly | |
| 103 | + // This must be handled separately | |
| 104 | 104 | if (exclude(valueType)) |
| 105 | 105 | continue; |
| 106 | 106 | sb.append(", c_map_").append(keyType).append('_').append(valueType).append(" map<").append(keyType).append(',').append(valueType).append('>'); |
| @@ -124,7 +124,7 @@ | ||
| 124 | 124 | public void preparedNativeTest() { |
| 125 | 125 | // Test preparing/bounding for all native types |
| 126 | 126 | for (DataType type : primitiveTypes) { |
| 127 | - // This must be handled separatly | |
| 127 | + // This must be handled separately | |
| 128 | 128 | if (exclude(type)) |
| 129 | 129 | continue; |
| 130 | 130 | |
| @@ -146,7 +146,7 @@ | ||
| 146 | 146 | public void preparedNativeTest2() { |
| 147 | 147 | // Test preparing/bounding for all native types |
| 148 | 148 | for (DataType type : primitiveTypes) { |
| 149 | - // This must be handled separatly | |
| 149 | + // This must be handled separately | |
| 150 | 150 | if (exclude(type)) |
| 151 | 151 | continue; |
| 152 | 152 | |
| @@ -166,7 +166,7 @@ | ||
| 166 | 166 | public void prepareListTest() { |
| 167 | 167 | // Test preparing/bounding for all possible list types |
| 168 | 168 | for (DataType rawType : primitiveTypes) { |
| 169 | - // This must be handled separatly | |
| 169 | + // This must be handled separately | |
| 170 | 170 | if (exclude(rawType)) |
| 171 | 171 | continue; |
| 172 | 172 | |
| @@ -191,7 +191,7 @@ | ||
| 191 | 191 | public void prepareListTest2() { |
| 192 | 192 | // Test preparing/bounding for all possible list types |
| 193 | 193 | for (DataType rawType : primitiveTypes) { |
| 194 | - // This must be handled separatly | |
| 194 | + // This must be handled separately | |
| 195 | 195 | if (exclude(rawType)) |
| 196 | 196 | continue; |
| 197 | 197 | |
| @@ -213,7 +213,7 @@ | ||
| 213 | 213 | public void prepareSetTest() { |
| 214 | 214 | // Test preparing/bounding for all possible set types |
| 215 | 215 | for (DataType rawType : primitiveTypes) { |
| 216 | - // This must be handled separatly | |
| 216 | + // This must be handled separately | |
| 217 | 217 | if (exclude(rawType)) |
| 218 | 218 | continue; |
| 219 | 219 | |
| @@ -238,7 +238,7 @@ | ||
| 238 | 238 | public void prepareSetTest2() { |
| 239 | 239 | // Test preparing/bounding for all possible set types |
| 240 | 240 | for (DataType rawType : primitiveTypes) { |
| 241 | - // This must be handled separatly | |
| 241 | + // This must be handled separately | |
| 242 | 242 | if (exclude(rawType)) |
| 243 | 243 | continue; |
| 244 | 244 | |
| @@ -260,12 +260,12 @@ | ||
| 260 | 260 | public void prepareMapTest() { |
| 261 | 261 | // Test preparing/bounding for all possible map types |
| 262 | 262 | for (DataType rawKeyType : primitiveTypes) { |
| 263 | - // This must be handled separatly | |
| 263 | + // This must be handled separately | |
| 264 | 264 | if (exclude(rawKeyType)) |
| 265 | 265 | continue; |
| 266 | 266 | |
| 267 | 267 | for (DataType rawValueType : primitiveTypes) { |
| 268 | - // This must be handled separatly | |
| 268 | + // This must be handled separately | |
| 269 | 269 | if (exclude(rawValueType)) |
| 270 | 270 | continue; |
| 271 | 271 | |
| @@ -291,12 +291,12 @@ | ||
| 291 | 291 | public void prepareMapTest2() { |
| 292 | 292 | // Test preparing/bounding for all possible map types |
| 293 | 293 | for (DataType rawKeyType : primitiveTypes) { |
| 294 | - // This must be handled separatly | |
| 294 | + // This must be handled separately | |
| 295 | 295 | if (exclude(rawKeyType)) |
| 296 | 296 | continue; |
| 297 | 297 | |
| 298 | 298 | for (DataType rawValueType : primitiveTypes) { |
| 299 | - // This must be handled separatly | |
| 299 | + // This must be handled separately | |
| 300 | 300 | if (exclude(rawValueType)) |
| 301 | 301 | continue; |
| 302 | 302 | |
| @@ -664,4 +664,40 @@ | ||
| 664 | 664 | } |
| 665 | 665 | fail("Did not find '" + toFind + "' in trace"); |
| 666 | 666 | } |
| 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 | + } | |
| 667 | 703 | } |
modified upgrade_guide/README.md +4 −0
| @@ -230,6 +230,10 @@ | ||
| 230 | 230 | ... |
| 231 | 231 | } |
| 232 | 232 | ``` |
| 233 | + | |
| 234 | +22. The interface `IdempotenceAwarePreparedStatement` has been removed | |
| 235 | + and now the `PreparedStatement` interface exposes 2 new methods, | |
| 236 | + `setIdempotent(Boolean)` and `isIdempotent()`. | |
| 233 | 237 | |
| 234 | 238 | ### 2.1.8 |
| 235 | 239 |