datumesh_store/lib.rs
1//! A PostgreSQL store for [Datumesh](https://datumesh.com): function descriptors, receipts,
2//! blobs, versions of cells over transaction time, lineage, namespaces and function names.
3//! [`PgStore`] implements [`Store`], and through it `datumesh_core`'s [`Resolver`], so
4//! `datumesh_core::verify` can recompute receipts of stored cells from it.
5//!
6//! The API is synchronous (the `postgres` driver, a small connection pool). Async callers run
7//! it on a blocking thread, e.g. `tokio::task::spawn_blocking`.
8//!
9//! [`PgStore::connect`] / [`PgStore::connect_with`] take a URL and [`PoolOptions`]; TLS
10//! (rustls) is used as the URL's `sslmode` says and always verifies the server's
11//! certificate and host name, against the Mozilla roots plus those added with
12//! [`PoolOptions::tls_root_certificates_pem`]. No type of the driver or of a TLS crate is
13//! part of the API, except in [`pg_unstable`] (any connection function, the driver
14//! itself), which is outside semver until 1.0.
15//!
16//! What is stored, and how:
17//!
18//! * Descriptors are checked by the core (`FunctionDescriptor::from_json`: since 0.3.1,
19//! spec-0.4.0's, with `signature.order` and argument `extent`) and stored in canonical
20//! form (JCS) under their hash. Receipts are verified (`datumesh_core::verify`, SPEC 10.1
21//! steps 1-6) against the key document stored for their signer host, so nothing
22//! unverified enters the store, and are stored canonical under their hash. Storing the
23//! same thing twice stores it once. A second, different valid signature over the same
24//! payload (possible only with a randomised signer: the payload names the key, so a
25//! rotated key gives a new hash) is refused ([`Error::Conflict`]) and the stored receipt
26//! kept.
27//! * Blobs are stored in the database, under the SHA-256 of their content.
28//! * Versions: from transaction time `tx` on, cell `cell_id` has the value of a receipt.
29//! Append-only and in tx order per cell, enforced by the database (triggers) as well as by
30//! [`Store::put_version`], which also checks that the receipt is stored and is of that
31//! cell at that tx.
32//! * Computed versions: from transaction time `tx` on, cell `cell_id` has the value a
33//! receipt computed, as of the receipt's asof. Derived rather than facts, so they are
34//! added in any order, keyed on (cell, tx, asof), insert-only
35//! ([`Store::put_computed`], [`Store::computed_asof`]).
36//! * Answering as of A: a receipt answers as of A only when its `tx` and its `asof` are
37//! both at or before A ([`Store::stored_asof`], and the [`Resolver`]'s `evaluate` of a
38//! stored function; [`Store::version_asof`] looks at `tx` only).
39//! * Lineage: `put_receipt` records an edge to each of the receipt's `inputs`, held or not:
40//! receipt hashes ([`Store::inputs`], [`Store::dependents`]) and, since 0.3.1, slice
41//! entries ([`Store::slice_entries`], [`Store::slice_dependents`]);
42//! [`Store::cell_dependents`] lifts both to cells.
43//! * Slices (0.3.1, SPEC 15.13): every stored receipt's cell is indexed by its typed
44//! arguments, so that [`Store::slice_members`] finds the members of a slice as of A with
45//! a range scan, in key order; [`Store::versions_between`] gives a version slice.
46//! [`Store::slice_values`] gives the members' values without receipts (0.3.2) and
47//! [`Store::slice_changed_after`] whether they may answer differently later.
48//! Manifests ([`Store::put_manifest`], served by [`Resolver::manifest`]) are kept by
49//! entry and found from each member ([`Store::member_manifests`]). A receipt citing a
50//! slice can be stored before its manifest, and a manifest before its members.
51//! * Namespaces: a host's key document (checked as SPEC 9.2 requires), with when it was
52//! validated. Store it before the host's receipts. Replacing it later (a key revoked or
53//! dropped) does not touch receipts already stored: they verified when they were stored,
54//! and putting one of them again is still idempotent.
55//! * Names: which function hash a fully qualified function name meant from a transaction
56//! time on. Append-only, like versions. [`Store::resolve_binding`] gives the binding
57//! answering as of A with when it began; [`Store::resolve_name`] its hash.
58//!
59//! Instants are compared as text (fixed width, `COLLATE "C"`), which is exact; each is also
60//! stored as `timestamptz`, which round-trips every instant of the specification exactly.
61//!
62//! Every [`Store`] method returns a `Result`: a malformed argument is [`Error::Invalid`] with
63//! the core's code, a database failure [`Error::Database`]. The [`Resolver`] methods keep the
64//! core's `Option` signatures; there a failure is logged with `tracing` (target
65//! `datumesh_store`) and answered `None`, which `verify` reports as unavailable.
66//!
67//! [`Store`] is sealed: only this crate implements it. The store is the trust boundary:
68//! everything in it was checked on the way in (every receipt verified against its host's
69//! key document), and its callers rely on that rather than checking again. A `dyn Store`
70//! is still a trait object as before.
71//!
72//! The schema comes from [`migrate::apply`] (the `datumesh-store migrate` command), through
73//! a store on the (possibly empty) database.
74
75#![forbid(unsafe_code)]
76#![deny(missing_docs)]
77
78use std::borrow::Cow;
79
80pub use datumesh_core::Resolver;
81use datumesh_core::{Cell, Scalar, Slice};
82
83mod error;
84pub mod migrate;
85mod pg;
86pub mod pg_unstable;
87mod pool;
88mod sql;
89mod tls;
90
91pub use error::{DatabaseError, Error, Result};
92pub use pg::PgStore;
93pub use pool::PoolOptions;
94
95/// A version of a cell: from transaction time `tx` on, the cell's value is that of the
96/// receipt `receipt_hash`, which states it as of `asof`.
97///
98/// A stored version ([`Store::put_version`]) or a computed one ([`Store::put_computed`]).
99#[non_exhaustive]
100#[derive(Clone, Debug, PartialEq, Eq, Hash)]
101pub struct Version {
102 /// The cell id.
103 pub cell_id: String,
104 /// The transaction time (an instant, SPEC 3.3).
105 pub tx: String,
106 /// The receipt stating the value.
107 pub receipt_hash: String,
108 /// The receipt's `asof` (an instant; never earlier than `tx`). Since 0.2.0.
109 pub asof: String,
110}
111
112/// A member of a slice ([`Store::slice_members`], [`Store::versions_between`]): a stored
113/// version and the bytes of its receipt (as [`receipt`](Resolver::receipt) returns them).
114/// Since 0.3.1.
115#[non_exhaustive]
116#[derive(Clone, Debug, PartialEq, Eq, Hash)]
117pub struct SliceMember {
118 /// The version answering (its cell, tx, receipt hash and asof).
119 pub version: Version,
120 /// The receipt's bytes, canonical JSON.
121 pub receipt: Vec<u8>,
122}
123
124/// A member of a slice with its value and without its receipt's bytes
125/// ([`Store::slice_values`]). Since 0.3.2.
126#[non_exhaustive]
127#[derive(Clone, Debug, PartialEq, Eq, Hash)]
128pub struct SliceValue {
129 /// The version answering (its cell, tx, receipt hash and asof).
130 pub version: Version,
131 /// The receipt's typed value.
132 pub value: Scalar,
133 /// The member's cell, with its arguments (from the store's typed-argument index; equal
134 /// to the receipt's `cell`, and `cell.id()` is `version.cell_id`).
135 pub cell: Cell,
136}
137
138/// A name binding: from transaction time `tx` on, a fully qualified function name means the
139/// function `fn_hash` ([`Store::bind_name`]). Since 0.3.0.
140#[non_exhaustive]
141#[derive(Clone, Debug, PartialEq, Eq, Hash)]
142pub struct Binding {
143 /// The function hash the name means.
144 pub fn_hash: String,
145 /// When the binding began: the transaction time from which the name means `fn_hash`
146 /// (an instant, SPEC 3.3).
147 pub tx: String,
148}
149
150/// A namespace's host and its key document.
151#[non_exhaustive]
152#[derive(Clone, Debug, PartialEq, Eq, Hash)]
153pub struct Namespace {
154 /// The host (a DNS name).
155 pub host: String,
156 /// The key document, canonical (JCS).
157 pub keydoc: Vec<u8>,
158 /// When the document was last retrieved and checked.
159 pub validated_at: String,
160 /// When the namespace was suspended, if it is. Nothing in this version sets it.
161 pub suspended_at: Option<String>,
162}
163
164/// A Datumesh store: a [`Resolver`] that can also be written to.
165///
166/// Instants are SPEC 3.3 instants (`YYYY-MM-DDTHH:MM:SS.ffffffZ`), hashes and cell ids 64
167/// lowercase hex digits, hosts DNS names and function names fully qualified. Every method,
168/// read or write, refuses anything else with the core's error code ([`Error::Invalid`]),
169/// and reports a database failure as [`Error::Database`] (or [`Error::PoolTimeout`]): a read
170/// answers `Ok(None)` / `Ok(vec![])` only when the store really holds nothing.
171///
172/// The [`Resolver`] methods keep the core's `Option` signatures: there a malformed argument
173/// or a database failure is `None` (logged with `tracing` when it is a failure), which
174/// `verify` reports as unavailable.
175///
176/// Only this crate implements `Store` (it is sealed, since 0.3.0): the store is the trust
177/// boundary, where every receipt is verified against its host's key document before it is
178/// kept, and what a store answers is relied on as checked. An implementation elsewhere
179/// could answer anything. `Store` stays object-safe (`Arc<dyn Store + Send + Sync>`). Even a complete implementation that
180/// delegates every method to a [`PgStore`] is refused (`E0277`: the private supertrait is
181/// not implemented; `tests/sealed.rs` checks that this is the only error):
182///
183/// ```compile_fail
184/// use std::borrow::Cow;
185/// use datumesh_core::{Cell, Scalar};
186/// use datumesh_core::Slice;
187/// use datumesh_store::{Binding, Namespace, PgStore, Resolver, Result, SliceMember, SliceValue, Store, Version};
188///
189/// struct Wrapper(PgStore);
190/// impl Resolver for Wrapper {
191/// fn descriptor(&self, h: &str) -> Option<Cow<'_, [u8]>> { self.0.descriptor(h) }
192/// fn receipt(&self, h: &str) -> Option<Cow<'_, [u8]>> { self.0.receipt(h) }
193/// fn evaluate(&self, c: &Cell, a: &str) -> Option<Scalar> { self.0.evaluate(c, a) }
194/// fn manifest(&self, e: &str) -> Option<Cow<'_, [u8]>> { self.0.manifest(e) }
195/// }
196/// impl Store for Wrapper {
197/// fn put_descriptor(&self, b: &[u8]) -> Result<String> { self.0.put_descriptor(b) }
198/// fn put_receipt(&self, b: &[u8]) -> Result<String> { self.0.put_receipt(b) }
199/// fn put_blob(&self, b: &[u8]) -> Result<String> { self.0.put_blob(b) }
200/// fn blob(&self, s: &str) -> Result<Option<Cow<'_, [u8]>>> { self.0.blob(s) }
201/// fn put_version(&self, c: &str, t: &str, r: &str) -> Result<()> { self.0.put_version(c, t, r) }
202/// fn version_asof(&self, c: &str, a: &str) -> Result<Option<Version>> { self.0.version_asof(c, a) }
203/// fn versions(&self, c: &str, f: &str, t: &str) -> Result<Vec<Version>> { self.0.versions(c, f, t) }
204/// fn latest(&self, c: &str) -> Result<Option<Version>> { self.0.latest(c) }
205/// fn dependents(&self, r: &str) -> Result<Vec<String>> { self.0.dependents(r) }
206/// fn inputs(&self, r: &str) -> Result<Vec<String>> { self.0.inputs(r) }
207/// fn put_namespace(&self, h: &str, k: &[u8], v: &str) -> Result<()> { self.0.put_namespace(h, k, v) }
208/// fn namespace(&self, h: &str) -> Result<Option<Namespace>> { self.0.namespace(h) }
209/// fn bind_name(&self, n: &str, f: &str, t: &str) -> Result<()> { self.0.bind_name(n, f, t) }
210/// fn resolve_name(&self, n: &str, a: &str) -> Result<Option<String>> { self.0.resolve_name(n, a) }
211/// fn resolve_binding(&self, n: &str, a: &str) -> Result<Option<Binding>> { self.0.resolve_binding(n, a) }
212/// fn put_computed(&self, c: &str, t: &str, r: &str) -> Result<()> { self.0.put_computed(c, t, r) }
213/// fn computed_asof(&self, c: &str, a: &str) -> Result<Option<Version>> { self.0.computed_asof(c, a) }
214/// fn stored_asof(&self, c: &str, a: &str) -> Result<Option<Version>> { self.0.stored_asof(c, a) }
215/// fn cell_dependents(&self, c: &str) -> Result<Vec<String>> { self.0.cell_dependents(c) }
216/// fn put_manifest(&self, b: &[u8]) -> Result<String> { self.0.put_manifest(b) }
217/// fn member_manifests(&self, r: &str) -> Result<Vec<String>> { self.0.member_manifests(r) }
218/// fn slice_entries(&self, r: &str) -> Result<Vec<String>> { self.0.slice_entries(r) }
219/// fn slice_dependents(&self, e: &str) -> Result<Vec<String>> { self.0.slice_dependents(e) }
220/// fn slice_members(&self, s: &Slice, a: &str) -> Result<Vec<SliceMember>> { self.0.slice_members(s, a) }
221/// fn versions_between(&self, c: &str, f: &str, t: &str, a: &str) -> Result<Vec<SliceMember>> { self.0.versions_between(c, f, t, a) }
222/// fn slice_values(&self, s: &Slice, a: &str) -> Result<Vec<SliceValue>> { self.0.slice_values(s, a) }
223/// fn slice_changed_after(&self, s: &Slice, a: &str) -> Result<bool> { self.0.slice_changed_after(s, a) }
224/// }
225/// ```
226pub trait Store: Resolver + sealed::Sealed {
227 /// Stores a descriptor (checked and canonicalised by the core); returns its function
228 /// hash. Idempotent.
229 fn put_descriptor(&self, bytes: &[u8]) -> Result<String>;
230 /// Stores a receipt that verifies (SPEC 10.1 steps 1-6) against the key document stored
231 /// for its signer host with [`put_namespace`](Store::put_namespace), in canonical form,
232 /// with an edge to each of its inputs; returns its hash. An input that is a slice entry
233 /// (`slice:<slice id>:<members hash>`, since 0.3.1) is recorded as such
234 /// ([`slice_entries`](Store::slice_entries)); its manifest need not be stored, before or
235 /// after (it is needed only to recompute, SPEC 10.1 step 7.5). The receipt's cell is
236 /// indexed by its typed arguments ([`slice_members`](Store::slice_members)). A receipt that does not verify
237 /// is refused with `verify`'s code (no key document for the host: `E_KEY_UNAVAILABLE`).
238 /// Idempotent: bytes whose canonical form is the stored copy return its hash without
239 /// checking the signature again (it verified when stored), even if the key has since
240 /// been revoked. Other bytes with a stored hash are verified first, then refused as
241 /// [`Error::Conflict`].
242 fn put_receipt(&self, bytes: &[u8]) -> Result<String>;
243 /// Stores blob content; returns its SHA-256 (lowercase hex). Idempotent.
244 fn put_blob(&self, bytes: &[u8]) -> Result<String>;
245 /// Blob content by SHA-256.
246 fn blob(&self, sha256: &str) -> Result<Option<Cow<'_, [u8]>>>;
247 /// Appends a version: from `tx` on, `cell_id` has the value of receipt `receipt_hash`,
248 /// which must be stored, of that cell and at that tx. `tx` must be later than the cell's
249 /// latest version ([`Error::NotLater`]).
250 fn put_version(&self, cell_id: &str, tx: &str, receipt_hash: &str) -> Result<()>;
251 /// The cell's latest version with `tx <= asof`.
252 fn version_asof(&self, cell_id: &str, asof: &str) -> Result<Option<Version>>;
253 /// The cell's versions with `from <= tx <= to`, in tx order.
254 fn versions(&self, cell_id: &str, from: &str, to: &str) -> Result<Vec<Version>>;
255 /// The cell's latest version.
256 fn latest(&self, cell_id: &str) -> Result<Option<Version>>;
257 /// The stored receipts that list `receipt_hash` among their inputs, by hash. Only as
258 /// an input of its own: a receipt read as a member of a slice is found through its
259 /// manifests ([`member_manifests`](Store::member_manifests), then
260 /// [`slice_dependents`](Store::slice_dependents)).
261 fn dependents(&self, receipt_hash: &str) -> Result<Vec<String>>;
262 /// The inputs of a stored receipt that are receipt hashes (whether or not they are
263 /// stored). Its slice entries are [`slice_entries`](Store::slice_entries).
264 fn inputs(&self, receipt_hash: &str) -> Result<Vec<String>>;
265 /// Records a host's key document, checked as a 200 response for that host would be
266 /// (SPEC 9.2, 9.3), validated at `validated_at`. Re-validation replaces the document; a
267 /// `validated_at` earlier than the recorded one is [`Error::NotLater`]. Receipts already
268 /// stored are not re-checked: they verified when they were stored (and putting one again
269 /// stays idempotent).
270 fn put_namespace(&self, host: &str, keydoc_bytes: &[u8], validated_at: &str) -> Result<()>;
271 /// A host's recorded key document.
272 fn namespace(&self, host: &str) -> Result<Option<Namespace>>;
273 /// Appends a binding: from `tx` on, the fully qualified function name `fn_name` means
274 /// `fn_hash`, whose descriptor must be stored. `tx` must be later than the name's latest
275 /// binding ([`Error::NotLater`]).
276 fn bind_name(&self, fn_name: &str, fn_hash: &str, tx: &str) -> Result<()>;
277 /// The function hash `fn_name` meant as of `asof`: the hash of
278 /// [`resolve_binding`](Store::resolve_binding)'s binding.
279 fn resolve_name(&self, fn_name: &str, asof: &str) -> Result<Option<String>>;
280 /// The binding of `fn_name` that answers as of `asof`: the one with the latest `tx <=
281 /// asof`, with its function hash and its `tx`, when it began; `None` before the first.
282 /// A name bound again to a hash it meant before (even the hash of the binding before
283 /// it) begins again: the later binding's `tx`. Since 0.3.0.
284 fn resolve_binding(&self, fn_name: &str, asof: &str) -> Result<Option<Binding>>;
285
286 /// Records that `receipt_hash` (stored, a receipt of `cell_id` at `tx`) is a computed
287 /// version of the cell. Insert-only, in any order (an older version may be added after a
288 /// newer one): `tx` need not be later than anything. The version's asof is the
289 /// receipt's. Idempotent for the same (cell_id, tx, asof, receipt_hash); another receipt
290 /// for an existing (cell_id, tx, asof) is [`Error::Conflict`] (its `hash` is the receipt
291 /// refused) and the stored one is kept. The receipt is checked as for
292 /// [`put_version`](Store::put_version): [`Error::NotFound`] when it is not stored,
293 /// [`Error::Mismatch`] when it is of another cell or at another tx. Since 0.2.0.
294 fn put_computed(&self, cell_id: &str, tx: &str, receipt_hash: &str) -> Result<()>;
295 /// The computed version that answers as of `asof`: the one with the greatest asof <=
296 /// `asof` (between equal asof, the greatest tx), or `None`. Since 0.2.0.
297 fn computed_asof(&self, cell_id: &str, asof: &str) -> Result<Option<Version>>;
298 /// The stored version that answers as of `asof`: the latest version (by tx) with `tx <=
299 /// asof` whose receipt's asof is also `<= asof`, or `None`. A version whose receipt is
300 /// late-signed (asof later than tx) answers only from its asof on; until then the
301 /// version before it answers. ([`version_asof`](Store::version_asof) keeps its tx-only
302 /// meaning.) Since 0.2.0.
303 fn stored_asof(&self, cell_id: &str, asof: &str) -> Result<Option<Version>>;
304 /// The cells that read `cell_id`: the cell ids of the stored receipts that list, among
305 /// their inputs, any stored receipt of `cell_id` (lineage: edges joined to receipts),
306 /// or (since 0.3.1) a slice entry whose stored manifest lists one as a member.
307 /// Ascending, each once. Since 0.2.0.
308 fn cell_dependents(&self, cell_id: &str) -> Result<Vec<String>>;
309
310 /// Stores a manifest (SPEC 15.13), `{"members": [...], "slice": {...}}`: parsed strictly
311 /// and checked by the core (`Manifest::from_json`: structure, members, their order and
312 /// limit, the slice object), kept in canonical form under its entry
313 /// `slice:<slice id>:<members hash>`, which it returns, and recorded for each member
314 /// ([`member_manifests`](Store::member_manifests)). The members need not be stored.
315 /// Idempotent; insert-only (the entry is a hash of the content, so a stored entry
316 /// always has these bytes). Served by [`manifest`](Resolver::manifest). Since 0.3.1.
317 fn put_manifest(&self, bytes: &[u8]) -> Result<String>;
318 /// The entries of the stored manifests that list `receipt_hash` among their members,
319 /// ascending: from a member, the slices it was read in (then
320 /// [`slice_dependents`](Store::slice_dependents)). Since 0.3.1.
321 fn member_manifests(&self, receipt_hash: &str) -> Result<Vec<String>>;
322 /// The slice entries among the inputs of a stored receipt, ascending. Since 0.3.1.
323 fn slice_entries(&self, receipt_hash: &str) -> Result<Vec<String>>;
324 /// The stored receipts whose inputs list the slice entry `entry`, by hash, ascending.
325 /// An entry is `slice:` and two 64-digit hashes (`E_CANON_HEX` otherwise). Since 0.3.1.
326 fn slice_dependents(&self, entry: &str) -> Result<Vec<String>>;
327 /// The members of `slice` as of `asof` (SPEC 15.13), with the stored version of each
328 /// that answers as of `asof` and its receipt's bytes.
329 ///
330 /// For a slice of cells: the cells of the slice's function (its `fn_hash`, `ns` and
331 /// `fn`, with the same argument names) whose bound arguments are the same typed scalar,
332 /// whose ranged arguments are of the range's type and within it (`lo <= v <= hi`,
333 /// inclusive, in the type's order: numbers as exact decimals, dates and instants
334 /// chronologically, strings by code point; `lo` after `hi` is empty), any value for an
335 /// open argument, and that have a stored version answering as of `asof`: as
336 /// [`stored_asof`](Store::stored_asof), the latest version with `tx <= asof` whose
337 /// receipt's `asof <= asof` (so a late-signed version is not a member's answer before
338 /// its asof, and a correction replaces a member's version from its asof on). Computed
339 /// versions are not members. In key order: by the open and ranged arguments in
340 /// ascending name order, each in its type's order (`false` before `true`; blobs as
341 /// text); the cell id last, for two cells whose key arguments are of different types.
342 ///
343 /// For a version slice `{"cell", "from", "to"}`: [`versions_between`](Store::versions_between)
344 /// of its cell. Empty when nothing matches. Since 0.3.1.
345 fn slice_members(&self, slice: &Slice, asof: &str) -> Result<Vec<SliceMember>>;
346 /// The stored versions of `cell_id` with `from <= tx <= to` that answer as of `asof`
347 /// (`tx <= asof` and the receipt's `asof <= asof`), ascending by tx, each with its
348 /// receipt's bytes: SPEC 15.13's version slice. Empty when `from` is after `to`. Since
349 /// 0.3.1.
350 fn versions_between(&self, cell_id: &str, from: &str, to: &str, asof: &str) -> Result<Vec<SliceMember>>;
351 /// The members [`slice_members`](Store::slice_members) gives, in its order and by its
352 /// answering rule, each as its version, its receipt's typed value and its cell (with its
353 /// arguments), without the receipt's bytes (the value is a stored column of the receipt
354 /// and the cell comes from the typed-argument index, neither parsed per call).
355 /// Since 0.3.2.
356 fn slice_values(&self, slice: &Slice, asof: &str) -> Result<Vec<SliceValue>>;
357 /// Whether the members of `slice`, or their versions, may answer differently as of some
358 /// time after `asof`: whether a cell matching the slice has a stored version whose
359 /// receipt's `asof` is after `asof` (its `tx` is never later than that), for a version
360 /// slice one with `tx` in `[from, to]`. `false` means [`slice_members`](Store::slice_members)
361 /// (and `slice_values`) give the same answer as of every time from `asof` on, until a
362 /// version is stored with an asof after it; `true` may be conservative (a late-signed
363 /// version superseded before its asof never answers). Index scans only, whatever the
364 /// slice's size: two bounded probes (at most 10,001 entries each) choose whether to walk
365 /// the function's receipts with an asof after `asof` or the slice's candidate cells,
366 /// whichever is fewer, and one query walks it, stopping at the first match. Since 0.3.2.
367 fn slice_changed_after(&self, slice: &Slice, asof: &str) -> Result<bool>;
368
369 /// [`computed_asof`](Store::computed_asof) and the bytes of the version's receipt (as
370 /// [`receipt`](Resolver::receipt) returns them), which is what a caller reads next.
371 /// The default asks for the two in turn (a receipt that cannot be read then is
372 /// [`Error::NotFound`]); [`PgStore`] answers both with one query. Since 0.2.0.
373 fn computed_asof_with_receipt(&self, cell_id: &str, asof: &str) -> Result<Option<(Version, Cow<'_, [u8]>)>> {
374 match self.computed_asof(cell_id, asof)? {
375 Some(v) => with_receipt(self, v).map(Some),
376 None => Ok(None),
377 }
378 }
379 /// [`stored_asof`](Store::stored_asof) and the bytes of the version's receipt, as
380 /// [`computed_asof_with_receipt`](Store::computed_asof_with_receipt) does. Since 0.2.0.
381 fn stored_asof_with_receipt(&self, cell_id: &str, asof: &str) -> Result<Option<(Version, Cow<'_, [u8]>)>> {
382 match self.stored_asof(cell_id, asof)? {
383 Some(v) => with_receipt(self, v).map(Some),
384 None => Ok(None),
385 }
386 }
387}
388
389/// The seal of [`Store`]: a public trait in a private module, so no other crate can name it,
390/// and so none can implement `Store`.
391mod sealed {
392 pub trait Sealed {}
393 impl Sealed for crate::PgStore {}
394}
395
396/// A version and its receipt's bytes, for the default `*_with_receipt` methods.
397fn with_receipt<S: Store + ?Sized>(store: &S, v: Version) -> Result<(Version, Cow<'_, [u8]>)> {
398 match store.receipt(&v.receipt_hash) {
399 Some(bytes) => Ok((v, bytes)),
400 None => Err(Error::NotFound { what: "receipt", key: v.receipt_hash }),
401 }
402}