/** * Adds a new statement to the BATCH statement these options are part of. * * @param statement the statement to add. * @return the BATCH statement these options are part of. */ public Batch add(RegularStatement statement) { return this.statement.add(statement); } }
/** * Builds a new {@code BATCH} query on the provided statements. * * <p>This method will build a logged batch (this is the default in CQL3). To create unlogged * batches, use {@link #unloggedBatch}. Also note that for convenience, if the provided statements * are counter statements, this method will create a {@code COUNTER} batch even though COUNTER * batches are never logged (so for counters, using this method is effectively equivalent to using * {@link #unloggedBatch}). * * @param statements the statements to batch. * @return a new {@code RegularStatement} that batch {@code statements}. */ public static Batch batch(RegularStatement... statements) { return new Batch(statements, true); }
/** * Adds a new statement to this batch. * * @param statement the new statement to add. * @return this batch. * @throws IllegalArgumentException if counter and non-counter operations are mixed. */ public Batch add(RegularStatement statement) { boolean isCounterOp = statement instanceof BuiltStatement && ((BuiltStatement) statement).isCounterOp(); if (this.isCounterOp == null) setCounterOp(isCounterOp); else if (isCounterOp() != isCounterOp) throw new IllegalArgumentException( "Cannot mix counter operations and non-counter operations in a batch statement"); this.statements.add(statement); if (statement instanceof BuiltStatement) { this.hasBindMarkers |= ((BuiltStatement) statement).hasBindMarkers; } else { // For non-BuiltStatement, we cannot know if it includes a bind makers and we assume it does. // In practice, // this means we will always serialize values as strings when there is non-BuiltStatement this.hasBindMarkers = true; this.nonBuiltStatementValues += ((SimpleStatement) statement).valuesCount(); } setDirty(); return this; }
@Override StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { StringBuilder builder = new StringBuilder(); builder.append( isCounterOp() ? "BEGIN COUNTER BATCH" : (logged ? "BEGIN BATCH" : "BEGIN UNLOGGED BATCH")); if (!usings.usings.isEmpty()) { builder.append(" USING "); Utils.joinAndAppend(builder, codecRegistry, " AND ", usings.usings, variables); } builder.append(' '); for (int i = 0; i < statements.size(); i++) { RegularStatement stmt = statements.get(i); if (stmt instanceof BuiltStatement) { BuiltStatement bst = (BuiltStatement) stmt; builder.append(maybeAddSemicolon(bst.buildQueryString(variables, codecRegistry))); } else { String str = stmt.getQueryString(codecRegistry); builder.append(str); if (!str.trim().endsWith(";")) builder.append(';'); // Note that we force hasBindMarkers if there is any non-BuiltStatement, so we know // that we can only get there with variables == null assert variables == null; } } builder.append("APPLY BATCH;"); return builder; }
protected void write(int n, boolean batch, ConsistencyLevel cl) { // We don't use insert for our test because the resultSet don't ship the queriedHost // Also note that we don't use tracing because this would trigger requests that screw up the // test for (int i = 0; i < n; ++i) if (batch) // BUG: WriteType == SIMPLE session() .execute( batch() .add(insertInto(tableName).values(new String[] {"k", "i"}, new Object[] {0, 0})) .setConsistencyLevel(cl)); else session() .execute( new SimpleStatement(String.format("INSERT INTO %s(k, i) VALUES (0, 0)", tableName)) .setConsistencyLevel(cl)); }
batch = batch() .add( insertInto("foo") .values( 3.4 })) .add( update("foo") .with(setIdx("a", 2, "foo")) .and(remove("c", "a")) .where(eq("k", 2))) .add( delete() .listElt("a", 3) .from("foo") .where(eq("k", 1))) .using(timestamp(42)); assertEquals(batch.toString(), query); assertEquals(batch.toString(), query); assertEquals(batch().toString(), "BEGIN BATCH APPLY BATCH;");
/** * Adds a new statement to this batch. * * @param statement the new statement to add. * @return this batch. * * @throws IllegalArgumentException if counter and non-counter operations * are mixed. */ public Batch add(RegularStatement statement) { boolean isCounterOp = statement instanceof BuiltStatement && ((BuiltStatement) statement).isCounterOp(); if (this.isCounterOp == null) setCounterOp(isCounterOp); else if (isCounterOp() != isCounterOp) throw new IllegalArgumentException("Cannot mix counter operations and non-counter operations in a batch statement"); this.statements.add(statement); if (statement instanceof BuiltStatement) this.hasBindMarkers = ((BuiltStatement)statement).hasBindMarkers; else // For non-BuiltStatement, we cannot know if it includes a bind makers. So we assume it does. this.hasBindMarkers = true; checkForBindMarkers(null); if (routingKey == null && statement.getRoutingKey() != null) routingKey = statement.getRoutingKey(); return this; }
@Override public Boolean isIdempotent() { if (idempotent != null) { return idempotent; } return isBatchIdempotent(statements); }
/** * Apply Options to the whole batch statement. * * @param options */ public void withOptions(BatchOptions options) { if (options != null) { if (options.getConsistencyLevel() != null) { b.setConsistencyLevel(options.getConsistencyLevel()); } if (options.getRetryPolicy() != null) { b.setRetryPolicy(options.getRetryPolicy()); } } }
@Override public String toString() { return batch.toString(); } }
@Override public BatchBuilder withConsistencyLevel(ConsistencyLevel consistency) { batch.setConsistencyLevel(consistency); return this; }
batch = batch() .add(update("foo").with(incr("a", 1))) .add(update("foo").with(incr("b", 2))) .add(update("foo").with(incr("c", 3))) .using(timestamp(42)); assertEquals(batch.toString(), query); batch = batch() .add(update("foo").with(incr("a"))) .add(update("foo").with(incr("b"))) .add(update("foo").with(incr("c"))) .using(timestamp(42)); assertEquals(batch.toString(), query); batch = batch() .add(update("foo").with(decr("a", 1))) .add(update("foo").with(decr("b", 2))) .add(update("foo").with(decr("c", 3))) .using(timestamp(42)); assertEquals(batch.toString(), query); batch = batch() .add(update("foo").with(decr("a"))) .add(update("foo").with(decr("b"))) .add(update("foo").with(decr("c")))
private void testWideBatchRows(int key) throws Throwable { // Write data Batch q = batch(); for (int i = 0; i < 4000; ++i) { q = q.add(insertInto("wide_batch_rows").value("k", key).value("i", i)); } session().execute(q.setConsistencyLevel(ConsistencyLevel.QUORUM)); // Read data ResultSet rs = session().execute(select("i").from("wide_batch_rows").where(eq("k", key))); // Verify data int i = 0; for (Row row : rs) { assertTrue(row.getInt("i") == i++); } }
batch = batch() .add( insertInto("baz", "foo") .values( 3.4 })) .add( update("foo") .with(setIdx("a", 2, "foo")) .and(remove("c", "a")) .where(eq("k", 2))) .add( delete() .listElt("a", 3) .from("foo") .where(eq("k", 1))) .using(timestamp(42)); assertEquals(batch.toString(), query); assertThat(batch.getKeyspace()) assertEquals(batch.toString(), query); assertEquals(batch().toString(), "BEGIN BATCH APPLY BATCH;");
@Override StringBuilder buildQueryString(List<Object> variables, CodecRegistry codecRegistry) { StringBuilder builder = new StringBuilder(); builder.append(isCounterOp() ? "BEGIN COUNTER BATCH" : (logged ? "BEGIN BATCH" : "BEGIN UNLOGGED BATCH")); if (!usings.usings.isEmpty()) { builder.append(" USING "); Utils.joinAndAppend(builder, codecRegistry, " AND ", usings.usings, variables); } builder.append(' '); for (int i = 0; i < statements.size(); i++) { RegularStatement stmt = statements.get(i); if (stmt instanceof BuiltStatement) { BuiltStatement bst = (BuiltStatement) stmt; builder.append(maybeAddSemicolon(bst.buildQueryString(variables, codecRegistry))); } else { String str = stmt.getQueryString(codecRegistry); builder.append(str); if (!str.trim().endsWith(";")) builder.append(';'); // Note that we force hasBindMarkers if there is any non-BuiltStatement, so we know // that we can only get there with variables == null assert variables == null; } } builder.append("APPLY BATCH;"); return builder; }
@Override public Boolean isIdempotent() { if (idempotent != null) { return idempotent; } return isBatchIdempotent(statements); }
Insert insert1 = QueryBuilder.insert... Batch batch = QueryBuilder.batch(insert1); Insert insert2 = QueryBuilder.insert... batch.add(insert2); CassandraOperations cassandraOperations = new CassandraTemplate(session); WriteOptions options = new WriteOptions(); options.setTtl(60); options.setConsistencyLevel(ConsistencyLevel.ONE); options.setRetryPolicy(RetryPolicy.DOWNGRADING_CONSISTENCY); cassandraOperations.execute(batch.toString(), options);
Batch(RegularStatement[] statements, boolean logged) { super(null, null, null); this.statements = statements.length == 0 ? new ArrayList<RegularStatement>() : new ArrayList<RegularStatement>(statements.length); this.logged = logged; this.usings = new Options(this); for (int i = 0; i < statements.length; i++) add(statements[i]); }
batch = batch() .add(insertInto(table).values(new String[] {"k", "a"}, new Object[] {42, 1})) .add(update(table).using(ttl(400))); assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), bb); assertEquals(batch.toString(), batch_query); batch_query += "SELECT * FROM foo WHERE k=42;"; batch_query += "APPLY BATCH;"; batch = batch().add(select().from("foo").where(eq("k", 42))); assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null); assertEquals(batch.toString(), batch_query); batch_query += "INSERT INTO foo.bar (a) VALUES (123);"; batch_query += "APPLY BATCH;"; batch = batch().using(timestamp(42)).add(insertInto("foo", "bar").value("a", 123)); assertEquals(batch.getRoutingKey(protocolVersion, codecRegistry), null); assertEquals(batch.toString(), batch_query);