aerospike_helpers.cdt_ctx module

Note

Requires server version >= 4.6.0

Helper functions to generate complex data type context (cdt_ctx) objects for use with operations on nested CDTs (list, map, etc).

Example:

import aerospike
from aerospike import exception as ex
from aerospike_helpers import cdt_ctx
from aerospike_helpers.operations import map_operations
from aerospike_helpers.operations import list_operations
import sys

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

key = ("test", "demo", "foo")
listWithMaps = [
    {"name": "John", "id": 100},
    {"name": "Bill", "id": 200}
]
binName = "users"

# Write the record
client.put(key, {binName: listWithMaps})

# Example 1: read the id of the second person on the list
# Get context of the second person
ctx = [cdt_ctx.cdt_ctx_list_index(1)]
ops = [
    map_operations.map_get_by_key(
        binName, "id", aerospike.MAP_RETURN_VALUE, ctx
    )
]

_, _, result = client.operate(key, ops)
print(result)
# {'users': 200}

# Example 2: add a new person and get their rating of Facebook
cindy = {
    "name": "Cindy",
    "id": 300,
    "ratings": {
        "Facebook": 4,
        "Snapchat": 5
    }
}

# Context list used for read operation after adding Cindy
# Cindy will be the third person (index 2)
# Then go to their ratings
ctx = [cdt_ctx.cdt_ctx_list_index(2), cdt_ctx.cdt_ctx_map_key("ratings")]
ops = [
    list_operations.list_append(binName, cindy),
    map_operations.map_get_by_key(
        binName, "Facebook", aerospike.MAP_RETURN_VALUE, ctx
    )
]

_, _, result = client.operate(key, ops)
print(result)
# {'users': 4}

# Example 3: create a CDT secondary index from a base64 encoded _cdt_ctx with info command
policy = {}

bs_b4_cdt = client.get_cdtctx_base64(ctx_list_index)

r = []
r.append("sindex-create:ns=test;set=demo;indexname=test_string_list_cdt_index")
# use index_type_string to convert enum value to string
r.append(";indextype=%s" % (cdt_ctx.index_type_string(aerospike.INDEX_TYPE_LIST)))
# use index_datatype_string to convert enum value to string
r.append(";indexdata=string_list,%s" % (cdt_ctx.index_datatype_string(aerospike.INDEX_STRING)))
r.append(";context=%s" % (bs_b4_cdt))
req = ''.join(r)

# print("req is ==========={}", req)
retobj = client.info_all(req, policy=None)
# print("res is ==========={}", res)
client.index_remove('test', 'test_string_list_cdt_index', policy)

# Cleanup
client.remove(key)
client.close()
aerospike_helpers.cdt_ctx.cdt_ctx_list_index(index)

Creates a nested cdt_ctx object to lookup an object in a list by index.

If the index is negative, the lookup starts backwards from the end of the list. If it is out of bounds, a parameter error will be returned.

Parameters:

index (int) – The index to look for in the list.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_list_index_create(index: int, order: int = 0, pad: bool = False) _cdt_ctx

Creates a nested cdt_ctx object to create an list and insert at a given index.

If a list already exists at the index, a new list will not be created. Any operations using this cdt_ctx object will be applied to the existing list.

If a non-list element exists at the index, an InvalidRequest will be thrown.

Parameters:
  • index (int) – The index to create the list at.

  • order (int) – The sort order to create the List with. (default: aerospike.LIST_UNORDERED)

  • pad (bool) – If index is out of bounds and pad is True, then the list will be created at the index with None elements inserted behind it. pad is only compatible with unordered lists.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_list_rank(rank)

Creates a nested cdt_ctx object to lookup an object in a list by rank.

If the rank is negative, the lookup starts backwards from the largest rank value.

Parameters:

rank (int) – The rank to look for in the list.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_list_value(value)

Creates a nested cdt_ctx object to lookup an object in a list by value.

Parameters:

value (object) – The value to look for in the list.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_map_index(index)

The cdt_ctx object is initialized to lookup an object in a map by index.

If the index is negative, the lookup starts backwards from the end of the map.

If it is out of bounds, a parameter error will be returned.

Parameters:

index (int) – The index to look for in the map.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_map_key(key)

The cdt_ctx object is initialized to lookup an object in a map by key.

Parameters:

key (object) – The key to look for in the map.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_map_key_create(key: any, order: int = 0) _cdt_ctx

Create a map with the given sort order at the given key.

Parameters:
  • key (object) – The key to create the map at.

  • order (int) – The sort order to create the List with. (default: aerospike.MAP_UNORDERED)

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_map_rank(rank)

The cdt_ctx object is initialized to lookup an object in a map by index.

If the rank is negative, the lookup starts backwards from the largest rank value.

Parameters:

rank (int) – The rank to look for in the map.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.cdt_ctx_map_value(value)

The cdt_ctx object is initialized to lookup an object in a map by value.

Parameters:

value (object) – The value to look for in the map.

Returns:

_cdt_ctx

aerospike_helpers.cdt_ctx.index_datatype_string(index_datatype)

Converts index_datatype enum value to string.

Parameters:

index_datatype (int) – The index_datatype to convert into equivalent string value.

Returns:

(string) - must be one of must be one of ‘numeric’, ‘string’, ‘geo2dsphere’

aerospike_helpers.cdt_ctx.index_type_string(index_type)

Converts index_type enum value to string.

Parameters:

index_type (int) – The index_type to convert into equivalent string value.

Returns:

(string) - must be one of ‘default’, ‘list’, ‘mapkeys’, ‘mapvalues’