radix_engine_interface/api/
actor_index_api.rs

1use crate::api::ActorStateHandle;
2use radix_common::data::scrypto::{scrypto_decode, scrypto_encode, ScryptoDecode, ScryptoEncode};
3use radix_engine_interface::api::CollectionIndex;
4use sbor::rust::prelude::*;
5use sbor::rust::vec::Vec;
6
7/// Api to manage an iterable index
8pub trait SystemActorIndexApi<E> {
9    /// Inserts an entry into an index
10    fn actor_index_insert(
11        &mut self,
12        object_handle: ActorStateHandle,
13        collection_index: CollectionIndex,
14        key: Vec<u8>,
15        buffer: Vec<u8>,
16    ) -> Result<(), E>;
17
18    /// Inserts an entry into an index
19    fn actor_index_insert_typed<K: ScryptoEncode, V: ScryptoEncode>(
20        &mut self,
21        object_handle: ActorStateHandle,
22        collection_index: CollectionIndex,
23        key: K,
24        value: V,
25    ) -> Result<(), E> {
26        self.actor_index_insert(
27            object_handle,
28            collection_index,
29            scrypto_encode(&key).unwrap(),
30            scrypto_encode(&value).unwrap(),
31        )
32    }
33
34    /// Removes an entry from an index
35    fn actor_index_remove(
36        &mut self,
37        object_handle: ActorStateHandle,
38        collection_index: CollectionIndex,
39        key: Vec<u8>,
40    ) -> Result<Option<Vec<u8>>, E>;
41
42    /// Removes an entry from an index
43    fn actor_index_remove_typed<V: ScryptoDecode>(
44        &mut self,
45        object_handle: ActorStateHandle,
46        collection_index: CollectionIndex,
47        key: Vec<u8>,
48    ) -> Result<Option<V>, E> {
49        let rtn = self
50            .actor_index_remove(object_handle, collection_index, key)?
51            .map(|e| scrypto_decode(&e).unwrap());
52        Ok(rtn)
53    }
54
55    /// Scans arbitrary elements of count from an index
56    fn actor_index_scan_keys(
57        &mut self,
58        object_handle: ActorStateHandle,
59        collection_index: CollectionIndex,
60        limit: u32,
61    ) -> Result<Vec<Vec<u8>>, E>;
62
63    /// Scans arbitrary elements of count from an index
64    fn actor_index_scan_keys_typed<K: ScryptoDecode>(
65        &mut self,
66        object_handle: ActorStateHandle,
67        collection_index: CollectionIndex,
68        limit: u32,
69    ) -> Result<Vec<K>, E> {
70        let entries = self
71            .actor_index_scan_keys(object_handle, collection_index, limit)?
72            .into_iter()
73            .map(|key| {
74                let key: K = scrypto_decode(&key).unwrap();
75                key
76            })
77            .collect();
78
79        Ok(entries)
80    }
81
82    /// Removes and returns arbitrary elements of count from an index
83    fn actor_index_drain(
84        &mut self,
85        object_handle: ActorStateHandle,
86        collection_index: CollectionIndex,
87        limit: u32,
88    ) -> Result<Vec<(Vec<u8>, Vec<u8>)>, E>;
89
90    /// Removes and returns arbitrary elements of count from an index
91    fn actor_index_drain_typed<K: ScryptoDecode, V: ScryptoDecode>(
92        &mut self,
93        object_handle: ActorStateHandle,
94        collection_index: CollectionIndex,
95        limit: u32,
96    ) -> Result<Vec<(K, V)>, E> {
97        let entries = self
98            .actor_index_drain(object_handle, collection_index, limit)?
99            .into_iter()
100            .map(|(key, value)| {
101                let key: K = scrypto_decode(&key).unwrap();
102                let value: V = scrypto_decode(&value).unwrap();
103                (key, value)
104            })
105            .collect();
106
107        Ok(entries)
108    }
109}