interstice_sdk_core/host_calls/
core.rs1use interstice_abi::{
2 CallQueryRequest, CallQueryResponse, CallReducerRequest, CallReducerResponse,
3 ClearTableRequest, ClearTableResponse, DeleteRowRequest, DeleteRowResponse,
4 DeterministicRandomRequest, DeterministicRandomResponse, HostCall, IndexKey, IndexQuery,
5 InsertRowRequest, InsertRowResponse, IntersticeValue, LogRequest, ModuleSelection,
6 NodeSelection, Row, ScheduleRequest, ScheduleResponse, TableGetByPrimaryKeyRequest,
7 TableGetByPrimaryKeyResponse, TableIndexScanRequest, TableIndexScanResponse, TableScanRequest,
8 TableScanResponse, TimeRequest, TimeResponse, UpdateRowRequest, UpdateRowResponse,
9};
10
11pub type NodeId = String;
12
13pub fn log(message: &str) {
14 let call = HostCall::Log(LogRequest {
15 message: message.to_string(),
16 });
17 host_call(call);
18}
19
20pub fn current_node_id() -> NodeId {
21 let call = HostCall::CurrentNodeId;
22 let pack = host_call(call);
23 let response: NodeId = unpack(pack);
24 return response;
25}
26
27pub fn call_reducer(
28 node_selection: NodeSelection,
29 module_selection: ModuleSelection,
30 reducer_name: String,
31 input: IntersticeValue,
32) -> Result<(), String> {
33 let call = HostCall::CallReducer(CallReducerRequest {
34 node_selection,
35 module_selection,
36 reducer_name,
37 input,
38 });
39
40 let pack = host_call(call);
41 let response: CallReducerResponse = unpack(pack);
42 match response {
43 CallReducerResponse::Ok => Ok(()),
44 CallReducerResponse::Err(err) => Err(err),
45 }
46}
47
48pub fn schedule(reducer_name: String, delay_ms: u64) -> Result<(), String> {
49 let call = HostCall::Schedule(ScheduleRequest {
50 reducer_name,
51 delay_ms,
52 });
53
54 let pack = host_call(call);
55 let response: ScheduleResponse = unpack(pack);
56 match response {
57 ScheduleResponse::Ok => Ok(()),
58 ScheduleResponse::Err(err) => Err(err),
59 }
60}
61
62pub fn call_query(
63 node_selection: NodeSelection,
64 module_selection: ModuleSelection,
65 query_name: String,
66 input: IntersticeValue,
67) -> Result<IntersticeValue, String> {
68 let call = HostCall::CallQuery(CallQueryRequest {
69 node_selection,
70 module_selection,
71 query_name,
72 input,
73 });
74
75 let pack = host_call(call);
76 let response: CallQueryResponse = unpack(pack);
77 match response {
78 CallQueryResponse::Ok(value) => Ok(value),
79 CallQueryResponse::Err(err) => Err(err),
80 }
81}
82
83pub fn deterministic_random_u64() -> Result<u64, String> {
84 let call = HostCall::DeterministicRandom(DeterministicRandomRequest {});
85 let pack = host_call(call);
86 let response: DeterministicRandomResponse = unpack(pack);
87 match response {
88 DeterministicRandomResponse::Ok(value) => Ok(value),
89 DeterministicRandomResponse::Err(err) => Err(err),
90 }
91}
92
93pub fn time_now_ms() -> Result<u64, String> {
94 let call = HostCall::Time(TimeRequest {});
95 let pack = host_call(call);
96 let response: TimeResponse = unpack(pack);
97 match response {
98 TimeResponse::Ok { unix_ms } => Ok(unix_ms),
99 TimeResponse::Err(err) => Err(err),
100 }
101}
102
103pub fn insert_row(
104 table_name: String,
105 row: Row,
106) -> Result<Row, String> {
107 let call = HostCall::InsertRow(InsertRowRequest {
108 table_name,
109 row,
110 });
111
112 let pack = host_call(call);
113 let response: InsertRowResponse = unpack(pack);
114 match response {
115 InsertRowResponse::Ok(row) => Ok(row),
116 InsertRowResponse::Err(err) => Err(err),
117 }
118}
119
120pub fn update_row(
121 table_name: String,
122 row: Row,
123) -> Result<(), String> {
124 let call = HostCall::UpdateRow(UpdateRowRequest {
125 table_name,
126 row,
127 });
128
129 let pack = host_call(call);
130 let response: UpdateRowResponse = unpack(pack);
131 match response {
132 UpdateRowResponse::Ok => Ok(()),
133 UpdateRowResponse::Err(err) => Err(err),
134 }
135}
136
137pub fn delete_row(
138 table_name: String,
139 primary_key: IndexKey,
140) -> Result<(), String> {
141 let call = HostCall::DeleteRow(DeleteRowRequest {
142 table_name,
143 primary_key,
144 });
145
146 let pack = host_call(call);
147 let response: DeleteRowResponse = unpack(pack);
148 match response {
149 DeleteRowResponse::Ok => Ok(()),
150 DeleteRowResponse::Err(err) => Err(err),
151 }
152}
153
154pub fn clear_table(module_selection: ModuleSelection, table_name: String) -> Result<(), String> {
155 let call = HostCall::ClearTable(ClearTableRequest {
156 module_selection,
157 table_name,
158 });
159
160 let pack = host_call(call);
161 let response: ClearTableResponse = unpack(pack);
162 match response {
163 ClearTableResponse::Ok => Ok(()),
164 ClearTableResponse::Err(err) => Err(err),
165 }
166}
167
168pub fn scan(module_selection: ModuleSelection, table_name: String) -> Result<Vec<Row>, String> {
169 let call = HostCall::TableScan(TableScanRequest {
170 module_selection,
171 table_name,
172 });
173
174 let pack = host_call(call);
175 let response: TableScanResponse = unpack(pack);
176 match response {
177 TableScanResponse::Ok { rows } => Ok(rows),
178 TableScanResponse::Err(err) => Err(err),
179 }
180}
181
182pub fn get_by_primary_key(
183 module_selection: ModuleSelection,
184 table_name: String,
185 primary_key: IndexKey,
186) -> Result<Option<Row>, String> {
187 let call = HostCall::TableGetByPrimaryKey(TableGetByPrimaryKeyRequest {
188 module_selection,
189 table_name,
190 primary_key,
191 });
192
193 let pack = host_call(call);
194 let response: TableGetByPrimaryKeyResponse = unpack(pack);
195 match response {
196 TableGetByPrimaryKeyResponse::Ok(row) => Ok(row),
197 TableGetByPrimaryKeyResponse::Err(err) => Err(err),
198 }
199}
200
201pub fn scan_index(
202 module_selection: ModuleSelection,
203 table_name: String,
204 field_name: String,
205 query: IndexQuery,
206) -> Result<Vec<Row>, String> {
207 let call = HostCall::TableIndexScan(TableIndexScanRequest {
208 module_selection,
209 table_name,
210 field_name,
211 query,
212 });
213
214 let pack = host_call(call);
215 let response: TableIndexScanResponse = unpack(pack);
216 match response {
217 TableIndexScanResponse::Ok { rows } => Ok(rows),
218 TableIndexScanResponse::Err(err) => Err(err),
219 }
220}
221
222use interstice_abi::{QueryContext, ReducerContext};
223
224use crate::host_calls::{host_call, unpack};
225
226pub trait HostLog {
227 fn log(&self, message: &str);
228}
229
230pub trait HostCurrentNodeId {
231 fn current_node_id(&self) -> NodeId;
232}
233
234pub trait HostTime {
235 fn time_now_ms(&self) -> Result<u64, String>;
236}
237
238pub trait HostDeterministicRandom {
239 fn deterministic_random_u64(&self) -> Result<u64, String>;
240}
241
242pub trait HostSchedule {
243 fn schedule(&self, reducer_name: &str, delay_ms: u64) -> Result<(), String>;
244}
245
246impl HostLog for ReducerContext {
247 fn log(&self, message: &str) {
248 log(message);
249 }
250}
251
252impl HostLog for QueryContext {
253 fn log(&self, message: &str) {
254 log(message);
255 }
256}
257
258impl HostCurrentNodeId for ReducerContext {
259 fn current_node_id(&self) -> NodeId {
260 current_node_id()
261 }
262}
263
264impl HostCurrentNodeId for QueryContext {
265 fn current_node_id(&self) -> NodeId {
266 current_node_id()
267 }
268}
269
270impl HostTime for ReducerContext {
271 fn time_now_ms(&self) -> Result<u64, String> {
272 time_now_ms()
273 }
274}
275
276impl HostTime for QueryContext {
277 fn time_now_ms(&self) -> Result<u64, String> {
278 time_now_ms()
279 }
280}
281
282impl HostDeterministicRandom for ReducerContext {
283 fn deterministic_random_u64(&self) -> Result<u64, String> {
284 deterministic_random_u64()
285 }
286}
287
288impl HostDeterministicRandom for QueryContext {
289 fn deterministic_random_u64(&self) -> Result<u64, String> {
290 deterministic_random_u64()
291 }
292}
293
294impl HostSchedule for ReducerContext {
295 fn schedule(&self, reducer_name: &str, delay_ms: u64) -> Result<(), String> {
296 schedule(reducer_name.to_string(), delay_ms)
297 }
298}