crossbank 0.1.2

Local, on-device key/value storage for Rust — one API on native (redb) and in the browser (IndexedDB). A Hive-shaped replacement, with no network, server, or sync.
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Byte-budget LRU bookkeeping for [`crate::Policy::Evictable`] lockers.
//!
//! # What is stored, and why in the same commit
//!
//! One `meta` record per live key:
//!
//! ```text
//!   lru::{locker_id u32 BE}::{key bytes}  ->  [tick u64 BE][bytes u32 BE]
//! ```
//!
//! written in **the same commit as the put it describes**. Anything else opens
//! a window where the record exists and its accounting does not (or the other
//! way round), and a budget that disagrees with the data it is supposed to
//! bound is worse than no budget at all.
//!
//! # Why a logical tick and not a clock
//!
//! `std::time::Instant` compiles on wasm32 and panics at runtime, and
//! `Date.now()` moves backwards when the user changes the system clock. The
//! ordering an LRU needs is *relative*, so a bank-wide `u64` counter answers it
//! exactly, cheaply, and identically on every platform.
//!
//! The counter is the same shape as [`super::chunk::ValueIds`]: seeded from
//! `meta` on first use, then a RAM cursor, with its high-water mark persisted
//! by **every** commit that allocates from it.
//!
//! That alone is not enough to make it monotonic across a reopen. A read
//! allocates a tick and — correctly — writes nothing, so a session that only
//! reads leaves the stored `next_tick` behind the cursor it advanced; and two
//! banks over one store each keep their own cursor, so the last one to commit
//! can lower the stored figure below ticks a *different* cursor already
//! recorded against keys. A reopen seeded from that number would re-issue
//! ticks that already sit in `lru::` rows, and the LRU would then shed the
//! wrong key. So [`load`] — which already reads every one of those rows —
//! also tells [`Ticks`] the highest tick it saw, and the clock starts above
//! it. No extra I/O, and still nothing written on the read path.
//!
//! # What `bytes` counts
//!
//! The value's own payload — what `postcard` produced — not its on-disk
//! footprint. The stored form goes through the filter chain (compression
//! changes the size) and may be split into chunk records (framing adds to it),
//! so the on-disk number is both backend-dependent and unstable across a
//! filter-chain change. A budget expressed in payload bytes is the one a
//! caller can reason about: it is the size of the data they handed us.

use std::collections::BTreeMap;

use crate::backend::api::{Backend, KeyRange, Op, ScanRequest, Table};
use crate::error::{Error, Result};
use crate::key::LockerId;

/// Meta key prefix for the LRU records of one locker.
const LRU_PREFIX: &[u8] = b"lru::";
const META_NEXT_TICK: &[u8] = b"next_tick";

/// One LRU record's value: `[tick u64 BE][bytes u32 BE]`.
const RECORD_LEN: usize = 12;

/// `lru::{locker_id BE}::`
pub(crate) fn locker_prefix(id: LockerId) -> Vec<u8> {
    let mut out = Vec::with_capacity(LRU_PREFIX.len() + 6);
    out.extend_from_slice(LRU_PREFIX);
    out.extend_from_slice(&id.to_be_bytes());
    out.extend_from_slice(b"::");
    out
}

/// `lru::{locker_id BE}::{key}`
pub(crate) fn entry_key(id: LockerId, key: &[u8]) -> Vec<u8> {
    let mut out = locker_prefix(id);
    out.extend_from_slice(key);
    out
}

/// The user key an LRU meta key describes, or `None` if it is not one of ours.
fn user_key<'a>(prefix: &[u8], meta_key: &'a [u8]) -> Option<&'a [u8]> {
    meta_key.strip_prefix(prefix)
}

/// One key's accounting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Entry {
    pub tick: u64,
    pub bytes: u64,
}

fn encode_entry(entry: Entry) -> Vec<u8> {
    let mut out = Vec::with_capacity(RECORD_LEN);
    out.extend_from_slice(&entry.tick.to_be_bytes());
    // Saturating rather than wrapping: a value past 4 GiB is accounted as
    // 4 GiB, which over-states nothing and keeps the record fixed-width.
    out.extend_from_slice(&(entry.bytes.min(u32::MAX as u64) as u32).to_be_bytes());
    out
}

fn parse_entry(raw: &[u8]) -> Option<Entry> {
    if raw.len() != RECORD_LEN {
        return None;
    }
    let tick = u64::from_be_bytes(raw[0..8].try_into().ok()?);
    let bytes = u32::from_be_bytes(raw[8..12].try_into().ok()?) as u64;
    Some(Entry { tick, bytes })
}

/// The op that records `key`'s accounting.
pub(crate) fn put_op(id: LockerId, key: &[u8], entry: Entry) -> Op {
    Op::Put {
        table: Table::Meta,
        key: entry_key(id, key),
        value: encode_entry(entry),
    }
}

/// The op that forgets one key's accounting.
pub(crate) fn delete_op(id: LockerId, key: &[u8]) -> Op {
    Op::Delete {
        table: Table::Meta,
        key: entry_key(id, key),
    }
}

/// The op that forgets a whole locker's accounting.
pub(crate) fn clear_op(id: LockerId) -> Op {
    Op::DeleteRange {
        table: Table::Meta,
        range: KeyRange::prefix(&locker_prefix(id)),
    }
}

/// The bank-wide logical clock the LRU orders by.
///
/// The same shape as [`super::chunk::ValueIds`], and for the same reason: one
/// cursor per bank, shared by every locker it opened, so two handles on one
/// name cannot hand out the same tick. The `std` mutex is **never** held
/// across an await — allocation is arithmetic.
#[derive(Debug, Default)]
pub struct Ticks {
    state: std::sync::Mutex<TickState>,
}

/// The cursor, and the floor the stored records put under it.
///
/// `floor` is kept separately from `next` because it is learned at open,
/// *before* anything has allocated: folding it into `next` there would seed
/// the cursor and stop [`Ticks::allocate`] ever reading the stored figure,
/// which may be higher still.
#[derive(Debug, Default)]
struct TickState {
    next: Option<u64>,
    floor: u64,
}

impl TickState {
    /// The next tick to issue, never below what the stored rows demand.
    fn cursor(&self, stored: Option<u64>) -> Option<u64> {
        let next = match (self.next, stored) {
            (Some(current), Some(stored)) => current.max(stored),
            (Some(current), None) => current,
            (None, Some(stored)) => stored,
            (None, None) => return None,
        };
        Some(next.max(self.floor))
    }
}

impl Ticks {
    fn lock(&self) -> Result<std::sync::MutexGuard<'_, TickState>> {
        self.state
            .lock()
            .map_err(|_| Error::backend("tick cursor was poisoned"))
    }

    fn take(&self) -> Result<Option<u64>> {
        let mut guard = self.lock()?;
        match guard.cursor(None) {
            None => Ok(None),
            Some(tick) => {
                guard.next = Some(advance(tick)?);
                Ok(Some(tick))
            }
        }
    }

    /// `max` rather than assignment: a concurrent allocation may have seeded
    /// the cursor while this one awaited the read, and a clock must only ever
    /// move forward.
    fn seed_and_take(&self, stored: u64) -> Result<u64> {
        let mut guard = self.lock()?;
        let tick = guard.cursor(Some(stored)).unwrap_or(stored);
        guard.next = Some(advance(tick)?);
        Ok(tick)
    }

    /// Raise the clock above a tick that storage already records.
    ///
    /// Called by [`load`] with the highest tick in the `lru::` rows it read.
    /// Nothing is written and no read is added: the rows were being scanned
    /// anyway. See this module's header for why the persisted counter alone
    /// cannot carry this.
    pub(crate) fn observe_recorded(&self, max_seen: u64) -> Result<()> {
        let at_least = advance(max_seen)?;
        let mut guard = self.lock()?;
        guard.floor = guard.floor.max(at_least);
        if let Some(current) = guard.next {
            guard.next = Some(current.max(guard.floor));
        }
        Ok(())
    }

    /// Allocate the next tick.
    ///
    /// Every commit carrying a tick allocated here must also carry
    /// [`Ticks::counter_op`], or a reopen would re-issue ticks already
    /// recorded against keys.
    pub(crate) async fn allocate(&self, backend: &dyn Backend) -> Result<u64> {
        if let Some(tick) = self.take()? {
            return Ok(tick);
        }
        let raw = backend.get(Table::Meta, META_NEXT_TICK).await?;
        let stored = match raw {
            None => 0,
            Some(bytes) => <[u8; 8]>::try_from(bytes.as_slice())
                .map(u64::from_be_bytes)
                .map_err(|_| Error::Corrupt("next_tick is not an 8-byte integer".into()))?,
        };
        self.seed_and_take(stored)
    }

    /// Persist the current high-water mark. Belongs in every allocating commit.
    pub(crate) fn counter_op(&self) -> Result<Op> {
        let guard = self.lock()?;
        let next = guard.cursor(None).unwrap_or(guard.floor);
        Ok(Op::Put {
            table: Table::Meta,
            key: META_NEXT_TICK.to_vec(),
            value: next.to_be_bytes().to_vec(),
        })
    }
}

fn advance(tick: u64) -> Result<u64> {
    tick.checked_add(1)
        .ok_or_else(|| Error::backend("tick space is exhausted"))
}

/// How many deferred tick bumps ride along on one write commit.
///
/// Bounded so a read-heavy burst cannot turn the next small write into a
/// commit of thousands of meta records.
pub(crate) const PENDING_PER_COMMIT: usize = 64;

/// What a commit will do to the budget, worked out before anything is written.
#[derive(Debug, Default)]
pub(crate) struct Plan {
    /// Keys to shed, least recently used first.
    pub victims: Vec<Vec<u8>>,
    /// The running total once the updates and the evictions have landed.
    pub total: u64,
}

/// One evictable locker's resident accounting.
#[derive(Debug)]
pub(crate) struct LruState {
    pub max_bytes: u64,
    entries: BTreeMap<Vec<u8>, Entry>,
    total: u64,
    /// Ticks bumped by `get` that no commit has carried yet. Reads must not
    /// write, so these ride along with the next write — or a `flush`.
    pending: BTreeMap<Vec<u8>, u64>,
    /// Set when another tab's news named a key whose size this tab could not
    /// work out. The accounting is then known to be incomplete, and the next
    /// commit reloads the `lru::` prefix before planning. See
    /// [`LruState::mark_dirty`].
    dirty: bool,
}

impl LruState {
    pub fn new(max_bytes: u64) -> Self {
        Self {
            max_bytes,
            entries: BTreeMap::new(),
            total: 0,
            pending: BTreeMap::new(),
            dirty: false,
        }
    }

    pub fn total(&self) -> u64 {
        self.total
    }

    /// Whether the accounting is known to be incomplete and wants a reload.
    pub fn is_dirty(&self) -> bool {
        self.dirty
    }

    /// Say that a remote change could not be accounted for.
    ///
    /// Nothing is guessed: the total simply stops being trustworthy until
    /// [`LruState::adopt_loaded`] replaces it with what storage records.
    pub fn mark_dirty(&mut self) {
        self.dirty = true;
    }

    /// Replace the accounting wholesale with a freshly loaded one, keeping the
    /// deferred read-ticks this tab has not committed yet.
    pub fn adopt_loaded(&mut self, loaded: LruState) {
        self.entries = loaded.entries;
        self.total = loaded.total;
        self.pending.retain(|k, _| self.entries.contains_key(k));
        self.dirty = false;
    }

    /// A tick that sorts after everything this state currently holds.
    ///
    /// Used for another tab's write, which this tab cannot allocate a real
    /// bank tick for without awaiting — a coherence callback must never await.
    /// It only has to order the remote write as "more recent than what is
    /// here", which this does.
    pub fn next_local_tick(&self) -> u64 {
        self.entries
            .values()
            .map(|e| e.tick)
            .max()
            .unwrap_or(0)
            .saturating_add(1)
    }

    /// Fold another tab's put into the accounting.
    ///
    /// `bytes` is `None` when the size could not be determined, which marks
    /// the state dirty rather than inventing a number.
    pub fn remote_put(&mut self, key: &[u8], bytes: Option<u64>) {
        let tick = self.next_local_tick();
        match bytes {
            Some(bytes) => {
                if let Some(old) = self.entries.insert(key.to_vec(), Entry { tick, bytes }) {
                    self.total = self.total.saturating_sub(old.bytes);
                }
                self.total = self.total.saturating_add(bytes);
            }
            None => {
                // The key exists, so it must be in the index and in the
                // ordering; only its size is unknown. Account it as zero and
                // let the reload correct the total.
                self.entries
                    .entry(key.to_vec())
                    .or_insert(Entry { tick, bytes: 0 });
                self.mark_dirty();
            }
        }
    }

    /// Fold another tab's delete into the accounting.
    pub fn remote_delete(&mut self, key: &[u8]) {
        if let Some(old) = self.entries.remove(key) {
            self.total = self.total.saturating_sub(old.bytes);
        }
        self.pending.remove(key);
    }

    /// Fold another tab's clear into the accounting.
    pub fn remote_clear(&mut self) {
        self.entries.clear();
        self.pending.clear();
        self.total = 0;
        self.dirty = false;
    }

    /// Note that `key` was read. RAM only — see [`LruState::pending`].
    pub fn touch(&mut self, key: &[u8], tick: u64) {
        if let Some(entry) = self.entries.get_mut(key) {
            entry.tick = tick;
            self.pending.insert(key.to_vec(), tick);
        }
    }

    /// The deferred tick bumps to carry on the next commit, and the ops.
    /// `skip` names keys the caller is already writing an LRU record for in
    /// this commit; a stale deferred tick must not overwrite a fresh one.
    pub fn pending_ops(&self, id: LockerId, skip: &[Vec<u8>]) -> (Vec<Vec<u8>>, Vec<Op>) {
        let mut keys = Vec::new();
        let mut ops = Vec::new();
        for (key, tick) in self.pending.iter().take(PENDING_PER_COMMIT) {
            if skip.iter().any(|s| s == key) {
                keys.push(key.clone());
                continue;
            }
            let Some(entry) = self.entries.get(key) else {
                continue;
            };
            keys.push(key.clone());
            ops.push(put_op(
                id,
                key,
                Entry {
                    tick: *tick,
                    bytes: entry.bytes,
                },
            ));
        }
        (keys, ops)
    }

    #[cfg(test)]
    pub fn pending_len(&self) -> usize {
        self.pending.len()
    }

    pub fn clear_pending(&mut self, keys: &[Vec<u8>]) {
        for key in keys {
            self.pending.remove(key);
        }
    }

    /// Work out what a commit will cost and who has to go, without touching
    /// anything. `keep` names the keys this same commit is writing, which must
    /// never be evicted to make room for themselves — a batch of them just as
    /// much as a single put. If the batch alone exceeds the budget, everything
    /// else is shed and the batch is left in place: refusing to store what we
    /// were just asked to store would be the wrong failure.
    ///
    /// `budget` is normally [`LruState::max_bytes`];
    /// [`crate::LazyLocker::evict_to`] passes a smaller one.
    pub fn plan(
        &self,
        updates: &[(Vec<u8>, Entry)],
        removals: &[Vec<u8>],
        cleared: bool,
        budget: u64,
        keep: &[Vec<u8>],
    ) -> Plan {
        let mut total = if cleared { 0 } else { self.total };

        if !cleared {
            for key in removals {
                if let Some(entry) = self.entries.get(key.as_slice()) {
                    total = total.saturating_sub(entry.bytes);
                }
            }
        }
        for (key, entry) in updates {
            if !cleared {
                if let Some(old) = self.entries.get(key.as_slice()) {
                    total = total.saturating_sub(old.bytes);
                }
            }
            total = total.saturating_add(entry.bytes);
        }

        if total <= budget {
            return Plan {
                victims: Vec::new(),
                total,
            };
        }

        // Only now is it worth materialising a candidate list. Eviction is the
        // rare path; a put that stays inside the budget must not pay for it.
        let mut candidates: Vec<(u64, &[u8], u64)> = Vec::new();
        if !cleared {
            for (key, entry) in &self.entries {
                if removals.iter().any(|r| r == key)
                    || updates.iter().any(|(k, _)| k == key)
                    || keep.iter().any(|k| k.as_slice() == key.as_slice())
                {
                    continue;
                }
                candidates.push((entry.tick, key.as_slice(), entry.bytes));
            }
        }
        for (key, entry) in updates {
            if keep.iter().any(|k| k == key) {
                continue;
            }
            candidates.push((entry.tick, key.as_slice(), entry.bytes));
        }
        candidates.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(b.1)));

        let mut victims = Vec::new();
        for (_, key, bytes) in candidates {
            if total <= budget {
                break;
            }
            total = total.saturating_sub(bytes);
            victims.push(key.to_vec());
        }
        Plan { victims, total }
    }

    /// Apply what [`LruState::plan`] described, once the commit has landed.
    pub fn apply(
        &mut self,
        updates: &[(Vec<u8>, Entry)],
        removals: &[Vec<u8>],
        cleared: bool,
        plan: &Plan,
    ) {
        if cleared {
            self.entries.clear();
            self.pending.clear();
        }
        for key in removals {
            self.entries.remove(key.as_slice());
            self.pending.remove(key.as_slice());
        }
        for (key, entry) in updates {
            self.entries.insert(key.clone(), *entry);
        }
        for key in &plan.victims {
            self.entries.remove(key.as_slice());
            self.pending.remove(key.as_slice());
        }
        self.total = plan.total;
    }

    /// Seed one key that storage holds but the LRU records nothing for.
    ///
    /// Happens when a locker written under `Precious` is reopened as
    /// `Evictable`. Size unknown, so it is accounted as zero and ordered
    /// first: it is shed before anything whose cost we actually know.
    pub fn adopt(&mut self, key: Vec<u8>) {
        self.entries
            .entry(key)
            .or_insert(Entry { tick: 0, bytes: 0 });
    }

    pub fn retain_keys(&mut self, live: impl Fn(&[u8]) -> bool) {
        self.entries.retain(|k, _| live(k));
        self.pending.retain(|k, _| live(k));
        self.total = self.entries.values().map(|e| e.bytes).sum();
    }
}

/// Read one locker's LRU records back at open.
///
/// One prefix scan of `meta`, paged like every other scan because an IndexedDB
/// cursor cannot outlive its transaction.
///
/// The scan also carries the clock: the highest tick it sees is handed to
/// `ticks`, so the bank never re-issues a tick that is already recorded
/// against a key. See this module's header.
pub(crate) async fn load(
    backend: &dyn Backend,
    id: LockerId,
    max_bytes: u64,
    ticks: &Ticks,
) -> Result<LruState> {
    let mut state = LruState::new(max_bytes);
    let prefix = locker_prefix(id);
    let mut range = KeyRange::prefix(&prefix);
    let mut max_tick: Option<u64> = None;

    loop {
        let page = backend
            .scan(ScanRequest {
                table: Table::Meta,
                range: range.clone(),
                reverse: false,
                limit: super::inner::SCAN_PAGE,
                want_values: true,
            })
            .await?;

        for (meta_key, value) in &page.items {
            let Some(key) = user_key(&prefix, meta_key) else {
                continue;
            };
            // A record we cannot parse is treated as absent rather than fatal:
            // losing one key's read-ordering is not worth refusing to open a
            // locker whose data is perfectly intact.
            if let Some(entry) = value.as_deref().and_then(parse_entry) {
                state.total = state.total.saturating_add(entry.bytes);
                max_tick = Some(max_tick.map_or(entry.tick, |m: u64| m.max(entry.tick)));
                state.entries.insert(key.to_vec(), entry);
            }
        }

        match page.resume {
            Some(last) => range.start = std::ops::Bound::Excluded(last),
            None => break,
        }
    }

    if let Some(max_tick) = max_tick {
        ticks.observe_recorded(max_tick)?;
    }

    Ok(state)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn state() -> LruState {
        let mut s = LruState::new(100);
        for (key, tick, bytes) in [(b"a", 1u64, 40u64), (b"b", 2, 40), (b"c", 3, 20)] {
            s.entries.insert(key.to_vec(), Entry { tick, bytes });
            s.total += bytes;
        }
        s
    }

    #[test]
    fn entry_records_round_trip() {
        let e = Entry {
            tick: 9_000_000_000,
            bytes: 4096,
        };
        assert_eq!(parse_entry(&encode_entry(e)), Some(e));
        assert_eq!(parse_entry(b"short"), None);
    }

    #[test]
    fn an_oversized_value_saturates_rather_than_wrapping() {
        let e = Entry {
            tick: 1,
            bytes: u64::from(u32::MAX) + 10_000,
        };
        let back = parse_entry(&encode_entry(e)).expect("fixed width");
        assert_eq!(back.bytes, u64::from(u32::MAX));
    }

    #[test]
    fn a_put_inside_the_budget_evicts_nothing() {
        let s = state();
        let plan = s.plan(
            &[(b"c".to_vec(), Entry { tick: 4, bytes: 20 })],
            &[],
            false,
            s.max_bytes,
            &[b"c".to_vec()],
        );
        assert!(plan.victims.is_empty());
        assert_eq!(plan.total, 100);
    }

    #[test]
    fn going_over_sheds_the_lowest_tick_first() {
        let s = state();
        // 40 + 40 + 20 + 30 = 130 > 100, so the oldest (a, tick 1) goes.
        let plan = s.plan(
            &[(b"d".to_vec(), Entry { tick: 4, bytes: 30 })],
            &[],
            false,
            s.max_bytes,
            &[b"d".to_vec()],
        );
        assert_eq!(plan.victims, vec![b"a".to_vec()]);
        assert_eq!(plan.total, 90);
    }

    #[test]
    fn the_key_being_written_is_never_its_own_victim() {
        let s = state();
        // One value larger than the whole budget: everything else goes, and
        // the key that caused it stays. Refusing to store what we were just
        // asked to store would be the wrong failure.
        let plan = s.plan(
            &[(
                b"big".to_vec(),
                Entry {
                    tick: 9,
                    bytes: 500,
                },
            )],
            &[],
            false,
            s.max_bytes,
            &[b"big".to_vec()],
        );
        assert_eq!(plan.victims.len(), 3);
        assert_eq!(plan.total, 500);
    }

    #[test]
    fn a_read_bumps_the_tick_and_defers_the_write() {
        let mut s = state();
        s.touch(b"a", 99);
        assert_eq!(s.pending_len(), 1);
        let (keys, ops) = s.pending_ops(7, &[]);
        assert_eq!(keys, vec![b"a".to_vec()]);
        assert_eq!(ops.len(), 1);
        // A key the same commit already records is not written twice.
        assert!(s.pending_ops(7, &[b"a".to_vec()]).1.is_empty());
        let plan = s.plan(
            &[(
                b"d".to_vec(),
                Entry {
                    tick: 100,
                    bytes: 30,
                },
            )],
            &[],
            false,
            s.max_bytes,
            &[b"d".to_vec()],
        );
        // `a` is no longer the oldest, so `b` is shed instead.
        assert_eq!(plan.victims, vec![b"b".to_vec()]);
    }

    /// Reading a locker's records at open must also start the clock above
    /// them. The persisted counter cannot be trusted on its own: reads never
    /// write it, and a second bank over the same store can lower it.
    ///
    /// Native only: it needs an executor, and wasm32 has none to block on.
    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn the_clock_opens_above_every_tick_the_records_hold() {
        let backend = crate::backend::MemoryBackend::new();
        futures::executor::block_on(async {
            backend
                .commit(vec![
                    put_op(
                        7,
                        b"a",
                        Entry {
                            tick: 41,
                            bytes: 10,
                        },
                    ),
                    put_op(
                        7,
                        b"b",
                        Entry {
                            tick: 900,
                            bytes: 10,
                        },
                    ),
                ])
                .await
                .expect("plant records");

            // No `next_tick` is stored at all, which reads as zero — exactly
            // the case where the rows are the only honest authority.
            let ticks = Ticks::default();
            let state = load(&backend, 7, 1_000, &ticks).await.expect("load");
            assert_eq!(state.total(), 20);

            let first = ticks.allocate(&backend).await.expect("allocate");
            assert!(
                first > 900,
                "opened on tick {first}, at or below the 900 already recorded"
            );
        });
    }

    /// The stored counter still wins when it is the higher of the two: a
    /// locker whose keys were all deleted has no rows left to speak for the
    /// ticks it already issued.
    #[cfg(not(target_arch = "wasm32"))]
    #[test]
    fn a_higher_stored_counter_is_not_dragged_back_down() {
        let backend = crate::backend::MemoryBackend::new();
        futures::executor::block_on(async {
            backend
                .commit(vec![
                    put_op(7, b"a", Entry { tick: 3, bytes: 10 }),
                    Op::Put {
                        table: Table::Meta,
                        key: META_NEXT_TICK.to_vec(),
                        value: 5_000u64.to_be_bytes().to_vec(),
                    },
                ])
                .await
                .expect("plant records");

            let ticks = Ticks::default();
            load(&backend, 7, 1_000, &ticks).await.expect("load");
            assert_eq!(ticks.allocate(&backend).await.expect("allocate"), 5_000);
        });
    }

    #[test]
    fn touching_a_key_we_do_not_hold_records_nothing() {
        let mut s = state();
        s.touch(b"absent", 42);
        assert_eq!(s.pending_len(), 0);
    }
}