Skip to content

Latest commit

 

History

History
188 lines (143 loc) · 5.69 KB

BatchExecute.md

File metadata and controls

188 lines (143 loc) · 5.69 KB

Batch Execute

If you want to perform multiple data modification operations in a single roundtrip, Session::batchExecute will be the choice.
The signature of Session::batchExecute is:

seastar::future<> batchExecute(BatchCommand&& command);

Different to query and execute, batchExecute takes BatchCommand instead of Command.
Only insert, update and delete statements can use in a batch command.

BatchCommand

BatchCommand can built with chain style or command style:

auto command = cql::BatchCommand()
	.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
		.prepareQuery()
		.openParameterSet()
			.addParameters(cql::Int(1), cql::Text("a"))
		.openParameterSet()
			.addParameters(cql::Int(2), cql::Text("b"))
		.openParameterSet()
			.addParameters(cql::Int(3), cql::Text("c"))
	.addQuery("update exmple_ks.my_table set name = ? where id = ?")
		.addParameters(cql::Text("aaa"), cql::Int(1))
	.addQuery("delete from exmple_ks.my_table where id = ?")
		.addParameters(cql::Int(3));

Equals

cql::BatchCommand command;
command.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
command.prepareQuery();
command.openParameterSet();
command.addParameters(cql::Int(1), cql::Text("a"));
command.openParameterSet();
command.addParameters(cql::Int(2), cql::Text("b"));
command.openParameterSet();
command.addParameters(cql::Int(3), cql::Text("c"));
command.addQuery("update exmple_ks.my_table set name = ? where id = ?");
command.addParameters(cql::Text("aaa"), cql::Int(1));
command.addQuery("delete from exmple_ks.my_table where id = ?");
command.addParameters(cql::Int(3));

Then pass to Session::batchExecute:

session.batchExecute(std::move(command)).then([] {
	std::cout << "batch was successful" << std::endl;
});

BatchCommand contains following options:

setType(BatchType)

Set the type of this batch, default is "Logged".
Supported batch types:

  • BatchType::Logged
  • BatchType::UnLogged
  • BatchType::Counter

setConsistency(ConsistencyLevel)

Set the consistency level of this query.
This will override the default setting in SessionConfiguration.
For more information see this page.
Supported consistencies:

  • ConsistencyLevel::Any
  • ConsistencyLevel::One
  • ConsistencyLevel::Two
  • ConsistencyLevel::Three
  • ConsistencyLevel::Quorum
  • ConsistencyLevel::All
  • ConsistencyLevel::LocalQuorum
  • ConsistencyLevel::EachQuorum
  • ConsistencyLevel::Serial
  • ConsistencyLevel::LocalSerial
  • ConsistencyLevel::LocalOne

addQuery(...)

Add a new query to this batch. There multiple overloads of this function:

BatchCommand& addQuery(std::string&& query) &;
BatchCommand& addQuery(const char* query, std::size_t size) &;
template <std::size_t Size>
BatchCommand& addQuery(const char(&query)[Size]) &;

Notice overload 2 and 3 won't copy the string to the internal buffer in BatchCommand, it can avoid the unnecessary memory copy, but you have to ensure the string is alive until Session::batchExecute is finished.

prepareQuery(bool)

Set should prepare the last query.
This will override the default setting in SessionConfiguration.
The prepare request will only be sent if the query isn't prepared before.
For more information see Prepare.

openParameterSet()

Open a new parameter set explicitly of the last query.
This function is optional if there only one parameter set, for example:

auto command = cql::BatchCommand()
	.addQuery("update exmple_ks.my_table set name = ? where id = ?")
		.addParameters(cql::Text("aaa"), cql::Int(1));

Euqals

auto command = cql::BatchCommand()
	.openParameterSet()
		.addQuery("update exmple_ks.my_table set name = ? where id = ?")
			.addParameters(cql::Text("aaa"), cql::Int(1));

But it's required if you want to open multiple parameter sets, for example:

auto command = cql::BatchCommand()
	.addQuery("insert into exmple_ks.my_table (id, name) values (?,?)")
		.prepareQuery()
		.openParameterSet()
			.addParameters(cql::Int(1), cql::Text("a"))
		.openParameterSet()
			.addParameters(cql::Int(2), cql::Text("b"))
		.openParameterSet()
			.addParameters(cql::Int(3), cql::Text("c"));

addParameter(T&&)

Add single query parameter bound by position to the last parameter set.
The position is incremental, when this function is called.

addParameters(Args&&...)

Add multiple query parameters bound by position to the last parameter set.
The position is incremental, when this function is called.

For example:

auto command = cql::BatchCommand()
	.addQuery("update exmple_ks.my_table set name = ? where id = ?")
		.addParameters(cql::Text("aaa"), cql::Int(1));

Equals

auto command = cql::BatchCommand()
	.addQuery("update exmple_ks.my_table set name = ? where id = ?")
		.addParameter(cql::Text("aaa"))
		.addParameter(cql::Int(1));

setSerialConsistency(ConsistencyLevel)

Set the serial consistency level of this query.
Can only be either SERIAL or LOCAL_SERIAL.

setDefaultTimestamp(std::chrono::system_clock::time_point)

Set the default timestamp of this query.
This will replace the server side assigned timestamp as default timestamp.
A timestamp in the query itself will still override this timestamp.

setMaxRetries(std::size_t)

Set the maximum retry times after the first try has failed, default is 0.
Please ensure all statements are idempotent, this driver won't check it.