본문으로 건너뛰기
버전: In Development

List & Map CDT Operations

client.operate()를 통한 원자적 서버 측 컬렉션 데이터 타입 (CDT) 작업입니다.

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()에 전달하는 작업 dict를 반환합니다:

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

Basic Write Operations

list_append(bin, val, policy=None) — 리스트 끝에 값을 추가합니다.

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

Basic Read Operations

list_get(bin, index)

특정 인덱스의 항목을 가져옵니다.

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

list_get_range(bin, index, count)

index부터 count개의 항목을 가져옵니다.

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

list_size(bin)

리스트의 항목 수를 반환합니다.

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

Remove Operations

list_remove(bin, index)

지정한 인덱스의 항목을 삭제합니다.

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

list_remove_range(bin, index, count)

index부터 count개의 항목을 삭제합니다.

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

list_pop(bin, index)

지정한 인덱스의 항목을 삭제하고 반환합니다.

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

list_pop_range(bin, index, count)

index부터 count개의 항목을 삭제하고 반환합니다.

ops = [list_ops.list_pop_range("colors", 0, 2)]
_, _, bins = client.operate(key, ops)
print(bins["colors"]) # list of removed items

list_trim(bin, index, count)

지정한 범위 [index, index+count) 밖의 항목을 삭제합니다.

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

list_clear(bin)

리스트의 모든 항목을 삭제합니다.

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

Sort & Order

list_sort(bin, sort_flags=0)

리스트를 제자리에서 정렬합니다.

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

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

list_set_order(bin, list_order=0)

리스트 정렬 타입을 설정합니다.

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

Advanced Read Operations (Value/Index/Rank)

이 작업들은 반환 내용을 제어하는 return_type 매개변수가 필요합니다.

list_get_by_value(bin, val, return_type)

지정한 값과 일치하는 항목을 가져옵니다.

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)

지정한 값 중 하나와 일치하는 항목을 가져옵니다.

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) 범위의 값을 가진 항목을 가져옵니다.

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)

지정한 반환 타입으로 인덱스 기반 항목을 가져옵니다.

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)

인덱스 범위로 항목을 가져옵니다.

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)

랭크 기반으로 항목을 가져옵니다 (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)

랭크 범위로 항목을 가져옵니다.

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

Advanced Delete Operations (Value/Index/Rank)

list_remove_by_value(bin, val, return_type)

지정한 값과 일치하는 항목을 삭제합니다.

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)

지정한 값 중 하나와 일치하는 항목을 삭제합니다.

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) 범위의 값을 가진 항목을 삭제합니다.

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)

인덱스 기반으로 항목을 삭제합니다.

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)

인덱스 범위로 항목을 삭제합니다.

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)

랭크 기반으로 항목을 삭제합니다.

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)

랭크 범위로 항목을 삭제합니다.

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

List Constants

상수설명
LIST_RETURN_NONE아무것도 반환하지 않음
LIST_RETURN_INDEX인덱스 반환
LIST_RETURN_REVERSE_INDEX역순 인덱스 반환
LIST_RETURN_RANK랭크 반환
LIST_RETURN_REVERSE_RANK역순 랭크 반환
LIST_RETURN_COUNT일치한 항목 수 반환
LIST_RETURN_VALUE값 반환
LIST_RETURN_EXISTS존재 여부 불리언 반환
LIST_UNORDERED비정렬 리스트 (기본값)
LIST_ORDERED정렬된 리스트 (정렬 순서 유지)
LIST_SORT_DEFAULT기본 정렬
LIST_SORT_DROP_DUPLICATES정렬 시 중복 제거

List Complete Example

import aerospike_py as aerospike
from aerospike_py import list_operations as list_ops

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

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

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

# 원자적 작업: 정렬, 상위 3개 가져오기, 크기 확인
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점 미만 점수 삭제
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")

# 새로운 점수 추가 및 업데이트된 크기 확인
ops = [
list_ops.list_append("scores", 97),
list_ops.list_size("scores"),
]
_, _, bins = client.operate(key, ops)
print(f"Total scores: {bins['scores']}")