storage-kit 0.0.1

Portable database and transactional state-store Kit.
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
use storage_kit::{
    StateStoreBackend, StateTransactionBackend,
    state::record::{StateKey, StateKeyPrefix, StateListRequest, StateRecord},
};

// Private cases shared by real embedded and PostgreSQL tests in this crate.
// This owner does not emulate storage or expose a production conformance API.
pub(super) struct StateCases<S: StateStoreBackend> {
    store: S,
    namespace: String,
}

impl<S: StateStoreBackend> StateCases<S> {
    pub(super) fn new(store: S) -> Self {
        Self {
            store,
            namespace: format!("portable-{}", uuid::Uuid::new_v4()),
        }
    }

    pub(super) fn key(&self, fragment: &str) -> StateKey {
        StateKey::new(&self.namespace, fragment).expect("key")
    }

    pub(super) async fn verify(&self) {
        self.revisions("revision", [1, 2, 3, 4, 5].map(serde_json::Value::from))
            .await;
        let padding = "x".repeat(2048);
        self.revisions(
            "revision-large",
            [1, 2, 3, 4, 5].map(|n| serde_json::json!({"n": n, "padding": &padding})),
        )
        .await;
        self.atomic_publication().await;
        self.pagination().await;
    }

    async fn revisions(&self, fragment: &str, values: [serde_json::Value; 5]) {
        let key = self.key(fragment);
        let mut transaction = self.store.begin_transaction().await.expect("transaction");
        assert!(
            transaction
                .load(key.clone())
                .await
                .expect("missing")
                .is_none()
        );
        transaction
            .delete(key.clone())
            .await
            .expect("delete absent at revision one");
        transaction
            .put(StateRecord::new(key.clone(), values[0].clone()))
            .await
            .expect("put");
        assert_eq!(
            transaction
                .load(key.clone())
                .await
                .expect("load")
                .expect("value")
                .revision
                .get(),
            2
        );
        transaction
            .put(StateRecord::new(key.clone(), values[1].clone()))
            .await
            .expect("second put");
        let before_delete = transaction
            .load(key.clone())
            .await
            .expect("load")
            .expect("value");
        assert_eq!(before_delete.revision.get(), 3);
        assert_eq!(before_delete.record.value.as_ref(), &values[1]);
        transaction
            .delete(key.clone())
            .await
            .expect("delete at revision four");
        assert!(
            transaction
                .load(key.clone())
                .await
                .expect("tombstone")
                .is_none()
        );
        transaction.commit().await.expect("commit tombstone");
        assert!(
            self.store
                .load(key.clone())
                .await
                .expect("deleted")
                .is_none()
        );
        let mut recreate = self.store.begin_transaction().await.expect("recreate");
        recreate
            .put(StateRecord::new(key.clone(), values[2].clone()))
            .await
            .expect("recreate");
        assert_eq!(
            recreate
                .load(key.clone())
                .await
                .expect("load")
                .expect("value")
                .revision
                .get(),
            5
        );
        recreate.commit().await.expect("commit recreate");
        let committed = self.store.load(key.clone()).await.expect("load");
        let retained_committed = committed.clone().expect("committed recreation");
        assert_eq!(retained_committed.revision.get(), 5);
        assert_eq!(retained_committed.record.value.as_ref(), &values[2]);
        assert_eq!(before_delete.revision.get(), 3);
        assert_eq!(before_delete.record.value.as_ref(), &values[1]);
        let mut rollback = self.store.begin_transaction().await.expect("rollback");
        rollback
            .put(StateRecord::new(key.clone(), values[3].clone()))
            .await
            .expect("staged put");
        let retained_staged = rollback
            .load(key.clone())
            .await
            .expect("read staged replacement")
            .expect("staged replacement");
        assert_eq!(retained_staged.revision.get(), 6);
        assert_eq!(retained_staged.record.value.as_ref(), &values[3]);
        rollback.rollback().await.expect("rollback");
        assert_eq!(self.store.load(key.clone()).await.expect("load"), committed);
        assert_eq!(retained_committed.record.value.as_ref(), &values[2]);
        assert_eq!(retained_staged.record.value.as_ref(), &values[3]);
        let mut dropped = self.store.begin_transaction().await.expect("dropped");
        dropped.delete(key.clone()).await.expect("dropped delete");
        drop(dropped);
        // A subsequent write also proves actor/native-writer progress after drop.
        let mut next = self.store.begin_transaction().await.expect("next");
        next.put(StateRecord::new(key.clone(), values[4].clone()))
            .await
            .expect("next put");
        next.commit().await.expect("next commit");
        let next_committed = self.store.load(key).await.expect("load").expect("value");
        assert_eq!(next_committed.revision.get(), 6);
        assert_eq!(next_committed.record.value.as_ref(), &values[4]);
        // A rolled-back staged revision may recur with different committed content.
        assert_eq!(retained_staged.revision, next_committed.revision);
        assert_eq!(retained_staged.record.value.as_ref(), &values[3]);
        assert_eq!(retained_committed.revision.get(), 5);
        assert_eq!(retained_committed.record.value.as_ref(), &values[2]);
        assert_eq!(before_delete.revision.get(), 3);
        assert_eq!(before_delete.record.value.as_ref(), &values[1]);
    }

    async fn atomic_publication(&self) {
        let keys = [
            self.key("history/0001"),
            self.key("head"),
            self.key("receipt"),
        ];
        let mut transaction = self.store.begin_transaction().await.expect("transaction");
        for key in &keys {
            assert!(
                transaction
                    .load(key.clone())
                    .await
                    .expect("absence fence")
                    .is_none()
            );
            transaction
                .put(StateRecord::new(
                    key.clone(),
                    serde_json::json!({"sequence": 1}),
                ))
                .await
                .expect("staged publication");
            assert!(
                self.store
                    .load(key.clone())
                    .await
                    .expect("uncommitted read")
                    .is_none()
            );
        }
        transaction.commit().await.expect("atomic publication");
        for key in &keys {
            let record = self
                .store
                .load(key.clone())
                .await
                .expect("load")
                .expect("committed");
            assert_eq!(record.revision.get(), 1);
            assert_eq!(
                record.record.value.as_ref(),
                &serde_json::json!({"sequence": 1})
            );
        }
        let mut rollback = self.store.begin_transaction().await.expect("rollback");
        for key in &keys {
            rollback.delete(key.clone()).await.expect("delete");
        }
        rollback.rollback().await.expect("rollback batch");
        for key in &keys {
            assert!(self.store.load(key.clone()).await.expect("load").is_some());
        }
    }

    pub(super) async fn pagination(&self) {
        let expected = [
            "item/A",
            "item/Z",
            "item/a",
            "item/a%_\\1",
            "item/a%_\\2",
            "item/é",
            "item/中",
        ];
        let tombstones = ["item/0", "item/a%_\\1x", "item/tombstone", "item/中tail"];
        let mut transaction = self.store.begin_transaction().await.expect("seed");
        for fragment in expected
            .iter()
            .rev()
            .chain(["ITEM/false", "item/aXX1"].iter())
            .chain(tombstones.iter())
        {
            transaction
                .put(StateRecord::new(
                    self.key(fragment),
                    serde_json::json!(fragment),
                ))
                .await
                .expect("seed page record");
        }
        for fragment in tombstones {
            transaction
                .delete(self.key(fragment))
                .await
                .expect("delete tombstone");
        }
        transaction
            .put(StateRecord::new(
                StateKey::new(format!("{}-neighbor", self.namespace), "item/A").expect("neighbor"),
                serde_json::json!("other namespace"),
            ))
            .await
            .expect("neighbor namespace");
        transaction.commit().await.expect("seed commit");
        let request = StateListRequest::new(&self.namespace)
            .expect("namespace")
            .with_key_prefix(StateKeyPrefix::new("item/").expect("prefix"))
            .with_limit(2)
            .expect("page limit");
        let mut sorted = expected.to_vec();
        sorted.push("item/aXX1");
        sorted.sort_unstable();
        let mut next = request.clone();
        for (index, expected_page) in sorted.chunks(2).enumerate() {
            let page = self.store.list(next).await.expect("ordered page");
            assert_eq!(
                page.records
                    .iter()
                    .map(|snapshot| snapshot.record.key.key())
                    .collect::<Vec<_>>(),
                expected_page,
                "each nonempty page advances in canonical UTF-8 order"
            );
            for snapshot in &page.records {
                assert_eq!(snapshot.record.key.namespace().as_str(), self.namespace);
                assert_eq!(snapshot.revision.get(), 1);
            }
            let expected_cursor = if (index + 1) * 2 < sorted.len() {
                expected_page.last().copied()
            } else {
                None
            };
            assert_eq!(
                page.next_start_after.as_deref(),
                expected_cursor,
                "only pages with more records carry their final key as a cursor"
            );
            next = match page.next_start_after {
                Some(cursor) => request
                    .clone()
                    .start_after(cursor)
                    .expect("exclusive cursor"),
                None => request.clone(),
            };
        }
        let literal = self
            .store
            .list(
                StateListRequest::new(&self.namespace)
                    .expect("namespace")
                    .with_key_prefix(StateKeyPrefix::new("item/a%_\\").expect("literal prefix")),
            )
            .await
            .expect("literal wildcard page");
        assert_eq!(
            literal
                .records
                .iter()
                .map(|record| record.record.key.key())
                .collect::<Vec<_>>(),
            ["item/a%_\\1", "item/a%_\\2"]
        );
        assert!(literal.next_start_after.is_none());
        for (prefix, cursor, limit, expected_keys, expected_cursor) in [
            (
                Some("item/"),
                "ITEM/false",
                2,
                ["item/A", "item/Z"].as_slice(),
                Some("item/Z"),
            ),
            (
                Some("item/"),
                "item/",
                2,
                ["item/A", "item/Z"].as_slice(),
                Some("item/Z"),
            ),
            (Some("item/A"), "item/A", 1, [].as_slice(), None),
            (
                Some("item/"),
                "item/B",
                2,
                ["item/Z", "item/a"].as_slice(),
                Some("item/a"),
            ),
            (
                Some("item/a%_\\"),
                "item/a",
                1,
                ["item/a%_\\1"].as_slice(),
                Some("item/a%_\\1"),
            ),
            (
                Some("item/a%_\\"),
                "item/a%_\\1",
                1,
                ["item/a%_\\2"].as_slice(),
                None,
            ),
            (
                Some("item/a%_\\"),
                "item/a%_\\1x",
                StateListRequest::MAX_LIMIT,
                ["item/a%_\\2"].as_slice(),
                None,
            ),
            (
                Some("item/"),
                "item/tombstone",
                2,
                ["item/é", "item/中"].as_slice(),
                None,
            ),
            (Some("item/"), "item0", 2, [].as_slice(), None),
            (Some("item/"), "zzz", 2, [].as_slice(), None),
            (Some("item/é"), "item/ê", 2, [].as_slice(), None),
            (None, "item/Z", 1, ["item/a"].as_slice(), Some("item/a")),
        ] {
            let mut request = StateListRequest::new(&self.namespace)
                .expect("namespace")
                .with_limit(limit)
                .expect("limit")
                .start_after(cursor)
                .expect("exclusive cursor");
            if let Some(prefix) = prefix {
                request = request.with_key_prefix(StateKeyPrefix::new(prefix).expect("prefix"));
            }
            let page = self
                .store
                .list(request)
                .await
                .expect("cursor and prefix intersection");
            assert_eq!(
                page.records
                    .iter()
                    .map(|row| row.record.key.key())
                    .collect::<Vec<_>>(),
                expected_keys,
                "prefix {prefix:?}, cursor {cursor:?}, limit {limit}"
            );
            assert_eq!(page.next_start_after.as_deref(), expected_cursor);
            for row in &page.records {
                assert_eq!(row.record.key.namespace().as_str(), self.namespace);
                assert_eq!(row.revision.get(), 1);
                assert_eq!(
                    row.record.value.as_ref(),
                    &serde_json::json!(row.record.key.key())
                );
            }
        }
        let empty = self
            .store
            .list(request.start_after("item/中").expect("end cursor"))
            .await
            .expect("empty tail");
        assert!(empty.records.is_empty());
        assert!(empty.next_start_after.is_none());
    }
}