Skip to content
Visit booth 3171 at Google Cloud Next to see how to unlock real-time decisions at scaleMore info

Map operations

Context

Context TypeDescription
MAP_INDEXFinds an element in a Map by index. A negative index is a lookup performed in reverse from the end of the map. If it is out of bounds, a parameter error is returned.
MAP_KEYFinds an element in a Map by key.
MAP_RANKFinds an element in a Map by rank. A negative rank is a lookup performed in reverse from the highest ranked.
MAP_VALUEFinds an element in a Map by value.
MAP_KEY_CREATEFinds an element in a Map by key. Creates the element if it does not exist.

The context parameter is a list of context types defining a path to the nested map element the operation should be applied to. Without a context it is assumed that the operation occurs at the top level of the map.

Write flags

FlagDescription
MODIFY_DEFAULTDefault: upserts; create or update map keys
CREATE_ONLYOnly create new map keys. Fail if the key already exists
UPDATE_ONLYOnly update existing map keys. Fail if a key does not exist
NO_FAILNo-op instead of fail if a policy violation occurred, such as CREATE_ONLY
DO_PARTIALWhen used with NO_FAIL, add elements that did not violate a policy

Return types

Return TypeDescription
KEYThe key of the element (in single result operations) or elements (in multi result operations)
VALUEThe value of the element (in single result operations) or elements (in multi result operations)
KEY_VALUEThe key/value pair of the element (in single result operations) or elements (in multi result operations)
ORDERED_MAPReturns results as an ordered Map
UNORDERED_MAPReturns results as an unordered Map
NONENothing is returned. It speeds up remove operations by not constructing a reply
COUNTNumber of elements returned
INDEXKey order position of the element, from 0 (smallest key) to N-1 (largest)
REVERSE_INDEXReverse key order position of the element, from 0 (largest key) to N-1 (smallest)
RANKValue order of the element, with 0 being the smallest value
REVERSE_RANKReverse value order of the element, with 0 being the largest value
INVERTEDInvert the search criteria. It can be combined with another return type in the range operations
EXISTSIf the return type is EXISTS, the function returns a boolean value. For example, using the Map function get_all_by_value with the return type EXISTS returns true if the specified value is contained in the Map. The Map function get_all_by_value_list with the EXISTS, returns true if the Map contains any of the specified values.

Modify operations

clear
clear()
Description

Clears the map. Map type stays the same.

Returns

null

decrement
decrement(createType, key, delta-value)
Description

Decrements values by delta-value for all items identified by key.

Only works for integer or float value types in the {key: value} pair. Create map bin with type=createType if bin did not exist.

Type interaction between value and delta-value

Value-TypeDelta: integerDelta: float
Map Entry: integerSubtract normallyTruncate to nearest integer and subtract
Map Entry: floatConvert integer to float and subtractSubtract normally
Returns

New value after decrement

increment
increment(createType, key, delta-value)
Description

Increments values by delta-value for all items identified by key.

Only works for integer or float value types in the {key: value} pair. Create map bin with type=createType if bin did not exist.

Type interaction between value and delta-value

Value-TypeDelta: integerDelta: float
Map Entry: integerAdd normallyTruncate to nearest integer and add
Map Entry: floatConvert integer to float and addAdd normally
Returns

New value after increment

put_items
put_items(bin, items[, writeFlags, createType, context])
Description

Creates a map bin with a specified order, if the bin does not exist. Takes a map of key/value items and adds them to the map bin.

Returns

The element count of the map after the operation, in the ‘bins’ part of the record under the bin name.

Order

A map newly created with put_items can be optionally declared as K_ORDERED, KV_ORDERED or UNORDERED (default) using the createType. In most clients this is the map order attribute of the map policy.

K_ORDERED maps are preferred over UNORDERED maps, with no real disadvantage in terms of space used.

Code sample
_, _, bins = client.operate(key, [map_operations.map_put_items("m", {"a":1, "b":{}})])
# {a: 1, b: {}}
print("The map has {} elements".format(bins["m"]))
# The map has 2 elements
ctx = [cdt_ctx.cdt_ctx_map_key("b")]
client.operate(key, [map_operations.map_put_items("m", {"c": 3, "d": 4}, ctx=ctx)])
# {a: 1, b: {c: 3, d: 4}}
put
put(bin, key, value[, writeFlags, createType, context])
Description

Creates a map bin with a specified order, if the bin does not exist. Adds a key/value element to the map.

Returns

The element count of the map after the operation, in the ‘bins’ part of the record under the bin name.

Order

A map newly created with put can be optionally declared as K_ORDERED, KV_ORDERED or UNORDERED (default) using the createType. In most clients this is the map order attribute of the map policy.

K_ORDERED maps are preferred over UNORDERED maps, with no real disadvantage in terms of space used.

Performance

For an in-memory namespace, ordered maps will have a 𝓞(log N) worst-case performance. For unordered maps or namespaces not in memory the worst-case performance is 𝓞(N).

Code sample
# {a: 1}
client.operate(key, [map_operations.map_put("m", "b", {})])
# {a: 1, b: {}}
ctx = [cdt_ctx.cdt_ctx_map_key("b")]
client.operate(key, [map_operations.map_put("m", "c", 3, ctx=ctx)])
# {a: 1, b: {c: 3}}
remove_all_by_key_list
remove_all_by_key_list(returnType, keyList)
Description

Remove all {key: value} pairs where map.key ∈ keyList.

Returns

Multi result

remove_all_by_value_list
remove_all_by_value_list(returnType, valueList)
Description

Remove all {key: value} pairs where map.value ∈ valueList.

Returns

Multi result

remove_all_by_value
remove_all_by_value(returnType, value)
Description

Remove all {key: value} pairs where map.value == value.

Returns

Multi result

remove_by_index_range
remove_by_index_range(returnType, index[, count])
Description

Remove all {key: value} pairs where k = indexof(map.key) and k >= index and k < index + count. Omitting count select element(s) where k >= origin + index.

Returns

Multi result

remove_by_index
remove_by_index(returnType, index)
Description

Remove {key: value} entry where map.key is the ith smallest key where i == index.

Returns

Single result

remove_by_key_interval
remove_by_key_interval(returnType, keyStart[, keyStop])
Description

Remove all {key: value} pairs where map.key >= keyStart and map.key < keyStop. Omitting keyStop select element(s) where map.key >= keyStart.

Returns

Multi result

remove_by_key_rel_index_range
remove_by_key_rel_index_range(returnType, key, index[, count])
Description

Remove all {key: value} pairs where origin = index(key), k = indexof(map.key) and k >= origin + index and k < origin + index + count. Omitting count select element(s) where k >= origin + index.

Returns

Multi result

remove_by_key
remove_by_key(returnType, key)
Description

Remove entry {key: value} where map.key == key.

Returns

Single result

remove_by_rank_range
remove_by_rank_range(returnType, rank[, count])
Description

Remove all {key: value} pairs where r = rankof(map.value) and r >= rank and r < rank + count. Omitting count select element(s) where r >= rank.

Returns

Multi result

remove_by_rank
remove_by_rank(returnType, rank)
Description

Remove {key: value} entry where map.value is the ith smallest value where i == rank.

Returns

Single result

remove_by_value_interval
remove_by_value_interval(returnType, valueStart[, valueStop])
Description

Remove all {key: value} pairs where map.value >= valueStart and map.value < valueStop. Omitting valueStop select element(s) where map.value >= valueStart.

Returns

Multi result

remove_by_value_rel_rank_range
remove_by_value_rel_rank_range(returnType, value, rank[, count])
Description

Remove all {key: value} pairs where origin = rank(value), r = rankof(map.value) and r >= origin + rank and r < origin + rank + count. Omitting count select element(s) where r >= origin + rank.

Returns

Multi result

Read operations

get_all_by_key_list
get_all_by_key_list(returnType, keyList)
Description

Get all {key: value} pairs where map.key ∈ keyList.

Returns

Multi result

get_all_by_value_list
get_all_by_value_list(returnType, valueList)
Description

Get all {key: value} pairs where map.value ∈ valueList.

Returns

Multi result

get_all_by_value
get_all_by_value(returnType, value)
Description

Get all {key: value} pairs where map.value == value.

Returns

Multi result

get_by_index_range
get_by_index_range(returnType, index[, count])
Description

Get all {key: value} pairs where k = indexof(map.key) and k >= index and k < index + count. Omitting count select element(s) where k >= origin + index.

Returns

Multi result

get_by_index
get_by_index(returnType, index)
Description

Get {key: value} entry where map.key is the ith smallest key where i == index.

Returns

Single result

get_by_key_interval
get_by_key_interval(returnType, keyStart[, keyStop])
Description

Get all {key: value} pairs where map.key >= keyStart and map.key < keyStop. Omitting keyStop select element(s) where map.key >= keyStart.

Returns

Multi result

get_by_key_rel_index_range
get_by_key_rel_index_range(returnType, key, index[, count])
Description

Get all {key: value} pairs where origin = index(key), k = indexof(map.key) and k >= origin + index and k < origin + index + count. Omitting count select element(s) where k >= origin + index.

Returns

Multi result

get_by_key
get_by_key(returnType, key)
Description

Get entry {key: value} where map.key == key.

Returns

Single result

get_by_rank_range
get_by_rank_range(returnType, rank[, count])
Description

Get all {key: value} pairs where r = rankof(map.value) and r >= rank and r < rank + count. Omitting count select element(s) where r >= rank.

Returns

Multi result

get_by_rank
get_by_rank(returnType, rank)
Description

Get {key: value} entry where map.value is the ith smallest value where i == rank.

Returns

Single result

get_by_value_interval
get_by_value_interval(returnType, valueStart[, valueStop])
Description

Get all {key: value} pairs where map.value >= valueStart and map.value < valueStop. Omitting valueStop select element(s) where map.value >= valueStart.

Returns

Multi result

get_by_value_rel_rank_range
get_by_value_rel_rank_range(returnType, value, rank[, count])
Description

Get all {key: value} pairs where origin = rank(value), r = rankof(map.value) and r >= origin + rank and r < origin + rank + count. Omitting count select element(s) where r >= origin + rank.

Returns

Multi result

size
size()
Returns

Element count

Set operations

set_type
set_type(type)
Description

Modifies the order of an existing map. Order can be UNORDERED, K_ORDERED, or KV_ORDERED

Returns

null

Performance

The worst-case performance of modifying an unordered map to a K_ORDERED or KV_ORDERED one is 𝓞(N log N).

Feedback

Was this page helpful?

What type of feedback are you giving?

What would you like us to know?

+Capture screenshot

Can we reach out to you?