harmos-storage 0.1.6

Harmos storage: durable journals, snapshots and Arrow-backed stream storage.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

// Typed Durable Segments With Streaming Replay And Cursor Pins.

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::{
    cell::Cell,
    collections::BTreeMap,
    fs::{self, File, OpenOptions},
    io::{BufReader, BufWriter, Write},
    path::PathBuf,
    sync::Arc,
};

// Third Party Imports
use serde::{Deserialize, Serialize};

// Local Imports
use super::{config::JsonStorageSettings, format};
use crate::arrow::RetainedMemory;
use crate::{
    BackendSnapshot, FileReference, MemorySize, ObjectEntry, Result, ScanPage, StorageBatch, StorageCursor, StorageSchema, StoreHistory,
    StoredIdentity, StoredRecord, StoredRow,
};

/* === Constants === */

const READ_BUFFER: usize = 8192;

/* === Macros === */

/* === Type Aliases === */

/* === Structs === */

pub(super) struct Log {
    pub root: PathBuf,
    pub history: StoreHistory,
    pub maximum: usize,
    pub saved: u64,
    segments: Vec<Segment>,
    pub snapshots: BTreeMap<Vec<u8>, (u64, u64)>,
    pub generation: u64,
    pub pruned: BTreeMap<u64, u64>,
    pub pruned_through: u64,
    pub identities: BTreeMap<String, StoredIdentity>,
    last_identity: Option<StoredIdentity>,
    pub lease: Arc<File>,
}

#[derive(PartialEq, Eq, Serialize, Deserialize)]
struct Header {
    kind: String,
    first: StorageCursor,
    through: u64,
    count: usize,
}

struct Segment {
    header: Header,
    path: PathBuf,
}

pub(super) struct Read<'a> {
    pub memory: &'a RetainedMemory,
    base: usize,
    pub retained: Cell<usize>,
    admitted: Cell<usize>,
}

/* === Enums === */

/* === Traits === */

pub(super) trait Row: MemorySize + Sized + Send + Sync + PartialEq + 'static {
    const KIND: &'static str;
    fn position(&self) -> &StorageCursor;
    fn encode(&self, writer: &mut dyn Write) -> Result<()>;
    fn decode(bytes: &[u8]) -> Result<Self>;
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize>;
    fn identity(&self) -> Option<StoredIdentity> {
        None
    }
    fn same_identity(&self, _identity: &StoredIdentity) -> bool {
        false
    }
}

/* === Implementations === */

impl Log {
    pub fn open(settings: &JsonStorageSettings, history: StoreHistory) -> Result<Self> {
        format::regular(&settings.root)?;
        fs::create_dir_all(&settings.root).map_err(format::io)?;
        let lease_path = settings.root.join("json.lock");
        format::regular(&lease_path)?;
        let lease = OpenOptions::new().create(true).truncate(false).read(true).write(true).open(lease_path).map_err(format::io)?;
        lease.try_lock().map_err(|_| format::conflict("storage root is already open"))?;
        for name in ["data", "staging", "files", "productions", "mutations", "releases"] {
            let path = settings.root.join(name);
            format::regular(&path)?;
            fs::create_dir_all(path).map_err(format::io)?;
        }
        let maximum = settings.max_entry_bytes.get();
        let identity = settings.root.join("history.json");
        match format::load::<StoreHistory>(&identity, maximum)? {
            Some(stored) => history.check(&stored)?,
            None => format::store(&identity, &history, maximum)?,
        }
        let mut segments = Vec::new();
        let mut identities = BTreeMap::new();
        let mut last_identity = None;
        for entry in fs::read_dir(settings.root.join("data")).map_err(format::io)? {
            let path = entry.map_err(format::io)?.path();
            format::regular(&path)?;
            if path.extension().is_none_or(|extension| extension != "jsonl") {
                continue;
            }
            let mut reader = BufReader::new(File::open(&path).map_err(format::io)?);
            let line = format::line(&mut reader, maximum)?.ok_or_else(|| format::corrupt("empty committed segment"))?;
            let header: Header = serde_json::from_slice(&line).map_err(format::json)?;
            history.check(&header.first.history)?;
            if header.first.sequence == 0
                || header.count == 0
                || header.through.checked_sub(header.first.sequence) != Some(header.count as u64 - 1)
            {
                return Err(format::corrupt("invalid segment range"));
            }
            let mut sequence = header.first.sequence;
            while let Some(bytes) = format::line(&mut reader, maximum)? {
                let (position, identity) = position(&header.kind, &bytes, last_identity.as_ref())?;
                if let Some(identity) = identity {
                    remember(&mut identities, identity.clone())?;
                    last_identity = Some(identity);
                }
                history.check(&position.history)?;
                if position.sequence != sequence {
                    return Err(format::corrupt("segment positions are not contiguous"));
                }
                sequence = sequence.checked_add(1).ok_or_else(|| format::corrupt("segment position overflow"))?;
            }
            if sequence - 1 != header.through {
                return Err(format::corrupt("segment row count differs"));
            }
            segments.push(Segment { header, path });
        }
        segments.sort_by_key(|segment| segment.header.first.sequence);
        let mut saved = 0_u64;
        for segment in &segments {
            if segment.header.first.sequence != saved + 1 {
                return Err(format::corrupt("committed storage has a cursor gap"));
            }
            saved = segment.header.through;
        }
        let pruning = format::load::<super::reads::Pruning>(&settings.root.join("pruned.json"), maximum)?;
        let pruned_through = pruning.as_ref().map_or(0, |pruning| pruning.through);
        let pruned = pruning.map(|pruning| pruning.positions).unwrap_or_default();
        let generation = pruned.values().copied().max().unwrap_or(0);
        Ok(Self {
            root: settings.root.clone(),
            history,
            maximum,
            saved,
            segments,
            snapshots: BTreeMap::new(),
            generation,
            pruned,
            pruned_through,
            identities,
            last_identity,
            lease: Arc::new(lease),
        })
    }

    pub fn cursor(&self) -> StorageCursor {
        StorageCursor { history: self.history.clone(), sequence: self.saved }
    }

    pub fn snapshot(&mut self, at: StorageCursor) -> Result<BackendSnapshot> {
        self.history.check(&at.history)?;
        if at.sequence > self.saved {
            return Err(format::invalid("snapshot exceeds saved frontier"));
        }
        if at.sequence < self.pruned_through {
            return Err(crate::Error::Gap { requested: at.sequence, available: self.pruned_through });
        }
        let mut token = vec![0; 16];
        getrandom::fill(&mut token)
            .map_err(|error| crate::Error::backend("JSON snapshot entropy", crate::BackendFailure::Permanent, error))?;
        self.snapshots.insert(token.clone(), (at.sequence, self.generation));
        Ok(BackendSnapshot { at, token })
    }

    pub fn pin(&self, snapshot: &BackendSnapshot) -> Result<u64> {
        self.history.check(&snapshot.at.history)?;
        self.snapshots
            .get(&snapshot.token)
            .filter(|pin| pin.0 == snapshot.at.sequence)
            .map(|pin| pin.1)
            .ok_or_else(|| format::invalid("snapshot is released or unknown"))
    }

    pub fn write<T: Row>(&mut self, batch: StorageBatch<T>) -> Result<StorageCursor> {
        let rows = batch.as_slice();
        if rows.is_empty() {
            return Err(format::invalid("typed batch is empty"));
        }
        let first = rows[0].position();
        self.history.check(&first.history)?;
        for (index, row) in rows.iter().enumerate() {
            self.history.check(&row.position().history)?;
            if first.sequence.checked_add(index as u64) != Some(row.position().sequence) {
                return Err(format::invalid("typed batch positions are not contiguous"));
            }
        }
        if first.sequence == 0 || first.sequence > self.saved + 1 {
            return Err(format::conflict("typed write skips the saved frontier"));
        }
        let overlap = rows.partition_point(|row| row.position().sequence <= self.saved);
        for row in &rows[..overlap] {
            let stored = self.bootstrap_one::<T>(row.position().sequence)?;
            if stored.as_ref() != Some(row) {
                return Err(format::conflict("typed retry differs from durable bytes"));
            }
        }
        if overlap == rows.len() {
            return Ok(self.cursor());
        }
        let rows = &rows[overlap..];
        let header = Header {
            kind: T::KIND.into(),
            first: rows[0].position().clone(),
            through: rows.last().expect("nonempty suffix").position().sequence,
            count: rows.len(),
        };
        let name = format!("{:020}-{:020}-{}.jsonl", header.first.sequence, header.through, T::KIND);
        let path = self.root.join("data").join(name);
        let pending = path.with_extension("pending");
        format::regular(&path)?;
        format::regular(&pending)?;
        let mut writer = BufWriter::new(File::create(&pending).map_err(format::io)?);
        format::write(&mut writer, self.maximum, |writer| format::encode(&header, writer))?;
        for row in rows {
            format::write(&mut writer, self.maximum, |writer| row.encode(writer))?;
        }
        writer.flush().map_err(format::io)?;
        writer.get_ref().sync_all().map_err(format::io)?;
        drop(writer);
        if path.exists() {
            let mut reader = BufReader::new(File::open(&path).map_err(format::io)?);
            let prior = format::line(&mut reader, self.maximum)?.ok_or_else(|| format::corrupt("missing uncertain commit header"))?;
            let prior: Header = serde_json::from_slice(&prior).map_err(format::json)?;
            if prior != header {
                return Err(format::conflict("uncertain typed commit range differs"));
            }
            for row in rows {
                let line = format::line(&mut reader, self.maximum)?.ok_or_else(|| format::corrupt("incomplete uncertain commit"))?;
                if T::decode(&line)? != *row {
                    return Err(format::conflict("uncertain typed commit differs"));
                }
            }
            if format::line(&mut reader, self.maximum)?.is_some() {
                return Err(format::conflict("uncertain commit has extra rows"));
            }
            fs::remove_file(pending).map_err(format::io)?;
        } else {
            fs::rename(pending, &path).map_err(format::io)?;
        }
        format::sync_directory(&self.root.join("data"))?;
        self.saved = header.through;
        for row in rows {
            if self.last_identity.as_ref().is_some_and(|identity| row.same_identity(identity)) {
                continue;
            }
            if let Some(identity) = row.identity() {
                remember(&mut self.identities, identity.clone())?;
                self.last_identity = Some(identity);
            }
        }
        self.segments.push(Segment { header, path });
        Ok(self.cursor())
    }

    pub(super) fn bootstrap_visit<T: Row>(&self, at: u64, after: u64, mut visitor: impl FnMut(T) -> Result<bool>) -> Result<()> {
        for segment in &self.segments {
            if segment.header.kind != T::KIND || segment.header.through <= after || segment.header.first.sequence > at {
                continue;
            }
            let mut reader = BufReader::with_capacity(READ_BUFFER, File::open(&segment.path).map_err(format::io)?);
            let header = format::line(&mut reader, self.maximum)?.ok_or_else(|| format::corrupt("segment header disappeared"))?;
            drop(header);
            while let Some(line) = format::line(&mut reader, self.maximum)? {
                let row = T::decode(&line)?;
                let sequence = row.position().sequence;
                if sequence > at {
                    break;
                }
                if sequence > after && !visitor(row)? {
                    return Ok(());
                }
            }
        }
        Ok(())
    }

    fn bootstrap_one<T: Row>(&self, sequence: u64) -> Result<Option<T>> {
        let mut found = None;
        self.bootstrap_visit::<T>(sequence, sequence.saturating_sub(1), |row| {
            found = Some(row);
            Ok(false)
        })?;
        Ok(found)
    }

    pub(super) fn visit_sized<T: Row>(
        &self, at: u64, after: u64, read: &Read<'_>, mut visitor: impl FnMut(T, usize, usize) -> Result<bool>,
    ) -> Result<()> {
        for segment in &self.segments {
            if segment.header.kind != T::KIND || segment.header.through <= after || segment.header.first.sequence > at {
                continue;
            }
            read.admit(READ_BUFFER)?;
            let mut reader = BufReader::with_capacity(READ_BUFFER, File::open(&segment.path).map_err(format::io)?);
            let header = format::line_admitted(&mut reader, self.maximum, |bytes| read.admit(add(READ_BUFFER, bytes)?).map(|_| ()))?
                .ok_or_else(|| format::corrupt("segment header disappeared"))?;
            drop(header);

            while let Some(line) =
                format::line_admitted(&mut reader, self.maximum, |bytes| read.admit(add(READ_BUFFER, bytes)?).map(|_| ()))?
            {
                let scratch = add(READ_BUFFER, line.allocated_bytes())?;
                let bound = T::decoding_memory_bytes(line.len()).ok_or_else(exhausted)?;
                read.admit(add(scratch, bound)?)?;
                let row = T::decode(&line)?;
                let resident = row.memory_size();
                if resident > bound {
                    return Err(format::corrupt("decoded row exceeds its memory bound"));
                }
                let live = read.admit(add(scratch, resident)?)?;
                let sequence = row.position().sequence;
                if sequence > at {
                    break;
                }
                if sequence > after && !visitor(row, line.len(), live)? {
                    return Ok(());
                }
            }
        }
        Ok(())
    }

    pub fn scan<T: Row>(
        &self, at: u64, generation: u64, page: ScanPage, memory: &RetainedMemory, base: usize, mut select: impl FnMut(&T) -> Result<bool>,
    ) -> Result<Vec<T>> {
        let read = Read::new(memory, base);
        self.scan_read(at, generation, page, &read, |row, _| select(row))
    }

    pub(super) fn scan_read<T: Row>(
        &self, at: u64, generation: u64, page: ScanPage, read: &Read<'_>, mut select: impl FnMut(&T, usize) -> Result<bool>,
    ) -> Result<Vec<T>> {
        let mut output = Vec::new();
        let mut bytes = 0usize;
        let mut heap = 0usize;
        read.retained.set(std::mem::size_of::<Vec<T>>());
        self.visit_sized::<T>(at, page.after, read, |row, size, live| {
            let sequence = row.position().sequence;
            if self.pruned.get(&sequence).is_some_and(|pruned| *pruned <= generation) || !select(&row, live)? {
                return Ok(true);
            }
            if !output.is_empty() && size > page.bytes.get().saturating_sub(bytes) {
                return Ok(false);
            }
            reserve(&mut output, page.rows.get(), read, live)?;
            bytes = add(bytes, size)?;
            heap = add(heap, row.memory_size().saturating_sub(std::mem::size_of::<T>()))?;
            output.push(row);
            let slots = output.capacity().checked_mul(std::mem::size_of::<T>()).ok_or_else(exhausted)?;
            read.retained.set(add(add(std::mem::size_of::<Vec<T>>(), slots)?, heap)?);
            Ok(output.len() < page.rows.get() && bytes < page.bytes.get())
        })?;
        Ok(output)
    }

    pub fn one<T: Row>(&self, sequence: u64, memory: &RetainedMemory, base: usize) -> Result<Option<T>> {
        let read = Read::new(memory, base);
        let mut found = None;
        self.visit_sized::<T>(sequence, sequence.saturating_sub(1), &read, |row, _, _| {
            found = Some(row);
            Ok(false)
        })?;
        Ok(found)
    }
}

impl Row for StorageSchema {
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize> {
        StorageSchema::decoding_memory_bytes(encoded_bytes)
    }
    const KIND: &'static str = "schemas";
    fn position(&self) -> &StorageCursor {
        &self.position
    }
    fn encode(&self, writer: &mut dyn Write) -> Result<()> {
        format::encode(self, writer)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(format::json)
    }
}

impl Row for StoredRecord {
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize> {
        format::flat_decoding_bytes(encoded_bytes, std::mem::size_of::<Self>())
    }
    const KIND: &'static str = "records";
    fn position(&self) -> &StorageCursor {
        &self.position
    }
    fn encode(&self, writer: &mut dyn Write) -> Result<()> {
        format::encode(self, writer)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(format::json)
    }
}

impl Row for StoredRow {
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize> {
        format::flat_decoding_bytes(encoded_bytes, std::mem::size_of::<Self>())
    }
    const KIND: &'static str = "stream";
    fn position(&self) -> &StorageCursor {
        &self.record.position
    }
    fn encode(&self, writer: &mut dyn Write) -> Result<()> {
        format::encode(self, writer)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(format::json)
    }
    fn identity(&self) -> Option<StoredIdentity> {
        Some(StoredIdentity::Stream {
            schema: self.record.schema.clone(),
            scope: self.record.scope.clone(),
            item: self.record.item.clone(),
            stream: self.stream.clone(),
            first: self.record.position.clone(),
        })
    }
    fn same_identity(&self, identity: &StoredIdentity) -> bool {
        matches!(identity, StoredIdentity::Stream {schema,scope,item,stream,..} if schema == &self.record.schema && scope == &self.record.scope && item == &self.record.item && stream == &self.stream)
    }
}

impl Row for ObjectEntry {
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize> {
        format::flat_decoding_bytes(encoded_bytes, std::mem::size_of::<Self>())
    }
    const KIND: &'static str = "objects";
    fn position(&self) -> &StorageCursor {
        &self.position
    }
    fn encode(&self, writer: &mut dyn Write) -> Result<()> {
        format::encode(self, writer)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(format::json)
    }
    fn identity(&self) -> Option<StoredIdentity> {
        Some(StoredIdentity::Object { schema: self.value.schema.clone(), key: self.value.item.clone(), first: self.position.clone() })
    }
    fn same_identity(&self, identity: &StoredIdentity) -> bool {
        matches!(identity, StoredIdentity::Object {schema,key,..} if schema == &self.value.schema && key == &self.value.item)
    }
}

impl Row for FileReference {
    fn decoding_memory_bytes(encoded_bytes: usize) -> Option<usize> {
        format::flat_decoding_bytes(encoded_bytes, std::mem::size_of::<Self>())
    }
    const KIND: &'static str = "files";
    fn position(&self) -> &StorageCursor {
        &self.position
    }
    fn encode(&self, writer: &mut dyn Write) -> Result<()> {
        format::encode(self, writer)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        serde_json::from_slice(bytes).map_err(format::json)
    }
}

impl<'a> Read<'a> {
    pub(super) fn new(memory: &'a RetainedMemory, base: usize) -> Self {
        Self { memory, base, retained: Cell::new(0), admitted: Cell::new(memory.bytes()) }
    }

    pub(super) fn admit(&self, bytes: usize) -> Result<usize> {
        let total = add(add(self.base, self.retained.get())?, bytes)?;
        self.ensure(total)?;
        Ok(total)
    }

    fn ensure(&self, total: usize) -> Result<()> {
        // The Blocking Job Retains Its Guard; Reconciliation Follows Completion.
        if total > self.admitted.get() {
            self.memory.try_grow_to(total)?;
            self.admitted.set(total);
        }
        Ok(())
    }
}

/* === Public Functions === */

/* === Private Functions === */

pub(super) fn exhausted() -> crate::Error {
    crate::Error::ResourceExhausted { resource: "JSON read memory" }
}

pub(super) fn add(left: usize, right: usize) -> Result<usize> {
    left.checked_add(right).ok_or_else(exhausted)
}

pub(super) fn reserve<T>(output: &mut Vec<T>, limit: usize, read: &Read<'_>, live: usize) -> Result<()> {
    if output.len() < output.capacity() {
        return Ok(());
    }
    let target = output.capacity().max(2).checked_mul(2).ok_or_else(exhausted)?.min(limit);
    let allocation = target.checked_mul(std::mem::size_of::<T>()).ok_or_else(exhausted)?;
    read.ensure(add(live, allocation)?)?;
    output.try_reserve_exact(target - output.len()).map_err(|_| exhausted())?;
    Ok(())
}

fn position(kind: &str, bytes: &[u8], previous: Option<&StoredIdentity>) -> Result<(StorageCursor, Option<StoredIdentity>)> {
    match kind {
        "schemas" => {
            let row = StorageSchema::decode(bytes)?;
            Ok((row.position, None))
        }
        "objects" => {
            let row = ObjectEntry::decode(bytes)?;
            let identity =
                if previous.is_some_and(|identity| identity.first().sequence <= row.position.sequence && row.same_identity(identity)) {
                    None
                } else {
                    row.identity()
                };
            Ok((row.position, identity))
        }
        "records" => {
            let row = StoredRecord::decode(bytes)?;
            Ok((row.position, None))
        }
        "stream" => {
            let row = StoredRow::decode(bytes)?;
            let identity = if previous
                .is_some_and(|identity| identity.first().sequence <= row.record.position.sequence && row.same_identity(identity))
            {
                None
            } else {
                row.identity()
            };
            Ok((row.record.position, identity))
        }
        "files" => {
            let row = FileReference::decode(bytes)?;
            Ok((row.position, None))
        }
        _ => Err(format::corrupt("unknown typed segment kind")),
    }
}

fn remember(identities: &mut BTreeMap<String, StoredIdentity>, identity: StoredIdentity) -> Result<()> {
    let key = match &identity {
        StoredIdentity::Stream { schema, scope, item, stream, .. } => {
            serde_json::to_string(&(schema, scope, item, stream)).map_err(format::json)?
        }
        StoredIdentity::Object { schema, key, .. } => serde_json::to_string(&(schema, key)).map_err(format::json)?,
    };
    if identities.get(&key).is_none_or(|prior| prior.first().sequence > identity.first().sequence) {
        identities.insert(key, identity);
    }
    Ok(())
}

/* === Tests === */