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
//! The [`KvStore`] trait: an async byte-to-byte map scoped by directory.
use Error;
use Future;
use Bound;
use crateRetryableStorageError;
use crate;
use crateKvStream;
/// Async ordered key-value store, scoped by directory.
///
/// This trait abstracts over the in-memory BTreeMap, redb, FoundationDB, and
/// browser IndexedDB backends. Store types and method futures use [`MaybeSend`]
/// (and store types also use [`MaybeSync`]): these mean `Send`/`Sync` on native
/// targets but impose no thread-safety requirement in a browser, where storage
/// handles may hold `JsValue`s. The browser exception deliberately targets only
/// `wasm32-unknown-unknown`, rather than `target_family = "wasm"`, because targets
/// such as `wasm32-wasip1-threads` have real threads. Every operation is async so
/// that network-backed and transactional engines fit without a blocking shim.
///
/// # Directories and keys
///
/// Every operation names a **directory** and a **key**. A directory is a
/// sequence of segments (`&[&[u8]]`; the empty slice is the root directory);
/// a key is an arbitrary byte sequence. The pair is the store's real key:
/// the same key bytes in different directories are distinct entries, and a
/// scan of one directory never yields another's keys.
///
/// **Directory segments are UTF-8** (the trait-level contract): every
/// operation validates its directory with [`validate_dir`](crate::validate_dir) and rejects a
/// non-UTF-8 segment with [`KeyError::DirSegmentNotUtf8`](crate::KeyError)
/// before touching the engine. Keys have no such restriction.
///
/// Directories are created **lazily by the first write**: a `set` into a
/// never-written directory allocates it (and any missing ancestor). Reads on
/// a missing directory are empty, not an error — `get` answers `None`,
/// `range` an empty stream. Existence is a query, [`KvDirNav::dir_exists`](crate::nav::KvDirNav::dir_exists)
/// (crate root) — a directory with zero keys still exists.
///
/// # Ordering
///
/// Keys are **arbitrary byte sequences** ordered **lexicographically by byte
/// value**, exactly as [`Ord for [u8]`](slice) defines it, within their
/// directory. Shorter keys sort before longer keys that extend them, so
/// `[1] < [1, 0] < [1, 0, 0] < [2]`. This is what makes a resolved
/// directory prefix a usable namespacing scheme and is not negotiable per
/// backend: a store whose engine orders differently must translate.
///
/// The empty key is a legal key and sorts first.
///
/// # Range scans
///
/// [`range`](KvStore::range) scans **one exact directory** between two key
/// bounds and returns a continuation-based [`KvStream`] of
/// [`KvPair`](crate::KvPair) whose `key` is the within-directory key — the
/// directory is implicit in the scan. This enables backends to fetch
/// incrementally, but does not require it: a backend may prefetch pages or
/// materialize a snapshot. [`KvStream::take`] limits continuation polling,
/// not necessarily backend fetches.
///
/// Bounds are over the within-directory key. A range whose keyed `start`
/// sorts after its keyed `end` is empty. Equal keyed bounds are empty when
/// either bound is excluded; two included equal bounds select that one key.
///
/// Scans across directories (subtree scans, multi-directory scans) are not
/// expressible in one call; the directory dimension is exact-match only.
///
/// # Atomicity
///
/// Transactional backends **buffer writes until commit**. Reads issued through
/// a handle must observe that handle's own buffered writes (read-your-writes),
/// but nothing becomes visible to other handles, and nothing is durable, until
/// the atomic commit is applied. `Ok(())` confirms that outcome; a
/// non-retryable commit error may leave it unknown.
///
/// This matters because the state machines built on top perform multi-key
/// updates — a swap row plus its registry summary, an index insertion plus the
/// row it points at — whose exactly-once guarantees depend on all-or-nothing
/// application. A partially applied session can double-credit, duplicate a
/// withdrawal, or strand a reservation forever.
/// The in-memory backend (the `fasm-storage-btreemap` crate) provides the same
/// buffering, read-your-writes, and drop-rollback contract with no durability:
/// a crash loses the base map, so it is test/simulation-only — the
/// transaction contract itself does not differ.
///
/// # Errors
///
/// The associated error type must be a real [`Error`] and must answer
/// [`RetryableStorageError::is_retryable`], because the layer above needs to
/// distinguish "the transaction conflicted, rerun the transition" from "this
/// data is corrupt, fail closed".
///
/// # Why `Sync`
///
/// Every method future is explicitly [`MaybeSend`]: it is `Send` on native
/// targets, so a generic caller can move (for example) `store.get(..)` across a
/// task boundary when `S: KvStore + Sync`. In a browser the marker does not
/// require `Send`, allowing both the store and its futures to remain
/// thread-local. [`MaybeSync`] supplies the native `Sync` requirement needed by
/// futures that capture `&Self`.