//! 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.usesuper::{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;typeMoved = (u64, u64);typeKept<T> = Mutex<HashMap<String, (Moved, Arc<HashSet<String>>, Arc<T>)>>;/// Where a read began and what it leaves aside, on its file's bus.pub(super) structAt<'a> {pub bus: &'a crate::events::EventBus,pub moved: Moved,pub aside: &'a Arc<HashSet<String>>,}static WHOLE: std::sync::OnceLock<Kept<Whole>> = std::sync::OnceLock::new();static FRAMES: std::sync::OnceLock<Kept<Frame>> = std::sync::OnceLock::new();/// What a list says beside its rows that no page changes.pub(super) structFrame {pub counts: serde_json::Map<String, Value>,pub links: Vec<Value>,/// The past the links were said with (a calendar's "Earlier · n").pub earlier: u64,pub menu: Vec<Value>,pub groups: Option<Vec<Value>>,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<String> {if asked.handed || definition.polled == Some(true) || definition.live_read.is_some() { returnNone; }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<T>(kept: &Kept<T>, at: &At, key: &str) -> Option<Arc<T>> {if !crate::methods::shell_kept::kept_anywhere() { returnNone; }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<T: Send + Sync + 'static>(kept: &Kept<T>, at: &At, key: String, value: Arc<T>) {if !crate::methods::shell_kept::kept_anywhere() { return; }letOk(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.letmut spent: Vec<Arc<T>> = 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<HashSet<String>>, Arc<Whole>)> {if !crate::methods::shell_kept::kept_anywhere() { returnNone; }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<Arc<Whole>> { take(WHOLE.get_or_init(Default::default), at, key)}pub(super) fn keep_whole(at: &At, key: String, whole: Arc<Whole>) { keep(WHOLE.get_or_init(Default::default), at, key, whole)}pub(super) fn frame(at: &At, key: &str) -> Option<Arc<Frame>> { take(FRAMES.get_or_init(Default::default), at, key)}pub(super) fn keep_frame(at: &At, key: String, frame: Arc<Frame>) { 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)] { ifletOk(mut kept) = kept.lock() { kept.clear(); } }ifletOk(mut kept) = FRAMES.get_or_init(Default::default).lock() { kept.clear(); }}