1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::api::ActorStateHandle;
use radix_engine_common::data::scrypto::{
    scrypto_decode, scrypto_encode, ScryptoDecode, ScryptoEncode,
};
use radix_engine_common::types::SortedKey;
use radix_engine_interface::api::CollectionIndex;
use sbor::rust::prelude::*;
use sbor::rust::vec::Vec;

pub trait SortedIndexKeyPayloadMarker {}
pub trait SortedIndexEntryPayloadMarker {}

pub trait ClientActorSortedIndexApi<E> {
    /// Inserts an entry into a sorted index
    fn actor_sorted_index_insert(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        sorted_key: SortedKey,
        buffer: Vec<u8>,
    ) -> Result<(), E>;

    /// Inserts an entry into a sorted index
    fn actor_sorted_index_insert_typed<V: ScryptoEncode>(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        sorted_key: SortedKey,
        value: V,
    ) -> Result<(), E> {
        self.actor_sorted_index_insert(
            object_handle,
            collection_index,
            sorted_key,
            scrypto_encode(&value).unwrap(),
        )
    }

    /// Removes an entry from a sorted index
    fn actor_sorted_index_remove(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        sorted_key: &SortedKey,
    ) -> Result<Option<Vec<u8>>, E>;

    /// Removes an entry from a sorted index
    fn actor_sorted_index_remove_typed<V: ScryptoDecode>(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        sorted_key: &SortedKey,
    ) -> Result<Option<V>, E> {
        let rtn = self
            .actor_sorted_index_remove(object_handle, collection_index, sorted_key)?
            .map(|e| scrypto_decode(&e).unwrap());
        Ok(rtn)
    }

    /// Scans the first elements of count from a sorted index
    fn actor_sorted_index_scan(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        count: u32,
    ) -> Result<Vec<(SortedKey, Vec<u8>)>, E>;

    /// Scans the first elements of count from a sorted index
    fn actor_sorted_index_scan_typed<K: ScryptoDecode, V: ScryptoDecode>(
        &mut self,
        object_handle: ActorStateHandle,
        collection_index: CollectionIndex,
        count: u32,
    ) -> Result<Vec<(K, V)>, E> {
        let entries = self
            .actor_sorted_index_scan(object_handle, collection_index, count)?
            .into_iter()
            .map(|(key, buf)| {
                let typed_key: K = scrypto_decode(&key.1).unwrap();
                let typed_value: V = scrypto_decode(&buf).unwrap();
                (typed_key, typed_value)
            })
            .collect();

        Ok(entries)
    }
}