kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b53119758b6e8b5120a0eb4803e82d16ac83a222223759b44f8ba5da41da6972 |
|---|---|
| Author | Dmitry Murashenkov <dmitry.murashenkov@mind.com> · 2016-06-09 12:14 +03:00 |
| Committer | Dmitry Murashenkov <dmitry.murashenkov@mind.com> · 2016-06-09 13:17 +03:00 |
| Parents | 47f8bf44bd1e |
7 files changed
modified changelog/README.md +1 −0
| @@ -2,6 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | ### 3.0.3 (in progress) |
| 4 | 4 | |
| 5 | +- [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence | |
| 5 | 6 | |
| 6 | 7 | ### 3.0.2 |
| 7 | 8 |
modified driver-core/src/main/java/com/datastax/driver/core/BatchStatement.java +8 −0
| @@ -243,6 +243,14 @@ | ||
| 243 | 243 | return null; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | + @Override | |
| 247 | + public Boolean isIdempotent() { | |
| 248 | + if (idempotent != null) { | |
| 249 | + return idempotent; | |
| 250 | + } | |
| 251 | + return isBatchIdempotent(statements); | |
| 252 | + } | |
| 253 | + | |
| 246 | 254 | void ensureAllSet() { |
| 247 | 255 | for (Statement statement : statements) |
| 248 | 256 | if (statement instanceof BoundStatement) |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +39 −3
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | import com.google.common.collect.ImmutableMap; |
| 24 | 24 | |
| 25 | 25 | import java.nio.ByteBuffer; |
| 26 | +import java.util.Collection; | |
| 26 | 27 | import java.util.Map; |
| 27 | 28 | |
| 28 | 29 | /** |
| @@ -498,11 +499,22 @@ | ||
| 498 | 499 | * {@link QueryOptions#getDefaultIdempotence()}. |
| 499 | 500 | * <p/> |
| 500 | 501 | * By default, this method returns {@code null} for all statements, except for |
| 501 | - * {@link BuiltStatement}s, where the value will be inferred from the query: if it updates | |
| 502 | - * counters, prepends/appends to a list, or uses a function call or | |
| 502 | + * <ul> | |
| 503 | + * <li>{@link BuiltStatement} - value will be inferred from the query: if it updates counters, | |
| 504 | + * prepends/appends to a list, or uses a function call or | |
| 503 | 505 | * {@link com.datastax.driver.core.querybuilder.QueryBuilder#raw(String)} anywhere in an inserted value, |
| 504 | 506 | * the result will be {@code false}; otherwise it will be {@code true}. |
| 505 | - * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides every other mechanism. | |
| 507 | + * </li> | |
| 508 | + * <li> | |
| 509 | + * {@link com.datastax.driver.core.querybuilder.Batch} and {@link BatchStatement}: | |
| 510 | + * <ol> | |
| 511 | + * <li>If any statement in batch has isIdempotent() false - return false</li> | |
| 512 | + * <li>If no statements with isIdempotent() false, but some have isIdempotent() null - return null</li> | |
| 513 | + * <li>Otherwise - return true</li> | |
| 514 | + * </ol> | |
| 515 | + * </li> | |
| 516 | + * </ul> | |
| 517 | + * In all cases, calling {@link #setIdempotent(boolean)} forces a value that overrides calculated value. | |
| 506 | 518 | * <p/> |
| 507 | 519 | * Note that when a statement is prepared ({@link Session#prepare(String)}), its idempotence flag will be propagated |
| 508 | 520 | * to all {@link PreparedStatement}s created from it. |
| @@ -566,4 +578,28 @@ | ||
| 566 | 578 | this.outgoingPayload = payload == null ? null : ImmutableMap.copyOf(payload); |
| 567 | 579 | return this; |
| 568 | 580 | } |
| 581 | + | |
| 582 | + protected static Boolean isBatchIdempotent(Collection<? extends Statement> statements) | |
| 583 | + { | |
| 584 | + boolean hasNullIdempotentStatements = false; | |
| 585 | + boolean hasNotIdempotentStatements = false; | |
| 586 | + for (Statement statement : statements) { | |
| 587 | + Boolean innerIdempotent = statement.isIdempotent(); | |
| 588 | + if (innerIdempotent == null) { | |
| 589 | + hasNullIdempotentStatements = true; | |
| 590 | + } | |
| 591 | + else if (!innerIdempotent) { | |
| 592 | + hasNotIdempotentStatements = true; | |
| 593 | + } | |
| 594 | + } | |
| 595 | + if (hasNotIdempotentStatements) { | |
| 596 | + return false; | |
| 597 | + } | |
| 598 | + else if (hasNullIdempotentStatements) { | |
| 599 | + return null; | |
| 600 | + } | |
| 601 | + else { | |
| 602 | + return true; | |
| 603 | + } | |
| 604 | + } | |
| 569 | 605 | } |
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +9 −4
| @@ -15,10 +15,7 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core.querybuilder; |
| 17 | 17 | |
| 18 | -import com.datastax.driver.core.CodecRegistry; | |
| 19 | -import com.datastax.driver.core.ProtocolVersion; | |
| 20 | -import com.datastax.driver.core.RegularStatement; | |
| 21 | -import com.datastax.driver.core.SimpleStatement; | |
| 18 | +import com.datastax.driver.core.*; | |
| 22 | 19 | |
| 23 | 20 | import java.nio.ByteBuffer; |
| 24 | 21 | import java.util.ArrayList; |
| @@ -175,6 +172,14 @@ | ||
| 175 | 172 | return statements.isEmpty() ? null : statements.get(0).getKeyspace(); |
| 176 | 173 | } |
| 177 | 174 | |
| 175 | + @Override | |
| 176 | + public Boolean isIdempotent() { | |
| 177 | + if (idempotent != null) { | |
| 178 | + return idempotent; | |
| 179 | + } | |
| 180 | + return isBatchIdempotent(statements); | |
| 181 | + } | |
| 182 | + | |
| 178 | 183 | /** |
| 179 | 184 | * The options of a BATCH statement. |
| 180 | 185 | */ |
added driver-core/src/test/java/com/datastax/driver/core/AbstractIdempotencyTest.java +177 −0
| @@ -0,0 +1,177 @@ | ||
| 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 | +import com.datastax.driver.core.querybuilder.Batch; | |
| 19 | +import org.testng.annotations.Test; | |
| 20 | + | |
| 21 | +import static org.testng.Assert.*; | |
| 22 | +import static org.testng.Assert.assertNull; | |
| 23 | + | |
| 24 | +public abstract class AbstractIdempotencyTest { | |
| 25 | + | |
| 26 | + protected abstract TestBatch createBatch(); | |
| 27 | + | |
| 28 | + /** | |
| 29 | + * Unify Batch and BatchStatement to avoid duplicating all tests | |
| 30 | + */ | |
| 31 | + protected interface TestBatch { | |
| 32 | + //Batch only accepts RegularStatement, so we use it for common interface | |
| 33 | + void add(RegularStatement statement); | |
| 34 | + Boolean isIdempotent(); | |
| 35 | + void setIdempotent(boolean idempotent); | |
| 36 | + } | |
| 37 | + | |
| 38 | + @Test(groups = "unit") | |
| 39 | + public void isIdempotent_should_return_true_if_no_statements_added() { | |
| 40 | + TestBatch batch = createBatch(); | |
| 41 | + assertTrue(batch.isIdempotent()); | |
| 42 | + } | |
| 43 | + | |
| 44 | + @Test(groups = "unit") | |
| 45 | + public void isIdempotent_should_return_true_if_all_statements_are_idempotent() { | |
| 46 | + TestBatch batch = createBatch(); | |
| 47 | + assertTrue(batch.isIdempotent()); | |
| 48 | + | |
| 49 | + batch.add(statementWithIdempotency(true)); | |
| 50 | + assertTrue(batch.isIdempotent()); | |
| 51 | + | |
| 52 | + batch.add(statementWithIdempotency(true)); | |
| 53 | + assertTrue(batch.isIdempotent()); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Test(groups = "unit") | |
| 57 | + public void isIdempotent_should_return_false_if_any_statements_is_nonidempotent() { | |
| 58 | + TestBatch batch = createBatch(); | |
| 59 | + assertTrue(batch.isIdempotent()); | |
| 60 | + | |
| 61 | + batch.add(statementWithIdempotency(true)); | |
| 62 | + assertTrue(batch.isIdempotent()); | |
| 63 | + | |
| 64 | + batch.add(statementWithIdempotency(false)); | |
| 65 | + assertFalse(batch.isIdempotent()); | |
| 66 | + | |
| 67 | + batch.add(statementWithIdempotency(true)); | |
| 68 | + assertFalse(batch.isIdempotent()); | |
| 69 | + } | |
| 70 | + | |
| 71 | + @Test(groups = "unit") | |
| 72 | + public void isIdempotent_should_return_null_if_no_nonidempotent_statements_and_some_are_nullidempotent() { | |
| 73 | + TestBatch batch = createBatch(); | |
| 74 | + assertTrue(batch.isIdempotent()); | |
| 75 | + | |
| 76 | + batch.add(statementWithIdempotency(true)); | |
| 77 | + assertTrue(batch.isIdempotent()); | |
| 78 | + | |
| 79 | + batch.add(statementWithIdempotency(null)); | |
| 80 | + assertNull(batch.isIdempotent()); | |
| 81 | + | |
| 82 | + batch.add(statementWithIdempotency(true)); | |
| 83 | + assertNull(batch.isIdempotent()); | |
| 84 | + } | |
| 85 | + | |
| 86 | + @Test(groups = "unit") | |
| 87 | + public void isIdempotent_should_return_false_if_both_nonidempotent_and_nullidempotent_statements_present() { | |
| 88 | + TestBatch batch = createBatch(); | |
| 89 | + assertTrue(batch.isIdempotent()); | |
| 90 | + | |
| 91 | + batch.add(statementWithIdempotency(true)); | |
| 92 | + assertTrue(batch.isIdempotent()); | |
| 93 | + | |
| 94 | + batch.add(statementWithIdempotency(null)); | |
| 95 | + assertNull(batch.isIdempotent()); | |
| 96 | + | |
| 97 | + batch.add(statementWithIdempotency(false)); | |
| 98 | + assertFalse(batch.isIdempotent()); | |
| 99 | + | |
| 100 | + batch.add(statementWithIdempotency(true)); | |
| 101 | + assertFalse(batch.isIdempotent()); | |
| 102 | + | |
| 103 | + batch.add(statementWithIdempotency(null)); | |
| 104 | + assertFalse(batch.isIdempotent()); | |
| 105 | + | |
| 106 | + batch.add(statementWithIdempotency(false)); | |
| 107 | + assertFalse(batch.isIdempotent()); | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Test(groups = "unit") | |
| 111 | + public void isIdempotent_should_return_override_flag_if_no_statements_added() { | |
| 112 | + TestBatch batch = createBatch(); | |
| 113 | + assertTrue(batch.isIdempotent()); | |
| 114 | + | |
| 115 | + batch.setIdempotent(false); | |
| 116 | + assertFalse(batch.isIdempotent()); | |
| 117 | + } | |
| 118 | + | |
| 119 | + @Test(groups = "unit") | |
| 120 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_true() { | |
| 121 | + TestBatch batch = createBatch(); | |
| 122 | + assertTrue(batch.isIdempotent()); | |
| 123 | + | |
| 124 | + batch.add(statementWithIdempotency(true)); | |
| 125 | + assertTrue(batch.isIdempotent()); | |
| 126 | + | |
| 127 | + batch.setIdempotent(false); | |
| 128 | + assertFalse(batch.isIdempotent()); | |
| 129 | + } | |
| 130 | + | |
| 131 | + @Test(groups = "unit") | |
| 132 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_null() { | |
| 133 | + TestBatch batch = createBatch(); | |
| 134 | + assertTrue(batch.isIdempotent()); | |
| 135 | + | |
| 136 | + batch.add(statementWithIdempotency(null)); | |
| 137 | + assertNull(batch.isIdempotent()); | |
| 138 | + | |
| 139 | + batch.setIdempotent(false); | |
| 140 | + assertFalse(batch.isIdempotent()); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @Test(groups = "unit") | |
| 144 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_false() { | |
| 145 | + TestBatch batch = createBatch(); | |
| 146 | + assertTrue(batch.isIdempotent()); | |
| 147 | + | |
| 148 | + batch.add(statementWithIdempotency(false)); | |
| 149 | + assertFalse(batch.isIdempotent()); | |
| 150 | + | |
| 151 | + batch.setIdempotent(true); | |
| 152 | + assertTrue(batch.isIdempotent()); | |
| 153 | + } | |
| 154 | + | |
| 155 | + @Test(groups = "unit") | |
| 156 | + public void isIdempotent_should_return_override_flag_if_calculated_idempotency_equals_override_value() { | |
| 157 | + TestBatch batch = createBatch(); | |
| 158 | + assertTrue(batch.isIdempotent()); | |
| 159 | + | |
| 160 | + batch.add(statementWithIdempotency(false)); | |
| 161 | + assertFalse(batch.isIdempotent()); | |
| 162 | + | |
| 163 | + batch.setIdempotent(false); | |
| 164 | + assertFalse(batch.isIdempotent()); | |
| 165 | + } | |
| 166 | + | |
| 167 | + private RegularStatement statementWithIdempotency(Boolean idempotency) { | |
| 168 | + RegularStatement statement = new SimpleStatement("fake statement"); | |
| 169 | + if (idempotency != null) { | |
| 170 | + statement.setIdempotent(idempotency); | |
| 171 | + assertEquals(statement.isIdempotent(), idempotency); | |
| 172 | + } else { | |
| 173 | + assertNull(statement.isIdempotent()); | |
| 174 | + } | |
| 175 | + return statement; | |
| 176 | + } | |
| 177 | +} |
added driver-core/src/test/java/com/datastax/driver/core/BatchStatementIdempotencyTest.java +44 −0
| @@ -0,0 +1,44 @@ | ||
| 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 | +public class BatchStatementIdempotencyTest extends AbstractIdempotencyTest { | |
| 19 | + | |
| 20 | + @Override | |
| 21 | + protected TestBatch createBatch() { | |
| 22 | + return new TestBatchStatementWrapper(); | |
| 23 | + } | |
| 24 | + | |
| 25 | + static class TestBatchStatementWrapper implements TestBatch { | |
| 26 | + | |
| 27 | + private final BatchStatement batch = new BatchStatement(); | |
| 28 | + | |
| 29 | + @Override | |
| 30 | + public void add(RegularStatement statement) { | |
| 31 | + batch.add(statement); | |
| 32 | + } | |
| 33 | + | |
| 34 | + @Override | |
| 35 | + public Boolean isIdempotent() { | |
| 36 | + return batch.isIdempotent(); | |
| 37 | + } | |
| 38 | + | |
| 39 | + @Override | |
| 40 | + public void setIdempotent(boolean idempotent) { | |
| 41 | + batch.setIdempotent(idempotent); | |
| 42 | + } | |
| 43 | + } | |
| 44 | +} |
added driver-core/src/test/java/com/datastax/driver/core/querybuilder/BatchIdempotencyTest.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.querybuilder; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.AbstractIdempotencyTest; | |
| 19 | +import com.datastax.driver.core.RegularStatement; | |
| 20 | + | |
| 21 | +public class BatchIdempotencyTest extends AbstractIdempotencyTest { | |
| 22 | + | |
| 23 | + @Override | |
| 24 | + protected AbstractIdempotencyTest.TestBatch createBatch() { | |
| 25 | + return new TestBatchWrapper(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + static class TestBatchWrapper implements TestBatch { | |
| 29 | + | |
| 30 | + private final Batch batch = QueryBuilder.batch(); | |
| 31 | + | |
| 32 | + @Override | |
| 33 | + public void add(RegularStatement statement) { | |
| 34 | + batch.add(statement); | |
| 35 | + } | |
| 36 | + | |
| 37 | + @Override | |
| 38 | + public Boolean isIdempotent() { | |
| 39 | + return batch.isIdempotent(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + public void setIdempotent(boolean idempotent) { | |
| 44 | + batch.setIdempotent(idempotent); | |
| 45 | + } | |
| 46 | + } | |
| 47 | +} |