radix_engine_interface/api/
actor_index_api.rs1use 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
7pub trait SystemActorIndexApi<E> {
9 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 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 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 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 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 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 #[allow(clippy::type_complexity)]
84 fn actor_index_drain(
85 &mut self,
86 object_handle: ActorStateHandle,
87 collection_index: CollectionIndex,
88 limit: u32,
89 ) -> Result<Vec<(Vec<u8>, Vec<u8>)>, E>;
90
91 fn actor_index_drain_typed<K: ScryptoDecode, V: ScryptoDecode>(
93 &mut self,
94 object_handle: ActorStateHandle,
95 collection_index: CollectionIndex,
96 limit: u32,
97 ) -> Result<Vec<(K, V)>, E> {
98 let entries = self
99 .actor_index_drain(object_handle, collection_index, limit)?
100 .into_iter()
101 .map(|(key, value)| {
102 let key: K = scrypto_decode(&key).unwrap();
103 let value: V = scrypto_decode(&value).unwrap();
104 (key, value)
105 })
106 .collect();
107
108 Ok(entries)
109 }
110}