aerospike_helpers.batch package

aerospike_helpers.batch.records module

Classes for the use with client batch APIs batch_write(), batch_operate(), batch_apply(), batch_remove().

class aerospike_helpers.batch.records.Apply(key, module, function, args, policy=None)

Bases: BatchRecord

BatchApply is used for executing Batch UDF (user defined function) apply commands with batch_write and retrieving results.

module

Name of the lua module previously registered with the server.

function

Name of the UDF to invoke.

args

List of arguments to pass to the UDF.

policy

An optional dictionary of batch apply policy flags.

__init__(key, module, function, args, policy=None)

Example:

# Create a batch Apply to apply UDF "test_func" to bin "a" from the record.
# Assume that "test_func" takes a bin name string as an argument.
# Assume the appropriate UDF module has already been registered.
import aerospike_helpers.operations as op


module = "my_lua"
function = "test_func"

bin_name = "a"
args = [
    bin_name
]

namespace = "test"
set = "demo"
user_key = 1
key = (namespace, set, user_key)

ba = Apply(key, module, function, args)
class aerospike_helpers.batch.records.BatchRecord(key)

Bases: object

BatchRecord provides the base fields for BatchRecord objects.

BatchRecord should usually be read from as a result and not created by the user. Its subclasses can be used as input to batch_write. Client methods batch_apply(), batch_operate(), batch_remove() with batch_records field as a list of these BatchRecord objects containing the batch request results.

key

The aerospike key to operate on.

Type:

tuple

record

The record corresponding to the requested key.

Type:

Record Tuple

result

The status code of the command.

Type:

int

in_doubt

Is it possible that the write command completed even though an error was generated. This may be the case when a client error occurs (like timeout) after the command was sent to the server.

Type:

bool

__init__(key)
class aerospike_helpers.batch.records.BatchRecords(batch_records=None)

Bases: object

BatchRecords is used as input and output for multiple batch APIs.

batch_records

A list of BatchRecord subtype objects used to define batched commands and hold results. BatchRecord Types can be Remove, Write, Read, and Apply.

Type:

list

result

The status code of the last batch call that used this BatchRecords. 0 if all batched commands succeeded (or if the only failures were FILTERED_OUT or RECORD_NOT_FOUND) Not 0 if an error occurred. The most common error is -16 (One or more batched commands failed).

Type:

int

__init__(batch_records=None)

Example:

import aerospike
import aerospike_helpers.operations.operations as op
from aerospike_helpers.batch.records import BatchRecords, Remove, Write, Read

# Setup
config = {
    "hosts": [("127.0.0.1", 3000)]
}
client = aerospike.client(config)

namespace = "test"
set_ = "demo"
keys = [
    (namespace, set_, 1),
    (namespace, set_, 2),
    (namespace, set_, 3),
]
bin_name = "id"
for key in keys:
    client.put(key, {bin_name: 1})

# Create a BatchRecords to remove a record, write a bin, and read a bin.
brs = BatchRecords(
    [
        Remove(
            key=keys[0],
        ),
        Write(
            key=keys[1],
            ops=[
                op.write(bin_name, 100),
                op.read(bin_name),
            ]
        ),
        Read(
            key=keys[2],
            ops=[
                op.read(bin_name)
            ]
        )
    ]
)

# Note this call will mutate brs and set results in it.
client.batch_write(brs)
for br in brs.batch_records:
    print(br.result)
    print(br.record)
# 0
# (('test', 'demo', 1, bytearray(b'...')), {'ttl': 4294967295, 'gen': 0}, {})
# 0
# (('test', 'demo', 2, bytearray(b'...')), {'ttl': 2592000, 'gen': 4}, {'id': 100})
# 0
# (('test', 'demo', 3, bytearray(b'...')), {'ttl': 2592000, 'gen': 3}, {'id': 1})
class aerospike_helpers.batch.records.Read(key, ops, read_all_bins=False, meta=None, policy=None)

Bases: BatchRecord

Read is used for executing Batch read commands with batch_write and retrieving results.

Deprecated since version 19.1.0: Deprecated the "ttl" option in the meta parameter. Use the policy parameter in a Write BatchRecord to set the "ttl" instead.

ops

list of aerospike operation dictionaries to perform on the record at key.

meta

the metadata to set for this command

read_all_bins

An optional bool, if True, read all bins in the record.

policy

An optional dictionary of Batch Read Policies.

__init__(key, ops, read_all_bins=False, meta=None, policy=None)

Example:

# Create a batch Read to read bin "a" from the record.
import aerospike
import aerospike_helpers.operations as op
from aerospike_helpers.batch.records import Read

bin_name = "a"

namespace = "test"
set = "demo"
user_key = 1
key = (namespace, set, user_key)

ops = [
    op.read(bin_name)
]

meta={"gen": 1, "ttl": aerospike.TTL_NEVER_EXPIRE}
br = Read(key, ops, meta=meta)
class aerospike_helpers.batch.records.Remove(key, policy=None)

Bases: BatchRecord

Remove is used for executing Batch remove commands with batch_write and retrieving results.

policy

An optional dictionary of batch remove policy flags.

Type:

Batch Remove Policies, optional

__init__(key, policy=None)

Example:

# Create a batch Remove to remove the record.
import aerospike_helpers.operations as op


namespace = "test"
set = "demo"
user_key = 1
key = (namespace, set, user_key)

br = Remove(key, ops)
class aerospike_helpers.batch.records.Write(key, ops, meta=None, policy=None)

Bases: BatchRecord

Write is used for executing Batch write commands with batch_write and retrieving batch write results.

Changed in version 19.1.0: Deprecated the meta["ttl"] parameter. Use the policy parameter to set ttl instead.

ops

A list of aerospike operation dictionaries to perform on the record at key.

meta

the metadata to set for this command

policy

An optional dictionary of batch write policy flags.

__init__(key, ops, meta=None, policy=None)

Example:

# Create a batch Write to increment bin "a" by 10 and read the result from the record.
import aerospike
import aerospike_helpers.operations as op
from aerospike_helpers.batch.records import Write

bin_name = "a"

namespace = "test"
set = "demo"
user_key = 1
key = (namespace, set, user_key)

ops = [
    op.increment(bin_name, 10),
    op.read(bin_name)
]

meta={"gen": 1, "ttl": aerospike.TTL_NEVER_EXPIRE}
bw = Write(key, ops, meta=meta)