Cotton code
94 lines · 3.7 KB · RustRaw
//! Which held databases have a schema move under way in this process — the//! one record every reader asks (passivemove rule 1, 18 Oct 2026).//!//! A copy stamped below this build opens AS IT IS once the move's first step//! has run, and its other tables move in the background, one per transaction//! (`cotton_store::shape_move::move_step`). While they do, a read that fails//! over one of them answers the typed `moving` line, a sync pass leaves the//! database alone, and Me counts what is left. The record is written by the//! heal that begins a move and by every step after it, and a database whose//! move is done leaves it.use std::collections::BTreeMap;use std::sync::Mutex;use cotton_store::shape_move::MoveLeft;static UNDER_WAY: Mutex<BTreeMap<String, MoveLeft>> = Mutex::new(BTreeMap::new());/// Every database with a move to make in this run, in the order it was/// first heard of — the open one first — and whether its move is done.static THIS_RUN: Mutex<Vec<(String, bool)>> = Mutex::new(Vec::new());fn this_run() -> std::sync::MutexGuard<'static, Vec<(String, bool)>> {    THIS_RUN.lock().unwrap_or_else(|poisoned| poisoned.into_inner())}/// Say where `workspace_id`'s move stands; `None` when it has none.pub fn note(workspace_id: &str, left: Option<MoveLeft>) {    let mut under_way = UNDER_WAY.lock().unwrap_or_else(|poisoned| poisoned.into_inner());    match left {        Some(left) => {            under_way.insert(workspace_id.to_string(), left);            expect(workspace_id);        }        None => {            under_way.remove(workspace_id);            if let Some(entry) = this_run().iter_mut().find(|(id, _)| id == workspace_id) {                entry.1 = true;            }        }    }}/// `workspace_id` has a move to make in this run: its copy is another/// build's and is opened for its move in turn.pub fn expect(workspace_id: &str) {    let mut run = this_run();    if !run.iter().any(|(id, _)| id == workspace_id) {        run.push((workspace_id.to_string(), false));    }}/// `workspace_id`'s move will not be made in this run after all (its copy/// did not open): it is no longer counted.pub fn give_up(workspace_id: &str) {    if left(workspace_id).is_none() {        this_run().retain(|(id, done)| id != workspace_id || *done);    }}/// Where this run's moves stand, while any is left: the database moving now/// (the first heard of whose move is not done), how many databases' moves are/// done, and how many there are in all. `None` once every one is done.pub fn progress() -> Option<Progress> {    let run = this_run();    let now = run.iter().find(|(_, done)| !done)?.0.clone();    Some(Progress { now, moved: run.iter().filter(|(_, done)| *done).count(), of: run.len() })}/// Every database with a move to make in this run, in the order it was/// first heard of: its id, whether its move is done, and — while it is under/// way — where it stands.pub fn this_run_list() -> Vec<(String, bool, Option<MoveLeft>)> {    let run = this_run().clone();    run.into_iter().map(|(id, done)| { let left = left(&id); (id, done, left) }).collect()}/// [`progress`]'s answer: typed counts, never a sentence.#[derive(Debug, Clone, PartialEq, Eq)]pub struct Progress {    pub now: String,    pub moved: usize,    pub of: usize,}/// Where `workspace_id`'s move stands, if one is under way.pub fn left(workspace_id: &str) -> Option<MoveLeft> {    UNDER_WAY.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).get(workspace_id).cloned()}/// Every database with a move under way, by id.pub fn all() -> Vec<(String, MoveLeft)> {    UNDER_WAY.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).iter().map(|(id, left)| (id.clone(), left.clone())).collect()}