allsource-core 0.29.0

High-performance event store core built in Rust
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
use super::EventStore;
use crate::{
    application::dto::EventDto,
    domain::entities::Event,
    error::{AllSourceError, Result},
    infrastructure::{
        hash_state::{
            FastMap, FastSet, fast_map, fast_map_with_capacity, fast_set, fast_set_with_capacity,
        },
        memory::ledger::{Owner, Tally},
        observability::heap::hash_entry_bytes,
        persistence::{
            archive_budget::ArchiveReadBudget,
            feed_log::{Appended, FeedLog, FeedLogRecord, FeedLogScope},
        },
    },
};
use serde::{Deserialize, Serialize};
use std::{
    collections::BTreeMap,
    mem::size_of,
    path::{Path, PathBuf},
    sync::{Arc, OnceLock},
};
use uuid::Uuid;

pub const MODEL_EVENT: &str = "allsource.read_model.committed";
pub const FEED_BYTES: usize = 512 * 1024;
const SCAN_WINDOW: usize = 10_000;
pub const DURABLE_LOG_ENV: &str = "ALLSOURCE_FEED_DURABLE_LOG";
/// Under the WAL directory.
pub const FEED_LOG_DIR: &str = "feed-log";

/// `ALLSOURCE_FEED_DURABLE_LOG` (default off, syntax in
/// [`FeedLogScope::parse`]) keeps the named tenants' feed epoch and positions
/// in a log, so a restart does not renumber them.
pub(super) fn open_feed_log(wal_dir: Option<&Path>, read_only: bool) -> Option<Arc<FeedLog>> {
    let scope = FeedLogScope::parse(std::env::var(DURABLE_LOG_ENV).ok().as_deref())?;
    let Some(wal_dir) = wal_dir.filter(|_| !read_only) else {
        tracing::warn!(
            "{DURABLE_LOG_ENV} needs a writable store with a WAL; feed positions stay per process"
        );
        return None;
    };
    let dir = wal_dir.join(FEED_LOG_DIR);
    match FeedLog::open(dir.clone(), scope) {
        Ok(log) => Some(Arc::new(log)),
        Err(error) => {
            tracing::error!(
                "Durable feed log unavailable at {}: {error}; feed positions stay per process",
                dir.display()
            );
            None
        }
    }
}

/// `ALLSOURCE_FEED_NARROW_INDEX=0` restores the feed that hydrates the whole
/// tenant on every call.
fn narrow_feed_enabled() -> bool {
    static ENABLED: OnceLock<bool> = OnceLock::new();
    *ENABLED.get_or_init(|| {
        std::env::var("ALLSOURCE_FEED_NARROW_INDEX").map_or(true, |value| value.trim() != "0")
    })
}

#[derive(Clone)]
pub(super) enum Locator {
    Memory,
    Archive { file: Arc<PathBuf>, row: u64 },
}

struct FeedEntry {
    id: Uuid,
    event_type: Arc<str>,
    entity_id: Arc<str>,
    at: Locator,
}

pub(super) struct FeedIndex {
    epoch: u64,
    events: Vec<FeedEntry>,
    ids: FastSet<Uuid>,
    /// Built from the tenant's whole archive plus memory, so it numbers every
    /// event without the tenant being resident.
    narrow: bool,
    /// Mirrors the tenant's [`FeedLog`] record for record: same epoch, same
    /// positions.
    durable: bool,
    /// Released when the tenant's feed is dropped.
    charged: Tally,
}

impl Default for FeedIndex {
    fn default() -> Self {
        Self {
            epoch: Uuid::new_v4().as_u64_pair().0 & ((1_u64 << 52) - 1),
            events: Vec::new(),
            ids: fast_set(),
            narrow: false,
            durable: false,
            charged: Tally::new(Owner::FeedIndex),
        }
    }
}

/// Bytes a feed entry holds beyond its slot in `events`: the two `Arc<str>`
/// allocations and its id in the set.
fn feed_entry_owned_bytes(entry: &FeedEntry) -> u64 {
    const ARC_HEADER: usize = 2 * size_of::<usize>();
    (2 * ARC_HEADER
        + entry.event_type.len()
        + entry.entity_id.len()
        + hash_entry_bytes::<Uuid, ()>()) as u64
}

fn push_entry(feed: &mut FeedIndex, entry: FeedEntry) {
    let before = feed.events.capacity();
    let owned = feed_entry_owned_bytes(&entry);
    feed.events.push(entry);
    feed.charged
        .grow(before, feed.events.capacity(), size_of::<FeedEntry>());
    feed.charged.add(owned);
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct FeedCursor {
    pub epoch: u64,
    pub position: usize,
}

#[derive(Debug, Serialize)]
pub struct FeedPage {
    pub tenant_id: String,
    pub cursor: FeedCursor,
    pub head: FeedCursor,
    pub events: Vec<EventDto>,
    pub positions: Vec<usize>,
    pub caught_up: bool,
}

pub(super) struct Candidate {
    pub(super) position: usize,
    pub(super) id: Uuid,
    pub(super) at: Locator,
}

struct Scan {
    epoch: u64,
    head: usize,
    start: FeedCursor,
    candidates: Vec<Candidate>,
    stop: usize,
}

/// Positions past `visible` are not served yet.
fn scan_feed(
    feed: &FeedIndex,
    visible: usize,
    cursor: Option<FeedCursor>,
    limit: usize,
    prefixes: &[String],
    entity: Option<&str>,
) -> Result<Scan> {
    let epoch = feed.epoch;
    let head = feed.events.len().min(visible);
    let start = cursor.unwrap_or(FeedCursor { epoch, position: 0 });
    if start.epoch != epoch || start.position > head {
        return Err(AllSourceError::VersionConflict {
            expected: start.epoch,
            current: epoch,
        });
    }
    let mut candidates = Vec::new();
    let mut position = start.position;
    if limit > 0 {
        let scan_end = head.min(start.position.saturating_add(SCAN_WINDOW));
        while position < scan_end {
            let entry = &feed.events[position];
            position += 1;
            if entity.is_none_or(|entity| &*entry.entity_id == entity)
                && (prefixes.is_empty()
                    || prefixes
                        .iter()
                        .any(|prefix| entry.event_type.starts_with(prefix.as_str())))
            {
                candidates.push(Candidate {
                    position: position - 1,
                    id: entry.id,
                    at: entry.at.clone(),
                });
                if candidates.len() == limit {
                    break;
                }
            }
        }
    }
    Ok(Scan {
        epoch,
        head,
        start,
        candidates,
        stop: position,
    })
}

impl EventStore {
    fn durable_feed_log(&self, tenant: &str) -> Option<&FeedLog> {
        self.feed_log.as_deref().filter(|log| log.includes(tenant))
    }

    pub(super) fn observe_feed_head(&self, event: &Event, _position: usize) {
        if event.event_type_str() == MODEL_EVENT {
            return;
        }
        if let Some(log) = self.durable_feed_log(event.tenant_id_str()) {
            self.observe_durable_feed(log, event);
            return;
        }
        let tenant = event.tenant_id_str();
        self.feed_indexes_charge.ensure(
            &self.feed_indexes,
            tenant,
            || tenant.to_owned(),
            FeedIndex::default,
            tenant.len() as u64,
        );
        let mut feed = self.feed_indexes.entry(tenant.to_owned()).or_default();
        if feed.ids.insert(event.id) {
            push_entry(
                &mut feed,
                FeedEntry {
                    id: event.id,
                    event_type: Arc::from(event.event_type_str()),
                    entity_id: Arc::from(event.entity_id_str()),
                    at: Locator::Memory,
                },
            );
        }
    }

    /// Drop a tenant's feed and release its key. The feed's own bytes go with
    /// its [`Tally`].
    pub(super) fn remove_feed_index(&self, tenant: &str) {
        if self.feed_indexes.remove(tenant).is_some() {
            self.feed_indexes_charge.release_owned(tenant.len() as u64);
        }
    }

    fn feed_entry_for(&self, tenant: &str) -> dashmap::mapref::one::RefMut<'_, String, FeedIndex> {
        self.feed_indexes_charge.ensure(
            &self.feed_indexes,
            tenant,
            || tenant.to_owned(),
            FeedIndex::default,
            tenant.len() as u64,
        );
        self.feed_indexes.entry(tenant.to_owned()).or_default()
    }

    fn insert_feed_index(&self, tenant: &str, feed: FeedIndex) {
        self.feed_indexes_charge.ensure(
            &self.feed_indexes,
            tenant,
            || tenant.to_owned(),
            FeedIndex::default,
            tenant.len() as u64,
        );
        self.feed_indexes.insert(tenant.to_owned(), feed);
    }

    pub(super) fn rebuild_tenant_feed(&self, tenant: &str, events: &[Event]) {
        self.remove_feed_index(tenant);
        for event in events
            .iter()
            .filter(|event| event.tenant_id_str() == tenant)
        {
            self.observe_feed_head(event, 0);
        }
    }

    /// Called under the events write lock, as is every durable index build,
    /// so the index and the log take records in the same order.
    fn observe_durable_feed(&self, log: &FeedLog, event: &Event) {
        let tenant = event.tenant_id_str();
        let record = FeedLogRecord::new(event.id, event.event_type_str(), event.entity_id_str());
        match log.append(tenant, record.clone()) {
            Ok(Appended::New) => {
                if let Some(mut feed) = self.feed_indexes.get_mut(tenant)
                    && feed.durable
                {
                    feed.ids.insert(record.id);
                    push_entry(
                        &mut feed,
                        FeedEntry {
                            id: record.id,
                            event_type: record.event_type,
                            entity_id: record.entity_id,
                            at: Locator::Memory,
                        },
                    );
                }
            }
            Ok(Appended::Duplicate | Appended::NotOpen) => {}
            Err(error) => {
                tracing::error!("Feed log append failed for event {}: {error}", event.id);
                // The next read rebuilds the index and re-appends what the log lacks.
                self.remove_feed_index(tenant);
            }
        }
    }

    /// Make every pending feed-log record durable. Runs before a checkpoint
    /// retires WAL segments and before boot recovery truncates the WAL, so
    /// the order the log gave recovered events is not lost with them.
    pub(super) fn sync_feed_log(&self) -> Result<()> {
        match &self.feed_log {
            Some(log) => self.publish_feed_log(log, None),
            None => Ok(()),
        }
    }

    /// The WAL is fsynced first so that no visible position outlives the
    /// event it names.
    fn publish_feed_log(&self, log: &FeedLog, tenant: Option<&str>) -> Result<()> {
        let pending = match tenant {
            Some(tenant) => log.has_pending(tenant),
            None => log.has_any_pending(),
        };
        if !pending {
            return Ok(());
        }
        if let Some(wal) = &self.wal {
            wal.sync()?;
        }
        match tenant {
            Some(tenant) => log.sync_tenant(tenant),
            None => log.sync_all(),
        }
    }

    fn ensure_durable_feed(&self, tenant: &str, log: &FeedLog) -> Result<()> {
        if !self
            .feed_indexes
            .get(tenant)
            .is_some_and(|feed| feed.durable)
        {
            self.build_durable_feed(tenant, log)?;
        }
        self.publish_feed_log(log, Some(tenant))
    }

    /// Index the tenant's log. A tenant without one is backfilled from the
    /// archive in path order plus memory, as the narrow index numbers it; a
    /// tenant with one gets any event the log lacks appended at the end, so
    /// no served position moves.
    fn build_durable_feed(&self, tenant: &str, log: &FeedLog) -> Result<()> {
        let _resident = self.cache_residency_gate.read();
        let keys = match &self.storage {
            Some(storage) => {
                let mut budget = ArchiveReadBudget::new(self.strict_archive_limits.clone());
                storage.read().scan_feed_keys(tenant, &mut budget)?
            }
            None => Vec::new(),
        };
        let events = self.events.write();
        if self
            .feed_indexes
            .get(tenant)
            .is_some_and(|feed| feed.durable)
        {
            return Ok(());
        }
        let mut names: FastMap<String, Arc<str>> = fast_map();
        let mut intern = |value: String| -> Arc<str> {
            names
                .entry(value)
                .or_insert_with_key(|value| Arc::from(value.as_str()))
                .clone()
        };
        let mut located: FastMap<Uuid, Locator> = fast_map_with_capacity(keys.len());
        let mut seen = fast_set_with_capacity(keys.len());
        let mut history = Vec::new();
        for key in keys {
            if key.event_type == MODEL_EVENT {
                continue;
            }
            located.entry(key.id).or_insert(Locator::Archive {
                file: key.file,
                row: key.row,
            });
            if seen.insert(key.id) {
                history.push(FeedLogRecord {
                    id: key.id,
                    event_type: intern(key.event_type),
                    entity_id: intern(key.entity_id),
                });
            }
        }
        for event in events.iter().filter(|event| {
            event.tenant_id_str() == tenant && event.event_type_str() != MODEL_EVENT
        }) {
            if seen.insert(event.id) {
                history.push(FeedLogRecord {
                    id: event.id,
                    event_type: intern(event.event_type_str().to_owned()),
                    entity_id: intern(event.entity_id_str().to_owned()),
                });
            }
        }
        if log.is_open(tenant) {
            let mut appended = 0_usize;
            for record in history {
                if log.append(tenant, record)? == Appended::New {
                    appended += 1;
                }
            }
            if appended > 0 {
                tracing::warn!("Feed log lacked {appended} archived or resident events; appended");
            }
        } else {
            if let Some(wal) = &self.wal {
                wal.sync()?;
            }
            log.create(tenant, &history)?;
        }
        let (epoch, records) = log
            .records(tenant)
            .ok_or_else(|| AllSourceError::StorageError("feed log is not open; retry".into()))?;
        let mut feed = FeedIndex {
            epoch,
            events: Vec::with_capacity(records.len()),
            ids: fast_set_with_capacity(records.len()),
            narrow: true,
            durable: true,
            charged: Tally::new(Owner::FeedIndex),
        };
        for record in records {
            feed.ids.insert(record.id);
            push_entry(
                &mut feed,
                FeedEntry {
                    at: located.get(&record.id).cloned().unwrap_or(Locator::Memory),
                    id: record.id,
                    event_type: record.event_type,
                    entity_id: record.entity_id,
                },
            );
        }
        self.insert_feed_index(tenant, feed);
        drop(events);
        Ok(())
    }

    /// Number the tenant's whole history from the archive's id, type and
    /// entity columns plus the events still only in memory. The epoch is new:
    /// positions from any earlier index of this tenant do not carry over.
    fn ensure_narrow_feed(&self, tenant: &str) -> Result<()> {
        if self
            .feed_indexes
            .get(tenant)
            .is_some_and(|feed| feed.narrow)
        {
            return Ok(());
        }
        let _resident = self.cache_residency_gate.read();
        let keys = match &self.storage {
            Some(storage) => {
                let mut budget = ArchiveReadBudget::new(self.strict_archive_limits.clone());
                storage.read().scan_feed_keys(tenant, &mut budget)?
            }
            None => Vec::new(),
        };
        // Ingest observes the feed under the events write lock, so holding it
        // makes the swap atomic with every concurrent append.
        let events = self.events.write();
        if self
            .feed_indexes
            .get(tenant)
            .is_some_and(|feed| feed.narrow)
        {
            return Ok(());
        }
        let mut names: FastMap<String, Arc<str>> = fast_map();
        let mut intern = |value: String| -> Arc<str> {
            names
                .entry(value)
                .or_insert_with_key(|value| Arc::from(value.as_str()))
                .clone()
        };
        let mut feed = FeedIndex {
            narrow: true,
            ..FeedIndex::default()
        };
        for key in keys {
            if key.event_type == MODEL_EVENT || !feed.ids.insert(key.id) {
                continue;
            }
            push_entry(
                &mut feed,
                FeedEntry {
                    id: key.id,
                    event_type: intern(key.event_type),
                    entity_id: intern(key.entity_id),
                    at: Locator::Archive {
                        file: key.file,
                        row: key.row,
                    },
                },
            );
        }
        for event in events.iter().filter(|event| {
            event.tenant_id_str() == tenant && event.event_type_str() != MODEL_EVENT
        }) {
            if feed.ids.insert(event.id) {
                push_entry(
                    &mut feed,
                    FeedEntry {
                        id: event.id,
                        event_type: intern(event.event_type_str().to_owned()),
                        entity_id: intern(event.entity_id_str().to_owned()),
                        at: Locator::Memory,
                    },
                );
            }
        }
        self.insert_feed_index(tenant, feed);
        drop(events);
        Ok(())
    }

    /// Epoch changes force a rebuild instead of silently skipping reordered history.
    pub fn tenant_feed(
        &self,
        tenant: &str,
        cursor: Option<FeedCursor>,
        limit: usize,
    ) -> Result<FeedPage> {
        self.tenant_feed_filtered(tenant, cursor, limit, &[])
    }

    pub fn tenant_feed_filtered(
        &self,
        tenant: &str,
        cursor: Option<FeedCursor>,
        limit: usize,
        prefixes: &[String],
    ) -> Result<FeedPage> {
        self.tenant_feed_scoped(tenant, cursor, limit, prefixes, None)
    }

    pub fn tenant_feed_scoped(
        &self,
        tenant: &str,
        cursor: Option<FeedCursor>,
        limit: usize,
        prefixes: &[String],
        entity: Option<&str>,
    ) -> Result<FeedPage> {
        if limit > 500 {
            return Err(AllSourceError::InvalidInput(
                "feed limit must be 0..500".into(),
            ));
        }
        let durable = self.durable_feed_log(tenant);
        if let Some(log) = durable {
            self.ensure_durable_feed(tenant, log)?;
        } else if narrow_feed_enabled() {
            self.ensure_narrow_feed(tenant)?;
        } else {
            self.ensure_tenant_loaded_with_integrity(tenant, true)?;
            if self.storage.is_some() && !self.tenant_loader.is_complete(tenant) {
                return Err(AllSourceError::StorageError(
                    "feed archive certification was evicted; retry".into(),
                ));
            }
        }
        let _resident = self.cache_residency_gate.read();
        let Scan {
            epoch,
            head: head_position,
            start,
            candidates,
            stop: scan_stop,
        } = match durable {
            Some(log) => {
                let visible = log.durable_len(tenant);
                match self.feed_indexes.get(tenant) {
                    Some(feed) if feed.durable => {
                        scan_feed(&feed, visible, cursor, limit, prefixes, entity)
                    }
                    // A default index here would serve a random epoch.
                    _ => Err(AllSourceError::StorageError(
                        "feed index was evicted; retry".into(),
                    )),
                }
            }
            None => scan_feed(
                &self.feed_entry_for(tenant),
                usize::MAX,
                cursor,
                limit,
                prefixes,
                entity,
            ),
        }?;
        let head = FeedCursor {
            epoch,
            position: head_position,
        };
        if limit == 0 {
            return Ok(FeedPage {
                tenant_id: tenant.to_owned(),
                cursor: head,
                head,
                events: vec![],
                positions: vec![],
                caught_up: true,
            });
        }
        let events = self.feed_payloads(tenant, &candidates)?;
        let mut output = Vec::new();
        let mut positions = Vec::new();
        let mut bytes = 1024;
        let mut cursor_position = scan_stop.max(start.position);
        for (candidate, event) in candidates.iter().zip(events) {
            let dto = EventDto::from(&event);
            let size = serde_json::to_vec(&dto)?.len() + 24;
            if bytes + size > FEED_BYTES {
                if output.is_empty() {
                    return Err(AllSourceError::InvalidInput(
                        "single feed event exceeds bounded feed response".into(),
                    ));
                }
                cursor_position = candidate.position;
                break;
            }
            bytes += size;
            output.push(dto);
            positions.push(candidate.position + 1);
        }
        Ok(FeedPage {
            tenant_id: tenant.to_owned(),
            cursor: FeedCursor {
                epoch,
                position: cursor_position,
            },
            head,
            events: output,
            positions,
            caught_up: cursor_position >= head_position,
        })
    }

    /// Payloads for `candidates`, in order. If an archive file moved under the
    /// index (compaction or repack), locators are remapped from a fresh narrow
    /// scan once; positions do not change.
    fn feed_payloads(&self, tenant: &str, candidates: &[Candidate]) -> Result<Vec<Event>> {
        if let Some(events) = self.read_feed_payloads(tenant, candidates)? {
            return Ok(events);
        }
        let remapped = self.remap_feed_locators(tenant, candidates)?;
        self.read_feed_payloads(tenant, &remapped)?.ok_or_else(|| {
            self.remove_feed_index(tenant);
            AllSourceError::StorageError("feed archive changed; retry".into())
        })
    }

    /// `None` when a locator no longer resolves.
    pub(super) fn read_feed_payloads(
        &self,
        tenant: &str,
        candidates: &[Candidate],
    ) -> Result<Option<Vec<Event>>> {
        let mut found: FastMap<Uuid, Event> = fast_map_with_capacity(candidates.len());
        // Path order fixes which error is reported when several files fail.
        let mut by_file: BTreeMap<Arc<PathBuf>, Vec<u64>> = BTreeMap::new();
        {
            let events = self.events.read();
            for candidate in candidates {
                match &candidate.at {
                    Locator::Memory => {
                        let event = self
                            .index
                            .get_by_id(&candidate.id)
                            .and_then(|offset| events.get(offset))
                            .filter(|event| event.id == candidate.id);
                        match event {
                            Some(event) => {
                                found.insert(candidate.id, event.clone());
                            }
                            None => return Ok(None),
                        }
                    }
                    Locator::Archive { file, row } => {
                        by_file.entry(Arc::clone(file)).or_default().push(*row);
                    }
                }
            }
        }
        if !by_file.is_empty() {
            let Some(storage) = &self.storage else {
                return Ok(None);
            };
            let storage = storage.read();
            let mut budget = ArchiveReadBudget::new(self.strict_archive_limits.clone());
            for (file, mut rows) in by_file {
                rows.sort_unstable();
                rows.dedup();
                match storage.read_feed_rows(&file, &rows, tenant, &mut budget) {
                    Ok(events) => {
                        for event in events {
                            found.insert(event.id, event);
                        }
                    }
                    Err(_) if !file.exists() => return Ok(None),
                    Err(error) => return Err(error),
                }
            }
        }
        let mut ordered = Vec::with_capacity(candidates.len());
        for candidate in candidates {
            match found.remove(&candidate.id) {
                Some(event) => ordered.push(event),
                None => return Ok(None),
            }
        }
        Ok(Some(ordered))
    }

    fn remap_feed_locators(
        &self,
        tenant: &str,
        candidates: &[Candidate],
    ) -> Result<Vec<Candidate>> {
        let keys = match &self.storage {
            Some(storage) => {
                let mut budget = ArchiveReadBudget::new(self.strict_archive_limits.clone());
                storage.read().scan_feed_keys(tenant, &mut budget)?
            }
            None => Vec::new(),
        };
        let mut located: FastMap<Uuid, Locator> = fast_map_with_capacity(keys.len());
        for key in keys {
            located.entry(key.id).or_insert(Locator::Archive {
                file: key.file,
                row: key.row,
            });
        }
        if let Some(mut feed) = self.feed_indexes.get_mut(tenant) {
            for entry in &mut feed.events {
                if let Some(at) = located.get(&entry.id) {
                    entry.at = at.clone();
                }
            }
        }
        Ok(candidates
            .iter()
            .map(|candidate| Candidate {
                position: candidate.position,
                id: candidate.id,
                at: located
                    .get(&candidate.id)
                    .cloned()
                    .unwrap_or(Locator::Memory),
            })
            .collect())
    }
}