Elffuss BraidHub

kikocis/java-driver public

Merge pull request #184 from olim7t/java358

Add ResultSet.wasApplied() for conditional queries (JAVA-358).
Changeset0b5c0256706fa15e43047a26596952fb369ae4613194b2a7761ca9a66bc60b41
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 18:42 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2014-08-27 18:42 +02:00
Parents60207be50813, 89ef3acd73d5

Browse the files at this changeset

3 files changed

modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +35 −3
@@ -40,9 +40,11 @@
4040 private static final Queue<List<ByteBuffer>> EMPTY_QUEUE = new ArrayDeque<List<ByteBuffer>>(0);
4141
4242 protected final ColumnDefinitions metadata;
43+ private final boolean wasApplied;
4344
44- private ArrayBackedResultSet(ColumnDefinitions metadata) {
45+ private ArrayBackedResultSet(ColumnDefinitions metadata, List<ByteBuffer> firstRow) {
4546 this.metadata = metadata;
47+ this.wasApplied = checkWasApplied(firstRow, metadata);
4648 }
4749
4850 static ArrayBackedResultSet fromMessage(Responses.Result msg, SessionManager session, ExecutionInfo info, Statement statement) {
@@ -127,6 +129,11 @@
127129 };
128130 }
129131
132+ @Override
133+ public boolean wasApplied() {
134+ return wasApplied;
135+ }
136+
130137 @Override
131138 public String toString() {
132139 StringBuilder sb = new StringBuilder();
@@ -143,7 +150,7 @@
143150 private SinglePage(ColumnDefinitions metadata,
144151 Queue<List<ByteBuffer>> rows,
145152 ExecutionInfo info) {
146- super(metadata);
153+ super(metadata, rows.peek());
147154 this.info = info;
148155 this.rows = rows;
149156 }
@@ -209,7 +216,10 @@
209216 SessionManager session,
210217 Statement statement) {
211218
212- super(metadata);
219+ // Note: as of Cassandra 2.1.0, it turns out that the result of a CAS update is never paged, so
220+ // we could hard-code the result of wasApplied in this class to "true". However, we can not be sure
221+ // that this will never change, so apply the generic check by peeking at the first row.
222+ super(metadata, rows.peek());
213223 this.currentPage = rows;
214224 this.infos.offer(info);
215225
@@ -380,4 +390,26 @@
380390 }
381391 }
382392 }
393+
394+ // This method checks the value of the "[applied]" column manually, to avoid instantiating an ArrayBackedRow
395+ // object that we would throw away immediately.
396+ private static boolean checkWasApplied(List<ByteBuffer> firstRow, ColumnDefinitions metadata) {
397+ // If the column is not present or not a boolean, we assume the query
398+ // was not a conditional statement, and therefore return true.
399+ if (firstRow == null)
400+ return true;
401+ int[] is = metadata.findAllIdx("[applied]");
402+ if (is == null)
403+ return true;
404+ int i = is[0];
405+ if (!DataType.cboolean().equals(metadata.getType(i)))
406+ return true;
407+
408+ // Otherwise return the value of the column
409+ ByteBuffer value = firstRow.get(i);
410+ if (value == null || value.remaining() == 0)
411+ return false;
412+
413+ return TypeCodec.BooleanCodec.instance.deserializeNoBoxing(value);
414+ }
383415 }
modified driver-core/src/main/java/com/datastax/driver/core/ResultSet.java +28 −0
@@ -185,4 +185,32 @@
185185 * @return a list of the execution info for all the queries made for this ResultSet.
186186 */
187187 public List<ExecutionInfo> getAllExecutionInfo();
188+
189+ /**
190+ * If the query that produced this ResultSet was a conditional update,
191+ * return whether it was successfully applied.
192+ * <p>
193+ * This is equivalent to calling:
194+ *
195+ * <pre>
196+ * rs.one().getBool("[applied]");
197+ * </pre>
198+ *
199+ * <p>
200+ * For consistency, this method always returns {@code true} for
201+ * non-conditional queries (although there is no reason to call the method
202+ * in that case).
203+ *
204+ * <p>
205+ * Note that, for versions of Cassandra strictly lower than 2.0.9 and 2.1.0-rc2,
206+ * a server-side bug (CASSANDRA-7337) causes this method to always return
207+ * {@code true} for batches containing conditional queries.
208+ * </p>
209+ *
210+ * @return if the query was a conditional update, whether it was applied.
211+ * {@code true} for other types of queries.
212+ *
213+ * @see <a href="https://issues.apache.org/jira/browse/CASSANDRA-7337">CASSANDRA-7337</a>
214+ */
215+ public boolean wasApplied();
188216 }
added driver-core/src/test/java/com/datastax/driver/core/ConditionalUpdateTest.java +67 −0
@@ -0,0 +1,67 @@
1+package com.datastax.driver.core;
2+
3+import java.util.Collection;
4+import java.util.Collections;
5+
6+import org.testng.annotations.Test;
7+import static org.testng.Assert.*;
8+
9+/**
10+ * Test {@link ResultSet#wasApplied()} for conditional updates.
11+ */
12+public class ConditionalUpdateTest extends CCMBridge.PerClassSingleNodeCluster {
13+
14+ @Override
15+ protected Collection<String> getTableDefinitions() {
16+ return Collections.singletonList("CREATE TABLE test(k1 int, k2 int, v int, PRIMARY KEY (k1, k2))");
17+ }
18+
19+ @Test(groups = "short")
20+ public void singleUpdateTest() {
21+ session.execute("TRUNCATE test");
22+ session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)");
23+
24+ ResultSet rs = session.execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 2");
25+ assertFalse(rs.wasApplied());
26+ // Ensure that reading the status does not consume a row:
27+ assertFalse(rs.isExhausted());
28+
29+ rs = session.execute("UPDATE test SET v = 3 WHERE k1 = 1 AND k2 = 1 IF v = 1");
30+ assertTrue(rs.wasApplied());
31+ assertFalse(rs.isExhausted());
32+
33+ // Non-conditional statement
34+ rs = session.execute("UPDATE test SET v = 4 WHERE k1 = 1 AND k2 = 1");
35+ assertTrue(rs.wasApplied());
36+ }
37+
38+ @Test(groups = "short")
39+ public void batchUpdateTest() {
40+ session.execute("TRUNCATE test");
41+ session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)");
42+ session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)");
43+
44+ PreparedStatement ps = session.prepare("UPDATE test SET v = :new WHERE k1 = :k1 AND k2 = :k2 IF v = :old");
45+ BatchStatement batch = new BatchStatement();
46+ batch.add(ps.bind().setInt("k1", 1).setInt("k2", 1).setInt("old", 2).setInt("new", 3)); // will fail
47+ batch.add(ps.bind().setInt("k1", 1).setInt("k2", 2).setInt("old", 1).setInt("new", 3));
48+
49+
50+ ResultSet rs = session.execute(batch);
51+ assertFalse(rs.wasApplied());
52+ }
53+
54+
55+ @Test(groups = "short")
56+ public void multipageResultSetTest() {
57+ session.execute("TRUNCATE test");
58+ session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 1, 1)");
59+ session.execute("INSERT INTO test (k1, k2, v) VALUES (1, 2, 1)");
60+
61+ // This is really contrived, we just want to cover the code path in ArrayBackedResultSet#MultiPage.
62+ // Currently CAS update results are never multipage, so it's hard to come up with a meaningful example.
63+ ResultSet rs = session.execute(new SimpleStatement("SELECT * FROM test WHERE k1 = 1").setFetchSize(1));
64+
65+ assertTrue(rs.wasApplied());
66+ }
67+}