reifydb-store-multi 0.8.1

Multi-version storage for OLTP operations with MVCC support
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, OnceLock};

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use reifydb_codec::key::encoded::EncodedKey;
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use reifydb_core::{common::CommitVersion, interface::store::EntryKind};
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use reifydb_runtime::actor::timers::TimerHandle;
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use reifydb_runtime::actor::{
	context::Context,
	mailbox::ActorRef,
	system::{ActorConfig, ActorSpawner},
	traits::{Actor, Directive},
};
use reifydb_runtime::sync::{rwlock::RwLock, waiter::WaiterHandle};
use reifydb_value::value::{datetime::DateTime, duration::Duration};
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use reifydb_value::{reifydb_assertions, util::cowvec::CowVec};
#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use tracing::{debug, error, warn};

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
use crate::tier::{TierBatch, TierStorage};
use crate::{
	flush::ShapePersistence,
	gc::EvictionWatermark,
	tier::{commit::buffer::MultiCommitBufferTier, persistent::MultiPersistentTier, read::MultiReadBufferTier},
};

#[derive(Clone)]
pub enum FlushMessage {
	Tick(DateTime),
	Shutdown,

	SetInterval(Duration),

	FlushPending {
		waiter: Arc<WaiterHandle>,
	},

	FlushAll {
		waiter: Arc<WaiterHandle>,
	},
}

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
pub struct FlushActorState {
	timer_handle: Option<TimerHandle>,
}

#[allow(dead_code)]
pub struct FlushActor {
	commit: MultiCommitBufferTier,
	persistent: MultiPersistentTier,
	flush_interval: Duration,
	persistence: Arc<OnceLock<Arc<dyn ShapePersistence>>>,
	eviction_watermark: Arc<RwLock<Option<Arc<dyn EvictionWatermark>>>>,
	read: Option<MultiReadBufferTier>,
}

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
type EvictablePartition = (Vec<(EncodedKey, CommitVersion, Option<CowVec<u8>>)>, Vec<(EncodedKey, CommitVersion)>);

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
impl FlushActor {
	pub fn new(
		commit: MultiCommitBufferTier,
		persistent: MultiPersistentTier,
		flush_interval: Duration,
		persistence: Arc<OnceLock<Arc<dyn ShapePersistence>>>,
		eviction_watermark: Arc<RwLock<Option<Arc<dyn EvictionWatermark>>>>,
		read: Option<MultiReadBufferTier>,
	) -> Self {
		Self {
			commit,
			persistent,
			flush_interval,
			persistence,
			eviction_watermark,
			read,
		}
	}

	pub fn spawn(
		spawner: &ActorSpawner,
		commit: MultiCommitBufferTier,
		persistent: MultiPersistentTier,
		flush_interval: Duration,
		persistence: Arc<OnceLock<Arc<dyn ShapePersistence>>>,
		eviction_watermark: Arc<RwLock<Option<Arc<dyn EvictionWatermark>>>>,
		read: Option<MultiReadBufferTier>,
	) -> ActorRef<FlushMessage> {
		let actor = Self::new(commit, persistent, flush_interval, persistence, eviction_watermark, read);
		spawner.spawn_coordination("persistent-flush", actor).actor_ref().clone()
	}

	fn eviction_cutoff(&self) -> Option<CommitVersion> {
		let cutoff = self.eviction_watermark.read().as_ref()?.watermark();
		if cutoff.0 == 0 {
			return None;
		}
		Some(cutoff)
	}

	fn is_persistent_shape(&self, kind: EntryKind) -> bool {
		match kind {
			EntryKind::Source(shape) | EntryKind::PartitionedSource(shape) => {
				self.persistence.get().map(|provider| provider.is_persistent(shape)).unwrap_or(true)
			}
			_ => true,
		}
	}

	fn sweep(&self, cutoff: CommitVersion) {
		let Some(entry_kinds) = self.list_evictable_kinds() else {
			return;
		};

		let mut plan: Vec<(EntryKind, bool, EvictablePartition)> = Vec::new();
		let mut batches: HashMap<CommitVersion, TierBatch> = HashMap::new();
		for kind in entry_kinds {
			let (to_persist, to_drop) = self.collect_evictable(kind, cutoff);
			if to_drop.is_empty() {
				continue;
			}
			let persistent_shape = self.is_persistent_shape(kind);
			if persistent_shape {
				for (key, version, value) in &to_persist {
					batches.entry(*version)
						.or_default()
						.entry(kind)
						.or_default()
						.push((key.clone(), value.clone()));
				}
			}
			plan.push((kind, persistent_shape, (to_persist, to_drop)));
		}
		if plan.is_empty() {
			return;
		}

		let accepted = if batches.values().any(|batch| !batch.is_empty()) {
			match self.persistent.persist_sweep(batches.into_iter().collect()) {
				Ok(accepted) => accepted,
				Err(e) => {
					error!(error = %e, "flush sweep: persist failed, aborting sweep");
					return;
				}
			}
		} else {
			Vec::new()
		};
		let persisted = accepted.len();

		let mut dropped = 0usize;
		for (kind, persistent_shape, (to_persist, to_drop)) in plan {
			self.refresh_read_tier(persistent_shape, &to_persist, &to_drop, &accepted);
			if let Some(count) = self.drop_from_commit(kind, to_drop) {
				dropped += count;
			}
		}

		self.checkpoint_and_maintain(cutoff, persisted, dropped);
	}

	#[inline]
	fn list_evictable_kinds(&self) -> Option<Vec<EntryKind>> {
		match self.commit.list_all_entry_kinds() {
			Ok(v) => Some(v),
			Err(e) => {
				warn!(error = %e, "flush sweep: list_all_entry_kinds failed");
				None
			}
		}
	}

	#[inline]
	fn collect_evictable(&self, kind: EntryKind, cutoff: CommitVersion) -> EvictablePartition {
		match &self.commit {
			MultiCommitBufferTier::Memory(s) => s.collect_evictable_below(kind, cutoff),
		}
	}

	#[inline]
	fn refresh_read_tier(
		&self,
		persistent_shape: bool,
		to_persist: &[(EncodedKey, CommitVersion, Option<CowVec<u8>>)],
		to_drop: &[(EncodedKey, CommitVersion)],
		accepted: &[EncodedKey],
	) {
		let Some(read) = &self.read else {
			return;
		};
		if persistent_shape {
			let accepted: HashSet<&[u8]> = accepted.iter().map(|k| k.as_slice()).collect();
			for (key, version, value) in to_persist {
				if accepted.contains(key.as_slice()) {
					read.insert(key.clone(), *version, value.clone());
				} else {
					read.invalidate(key);
				}
			}
		} else {
			for (key, _) in to_drop {
				read.invalidate(key);
			}
		}
	}

	#[inline]
	fn drop_from_commit(&self, kind: EntryKind, to_drop: Vec<(EncodedKey, CommitVersion)>) -> Option<usize> {
		let drop_count = to_drop.len();
		reifydb_assertions! {
			assert!(
				drop_count > 0,
				"sweep must only reach drop_from_commit with a non-empty drop set; an empty drop \
				 issues a no-op commit-buffer drop and lets the dropped counter run for zero work \
				 (kind={kind:?})"
			);
		}
		let mut batches: HashMap<EntryKind, Vec<(EncodedKey, CommitVersion)>> = HashMap::new();
		batches.insert(kind, to_drop);
		if let Err(e) = self.commit.drop(batches) {
			warn!(?kind, error = %e, "flush sweep: commit buffer drop failed");
			return None;
		}
		Some(drop_count)
	}

	#[inline]
	fn checkpoint_and_maintain(&self, cutoff: CommitVersion, persisted: usize, dropped: usize) {
		if persisted > 0 || dropped > 0 {
			debug!(cutoff = cutoff.0, persisted, dropped, "flush sweep completed");
			if persisted > 0
				&& let Err(e) = self.persistent.maybe_checkpoint()
			{
				warn!(error = %e, "flush sweep: checkpoint failed");
			}
			self.commit.maintenance();
		}
	}
}

#[cfg(all(feature = "sqlite", not(target_arch = "wasm32")))]
impl Actor for FlushActor {
	type State = FlushActorState;
	type Message = FlushMessage;

	fn init(&self, ctx: &Context<FlushMessage>) -> FlushActorState {
		debug!("Persistent flush actor started");
		let timer_handle = ctx.schedule_tick(self.flush_interval.to_std(), |nanos| {
			FlushMessage::Tick(DateTime::from_nanos(nanos))
		});
		FlushActorState {
			timer_handle: Some(timer_handle),
		}
	}

	fn handle(&self, state: &mut FlushActorState, msg: FlushMessage, ctx: &Context<FlushMessage>) -> Directive {
		if ctx.is_cancelled() {
			if let Some(cutoff) = self.eviction_cutoff() {
				self.sweep(cutoff);
			}
			return Directive::Stop;
		}
		match msg {
			FlushMessage::Tick(_) => {
				if let Some(cutoff) = self.eviction_cutoff() {
					self.sweep(cutoff);
				}
			}
			FlushMessage::SetInterval(interval) => {
				if let Some(handle) = state.timer_handle.take() {
					handle.cancel();
				}
				state.timer_handle = Some(ctx.schedule_tick(interval.to_std(), |nanos| {
					FlushMessage::Tick(DateTime::from_nanos(nanos))
				}));
			}
			FlushMessage::Shutdown => {
				debug!("Persistent flush actor shutting down");
				if let Some(cutoff) = self.eviction_cutoff() {
					self.sweep(cutoff);
				}
				return Directive::Stop;
			}
			FlushMessage::FlushPending {
				waiter,
			} => {
				if let Some(cutoff) = self.eviction_cutoff() {
					self.sweep(cutoff);
				}
				waiter.notify();
			}
			FlushMessage::FlushAll {
				waiter,
			} => {
				self.sweep(CommitVersion(u64::MAX));
				waiter.notify();
			}
		}
		Directive::Continue
	}

	fn post_stop(&self) {
		debug!("Persistent flush actor stopped");
	}

	fn config(&self) -> ActorConfig {
		ActorConfig::new().mailbox_capacity(4096)
	}
}

#[cfg(all(test, feature = "sqlite", not(target_arch = "wasm32")))]
mod tests {
	use reifydb_core::interface::catalog::{id::TableId, shape::ShapeId};
	use reifydb_runtime::shutdown::Shutdown;
	use reifydb_sqlite::SqliteTempPathGuard;
	use reifydb_value::util::cowvec::CowVec;

	use super::*;
	use crate::tier::{VersionedGetResult, read::ReadBufferConfig};

	fn ek(s: &str) -> EncodedKey {
		EncodedKey::new(s.as_bytes().to_vec())
	}

	fn val(s: &str) -> CowVec<u8> {
		CowVec::new(s.as_bytes().to_vec())
	}

	fn write(buffer: &MultiCommitBufferTier, kind: EntryKind, key: &EncodedKey, version: u64, value: &str) {
		buffer.set(CommitVersion(version), HashMap::from([(kind, vec![(key.clone(), Some(val(value)))])]))
			.unwrap();
	}

	struct StaticWatermark(CommitVersion);

	impl EvictionWatermark for StaticWatermark {
		fn watermark(&self) -> CommitVersion {
			self.0
		}
	}

	struct AllPersistent;

	impl ShapePersistence for AllPersistent {
		fn is_persistent(&self, _shape: ShapeId) -> bool {
			true
		}
	}

	struct NonePersistent;

	impl ShapePersistence for NonePersistent {
		fn is_persistent(&self, _shape: ShapeId) -> bool {
			false
		}
	}

	fn build_actor(
		persistence: Arc<dyn ShapePersistence>,
		watermark: Option<CommitVersion>,
	) -> (FlushActor, SqliteTempPathGuard) {
		let buffer = MultiCommitBufferTier::memory();
		let (persistent, guard) = MultiPersistentTier::sqlite_in_memory();
		let persistence_lock: Arc<OnceLock<Arc<dyn ShapePersistence>>> = Arc::new(OnceLock::new());
		let _ = persistence_lock.set(persistence);
		let watermark_lock: Arc<RwLock<Option<Arc<dyn EvictionWatermark>>>> = Arc::new(RwLock::new(None));
		if let Some(w) = watermark {
			*watermark_lock.write() = Some(Arc::new(StaticWatermark(w)));
		}
		(
			FlushActor::new(
				buffer,
				persistent,
				Duration::from_seconds(5).unwrap(),
				persistence_lock,
				watermark_lock,
				None,
			),
			guard,
		)
	}

	fn build_actor_with_read(
		persistence: Arc<dyn ShapePersistence>,
		watermark: CommitVersion,
		read: MultiReadBufferTier,
	) -> (FlushActor, SqliteTempPathGuard) {
		let buffer = MultiCommitBufferTier::memory();
		let (persistent, guard) = MultiPersistentTier::sqlite_in_memory();
		let persistence_lock: Arc<OnceLock<Arc<dyn ShapePersistence>>> = Arc::new(OnceLock::new());
		let _ = persistence_lock.set(persistence);
		let watermark_lock: Arc<RwLock<Option<Arc<dyn EvictionWatermark>>>> = Arc::new(RwLock::new(None));
		*watermark_lock.write() = Some(Arc::new(StaticWatermark(watermark)));
		(
			FlushActor::new(
				buffer,
				persistent,
				Duration::from_seconds(5).unwrap(),
				persistence_lock,
				watermark_lock,
				Some(read),
			),
			guard,
		)
	}

	#[test]
	fn eviction_cutoff_is_none_without_watermark() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), None);
		assert!(actor.eviction_cutoff().is_none(), "no watermark set => no eviction");
	}

	#[test]
	fn eviction_cutoff_is_none_at_zero() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(0)));
		assert!(actor.eviction_cutoff().is_none());
	}

	#[test]
	fn sweep_persists_then_evicts_persistent_shape_below_watermark() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(2)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(1)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 1, "v1");
		write(&actor.commit, kind, &key, 2, "v2");
		write(&actor.commit, kind, &key, 3, "v3");

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(
				actor.commit.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"v2 must be gone from the buffer after eviction"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::Value { .. }
			),
			"v2 must survive in the persistent tier"
		);

		assert_eq!(
			actor.commit.get(kind, key.as_ref(), CommitVersion(3)).unwrap().value().as_deref(),
			Some(b"v3".as_slice()),
			"v3 (> cutoff) must stay in the buffer"
		);
	}

	#[test]
	fn sweep_evicts_non_persistent_shape_without_persisting() {
		let (actor, _guard) = build_actor(Arc::new(NonePersistent), Some(CommitVersion(2)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(7)));
		let key = ek("ephemeral");
		write(&actor.commit, kind, &key, 1, "v1");
		write(&actor.commit, kind, &key, 2, "v2");
		write(&actor.commit, kind, &key, 3, "v3");

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(
				actor.commit.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"non-persistent shape must still be evicted below the watermark"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"non-persistent shape must NOT be written to the persistent tier"
		);
		assert_eq!(
			actor.commit.get(kind, key.as_ref(), CommitVersion(3)).unwrap().value().as_deref(),
			Some(b"v3".as_slice()),
			"v3 (> cutoff) must stay resident even for a non-persistent shape"
		);
	}

	#[test]
	fn sweep_keeps_everything_when_all_above_watermark() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(1)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(3)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 5, "v5");

		actor.sweep(CommitVersion(1));

		assert_eq!(
			actor.commit.get(kind, key.as_ref(), CommitVersion(5)).unwrap().value().as_deref(),
			Some(b"v5".as_slice()),
			"a version above the watermark must never be evicted"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(5)).unwrap(),
				VersionedGetResult::NotFound
			),
			"nothing below the watermark => nothing persisted"
		);
	}

	#[test]
	fn sweep_seeds_evicted_keys_into_the_read_tier() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(AllPersistent), CommitVersion(2), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(11)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 1, "v1");
		write(&actor.commit, kind, &key, 2, "v2");

		read.insert(key.clone(), CommitVersion(2), Some(val("stale")));

		actor.sweep(CommitVersion(2));

		match read.get(&key, CommitVersion(2)) {
			VersionedGetResult::Value {
				value,
				..
			} => assert_eq!(
				value.as_ref(),
				val("v2").as_ref(),
				"the read tier must hold the persisted value, not the stale one"
			),
			other => panic!("the sweep must seed the evicted key into the read tier, got {other:?}"),
		}
	}

	#[test]
	fn sweep_seeds_tombstone_into_read_tier() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(AllPersistent), CommitVersion(2), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(21)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 1, "v1");
		actor.commit.set(CommitVersion(2), HashMap::from([(kind, vec![(key.clone(), None)])])).unwrap();

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(read.get(&key, CommitVersion(2)), VersionedGetResult::Tombstone),
			"an evicted tombstone must be seeded into the read tier as a definitive miss, not left absent \
			 (which would fall through and risk resurrecting an older value)"
		);
	}

	#[test]
	fn sweep_invalidates_rejected_key_but_seeds_accepted() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(AllPersistent), CommitVersion(2), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(22)));
		let rejected = ek("rejected");
		let accepted = ek("accepted");

		actor.persistent
			.set(CommitVersion(3), HashMap::from([(kind, vec![(rejected.clone(), Some(val("high")))])]))
			.unwrap();

		read.insert(rejected.clone(), CommitVersion(2), Some(val("stale")));

		write(&actor.commit, kind, &rejected, 2, "low");
		write(&actor.commit, kind, &accepted, 2, "b");

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(read.get(&rejected, CommitVersion(2)), VersionedGetResult::NotFound),
			"a guard-rejected key must be invalidated in the read tier so reads fall through to the newer \
			 persisted value, never serving the stale entry"
		);
		match read.get(&accepted, CommitVersion(2)) {
			VersionedGetResult::Value {
				value,
				..
			} => assert_eq!(value.as_ref(), val("b").as_ref(), "the accepted key must be seeded"),
			other => panic!("the accepted key must be seeded into the read tier, got {other:?}"),
		}
	}

	#[test]
	fn sweep_seed_respects_read_tier_downgrade_guard() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(AllPersistent), CommitVersion(2), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(23)));
		let key = ek("k");

		read.insert(key.clone(), CommitVersion(5), Some(val("newer")));

		write(&actor.commit, kind, &key, 2, "older");
		actor.sweep(CommitVersion(2));

		match read.get(&key, CommitVersion(5)) {
			VersionedGetResult::Value {
				value,
				..
			} => assert_eq!(
				value.as_ref(),
				val("newer").as_ref(),
				"the older seeded value must not overwrite a newer resident read-tier entry"
			),
			other => panic!("the newer read-tier entry must survive the sweep's seed, got {other:?}"),
		}
	}

	#[test]
	fn sweep_invalidates_ephemeral_shape_in_read_tier() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(NonePersistent), CommitVersion(2), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(24)));
		let key = ek("k");

		read.insert(key.clone(), CommitVersion(2), Some(val("stale")));
		write(&actor.commit, kind, &key, 2, "v2");

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(read.get(&key, CommitVersion(2)), VersionedGetResult::NotFound),
			"an ephemeral (persistent:false) shape must be invalidated in the read tier, never seeded"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"an ephemeral shape must not be persisted"
		);
	}

	#[test]
	fn sweep_seeds_accepted_keys_across_version_buckets() {
		let read = MultiReadBufferTier::new(ReadBufferConfig {
			resident_pages: 16,
			..Default::default()
		});
		let (actor, _guard) = build_actor_with_read(Arc::new(AllPersistent), CommitVersion(4), read.clone());
		let kind = EntryKind::Source(ShapeId::Table(TableId(25)));
		let a = ek("a");
		let b = ek("b");
		write(&actor.commit, kind, &a, 1, "a1");
		write(&actor.commit, kind, &a, 2, "a2");
		write(&actor.commit, kind, &b, 3, "b3");
		write(&actor.commit, kind, &b, 4, "b4");

		actor.sweep(CommitVersion(4));

		match read.get(&a, CommitVersion(4)) {
			VersionedGetResult::Value {
				value,
				..
			} => assert_eq!(value.as_ref(), val("a2").as_ref(), "a's latest-<=W (v2) must be seeded"),
			other => panic!("key a must be seeded across version buckets, got {other:?}"),
		}
		match read.get(&b, CommitVersion(4)) {
			VersionedGetResult::Value {
				value,
				..
			} => assert_eq!(value.as_ref(), val("b4").as_ref(), "b's latest-<=W (v4) must be seeded"),
			other => panic!("key b must be seeded across version buckets, got {other:?}"),
		}
	}

	#[test]
	fn sweep_persists_tombstone_so_deleted_keys_stay_deleted_after_eviction() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(2)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(12)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 1, "v1");
		actor.commit.set(CommitVersion(2), HashMap::from([(kind, vec![(key.clone(), None)])])).unwrap();

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(
				actor.commit.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"both versions are gone from the buffer"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::Tombstone
			),
			"the persisted latest value must be the tombstone - the row must not resurrect"
		);
	}

	#[test]
	fn sweep_evicts_below_and_keeps_above_across_multiple_keys() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(2)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(13)));
		let cold = ek("cold");
		let hot = ek("hot");
		write(&actor.commit, kind, &cold, 1, "cold1");
		write(&actor.commit, kind, &hot, 4, "hot4");

		actor.sweep(CommitVersion(2));

		assert!(
			matches!(
				actor.commit.get(kind, cold.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::NotFound
			),
			"cold (v1 <= cutoff) must be evicted from the buffer"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, cold.as_ref(), CommitVersion(2)).unwrap(),
				VersionedGetResult::Value { .. }
			),
			"cold must survive in persistent"
		);
		assert_eq!(
			actor.commit.get(kind, hot.as_ref(), CommitVersion(4)).unwrap().value().as_deref(),
			Some(b"hot4".as_slice()),
			"hot (v4 > cutoff) must stay resident in the buffer"
		);
		assert!(
			matches!(
				actor.persistent.get(kind, hot.as_ref(), CommitVersion(4)).unwrap(),
				VersionedGetResult::NotFound
			),
			"hot must not be persisted - it is above the watermark"
		);
	}

	#[test]
	fn flush_all_persists_every_key_regardless_of_watermark() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(1)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(101)));
		let cold = ek("cold");
		let hot = ek("hot");
		write(&actor.commit, kind, &cold, 2, "cold2");
		write(&actor.commit, kind, &hot, 50, "hot50");

		actor.sweep(CommitVersion(u64::MAX));

		assert_eq!(
			actor.persistent.get(kind, cold.as_ref(), CommitVersion(u64::MAX)).unwrap().value().as_deref(),
			Some(b"cold2".as_slice()),
			"a key committed above the watermark must be persisted by a full flush"
		);
		assert_eq!(
			actor.persistent.get(kind, hot.as_ref(), CommitVersion(u64::MAX)).unwrap().value().as_deref(),
			Some(b"hot50".as_slice()),
			"the latest committed value of every key must survive a full flush"
		);
		assert!(
			matches!(
				actor.commit.get(kind, hot.as_ref(), CommitVersion(u64::MAX)).unwrap(),
				VersionedGetResult::NotFound
			),
			"a full flush drains the buffer after persisting"
		);
	}

	#[test]
	fn sweep_aborts_and_keeps_buffer_when_persist_fails() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(2)));
		let row_kind = EntryKind::Source(ShapeId::Table(TableId(31)));
		let dict_kind = EntryKind::Multi;
		let row_key = ek("row-referencing-id-7");
		let dict_key = ek("dictionary-entry-7");
		write(&actor.commit, row_kind, &row_key, 1, "id=7");
		write(&actor.commit, dict_kind, &dict_key, 1, "entry-7");

		actor.persistent.shutdown();
		actor.sweep(CommitVersion(2));

		assert_eq!(
			actor.commit.get(row_kind, row_key.as_ref(), CommitVersion(2)).unwrap().value().as_deref(),
			Some(b"id=7".as_slice()),
			"a failed persist must leave the row write in the commit buffer, not drop the only copy"
		);
		assert_eq!(
			actor.commit.get(dict_kind, dict_key.as_ref(), CommitVersion(2)).unwrap().value().as_deref(),
			Some(b"entry-7".as_slice()),
			"a failed persist must leave the dictionary write in the commit buffer, not drop the only copy"
		);
	}

	#[test]
	fn persist_sweep_errors_when_storage_is_shut_down() {
		let (persistent, _guard) = MultiPersistentTier::sqlite_in_memory();
		persistent.shutdown();

		let kind = EntryKind::Source(ShapeId::Table(TableId(32)));
		let batches = vec![(CommitVersion(1), HashMap::from([(kind, vec![(ek("k"), Some(val("v")))])]))];
		assert!(
			persistent.persist_sweep(batches).is_err(),
			"a shut-down persistent tier must refuse the sweep loudly so the buffer is not dropped"
		);
	}

	#[test]
	fn sweep_persists_all_kinds_and_versions_together() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(3)));
		let row_kind = EntryKind::Source(ShapeId::Table(TableId(33)));
		let dict_kind = EntryKind::Multi;
		let row_key = ek("row-referencing-id-9");
		let dict_key = ek("dictionary-entry-9");
		write(&actor.commit, row_kind, &row_key, 3, "id=9");
		write(&actor.commit, dict_kind, &dict_key, 2, "entry-9");

		actor.sweep(CommitVersion(3));

		assert_eq!(
			actor.persistent.get(row_kind, row_key.as_ref(), CommitVersion(3)).unwrap().value().as_deref(),
			Some(b"id=9".as_slice()),
			"the row write must be durable after the sweep"
		);
		assert_eq!(
			actor.persistent
				.get(dict_kind, dict_key.as_ref(), CommitVersion(3))
				.unwrap()
				.value()
				.as_deref(),
			Some(b"entry-9".as_slice()),
			"the dictionary write committed at an earlier version must be durable in the same sweep"
		);
		assert!(
			matches!(
				actor.commit.get(row_kind, row_key.as_ref(), CommitVersion(3)).unwrap(),
				VersionedGetResult::NotFound
			),
			"a persisted row write must be drained from the buffer"
		);
		assert!(
			matches!(
				actor.commit.get(dict_kind, dict_key.as_ref(), CommitVersion(3)).unwrap(),
				VersionedGetResult::NotFound
			),
			"a persisted dictionary write must be drained from the buffer"
		);
	}

	#[test]
	fn flush_all_persists_latest_tombstone_above_watermark() {
		let (actor, _guard) = build_actor(Arc::new(AllPersistent), Some(CommitVersion(1)));
		let kind = EntryKind::Source(ShapeId::Table(TableId(102)));
		let key = ek("k");
		write(&actor.commit, kind, &key, 5, "v5");
		actor.commit.set(CommitVersion(9), HashMap::from([(kind, vec![(key.clone(), None)])])).unwrap();

		actor.sweep(CommitVersion(u64::MAX));

		assert!(
			matches!(
				actor.persistent.get(kind, key.as_ref(), CommitVersion(u64::MAX)).unwrap(),
				VersionedGetResult::Tombstone
			),
			"a delete committed above the watermark must persist as a tombstone, not resurrect"
		);
	}
}