//! Coordinates independently paginated sources and tags rows with their backend. //! Resetting a listing invalidates pending pages without mixing source cursors. use super::PageCursor; use super::PageLoadMode; use super::PickerPage; use crate::backend_id::BackendId; use std::collections::HashMap; use std::collections::HashSet; #[derive(Default)] pub(super) struct ThreadCatalog { sources: HashMap, next_request: u64, } #[derive(Default)] struct Source { cursor: Option, mode: Option, pending: Option, exhausted: bool, } pub(super) struct PageRequest { backend: BackendId, token: u64, pub(super) cursor: Option, pub(super) mode: PageLoadMode, } impl ThreadCatalog { pub(super) fn next_cursor(&self, excluded: &HashSet) -> Option { if self.sources.iter().any(|(id, source)| { !source.exhausted && source.pending.is_none() && !excluded.contains(id) }) { Some(PageCursor::Catalog) } else { self.sources .values() .any(|source| source.pending.is_some()) .then_some(PageCursor::SourcesPending) } } pub(super) fn reset(&mut self) { self.sources.clear(); } /// A slow or exhausted source does not prevent another source from loading. pub(super) fn begin_page( &mut self, backend: BackendId, initial_mode: PageLoadMode, ) -> Option { let source = self.sources.entry(backend).or_default(); if source.pending.is_some() || source.exhausted { return None; } let mode = *source.mode.get_or_insert(initial_mode); let token = self.next_request; self.next_request += 1; source.pending = Some(token); Some(PageRequest { backend, token, cursor: source.cursor.clone(), mode, }) } /// Errors retain the cursor for an explicit retry and never become empty pages. pub(super) fn finish_page( &mut self, request: PageRequest, page: std::io::Result, ) -> Option> { let source = self.sources.get_mut(&request.backend)?; if source.pending != Some(request.token) { return None; } source.pending = None; source.mode = Some(request.mode); Some(page.map(|mut page| { source.cursor = page.next_cursor.clone(); source.exhausted = source.cursor.is_none(); for row in &mut page.rows { row.backend_id = Some(request.backend); } page })) } } #[cfg(test)] #[path = "catalog_tests.rs"] mod tests;