Elffuss BraidHub

kikocis/java-driver public

JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
Changesetf70a42a4e0dd502e3d95d6a6ae727a3a5377dd372605acab6f136fc8adc6fd66
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2016-02-26 17:00 UTC
Committerolim7t <omichallat+github@gmail.com> · 2016-03-03 20:21 +01:00
Parents449ad3d4341b

Browse the files at this changeset

10 files changed

modified changelog/README.md +1 −0
@@ -28,6 +28,7 @@
2828 - [bug] JAVA-1064: getTable create statement doesn't properly handle quotes in primary key.
2929 - [improvement] JAVA-888: Add cluster-wide percentile tracker.
3030 - [improvement] JAVA-963: Automatically register PercentileTracker from components that use it.
31+- [bug] JAVA-1089: Set LWT made from BuiltStatements to non-idempotent.
3132
3233 Merged from 2.0 branch:
3334
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +1 −1
@@ -37,7 +37,7 @@
3737 private int nonBuiltStatementValues;
3838
3939 Batch(RegularStatement[] statements, boolean logged) {
40- super((String) null);
40+ super(null, null, null);
4141 this.statements = statements.length == 0
4242 ? new ArrayList<RegularStatement>()
4343 : new ArrayList<RegularStatement>(statements.length);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +7 −13
@@ -28,7 +28,7 @@
2828 */
2929 public abstract class BuiltStatement extends RegularStatement {
3030
31- private static final Pattern lowercaseId = Pattern.compile("[a-z][a-z0-9_]*");
31+ private static final Pattern lowercaseAlphanumeric = Pattern.compile("[a-z][a-z0-9_]*");
3232
3333 private final List<ColumnMetadata> partitionKey;
3434 private final ByteBuffer[] routingKey;
@@ -46,22 +46,16 @@
4646 boolean hasBindMarkers;
4747 private boolean forceNoValues;
4848
49- BuiltStatement(String keyspace) {
50- this.partitionKey = null;
51- this.routingKey = null;
49+ BuiltStatement(String keyspace, List<ColumnMetadata> partitionKey, ByteBuffer[] routingKey) {
50+ this.partitionKey = partitionKey;
51+ this.routingKey = routingKey;
5252 this.keyspace = keyspace;
5353 }
5454
55- BuiltStatement(TableMetadata tableMetadata) {
56- this.partitionKey = tableMetadata.getPartitionKey();
57- this.routingKey = new ByteBuffer[tableMetadata.getPartitionKey().size()];
58- this.keyspace = escapeId(tableMetadata.getKeyspace().getName());
59- }
60-
6155 // Same as Metadata.escapeId, but we don't have access to it here.
62- protected String escapeId(String ident) {
56+ protected static String escapeId(String ident) {
6357 // we don't need to escape if it's lowercase and match non-quoted CQL3 ids.
64- return lowercaseId.matcher(ident).matches() ? ident : Metadata.quote(ident);
58+ return lowercaseAlphanumeric.matcher(ident).matches() ? ident : Metadata.quote(ident);
6559 }
6660
6761 @Override
@@ -292,7 +286,7 @@
292286 T statement;
293287
294288 ForwardingStatement(T statement) {
295- super((String) null);
289+ super(null, null, null);
296290 this.statement = statement;
297291 }
298292
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +73 −16
@@ -15,8 +15,10 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18+import com.datastax.driver.core.ColumnMetadata;
1819 import com.datastax.driver.core.TableMetadata;
1920
21+import java.nio.ByteBuffer;
2022 import java.util.ArrayList;
2123 import java.util.List;
2224
@@ -33,21 +35,33 @@
3335 private boolean ifExists;
3436
3537 Delete(String keyspace, String table, List<Selector> columns) {
36- super(keyspace);
37- this.table = table;
38- this.columns = columns;
39- this.where = new Where(this);
40- this.usings = new Options(this);
41- this.conditions = new Conditions(this);
38+ this(keyspace, table, null, null, columns);
4239 }
4340
4441 Delete(TableMetadata table, List<Selector> columns) {
45- super(table);
46- this.table = escapeId(table.getName());
42+ this(escapeId(table.getKeyspace().getName()),
43+ escapeId(table.getName()),
44+ new ByteBuffer[table.getPartitionKey().size()],
45+ table.getPartitionKey(),
46+ columns);
47+ }
48+
49+ Delete(String keyspace,
50+ String table,
51+ ByteBuffer[] routingKey,
52+ List<ColumnMetadata> partitionKey,
53+ List<Selector> columns) {
54+ super(keyspace, partitionKey, routingKey);
55+ this.table = table;
4756 this.columns = columns;
4857 this.where = new Where(this);
4958 this.usings = new Options(this);
5059 this.conditions = new Conditions(this);
60+
61+ // This is for JAVA-1089, if the query deletes an element in a list, the statement should be non-idempotent.
62+ if (!areIdempotent(columns)) {
63+ setNonIdempotentOps();
64+ }
5165 }
5266
5367 @Override
@@ -109,6 +123,9 @@
109123 * Adds a conditions clause (IF) to this statement.
110124 * <p/>
111125 * This is a shorter/more readable version for {@code onlyIf().and(condition)}.
126+ * <p/>
127+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
128+ * for more information.
112129 *
113130 * @param condition the condition to add.
114131 * @return the conditions of this query to which more conditions can be added.
@@ -119,6 +136,9 @@
119136
120137 /**
121138 * Adds a conditions clause (IF) to this statement.
139+ * <p/>
140+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
141+ * for more information.
122142 *
123143 * @return the conditions of this query to which more conditions can be added.
124144 */
@@ -160,11 +180,14 @@
160180 * Please keep in mind that using this option has a non negligible
161181 * performance impact and should be avoided when possible.
162182 * </p>
183+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
184+ * for more information.
163185 *
164186 * @return this DELETE statement.
165187 */
166188 public Delete ifExists() {
167189 this.ifExists = true;
190+ setNonIdempotentOps();
168191 return this;
169192 }
170193
@@ -188,6 +211,9 @@
188211 public Where and(Clause clause) {
189212 clauses.add(clause);
190213 statement.maybeAddRoutingKey(clause.name(), clause.firstValue());
214+ if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) {
215+ statement.setNonIdempotentOps();
216+ }
191217 checkForBindMarkers(clause);
192218 return this;
193219 }
@@ -353,7 +379,7 @@
353379 * @return this in-build DELETE Selection
354380 */
355381 public Selection listElt(String columnName, int idx) {
356- columns.add(new CollectionElementSelector(columnName, idx));
382+ columns.add(new ListElementSelector(columnName, idx));
357383 return this;
358384 }
359385
@@ -366,7 +392,7 @@
366392 * @return this in-build DELETE Selection
367393 */
368394 public Selection listElt(String columnName, BindMarker idx) {
369- columns.add(new CollectionElementSelector(columnName, idx));
395+ columns.add(new ListElementSelector(columnName, idx));
370396 return this;
371397 }
372398
@@ -378,7 +404,7 @@
378404 * @return this in-build DELETE Selection
379405 */
380406 public Selection setElt(String columnName, Object element) {
381- columns.add(new CollectionElementSelector(columnName, element));
407+ columns.add(new SetElementSelector(columnName, element));
382408 return this;
383409 }
384410
@@ -391,7 +417,7 @@
391417 * @return this in-build DELETE Selection
392418 */
393419 public Selection setElt(String columnName, BindMarker element) {
394- columns.add(new CollectionElementSelector(columnName, element));
420+ columns.add(new SetElementSelector(columnName, element));
395421 return this;
396422 }
397423
@@ -403,7 +429,7 @@
403429 * @return this in-build DELETE Selection
404430 */
405431 public Selection mapElt(String columnName, Object key) {
406- columns.add(new CollectionElementSelector(columnName, key));
432+ columns.add(new MapElementSelector(columnName, key));
407433 return this;
408434 }
409435 }
@@ -413,7 +439,7 @@
413439 * A selector can be either a column name,
414440 * a list element, a set element or a map entry.
415441 */
416- static class Selector extends Utils.Appendeable {
442+ private static class Selector extends Utils.Appendeable {
417443
418444 private final String columnName;
419445
@@ -442,9 +468,9 @@
442468 /**
443469 * A selector representing a list index, a set element or a map key in a DELETE selection clause.
444470 */
445- static class CollectionElementSelector extends Selector {
471+ private static class CollectionElementSelector extends Selector {
446472
447- private final Object key;
473+ protected final Object key;
448474
449475 CollectionElementSelector(String columnName, Object key) {
450476 super(columnName);
@@ -466,6 +492,36 @@
466492
467493 }
468494
495+ private static class ListElementSelector extends CollectionElementSelector {
496+
497+ ListElementSelector(String columnName, Object key) {
498+ super(columnName, key);
499+ }
500+ }
501+
502+ private static class SetElementSelector extends CollectionElementSelector {
503+
504+ SetElementSelector(String columnName, Object key) {
505+ super(columnName, key);
506+ }
507+ }
508+
509+ private static class MapElementSelector extends CollectionElementSelector {
510+
511+ MapElementSelector(String columnName, Object key) {
512+ super(columnName, key);
513+ }
514+ }
515+
516+ private boolean areIdempotent(List<Selector> selectors) {
517+ for (Selector sel : selectors) {
518+ if (sel instanceof ListElementSelector) {
519+ return false;
520+ }
521+ }
522+ return true;
523+ }
524+
469525 /**
470526 * Conditions for a DELETE statement.
471527 * <p>
@@ -495,6 +551,7 @@
495551 * @return this {@code Conditions} clause.
496552 */
497553 public Conditions and(Clause condition) {
554+ statement.setNonIdempotentOps();
498555 conditions.add(condition);
499556 checkForBindMarkers(condition);
500557 return this;
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +19 −5
@@ -15,8 +15,10 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18+import com.datastax.driver.core.ColumnMetadata;
1819 import com.datastax.driver.core.TableMetadata;
1920
21+import java.nio.ByteBuffer;
2022 import java.util.ArrayList;
2123 import java.util.Arrays;
2224 import java.util.List;
@@ -33,14 +35,22 @@
3335 private boolean ifNotExists;
3436
3537 Insert(String keyspace, String table) {
36- super(keyspace);
37- this.table = table;
38- this.usings = new Options(this);
38+ this(keyspace, table, null, null);
3939 }
4040
4141 Insert(TableMetadata table) {
42- super(table);
43- this.table = escapeId(table.getName());
42+ this(escapeId(table.getKeyspace().getName()),
43+ escapeId(table.getName()),
44+ new ByteBuffer[table.getPartitionKey().size()],
45+ table.getPartitionKey());
46+ }
47+
48+ Insert(String keyspace,
49+ String table,
50+ ByteBuffer[] routingKey,
51+ List<ColumnMetadata> partitionKey) {
52+ super(keyspace, partitionKey, routingKey);
53+ this.table = table;
4454 this.usings = new Options(this);
4555 }
4656
@@ -156,10 +166,14 @@
156166 * <p/>
157167 * Please keep in mind that using this option has a non negligible
158168 * performance impact and should be avoided when possible.
169+ * <p/>
170+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
171+ * for more information.
159172 *
160173 * @return this INSERT statement.
161174 */
162175 public Insert ifNotExists() {
176+ this.setNonIdempotentOps();
163177 this.ifNotExists = true;
164178 return this;
165179 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +19 −7
@@ -15,8 +15,10 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18+import com.datastax.driver.core.ColumnMetadata;
1819 import com.datastax.driver.core.TableMetadata;
1920
21+import java.nio.ByteBuffer;
2022 import java.util.ArrayList;
2123 import java.util.Arrays;
2224 import java.util.Collections;
@@ -38,16 +40,26 @@
3840 private boolean allowFiltering;
3941
4042 Select(String keyspace, String table, List<Object> columnNames, boolean isDistinct) {
41- super(keyspace);
42- this.table = table;
43- this.isDistinct = isDistinct;
44- this.columnNames = columnNames;
45- this.where = new Where(this);
43+ this(keyspace, table, null, null, columnNames, isDistinct);
4644 }
4745
4846 Select(TableMetadata table, List<Object> columnNames, boolean isDistinct) {
49- super(table);
50- this.table = escapeId(table.getName());
47+ this(escapeId(table.getKeyspace().getName()),
48+ escapeId(table.getName()),
49+ new ByteBuffer[table.getPartitionKey().size()],
50+ table.getPartitionKey(),
51+ columnNames,
52+ isDistinct);
53+ }
54+
55+ Select(String keyspace,
56+ String table,
57+ ByteBuffer[] routingKey,
58+ List<ColumnMetadata> partitionKey,
59+ List<Object> columnNames,
60+ boolean isDistinct) {
61+ super(keyspace, partitionKey, routingKey);
62+ this.table = table;
5163 this.isDistinct = isDistinct;
5264 this.columnNames = columnNames;
5365 this.where = new Where(this);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +15 −4
@@ -15,8 +15,10 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18+import com.datastax.driver.core.ColumnMetadata;
1819 import com.datastax.driver.core.TableMetadata;
1920
21+import java.nio.ByteBuffer;
2022 import java.util.List;
2123
2224 /**
@@ -27,13 +29,22 @@
2729 private final String table;
2830
2931 Truncate(String keyspace, String table) {
30- super(keyspace);
31- this.table = table;
32+ this(keyspace, table, null, null);
3233 }
3334
3435 Truncate(TableMetadata table) {
35- super(table);
36- this.table = escapeId(table.getName());
36+ this(escapeId(table.getKeyspace().getName()),
37+ escapeId(table.getName()),
38+ new ByteBuffer[table.getPartitionKey().size()],
39+ table.getPartitionKey());
40+ }
41+
42+ Truncate(String keyspace,
43+ String table,
44+ ByteBuffer[] routingKey,
45+ List<ColumnMetadata> partitionKey) {
46+ super(keyspace, partitionKey, routingKey);
47+ this.table = table;
3748 }
3849
3950 @Override
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +31 −11
@@ -15,9 +15,11 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18+import com.datastax.driver.core.ColumnMetadata;
1819 import com.datastax.driver.core.TableMetadata;
1920 import com.datastax.driver.core.querybuilder.Assignment.CounterAssignment;
2021
22+import java.nio.ByteBuffer;
2123 import java.util.ArrayList;
2224 import java.util.List;
2325
@@ -34,18 +36,22 @@
3436 private boolean ifExists;
3537
3638 Update(String keyspace, String table) {
37- super(keyspace);
38- this.table = table;
39- this.assignments = new Assignments(this);
40- this.where = new Where(this);
41- this.usings = new Options(this);
42- this.conditions = new Conditions(this);
43- this.ifExists = false;
39+ this(keyspace, table, null, null);
4440 }
4541
4642 Update(TableMetadata table) {
47- super(table);
48- this.table = escapeId(table.getName());
43+ this(escapeId(table.getKeyspace().getName()),
44+ escapeId(table.getName()),
45+ new ByteBuffer[table.getPartitionKey().size()],
46+ table.getPartitionKey());
47+ }
48+
49+ Update(String keyspace,
50+ String table,
51+ ByteBuffer[] routingKey,
52+ List<ColumnMetadata> partitionKey) {
53+ super(keyspace, partitionKey, routingKey);
54+ this.table = table;
4955 this.assignments = new Assignments(this);
5056 this.where = new Where(this);
5157 this.usings = new Options(this);
@@ -135,6 +141,9 @@
135141 * Adds a conditions clause (IF) to this statement.
136142 * <p/>
137143 * This is a shorter/more readable version for {@code onlyIf().and(condition)}.
144+ * <p/>
145+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
146+ * for more information.
138147 *
139148 * @param condition the condition to add.
140149 * @return the conditions of this query to which more conditions can be added.
@@ -210,6 +219,9 @@
210219
211220 /**
212221 * Adds a condition to the UPDATE statement those assignments are part of.
222+ * <p/>
223+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
224+ * for more information.
213225 *
214226 * @param condition the condition to add.
215227 * @return the conditions for the UPDATE statement those assignments are part of.
@@ -240,6 +252,9 @@
240252 public Where and(Clause clause) {
241253 clauses.add(clause);
242254 statement.maybeAddRoutingKey(clause.name(), clause.firstValue());
255+ if (!hasNonIdempotentOps() && !Utils.isIdempotent(clause)) {
256+ statement.setNonIdempotentOps();
257+ }
243258 checkForBindMarkers(clause);
244259 return this;
245260 }
@@ -266,6 +281,9 @@
266281
267282 /**
268283 * Adds a condition to the UPDATE statement this WHERE clause is part of.
284+ * <p/>
285+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
286+ * for more information.
269287 *
270288 * @param condition the condition to add.
271289 * @return the conditions for the UPDATE statement this WHERE clause is part of.
@@ -278,21 +296,22 @@
278296 * Sets the 'IF EXISTS' option for the UPDATE statement this WHERE clause
279297 * is part of.
280298 * <p/>
281- * <p>
282299 * An update with that option will report whether the statement actually
283300 * resulted in data being updated. The existence check and update are
284301 * done transactionally in the sense that if multiple clients attempt to
285302 * update a given row with this option, then at most one may succeed.
286303 * </p>
287- * <p>
288304 * Please keep in mind that using this option has a non negligible
289305 * performance impact and should be avoided when possible.
290306 * </p>
307+ * This will configure the statement as non-idempotent, see {@link com.datastax.driver.core.Statement#isIdempotent()}
308+ * for more information.
291309 *
292310 * @return the UPDATE statement this WHERE clause is part of.
293311 */
294312 public IfExists ifExists() {
295313 statement.ifExists = true;
314+ statement.setNonIdempotentOps();
296315 return new IfExists(statement);
297316 }
298317 }
@@ -378,6 +397,7 @@
378397 * @return this {@code Conditions} clause.
379398 */
380399 public Conditions and(Clause condition) {
400+ statement.setNonIdempotentOps();
381401 conditions.add(condition);
382402 checkForBindMarkers(condition);
383403 return this;
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +3 −0
@@ -307,6 +307,9 @@
307307 if (!isIdempotent(entry.getKey()) || !isIdempotent(entry.getValue()))
308308 return false;
309309 }
310+ } else if (value instanceof Clause) {
311+ Object clauseValue = ((Clause) value).firstValue();
312+ return isIdempotent(clauseValue);
310313 }
311314 return true;
312315 }
modified driver-core/src/test/java/com/datastax/driver/core/StatementIdempotenceTest.java +17 −0
@@ -137,6 +137,23 @@
137137 update("foo").with(set("v", Lists.newArrayList(now()))).where(eq("k", 1)),
138138 update("foo").with(set("v", Lists.newArrayList(uuid()))).where(eq("k", 1)),
139139
140+ delete().from("foo").where(lt("k", fcall("now"))),
141+ delete().from("foo").where(lt("k", now())),
142+ update("foo").where(eq("k", fcall("now"))),
143+ delete().listElt("a", 1).from("test_coll"),
144+
145+ // LWT
146+ update("foo").where(eq("is", "charlie?")).ifExists(),
147+ update("foo").where(eq("good", "drivers")).onlyIf(contains("developers", "datastax")),
148+ update("foo").onlyIf().and(contains("developers", "datastax")).where(eq("good", "drivers")),
149+ update("foo").onlyIf(contains("developers", "datastax")).with(set("v", 0)),
150+ update("foo").with(set("v", 0)).onlyIf(contains("hello", "world")),
151+
152+ insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())).ifNotExists(),
153+
154+ delete().from("foo").where(eq("k", 2)).ifExists(),
155+ delete().from("foo").onlyIf(eq("k", 2)),
156+
140157 // raw() calls
141158
142159 insertInto("foo").value("k", 1).value("v", raw("foo()")),