Batch Operation Builders

BatchOperationBuilder - Builder for chaining operations across multiple keys.

class aerospike_sdk.aio.operations.batch.BatchOpType[source]

Bases: Enum

Type of batch operation.

INSERT = 'insert'
UPDATE = 'update'
UPSERT = 'upsert'
REPLACE = 'replace'
REPLACE_IF_EXISTS = 'replace_if_exists'
DELETE = 'delete'
class aerospike_sdk.aio.operations.batch.BatchBinBuilder[source]

Bases: object

Builder for chaining bin operations within a batch key operation.

Example

batch.insert(key).bin(“name”).set_to(“Alice”).bin(“age”).set_to(25)

__init__(key_op, bin_name)[source]
set_to(value)[source]

Set a bin value.

Parameters:

value (Any) – The value to set.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

add(value)[source]

Add value to the bin (numeric increment).

Return type:

BatchKeyOperationBuilder

increment_by(value)[source]

Alias for add().

Return type:

BatchKeyOperationBuilder

append(value)[source]

Append a string to a bin value.

Parameters:

value (str) – The string to append.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

prepend(value)[source]

Prepend a string to a bin value.

Parameters:

value (str) – The string to prepend.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

select_from(expression, *, ignore_eval_failure=False)[source]

Read the result of an expression into this bin.

Parameters:
  • expression (Union[str, FilterExpression]) – AEL string or pre-built FilterExpression.

  • ignore_eval_failure (bool) – If True, suppress evaluation errors.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

insert_from(expression, *, ignore_op_failure=False, ignore_eval_failure=False, delete_if_null=False)[source]

Write expression result to this bin only if it does not already exist.

Parameters:
  • expression (Union[str, FilterExpression]) – AEL string or pre-built FilterExpression.

  • ignore_op_failure (bool) – If True, suppress BIN_EXISTS_ERROR.

  • ignore_eval_failure (bool) – If True, suppress evaluation errors.

  • delete_if_null (bool) – If True, delete bin when expression evaluates to nil.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

update_from(expression, *, ignore_op_failure=False, ignore_eval_failure=False, delete_if_null=False)[source]

Write expression result to this bin only if it already exists.

Parameters:
  • expression (Union[str, FilterExpression]) – AEL string or pre-built FilterExpression.

  • ignore_op_failure (bool) – If True, suppress BIN_NOT_FOUND.

  • ignore_eval_failure (bool) – If True, suppress evaluation errors.

  • delete_if_null (bool) – If True, delete bin when expression evaluates to nil.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

upsert_from(expression, *, ignore_op_failure=False, ignore_eval_failure=False, delete_if_null=False)[source]

Write expression result to this bin (create or update).

Parameters:
  • expression (Union[str, FilterExpression]) – AEL string or pre-built FilterExpression.

  • ignore_op_failure (bool) – If True, suppress policy errors.

  • ignore_eval_failure (bool) – If True, suppress evaluation errors.

  • delete_if_null (bool) – If True, delete bin when expression evaluates to nil.

Return type:

BatchKeyOperationBuilder

Returns:

The parent BatchKeyOperationBuilder for chaining.

class aerospike_sdk.aio.operations.batch.BatchKeyOperationBuilder[source]

Bases: object

Builder for a single key’s operation within a batch.

This class allows chaining bin operations and then continuing to add more keys to the batch.

Example

batch.insert(key1).bin(“name”).set_to(“Alice”)

.update(key2).bin(“counter”).add(1)

__init__(batch, key, op_type)[source]
bin(bin_name)[source]

Start a bin operation chain.

Parameters:

bin_name (str) – The name of the bin.

Return type:

BatchBinBuilder

Returns:

A BatchBinBuilder for chaining bin operations.

Example

batch.insert(key).bin(“name”).set_to(“Alice”).bin(“age”).set_to(25)

put(bins)[source]

Set multiple bins at once.

Parameters:

bins (Dict[str, Any]) – Dictionary of bin name to value mappings.

Return type:

BatchKeyOperationBuilder

Returns:

self for method chaining.

Example

batch.insert(key).put({“name”: “Alice”, “age”: 25})

insert(key)[source]

Add an insert operation for another key.

Return type:

BatchKeyOperationBuilder

update(key)[source]

Add an update operation for another key.

Return type:

BatchKeyOperationBuilder

upsert(key)[source]

Add an upsert operation for another key.

Return type:

BatchKeyOperationBuilder

replace(key)[source]

Add a replace operation for another key.

Return type:

BatchKeyOperationBuilder

replace_if_exists(key)[source]

Add a replace-if-exists operation for another key.

Return type:

BatchKeyOperationBuilder

delete(key)[source]

Add a delete operation for another key.

Return type:

BatchKeyOperationBuilder

async execute()[source]

Execute all batch operations.

Return type:

RecordStream

class aerospike_sdk.aio.operations.batch.BatchOperationBuilder[source]

Bases: object

Builder for chaining operations across multiple keys.

This class enables method chaining of operations on different keys, which are then executed as a single batch operation.

Example:

results = await session.batch() \
    .insert(key1).bin("name").set_to("Alice").bin("age").set_to(25) \
    .update(key2).bin("counter").add(1) \
    .delete(key3) \
    .execute()

The operations are collected and executed together using the async client’s batch_operate method for optimal performance.

__init__(client, behavior=None, txn=None)[source]

Initialize a BatchOperationBuilder.

Parameters:
  • client (Client) – The underlying async client.

  • behavior (Optional[Behavior]) – Optional Behavior for deriving policies.

  • txn (Optional[Txn]) – Optional active Txn captured from a transactional session; stamped on the outer batch policy at execute. None means no transaction participation.

with_txn(txn)[source]

Opt this batch into (or out of) a specific transaction.

See aerospike_sdk.aio.operations.query.QueryBuilder.with_txn() for semantics.

Parameters:

txn (Optional[Txn]) – The Txn to participate in, or None to opt out.

Return type:

BatchOperationBuilder

Returns:

This builder for chaining.

insert(key)[source]

Add an insert (create only) operation for a key.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for chaining bin operations.

Example

batch.insert(key).bin(“name”).set_to(“Alice”)

update(key)[source]

Add an update (update only) operation for a key.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for chaining bin operations.

Example

batch.update(key).bin(“counter”).add(1)

upsert(key)[source]

Add an upsert (create or update) operation for a key.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for chaining bin operations.

Example

batch.upsert(key).bin(“name”).set_to(“Bob”)

replace(key)[source]

Add a replace (create or replace) operation for a key.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for chaining bin operations.

Example

batch.replace(key).put({“name”: “Charlie”, “age”: 35})

replace_if_exists(key)[source]

Add a replace-if-exists operation for a key.

This operation will fail if the record does not exist.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for chaining bin operations.

Example

batch.replace_if_exists(key).put({“name”: “Updated”, “status”: “active”})

delete(key)[source]

Add a delete operation for a key.

Parameters:

key (Key) – The key for the record.

Return type:

BatchKeyOperationBuilder

Returns:

A BatchKeyOperationBuilder for continuing the chain.

Example

batch.delete(key1).delete(key2).execute()

async execute()[source]

Execute all batch operations.

Example:

stream = await (
    session.batch()
    .insert(key1)
    .bin("name").set_to("Ada")
    .upsert(key2)
    .bin("n").set_to(1)
    .execute()
)
rows = await stream.collect()
Return type:

RecordStream

Returns:

A RecordStream of per-key RecordResult items.

Raises:

ValueError – If no operations have been added.