myrmic-sdk 0.7.0

SDK for writing Myrmic cells (WASM modules deployed and managed by the sorg infrastructure): host function bindings for db, tap, outlet, gpio, ble, gateway, cell messaging, logging and time, plus the #[cmd]/#[monitor] export macros.
Documentation
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Typed access to a `tb` table, plus lazy iterators over its rows and keys.

use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;

use core::borrow::Borrow;
use core::marker::PhantomData;

use crate::{Codec, Postcard, Result, Sri};

use crate::db::{Cursor, Scope, TbOrderBy, tb_append, tb_count, tb_delete, tb_get, tb_list};

/// Encode a value into the raw entity-id bytes used to key a table.
pub trait AsEid {
    /// The raw entity-id bytes for this value.
    fn as_eid(&self) -> Vec<u8>;
}

/// A key type that can be reconstructed from the raw entity-id bytes of a
/// stored row, as well as encoded into them.
pub trait TableKey: AsEid + Sized {
    /// Reconstruct a key from its raw entity-id bytes, as stored in the table.
    fn from_eid(eid: &[u8]) -> Self;
}

impl AsEid for str {
    fn as_eid(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }
}

impl AsEid for String {
    fn as_eid(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }
}

impl TableKey for String {
    fn from_eid(eid: &[u8]) -> Self {
        String::from_utf8_lossy(eid).into_owned()
    }
}

impl AsEid for [u8] {
    fn as_eid(&self) -> Vec<u8> {
        self.to_vec()
    }
}

impl AsEid for Vec<u8> {
    fn as_eid(&self) -> Vec<u8> {
        self.clone()
    }
}

impl TableKey for Vec<u8> {
    fn from_eid(eid: &[u8]) -> Self {
        eid.to_vec()
    }
}

impl AsEid for Sri {
    fn as_eid(&self) -> Vec<u8> {
        self.to_bytes().to_vec()
    }
}

impl TableKey for Sri {
    fn from_eid(eid: &[u8]) -> Self {
        let mut bytes = [0u8; 16];
        let n = eid.len().min(16);
        bytes[..n].copy_from_slice(&eid[..n]);
        Sri::from_bytes(bytes)
    }
}

/// A typed handle to a `tb` table, storing serde values `V` keyed by `K`.
///
/// Construct one with [`Table::new`] (or [`Table::new_in`] for a non-default
/// [`Scope`]) and operate on it via [`get`](Self::get),
/// [`insert`](Self::insert), [`iter`](Self::iter), etc.
pub struct Table<V, K = String, C = Postcard> {
    scope: Scope,
    name: &'static str,
    _marker: PhantomData<(V, K, C)>,
}

impl<V, K, C> Table<V, K, C> {
    /// A handle to the table `name` in the default private [`Scope`].
    pub const fn new(name: &'static str) -> Self {
        Self::new_in(name, Scope::private())
    }

    /// A handle to the table `name` in an explicit `scope`.
    pub const fn new_in(name: &'static str, scope: Scope) -> Self {
        Self {
            scope,
            name,
            _marker: PhantomData,
        }
    }

    fn scope(&self) -> Scope {
        self.scope.clone()
    }
}

impl<V, K, C> Table<V, K, C>
where
    V: serde::Serialize + serde::de::DeserializeOwned,
    C: Codec,
{
    /// Fetches the value stored under `key`, or `None` if there is no such row.
    pub fn get<Q>(&self, key: &Q) -> Result<Option<V>>
    where
        K: Borrow<Q>,
        Q: AsEid + ?Sized,
    {
        let mut req = [0u8; 256];
        let mut resp = alloc::vec![0u8; 4096];
        tb_get(
            self.scope(),
            String::from(self.name),
            key.as_eid(),
            &mut req,
            &mut resp,
        )?
        .map(|b| C::decode(&b))
        .transpose()
    }

    /// Inserts `val` under a host-generated entity id.
    pub fn insert(&self, val: &V) -> Result<()> {
        self.insert_impl(None, C::encode(val)?)
    }

    /// Inserts `val` under `key`, replacing any existing value.
    pub fn insert_with<Q>(&self, key: &Q, val: &V) -> Result<()>
    where
        K: Borrow<Q>,
        Q: AsEid + ?Sized,
    {
        self.insert_impl(Some(key.as_eid()), C::encode(val)?)
    }

    /// Neither entry point reports the row id back, so both append: the host
    /// batches the write into the handler's transaction instead of spending a
    /// round trip to tell us an id we would drop.
    fn insert_impl(&self, key: Option<Vec<u8>>, val: Vec<u8>) -> Result<()> {
        let mut req = alloc::vec![0u8; val.len() + 1024];
        tb_append(self.scope(), String::from(self.name), key, val, &mut req)
            .map_err(|_| "Table::insert")?;
        Ok(())
    }

    /// Deletes the row keyed `key`, if any.
    pub fn delete<Q>(&self, key: &Q) -> Result<()>
    where
        K: Borrow<Q>,
        Q: AsEid + ?Sized,
    {
        let mut buf = [0u8; 256];
        tb_delete(
            self.scope(),
            String::from(self.name),
            key.as_eid(),
            &mut buf,
        )
        .map_err(|_| "Table::delete")?;
        Ok(())
    }

    /// The number of rows in the table.
    pub fn count(&self) -> Result<usize> {
        let mut req = [0u8; 256];
        let mut resp = [0u8; 64];
        tb_count(self.scope(), String::from(self.name), &mut req, &mut resp)
            .map_err(|_| "Table::count")
    }

    /// All values, collected in ascending key order.
    pub fn list(&self) -> Result<Vec<V>> {
        let mut out = Vec::new();
        self.for_each(|v| out.push(v))?;
        Ok(out)
    }

    /// Like [`Self::list`], but returns values in descending key order.
    pub fn list_rev(&self) -> Result<Vec<V>> {
        let mut out = Vec::new();
        self.for_each_rev(|v| out.push(v))?;
        Ok(out)
    }

    /// Visit every value, in ascending key order.
    pub fn for_each<F: FnMut(V)>(&self, mut f: F) -> Result<()> {
        let scope = self.scope();
        for_each_ordered::<_, V, C>(&scope, self.name, TbOrderBy::KeyAsc, |_eid, val: V| f(val))
    }

    /// Visit every value, in descending key order.
    pub fn for_each_rev<F: FnMut(V)>(&self, mut f: F) -> Result<()> {
        let scope = self.scope();
        for_each_ordered::<_, V, C>(&scope, self.name, TbOrderBy::KeyDesc, |_eid, val: V| f(val))
    }

    /// Internal iterating through undecoded `(eid, value)` byte rows.
    fn iter_raw(&self, order: TbOrderBy) -> IterRaw {
        IterRaw::new(self.scope(), String::from(self.name), order)
    }

    /// Lazily stream `(key, value)` pairs in ascending key order.
    pub fn iter(&self) -> Iter<V, K, C> {
        Iter {
            raw: self.iter_raw(TbOrderBy::KeyAsc),
            _marker: PhantomData,
        }
    }

    /// Like [`Self::iter`], but streams `(key, value)` pairs in descending key order.
    pub fn iter_rev(&self) -> Iter<V, K, C> {
        Iter {
            raw: self.iter_raw(TbOrderBy::KeyDesc),
            _marker: PhantomData,
        }
    }

    /// Lazily stream the table's keys in ascending order.
    pub fn keys(&self) -> Keys<K> {
        Keys {
            raw: self.iter_raw(TbOrderBy::KeyAsc),
            _marker: PhantomData,
        }
    }

    /// Like [`Self::keys`], but streams the table's keys in descending order.
    pub fn keys_rev(&self) -> Keys<K> {
        Keys {
            raw: self.iter_raw(TbOrderBy::KeyDesc),
            _marker: PhantomData,
        }
    }

    /// All keys in the table, collected.
    pub fn ids(&self) -> Result<Vec<K>>
    where
        K: TableKey,
    {
        self.keys().collect()
    }

    /// Materialise the whole table into a `key -> value` map by collecting
    /// [`Self::iter`].
    /// Prefer the lazy [`Self::iter`]/[`Self::for_each`] for large tables.
    pub fn to_map(&self) -> Result<BTreeMap<K, V>>
    where
        K: TableKey + Ord,
    {
        self.iter().collect()
    }
}

/// Internal iterator that doesn't decode anything,
/// _just_ deals with getting rows lazily out of the table.
struct IterRaw {
    scope: Scope,
    table: String,
    cursor: Option<Cursor>,
    order: TbOrderBy,
    done: bool,
}

impl IterRaw {
    fn new(scope: Scope, table: String, order: TbOrderBy) -> Self {
        Self {
            scope,
            table,
            cursor: None,
            order,
            done: false,
        }
    }
}

impl Iterator for IterRaw {
    type Item = Result<(Vec<u8>, Vec<u8>)>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }
        let mut req = [0u8; 256];
        let mut resp = alloc::vec![0u8; 4096];
        let row = tb_list(
            self.scope.clone(),
            self.table.clone(),
            self.cursor.take(),
            Some(1),
            Some(self.order),
            &mut req,
            &mut resp,
        );

        let rows = match row {
            Ok(rows) => rows,
            Err(_) => {
                self.done = true;
                return Some(Err("tb_list"));
            }
        };
        let Some((eid, val)) = rows.into_iter().next() else {
            self.done = true;
            return None;
        };
        self.cursor = Some(Cursor::After(eid.clone()));
        Some(Ok((eid, val)))
    }
}

/// Lazy iterator over a [`Table`] yielding decoded `(key, value)` pairs
pub struct Iter<V, K, C = Postcard> {
    raw: IterRaw,
    _marker: PhantomData<(V, K, C)>,
}

impl<V, K, C> Iterator for Iter<V, K, C>
where
    V: serde::de::DeserializeOwned,
    K: TableKey,
    C: Codec,
{
    type Item = Result<(K, V)>;

    fn next(&mut self) -> Option<Self::Item> {
        let (eid, val) = match self.raw.next()? {
            Ok(pair) => pair,
            Err(e) => return Some(Err(e)),
        };
        Some(C::decode::<V>(&val).map(|v| (K::from_eid(&eid), v)))
    }
}

/// Lazy iterator over a [`Table`]'s keys
pub struct Keys<K = String> {
    raw: IterRaw,
    _marker: PhantomData<K>,
}

impl<K> Iterator for Keys<K>
where
    K: TableKey,
{
    type Item = Result<K>;

    fn next(&mut self) -> Option<Self::Item> {
        match self.raw.next()? {
            Ok((eid, _val)) => Some(Ok(K::from_eid(&eid))),
            Err(e) => Some(Err(e)),
        }
    }
}

/// Visits every row of `table` in `scope` in ascending key order, passing the
/// raw entity id and the [`Postcard`]-decoded value; rows that fail to decode
/// are skipped.
pub fn for_each<F, T>(scope: &Scope, table: &str, f: F) -> Result<()>
where
    F: for<'a> FnMut(&'a [u8], T),
    T: serde::de::DeserializeOwned,
{
    for_each_ordered::<_, T, Postcard>(scope, table, TbOrderBy::KeyAsc, f)
}

fn for_each_ordered<F, T, C>(scope: &Scope, table: &str, order: TbOrderBy, mut f: F) -> Result<()>
where
    F: for<'a> FnMut(&'a [u8], T),
    T: serde::de::DeserializeOwned,
    C: Codec,
{
    for_each_raw_ordered(scope, table, order, |eid, val| {
        if let Ok(s) = C::decode::<T>(&val) {
            f(eid, s);
        }
    })
}

/// Visits every `(eid, value)` row of `table` in `scope` in ascending key
/// order, without decoding the values.
pub fn for_each_raw<F>(scope: &Scope, table: &str, f: F) -> Result<()>
where
    F: for<'a> FnMut(&'a [u8], Vec<u8>),
{
    for_each_raw_ordered(scope, table, TbOrderBy::KeyAsc, f)
}

fn for_each_raw_ordered<F>(scope: &Scope, table: &str, order: TbOrderBy, mut f: F) -> Result<()>
where
    F: for<'a> FnMut(&'a [u8], Vec<u8>),
{
    for row in IterRaw::new(scope.clone(), String::from(table), order) {
        let (eid, val) = row?;
        f(eid.as_slice(), val);
    }
    Ok(())
}