//! What `shell.list` keeps between asks: a whole-read list's cut and the //! list's frame (its counts, links, menu, groups, title), each beside how //! often the file had moved on when it was read (`EventBus::moved`) and the //! store tables the list leaves aside (`shell_kept::aside`). It stands by //! the kept answer's own rule (`EventBus::stands`): a move naming only //! tables aside leaves it standing, so another page of the list after //! another table's write reads no row; any other move reads it again. A //! table the shell polls, or whose rows a live read dresses, moves without //! an event and is never kept. use super::{Asked, Whole}; use cotton_core::definitions::TableDefinition; use serde_json::{json, Value}; use std::collections::{HashMap, HashSet}; use std::sync::{Arc, Mutex}; /// How many lists each kind keeps before it forgets them all. const MOST: usize = 32; type Moved = (u64, u64); type Kept = Mutex>, Arc)>>; /// Where a read began and what it leaves aside, on its file's bus. pub(super) struct At<'a> { pub bus: &'a crate::events::EventBus, pub moved: Moved, pub aside: &'a Arc>, } static WHOLE: std::sync::OnceLock> = std::sync::OnceLock::new(); static FRAMES: std::sync::OnceLock> = std::sync::OnceLock::new(); /// What a list says beside its rows that no page changes. pub(super) struct Frame { pub counts: serde_json::Map, pub links: Vec, /// The past the links were said with (a calendar's "Earlier ยท n"). pub earlier: u64, pub menu: Vec, pub groups: Option>, pub title: String, pub head: Value, pub plus: Value, } /// The ask's key: everything it names but the page (`cursor`, `limit`) /// and the held rows of a pick, with the reader's zone and day. None for an /// ask whose rows a shell's source handed, and for a table that moves with /// no event (polled, or dressed by a live read). pub(super) fn key(asked: &Asked, definition: &TableDefinition, today: &str) -> Option { if asked.handed || definition.polled == Some(true) || definition.live_read.is_some() { return None; } Some(json!({ "table": asked.table, "view": asked.view, "where": asked.filter, "query": asked.query, "project": asked.project, "archived": asked.archived, "section": asked.section, "pick": asked.pick.is_some(), "zone": asked.zone, "today": today, }).to_string()) } fn take(kept: &Kept, at: &At, key: &str) -> Option> { if !crate::methods::shell_kept::kept_anywhere() { return None; } let kept = kept.lock().ok()?; kept.get(&keyed(at.moved, key)).filter(|(moved, aside, _)| at.bus.stands(*moved, aside)).map(|(_, _, value)| Arc::clone(value)) } fn keep(kept: &Kept, at: &At, key: String, value: Arc) { if !crate::methods::shell_kept::kept_anywhere() { return; } let Ok(mut kept) = kept.lock() else { return }; // What a move since has spent, this file's; past the cap, everything. // It is let go on another thread: a cut over a book frees a book's rows. let mut spent: Vec> = Vec::new(); kept.retain(|_, (moved, aside, value)| moved.0 != at.moved.0 || at.bus.stands(*moved, aside) || { spent.push(Arc::clone(value)); false }); if kept.len() >= MOST { spent.extend(kept.drain().map(|(_, (_, _, value))| value)); } kept.insert(keyed(at.moved, &key), (at.moved, Arc::clone(at.aside), value)); drop(kept); if !spent.is_empty() { super::let_go(spent); } } /// The key under the bus it was read on: two handles never share a read. fn keyed(moved: Moved, ask: &str) -> String { format!("{}:{ask}", moved.0) } /// The whole cut this ask kept on this file whether or not it stands, /// with where it was read and what it left aside: what a patch starts from. pub(super) fn stale_whole(at: &At, key: &str) -> Option<(Moved, Arc>, Arc)> { if !crate::methods::shell_kept::kept_anywhere() { return None; } let kept = WHOLE.get_or_init(Default::default).lock().ok()?; kept.get(&keyed(at.moved, key)).map(|(moved, aside, whole)| (*moved, Arc::clone(aside), Arc::clone(whole))) } pub(super) fn whole(at: &At, key: &str) -> Option> { take(WHOLE.get_or_init(Default::default), at, key) } pub(super) fn keep_whole(at: &At, key: String, whole: Arc) { keep(WHOLE.get_or_init(Default::default), at, key, whole) } pub(super) fn frame(at: &At, key: &str) -> Option> { take(FRAMES.get_or_init(Default::default), at, key) } pub(super) fn keep_frame(at: &At, key: String, frame: Arc) { keep(FRAMES.get_or_init(Default::default), at, key, frame) } /// Everything kept, forgotten: a bench's first ask. #[cfg(test)] pub(super) fn forget() { for kept in [WHOLE.get_or_init(Default::default)] { if let Ok(mut kept) = kept.lock() { kept.clear(); } } if let Ok(mut kept) = FRAMES.get_or_init(Default::default).lock() { kept.clear(); } }