본문으로 건너뛰기

List 및 Map CDT 작업

client.operate()로 서버에서 원자적으로 실행하는 collection data type(CDT) operation을 설명합니다.

from aerospike_py import list_operations as list_ops
from aerospike_py import map_operations as map_ops
import aerospike_py as aerospike

List CDT Operations

list_ops.* 함수는 client.operate() 또는 client.operate_ordered()에 전달할 operation dict를 반환합니다.

ops = [
list_ops.list_append("scores", 100),
list_ops.list_size("scores"),
]
_, _, bins = client.operate(key, ops)

기본 Write Operation

list_append(bin, val, policy=None) — list 끝에 value append.

ops = [list_ops.list_append("colors", "red")]
client.operate(key, ops)

기본 Read Operation

list_get(bin, index)

특정 index 의 item 가져오기.

ops = [list_ops.list_get("scores", 0)]
_, _, bins = client.operate(key, ops)
print(bins["scores"]) # 첫 element

list_get_range(bin, index, count)

index 부터 count 개 item 가져오기.

ops = [list_ops.list_get_range("scores", 0, 3)]
_, _, bins = client.operate(key, ops)
print(bins["scores"]) # 첫 3개 element

list_size(bin)

list 의 item 수 반환.

ops = [list_ops.list_size("scores")]
_, _, bins = client.operate(key, ops)
print(bins["scores"]) # 예: 5

Remove Operation

list_remove(bin, index)

주어진 index 의 item 제거.

ops = [list_ops.list_remove("colors", 0)]
client.operate(key, ops)

list_remove_range(bin, index, count)

index 부터 count 개 item 제거.

ops = [list_ops.list_remove_range("colors", 1, 2)]
client.operate(key, ops)

list_pop(bin, index)

주어진 index 의 item 을 제거하고 반환.

ops = [list_ops.list_pop("colors", 0)]
_, _, bins = client.operate(key, ops)
print(bins["colors"]) # 제거된 item

list_pop_range(bin, index, count)

index 부터 count 개 item 을 제거하고 반환.

ops = [list_ops.list_pop_range("colors", 0, 2)]
_, _, bins = client.operate(key, ops)
print(bins["colors"]) # 제거된 item 의 list

list_trim(bin, index, count)

지정된 범위 [index, index+count) 밖의 item 제거.

ops = [list_ops.list_trim("scores", 1, 3)]
client.operate(key, ops)

list_clear(bin)

list 의 모든 item 제거.

ops = [list_ops.list_clear("scores")]
client.operate(key, ops)

Sort & Order

list_sort(bin, sort_flags=0)

list 를 in place 로 sort.

ops = [list_ops.list_sort("scores")]
client.operate(key, ops)

# sort 중 중복 제거
ops = [list_ops.list_sort("scores", aerospike.LIST_SORT_DROP_DUPLICATES)]
client.operate(key, ops)

list_set_order(bin, list_order=0)

list ordering 타입 설정.

ops = [list_ops.list_set_order("scores", aerospike.LIST_ORDERED)]
client.operate(key, ops)

Advanced Read Operation (Value/Index/Rank 기반)

이 operation 들은 반환되는 내용을 제어하는 return_type 파라미터 필요.

list_get_by_value(bin, val, return_type)

주어진 value 와 매칭되는 item 가져오기.

ops = [list_ops.list_get_by_value("tags", "urgent", aerospike.LIST_RETURN_INDEX)]
_, _, bins = client.operate(key, ops)

list_get_by_value_list(bin, values, return_type)

주어진 value 중 어느 하나와 매칭되는 item 가져오기.

ops = [list_ops.list_get_by_value_list(
"tags", ["urgent", "important"], aerospike.LIST_RETURN_COUNT
)]
_, _, bins = client.operate(key, ops)

list_get_by_value_range(bin, begin, end, return_type)

[begin, end) 범위의 value 를 가진 item 가져오기.

ops = [list_ops.list_get_by_value_range(
"scores", 80, 100, aerospike.LIST_RETURN_VALUE
)]
_, _, bins = client.operate(key, ops)

list_get_by_index(bin, index, return_type)

지정된 return type 으로 index 기반 item 가져오기.

ops = [list_ops.list_get_by_index("scores", 0, aerospike.LIST_RETURN_VALUE)]
_, _, bins = client.operate(key, ops)

list_get_by_index_range(bin, index, return_type, count=None)

index range 로 item 가져오기.

ops = [list_ops.list_get_by_index_range(
"scores", 2, aerospike.LIST_RETURN_VALUE, count=3
)]
_, _, bins = client.operate(key, ops)

list_get_by_rank(bin, rank, return_type)

rank 기반 item 가져오기 (0 = 최소).

ops = [list_ops.list_get_by_rank("scores", 0, aerospike.LIST_RETURN_VALUE)]
_, _, bins = client.operate(key, ops)

list_get_by_rank_range(bin, rank, return_type, count=None)

rank range 로 item 가져오기.

ops = [list_ops.list_get_by_rank_range(
"scores", -3, aerospike.LIST_RETURN_VALUE, count=3
)]
_, _, bins = client.operate(key, ops)

Advanced Remove Operation (Value/Index/Rank 기반)

list_remove_by_value(bin, val, return_type)

주어진 value 와 매칭되는 item 제거.

ops = [list_ops.list_remove_by_value("tags", "temp", aerospike.LIST_RETURN_COUNT)]
_, _, bins = client.operate(key, ops)

list_remove_by_value_list(bin, values, return_type)

주어진 value 중 어느 하나와 매칭되는 item 제거.

ops = [list_ops.list_remove_by_value_list(
"tags", ["temp", "debug"], aerospike.LIST_RETURN_NONE
)]
client.operate(key, ops)

list_remove_by_value_range(bin, begin, end, return_type)

[begin, end) 범위의 value 를 가진 item 제거.

ops = [list_ops.list_remove_by_value_range(
"scores", 0, 50, aerospike.LIST_RETURN_COUNT
)]
_, _, bins = client.operate(key, ops)

list_remove_by_index(bin, index, return_type)

index 기반 item 제거.

ops = [list_ops.list_remove_by_index("scores", 0, aerospike.LIST_RETURN_VALUE)]
_, _, bins = client.operate(key, ops)

list_remove_by_index_range(bin, index, return_type, count=None)

index range 로 item 제거.

ops = [list_ops.list_remove_by_index_range(
"scores", 0, aerospike.LIST_RETURN_NONE, count=2
)]
client.operate(key, ops)

list_remove_by_rank(bin, rank, return_type)

rank 기반 item 제거.

ops = [list_ops.list_remove_by_rank("scores", 0, aerospike.LIST_RETURN_VALUE)]
_, _, bins = client.operate(key, ops)

list_remove_by_rank_range(bin, rank, return_type, count=None)

rank range 로 item 제거.

ops = [list_ops.list_remove_by_rank_range(
"scores", 0, aerospike.LIST_RETURN_NONE, count=2
)]
client.operate(key, ops)

List Constant

Constant설명
LIST_RETURN_NONE아무것도 반환 안 함
LIST_RETURN_INDEXindex 반환
LIST_RETURN_REVERSE_INDEXreverse index 반환
LIST_RETURN_RANKrank 반환
LIST_RETURN_REVERSE_RANKreverse rank 반환
LIST_RETURN_COUNT매칭된 item 수 반환
LIST_RETURN_VALUEvalue 반환
LIST_RETURN_EXISTSboolean 존재 여부 반환
LIST_UNORDEREDunordered list (default)
LIST_ORDEREDordered list (sort order 유지)
LIST_SORT_DEFAULTdefault sort
LIST_SORT_DROP_DUPLICATESsort 시 중복 제거

List 전체 예제

import aerospike_py as aerospike
from aerospike_py import list_operations as list_ops

with aerospike.client({
"hosts": [("127.0.0.1", 3000)],
}).connect() as client:

key = ("test", "demo", "player1")

# scores list 초기화
client.put(key, {"scores": [85, 92, 78, 95, 88]})

# Atomic: sort + top 3 가져오기 + size
ops = [
list_ops.list_sort("scores"),
list_ops.list_get_by_rank_range(
"scores", -3, aerospike.LIST_RETURN_VALUE, count=3
),
]
_, _, bins = client.operate(key, ops)
print(f"Top 3 scores: {bins['scores']}")

# 80 미만 score 제거
ops = [
list_ops.list_remove_by_value_range(
"scores", 0, 80, aerospike.LIST_RETURN_COUNT
),
]
_, _, bins = client.operate(key, ops)
print(f"Removed {bins['scores']} low scores")

# 새 score append + 업데이트된 size 가져오기
ops = [
list_ops.list_append("scores", 97),
list_ops.list_size("scores"),
]
_, _, bins = client.operate(key, ops)
print(f"Total scores: {bins['scores']}")