~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
1568 lines · 59.7 KB Raw History
//! Every account, by DID, handle, email and password reset token, each
//! loaded on its first use (`spec/STORE.md`, "Scale"): a start makes no
//! `Account` but those the service log marks (`cache::Eager`).
//!
//! What is kept of the accounts is the service checkpoint, a table
//! (`compact`), and the service log after it. A start reads the table's
//! summary and its entries that are not one account's, and replays the log:
//! an entry that is not one account's is applied, and one that is, is not.
//! Where its frame is, is noted under its DID, with what it changes of the
//! table's hints (`Overlay`): a new account's handle, email and ordinal, an
//! email changed, a handle a mark gives, a password reset token, a
//! deletion. So a start holds, of the log, positions and a few short
//! strings.
//!
//! **Loading** an account (`Service::load`): its row of the table, if it
//! has one, then its entries in the frames noted, read again from the log,
//! all replayed (`compact::One`). That makes its `Account`, its sessions
//! and app passwords (`sessions::Sessions`), its preferences
//! (`preferences::Preferences`) and its password reset token
//! (`email::Resets`). Loaded, it stays, and every change to it is made in
//! memory as it is logged: so nothing is noted of what this run logs. The
//! loads of one account run one at a time, so two requests for it make one
//! `Account`; those of other accounts run beside it, each reading its own
//! entries as the others read theirs, as many as come at once (`LOADING`,
//! `spec/STORE.md`, "The first request to a cold account").
//! What cannot be parsed stops the server (`cache::unrecoverable`).
//!
//! **Lookups.** By DID: the accounts loaded, or else a load. By handle,
//! email or reset token: the accounts loaded, or else the accounts the
//! overlay and the table hint at are loaded, and the accounts loaded are
//! asked again: a hint only says where to look, and an account has what its
//! entries say. Whether a handle or an email is taken is asked the same
//! way, so it sees every account, loaded or not.
//!
//! **A compaction** of the service log puts its table in place with the
//! directory held (`Accounts::install`): from then on the new table is
//! read, and what was noted of the logs it covers is forgotten.
//!
//! **Order.** Each account has an ordinal, its place in the order accounts
//! were made: the table's `O` entries, then those of the accounts made
//! since (`Directory::order`), given as the account's entry is logged.
//! `listRepos` lists in that order, and its cursor is an ordinal.
//!
//! **Directories of no account** in `repos/`, of an account whose making
//! was cut short or whose deletion was, are removed by `Accounts::sweep`,
//! which the server runs once it serves: a start lists nothing.

use crate::compact::{self, One, ServiceTable};
use crate::log::ACCOUNTS_LOG;
use crate::{Account, Locked, Service, email, read, write};
use pds_store::table::BlockCache;
use pds_store::{Layout, Pos};
use serde_json::Value as Json;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::Path;
use std::sync::{Arc, Mutex, PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard};

/// What a start notes of one account's entries in the service log after
/// its checkpoint.
#[derive(Default)]
struct Noted {
    /// Where the frames that hold its entries are, in order.
    frames: Vec<Pos>,
    /// Whether the log holds its `account` entry: it was made since the checkpoint.
    made: bool,
    /// The generation of the log that deleted it, if one did: its row of a
    /// table of that generation or before does not count.
    void: Option<u64>,
    /// Whether it is deleted now: not made again since.
    deleted: bool,
}

/// What a start notes of the service log after its checkpoint: of each
/// account it names, where its entries are, and the hints they change,
/// each with the generation of the log that gave it.
#[derive(Default)]
struct Overlay {
    accounts: HashMap<String, Noted>,
    /// The account each handle, email and password reset token was last given to.
    handles: HashMap<String, (String, u64)>,
    emails: HashMap<String, (String, u64)>,
    tokens: HashMap<String, (String, u64)>,
}

impl Overlay {
    fn hints(&self, section: u8) -> &HashMap<String, (String, u64)> {
        match section {
            compact::HANDLE => &self.handles,
            compact::EMAIL => &self.emails,
            _ => &self.tokens,
        }
    }

    /// Forget what was noted of the logs before `generation`, which a table
    /// of that generation covers.
    fn forget(&mut self, generation: u64) {
        self.accounts.retain(|_, n| {
            n.frames.retain(|p| p.generation >= generation);
            if n.void.is_some_and(|g| g < generation) {
                n.void = None;
                if n.deleted {
                    return false;
                }
            }
            !n.frames.is_empty() || n.void.is_some()
        });
        for hints in [&mut self.handles, &mut self.emails, &mut self.tokens] {
            hints.retain(|_, (_, g)| *g >= generation);
        }
    }
}

/// The accounts loaded, by DID, handle and email, and where the rest are.
pub(crate) struct Directory {
    /// The accounts loaded.
    pub by_did: HashMap<String, Arc<Account>>,
    /// The handle and the email of each account loaded.
    pub handles: HashMap<String, String>,
    pub emails: HashMap<String, String>,
    /// The handles and emails of accounts being created, and the handles
    /// of changes in flight.
    pub claimed_handles: HashSet<String>,
    pub claimed_emails: HashSet<String>,
    /// The DIDs of accounts moving here, being created (`spec/MIGRATION.md`).
    pub claimed_dids: HashSet<String>,
    /// The DIDs of accounts deleted since the server started.
    pub deleted: HashSet<String>,
    /// The DIDs of accounts being made: each has a directory in `repos/`
    /// before it is an account, and may have an ordinal.
    making: HashSet<String>,
    /// The ordinal of each account made since the table, in the logs a
    /// start replayed and since.
    order: BTreeMap<u64, String>,
    next_ordinal: u64,
    /// How many accounts there are.
    count: u64,
    table: Option<ServiceTable>,
    overlay: Overlay,
    layout: Layout,
    /// How many rows of the table were read.
    #[cfg(test)]
    pub rows_read: std::sync::atomic::AtomicU64,
}

impl Directory {
    /// The directory of a start: no account loaded, `table` the service
    /// checkpoint, if there is one, and nothing noted of the log yet.
    pub fn new(layout: Layout, table: Option<ServiceTable>) -> Directory {
        let (next_ordinal, count) = table
            .as_ref()
            .map_or((0, 0), |t| (t.summary.next_ordinal, t.summary.accounts));
        Directory {
            by_did: HashMap::new(),
            handles: HashMap::new(),
            emails: HashMap::new(),
            claimed_handles: HashSet::new(),
            claimed_emails: HashSet::new(),
            claimed_dids: HashSet::new(),
            deleted: HashSet::new(),
            making: HashSet::new(),
            order: BTreeMap::new(),
            next_ordinal,
            count,
            table,
            overlay: Overlay::default(),
            layout,
            #[cfg(test)]
            rows_read: Default::default(),
        }
    }

    pub fn table(&self) -> Option<&ServiceTable> {
        self.table.as_ref()
    }

    /// How many accounts there are: those `listRepos` lists.
    pub fn count(&self) -> u64 {
        self.count
    }

    /// The ordinal the next account made is given: past every cursor.
    pub fn next_ordinal(&self) -> u64 {
        self.next_ordinal
    }

    // ---- What a start notes --------------------------------------------------

    /// Note an entry of the account `did` in the frame at `pos` of the log
    /// a start replays: an error if it makes an account the log made
    /// already, or is of a DID that is no account, as far as that is known
    /// with no row read (`may_be`).
    pub fn note(&mut self, pos: Pos, did: &str, e: &Json) -> Result<(), String> {
        let noted = self.overlay.accounts.get(did);
        if e["t"] == "account" {
            if noted.is_some_and(|n| n.made && !n.deleted) {
                return Err(format!("a second account with the DID of {did}"));
            }
        } else if !noted.is_some_and(|n| n.deleted) && !self.may_be(did) {
            return Err(format!(
                "a {} entry of {did:?}, which no account has",
                e["t"]
            ));
        }
        let Overlay {
            accounts,
            handles,
            emails,
            tokens,
        } = &mut self.overlay;
        let n = accounts.entry(did.to_string()).or_default();
        let hint = |hints: &mut HashMap<String, (String, u64)>, name: &Json| {
            if let Some(name) = name.as_str() {
                hints.insert(name.to_string(), (did.to_string(), pos.generation));
            }
        };
        match e["t"].as_str() {
            Some("account") => {
                n.made = true;
                n.deleted = false;
                self.order.insert(self.next_ordinal, did.to_string());
                self.next_ordinal += 1;
                self.count += 1;
                hint(handles, &e["handle"]);
                hint(emails, &e["email"]);
            }
            // Nothing is kept of an account deleted.
            _ if n.deleted => return Ok(()),
            Some("updateEmail") => hint(emails, &e["email"]),
            Some("mailToken") if e["purpose"] == email::Purpose::ResetPassword.name() => {
                hint(tokens, &e["token"])
            }
            Some(crate::cache::DIR) => hint(handles, &e["handle"]),
            _ => {}
        }
        if n.frames.last() != Some(&pos) {
            n.frames.push(pos);
        }
        Ok(())
    }

    /// Note that the log of `generation` a start replays deletes `did`.
    pub fn note_deleted(&mut self, did: &str, generation: u64) {
        let n = self.overlay.accounts.entry(did.to_string()).or_default();
        if !n.deleted {
            self.count = self.count.saturating_sub(1);
        }
        *n = Noted {
            void: Some(generation),
            deleted: true,
            ..Noted::default()
        };
        self.order.retain(|_, d| d != did);
    }

    /// Whether the log a start replayed, or this run, deleted `did`, and it
    /// was not made again since.
    pub fn is_noted_deleted(&self, did: &str) -> bool {
        self.overlay.accounts.get(did).is_some_and(|n| n.deleted)
    }

    /// Whether what the table holds of `did` does not count: a log it does
    /// not cover deleted the account, whether or not it was made again.
    fn is_void(&self, did: &str) -> bool {
        self.overlay
            .accounts
            .get(did)
            .is_some_and(|n| n.void.is_some())
    }

    /// Whether `did` may be an account, as the table's filter and what was
    /// noted of the log say, reading no row: false only if it is none.
    pub fn may_be(&self, did: &str) -> bool {
        if self.by_did.contains_key(did) {
            return true;
        }
        if self.deleted.contains(did) {
            return false;
        }
        match self.overlay.accounts.get(did) {
            Some(n) if n.deleted => false,
            Some(n) if n.made => true,
            n => {
                n.is_none_or(|n| n.void.is_none())
                    && self.table.as_ref().is_some_and(|t| t.may_hold(did))
            }
        }
    }

    // ---- Reading the accounts not loaded ---------------------------------------

    /// Whether `did` is an account, loaded or not, or one being made.
    fn knows(&self, did: &str) -> Result<bool, String> {
        if self.by_did.contains_key(did) || self.making.contains(did) {
            return Ok(true);
        }
        if self.deleted.contains(did) {
            return Ok(false);
        }
        match self.overlay.accounts.get(did) {
            Some(n) if n.deleted => Ok(false),
            Some(n) if n.made => Ok(true),
            n => match &self.table {
                Some(t) if n.is_none_or(|n| n.void.is_none()) => t.has(did),
                _ => Ok(false),
            },
        }
    }

    /// The account `did`, which is not loaded, as its row of the table and
    /// its entries in the frames noted leave it: none if it is no account.
    fn entries_of(&self, did: &str) -> Result<Option<One>, String> {
        if self.deleted.contains(did) {
            return Ok(None);
        }
        let noted = self.overlay.accounts.get(did);
        if noted.is_some_and(|n| n.deleted) {
            return Ok(None);
        }
        let mut one = One::default();
        let row = match &self.table {
            Some(t) if noted.is_none_or(|n| n.void.is_none()) => t.row(did)?,
            _ => None,
        };
        if let Some((_, entries)) = row {
            #[cfg(test)]
            self.rows_read
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
            for e in entries {
                one.apply(e)?;
            }
        }
        for pos in noted.map_or(&[][..], |n| &n.frames) {
            let path = self.layout.log(pos.generation);
            let entries = pds_store::read_frame(&path, pos.offset, pos.len)
                .map_err(|e| format!("{}: {e}", path.display()))?;
            for e in entries {
                if compact::of_account(&e)? == Some(did) {
                    one.apply(e)?;
                }
            }
        }
        Ok(one.is_made().then_some(one))
    }

    /// The accounts not loaded that the overlay and the table hint may have
    /// `name`, a handle, an email or a password reset token (`section`, as
    /// `compact` names them).
    fn hinted(&self, section: u8, name: &str) -> Result<Vec<String>, String> {
        let mut dids: Vec<String> = self
            .overlay
            .hints(section)
            .get(name)
            .map(|(did, _)| did.clone())
            .into_iter()
            .collect();
        if let Some(t) = &self.table {
            dids.extend(t.hints(section, name)?);
        }
        dids.retain(|did| !self.by_did.contains_key(did) && !self.deleted.contains(did));
        dids.dedup();
        Ok(dids)
    }

    /// Give `f` each account's ordinal and DID from ordinal `from` on, in
    /// the order they were made, until it answers false: the table's, then
    /// those made since, the accounts deleted passed over.
    fn each_ordered(&self, from: u64, mut f: impl FnMut(u64, &str) -> bool) -> Result<(), String> {
        if let Some(t) = &self.table {
            for r in t.order(from) {
                let (n, did) = r?;
                // An ordinal of an account deleted since is no account's:
                // made again, it has another.
                if !self.is_void(&did) && !f(n, &did) {
                    return Ok(());
                }
            }
        }
        for (n, did) in self.order.range(from..) {
            if !self.deleted.contains(did) && !f(*n, did) {
                break;
            }
        }
        Ok(())
    }

    fn fail(&self, why: &str) -> ! {
        crate::cache::unrecoverable(&self.layout.dir, why)
    }

    /// Every account's DID, in the order they were made, but those being made.
    pub fn dids(&self) -> Vec<String> {
        let mut out = Vec::with_capacity(self.count as usize);
        self.each_ordered(0, |_, did| {
            if !self.making.contains(did) {
                out.push(did.to_string());
            }
            true
        })
        .unwrap_or_else(|why| self.fail(&why));
        out
    }

    /// At most `limit` accounts from ordinal `from` on, each by its ordinal
    /// and DID, in the order they were made, stopping before one still
    /// being made.
    pub fn page(&self, from: u64, limit: usize) -> Vec<(u64, String)> {
        let mut out = Vec::new();
        self.each_ordered(from, |n, did| {
            if out.len() == limit || self.making.contains(did) {
                return false;
            }
            out.push((n, did.to_string()));
            true
        })
        .unwrap_or_else(|why| self.fail(&why));
        out
    }

    // ---- Changing it -----------------------------------------------------------

    /// An account is being made: `did` is no account yet, and its directory
    /// in `repos/` is its own.
    pub fn making(&mut self, did: &str) {
        self.making.insert(did.to_string());
    }

    /// The account `did` is being made no longer: made, or not.
    pub fn made(&mut self, did: &str) {
        self.making.remove(did);
    }

    /// The entry of the account `did`, being made, is logged: it has the
    /// next ordinal. With the directory held, so that the ordinals are in
    /// the order the service log has the accounts' entries.
    pub fn logged(&mut self, did: &str) {
        self.order.insert(self.next_ordinal, did.to_string());
        self.next_ordinal += 1;
    }

    /// An account made now, whose entry is logged: loaded from now on. One
    /// deleted before, moving here again with its DID (`spec/MIGRATION.md`),
    /// is an account again: what the table holds of it still does not count.
    pub fn insert_new(&mut self, a: Arc<Account>) {
        self.making.remove(&a.did);
        self.deleted.remove(&a.did);
        if let Some(n) = self.overlay.accounts.get_mut(&a.did) {
            n.deleted = false;
            n.made = true;
        }
        self.count += 1;
        self.hold(a);
    }

    fn hold(&mut self, a: Arc<Account>) {
        self.handles.insert(a.handle(), a.did.clone());
        if let Some(e) = &email::lock(&a.mail).email {
            self.emails.insert(e.clone(), a.did.clone());
        }
        self.by_did.insert(a.did.clone(), a);
    }

    /// An account loaded: an error if its handle or its email is another
    /// account's of those loaded, or held for one.
    pub fn insert_loaded(&mut self, a: Arc<Account>) -> Result<(), String> {
        if let Some(e) = &email::lock(&a.mail).email {
            if let Some(other) = self.emails.get(e).filter(|o| **o != a.did) {
                return Err(format!(
                    "a second account with the email of {other}, {e}: {}",
                    a.did
                ));
            }
            if self.claimed_emails.contains(e) {
                return Err(format!(
                    "{e} is the email of {} and held for another account",
                    a.did
                ));
            }
        }
        let handle = a.handle();
        if let Some(other) = self.handles.get(&handle).filter(|o| **o != a.did) {
            return Err(format!(
                "{handle} is the handle of both {other} and {}",
                a.did
            ));
        }
        if self.claimed_handles.contains(&handle) {
            return Err(format!(
                "{handle} is the handle of {} and held for another account",
                a.did
            ));
        }
        self.hold(a);
        Ok(())
    }

    /// An account deleted, as its deletion is logged, in the log of
    /// `generation`: no account from now on, its handle and its email,
    /// `email`, free, and what a table that does not cover that log holds of
    /// it, its ordinal too, counts no more.
    pub fn remove(&mut self, a: &Account, email: Option<&str>, generation: u64) {
        if self.by_did.remove(&a.did).is_some() {
            self.count = self.count.saturating_sub(1);
        }
        self.deleted.insert(a.did.clone());
        self.overlay.accounts.insert(
            a.did.clone(),
            Noted {
                void: Some(generation),
                deleted: true,
                ..Noted::default()
            },
        );
        self.order.retain(|_, d| *d != a.did);
        let handle = a.handle();
        if self.handles.get(&handle) == Some(&a.did) {
            self.handles.remove(&handle);
        }
        if let Some(e) = email
            && self.emails.get(e) == Some(&a.did)
        {
            self.emails.remove(e);
        }
    }
}

/// How many stripes the loads of accounts are of (`Accounts::loading`):
/// loads of accounts of one stripe wait for each other, so many that few
/// do, however many accounts are loaded at once, and few enough that the
/// locks cost nothing to hold.
const LOADING: usize = 64;

/// The stripe of the loads of the account `did`.
fn stripe(did: &str) -> usize {
    use std::hash::{Hash, Hasher};
    let mut h = std::hash::DefaultHasher::new();
    did.hash(&mut h);
    (h.finish() % LOADING as u64) as usize
}

/// The directory, behind its lock, and what loads the accounts into it.
pub(crate) struct Accounts {
    directory: RwLock<Directory>,
    /// Held by a load, the one of its DID's stripe (`stripe`): so that one
    /// load of an account runs at a time, and those of others beside it.
    loading: Box<[Mutex<()>]>,
    layout: Layout,
    /// The blocks read of the table, shared with the accounts' tables.
    blocks: Arc<BlockCache>,
}

impl Locked for Accounts {
    type Read<'a> = RwLockReadGuard<'a, Directory>;
    type Write<'a> = RwLockWriteGuard<'a, Directory>;

    fn read_lock(&self) -> RwLockReadGuard<'_, Directory> {
        self.directory.read_lock()
    }

    fn write_lock(&self) -> RwLockWriteGuard<'_, Directory> {
        self.directory.write_lock()
    }
}

impl Accounts {
    pub fn new(layout: Layout, blocks: Arc<BlockCache>, directory: Directory) -> Accounts {
        Accounts {
            directory: RwLock::new(directory),
            loading: (0..LOADING).map(|_| Mutex::new(())).collect(),
            layout,
            blocks,
        }
    }

    /// Put the table a compaction wrote, of `generation`, in place by
    /// `rename`, with the directory held: lookups read the table before
    /// until then and the new one after, and what a start noted of the logs
    /// the new one covers is forgotten, so that an account not loaded is
    /// read from the new table alone. The table is opened where it was
    /// written, before it is renamed: what fails, fails with the table
    /// before in place.
    pub fn install(
        &self,
        generation: u64,
        rename: &mut dyn FnMut() -> Result<(), String>,
    ) -> Result<(), String> {
        let tmp = self.layout.tmp();
        let file = std::fs::File::open(&tmp).map_err(|e| format!("{}: {e}", tmp.display()))?;
        let table = ServiceTable::open_file(&self.layout.checkpoint(), file, self.blocks.clone())?;
        if table.summary.generation != generation {
            return Err(format!(
                "{}: a table of generation {}, not {generation}",
                tmp.display(),
                table.summary.generation
            ));
        }
        let mut d = write(self);
        rename()?;
        // An account made since the table before has its ordinal in the new one.
        d.order = d.order.split_off(&table.summary.next_ordinal);
        d.overlay.forget(generation);
        d.table = Some(table);
        Ok(())
    }

    /// Remove every directory in `repos/` of no account: one whose account
    /// was being made when the server stopped, or was deleted. Not that of
    /// an account being made now. With a journal, each is forgotten there
    /// first, durably (`pds_store::Journal::forget`).
    pub fn sweep(&self, journal: Option<&pds_store::Journal>) {
        let repos = self.layout.dir.join("repos");
        let Ok(entries) = std::fs::read_dir(&repos) else {
            return;
        };
        for e in entries.flatten() {
            let name = e.file_name();
            let known = name.to_str().is_some_and(|did| {
                let d = read(self);
                d.knows(did).unwrap_or_else(|why| d.fail(&why))
            });
            if known {
                continue;
            }
            if let Some(j) = journal
                && let Err(why) = j.forget(&e.path())
            {
                crate::ops::failed(
                    "removing a directory of no account failed",
                    format!("{}: {why}", e.path().display()),
                );
                continue;
            }
            match std::fs::remove_dir_all(e.path()) {
                Err(why) if why.kind() != std::io::ErrorKind::NotFound => crate::ops::failed(
                    "removing a directory of no account failed",
                    format!("{}: {why}", e.path().display()),
                ),
                _ => {}
            }
        }
    }
}

impl Service {
    /// The account a `repo` or `did` argument names, by its DID or handle.
    pub(crate) fn account(&self, id: &str) -> Option<Arc<Account>> {
        if id.starts_with("did:") {
            self.by_did(id)
        } else {
            self.by_handle(id)
        }
    }

    /// The account of this DID, loaded if it is not.
    pub(crate) fn by_did(&self, did: &str) -> Option<Arc<Account>> {
        if let Some(a) = read(&self.directory).by_did.get(did) {
            return Some(a.clone());
        }
        self.load(did)
            .unwrap_or_else(|why| crate::cache::unrecoverable(&self.dir, &why))
    }

    /// The account that has this handle, in lower case.
    pub(crate) fn by_handle(&self, handle: &str) -> Option<Arc<Account>> {
        let loaded = |d: &Directory| d.by_did.get(d.handles.get(handle)?).cloned();
        if let Some(a) = loaded(&read(&self.directory)) {
            return Some(a);
        }
        self.load_hinted(compact::HANDLE, handle);
        loaded(&read(&self.directory))
    }

    /// The account that has this email, in lower case.
    pub(crate) fn by_email(&self, email: &str) -> Option<Arc<Account>> {
        let loaded = |d: &Directory| d.by_did.get(d.emails.get(email)?).cloned();
        if let Some(a) = loaded(&read(&self.directory)) {
            return Some(a);
        }
        self.load_hinted(compact::EMAIL, email);
        loaded(&read(&self.directory))
    }

    /// The account whose password reset token this is, in upper case.
    pub(crate) fn by_reset(&self, token: &str) -> Option<Arc<Account>> {
        if self.resets.of(token).is_none() {
            self.load_hinted(compact::TOKEN, token);
        }
        self.by_did(&self.resets.of(token)?)
    }

    /// Load the accounts hinted to have `name` (`Directory::hinted`): once
    /// they are, the accounts loaded say whether any account has it, as
    /// one not loaded has only what the table and the log noted say.
    pub(crate) fn load_hinted(&self, section: u8, name: &str) {
        let dids = {
            let d = read(&self.directory);
            d.hinted(section, name).unwrap_or_else(|why| d.fail(&why))
        };
        for did in dids {
            self.by_did(&did);
        }
    }

    /// Load the account `did`, if it is one and is not loaded: an error if
    /// what is kept of it cannot be made an account, or its handle or email
    /// is another's of those loaded.
    pub(crate) fn load(&self, did: &str) -> Result<Option<Arc<Account>>, String> {
        // What is no account waits for no load.
        if !read(&self.directory).may_be(did) {
            return Ok(None);
        }
        let _one = self.directory.loading[stripe(did)]
            .lock()
            .unwrap_or_else(PoisonError::into_inner);
        if let Some(a) = read(&self.directory).by_did.get(did) {
            return Ok(Some(a.clone()));
        }
        let Some(a) = self.build(did)? else {
            return Ok(None);
        };
        write(&self.directory)
            .insert_loaded(a.clone())
            .map_err(|why| format!("{ACCOUNTS_LOG}: {why}"))?;
        Ok(Some(a))
    }

    /// Make the account `did`, which is not loaded, of what is kept of it,
    /// with its sessions and app passwords, its preferences and its
    /// password reset token: not yet in the directory. None if it is no
    /// account.
    pub(crate) fn build(&self, did: &str) -> Result<Option<Arc<Account>>, String> {
        let at = |why: String| format!("{ACCOUNTS_LOG}: {did}: {why}");
        // The directory is held while the log is read: no compaction puts
        // its table in place, and deletes the logs noted, meanwhile.
        let Some(one) = read(&self.directory).entries_of(did).map_err(at)? else {
            return Ok(None);
        };
        let parts = one.parts().map_err(at)?;
        if parts.entry.did != did {
            return Err(at(format!("kept as the account {}", parts.entry.did)));
        }
        self.resets.note(did, &parts.mail);
        self.sessions.put(did, parts.keep, self.sessions.now());
        if let Some(prefs) = parts.prefs {
            self.prefs.put(did, prefs);
        }
        Ok(Some(parts.entry.account(
            None,
            None,
            &self.ctx,
            Some(parts.mail),
            parts.state,
        )))
    }

    /// Remove the directories in `repos/` of no account (`Accounts::sweep`).
    pub(crate) fn sweep_repos(&self) {
        self.directory.sweep(self.ctx.files.journal().map(|j| &**j));
    }
}

/// Remove the directories in `repos/` of no account, once, while the
/// server serves.
pub(crate) fn sweep_once(svc: &crate::Shared) {
    let svc = svc.clone();
    tokio::task::spawn_blocking(move || svc.sweep_repos());
}

/// The bytes of the logs and the checkpoint of the account `did`, which is
/// not loaded: its checkpoint's size, and what each log holds, less the
/// room a kill left after its frames (`pds_store::log_len`).
pub(crate) fn files_bytes(dir: &Path, did: &str) -> u64 {
    let layout = crate::store::account_layout(dir, did);
    let logs: u64 = layout
        .generations()
        .unwrap_or_default()
        .into_iter()
        .map(|g| pds_store::log_len(&layout.log(g)).unwrap_or(0))
        .sum();
    logs + std::fs::metadata(layout.checkpoint()).map_or(0, |m| m.len())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::email::testing::last_token;
    use crate::testing::*;
    use serde_json::json;
    use std::collections::BTreeMap;
    use std::path::PathBuf;
    use std::sync::atomic::Ordering;

    const CREATE: &str = "com.atproto.server.createAccount";
    const LOGIN: &str = "com.atproto.server.createSession";
    const REQUEST_RESET: &str = "com.atproto.server.requestPasswordReset";
    const LIST: &str = "com.atproto.sync.listRepos";

    /// A data directory, the directory its server's emails go to, and its PLC directory.
    struct Env {
        plc: String,
        dir: PathBuf,
        sink: PathBuf,
    }

    impl Env {
        fn at(name: &str, plc: &str) -> Env {
            let sink = scratch(&format!("{name}-mail"));
            std::fs::create_dir_all(&sink).unwrap();
            Env {
                plc: plc.to_string(),
                dir: scratch(name),
                sink,
            }
        }

        async fn new(name: &str) -> Env {
            Env::at(name, &plc().await)
        }

        fn open(&self, load_all: bool) -> crate::Shared {
            let mut c = config(&self.plc);
            c.load_all = load_all;
            c.mailer = email::Mailer::sink(&self.sink);
            c.proxy.appview = Some(crate::proxy::AppView {
                url: "http://127.0.0.1:1".into(),
                did: "did:web:appview.test".into(),
            });
            Arc::new(Service::open(&self.dir, c).unwrap())
        }

        /// The server, every account loaded as it starts.
        fn eager(&self) -> crate::Shared {
            self.open(true)
        }

        /// The server as it runs: each account loaded on first use.
        fn lazy(&self) -> crate::Shared {
            self.open(false)
        }

        /// Delete the account `did`, whose password is `<name>-pw`.
        async fn delete(&self, s: &crate::Shared, name: &str, did: &str, token: &str) {
            call(
                s,
                "com.atproto.server.requestAccountDelete",
                Some(token),
                Json::Null,
            )
            .await
            .unwrap();
            let body = json!({ "did": did, "password": format!("{name}-pw"), "token": last_token(&self.sink) });
            call(s, "com.atproto.server.deleteAccount", None, body)
                .await
                .unwrap();
        }
    }

    impl Drop for Env {
        fn drop(&mut self) {
            let _ = std::fs::remove_dir_all(&self.dir);
            let _ = std::fs::remove_dir_all(&self.sink);
        }
    }

    fn loaded(s: &Service) -> usize {
        read(&s.directory).by_did.len()
    }

    fn rows_read(s: &Service) -> u64 {
        read(&s.directory).rows_read.load(Ordering::Relaxed)
    }

    /// Append to the service log of a data directory a server has started
    /// on, with no server running, `n` accounts as `createAccount` logs
    /// them, each with the password `pw`, and seqs reserved, so that a
    /// start need not read every account's log for them. Answers their DIDs.
    fn synthetic(dir: &Path, n: usize) -> Vec<String> {
        let hash = crate::server::password::hash("pw");
        let log = crate::log::accounts_log(dir);
        let mut bytes = crate::log::log_bytes(&log);
        bytes.extend(pds_store::frame(&[
            json!({ "t": crate::events::SEQS, "upto": 100_000 }),
        ]));
        let mut dids = Vec::new();
        for i in 0..n {
            let did = format!("did:plc:{i:024}");
            bytes.extend(pds_store::frame(&[json!({
                "t": "account", "did": did, "handle": format!("u{i}.test"),
                "email": format!("u{i}@mail.com"), "password": hash,
                "key": hex::encode([7u8; 32]), "kemKey": null, "didDoc": { "id": did },
            })]));
            dids.push(did);
        }
        std::fs::write(&log, bytes).unwrap();
        dids
    }

    /// A page of `listRepos`: its accounts' DIDs, and its cursor.
    async fn list(
        s: &crate::Shared,
        cursor: Option<&str>,
        limit: usize,
    ) -> (Vec<String>, Option<String>) {
        let mut p = vec![("limit".to_string(), limit.to_string())];
        p.extend(cursor.map(|c| ("cursor".to_string(), c.to_string())));
        let got = call_with(s, LIST, None, &p, Json::Null).await.unwrap();
        let dids = got["repos"]
            .as_array()
            .unwrap()
            .iter()
            .map(|r| r["did"].as_str().unwrap().to_string())
            .collect();
        (dids, got["cursor"].as_str().map(str::to_string))
    }

    /// A start on a compacted service log of a few hundred accounts reads
    /// no account's row and makes no account, and neither does compacting
    /// it; each account is loaded when it is first asked for, by whatever
    /// names it, once.
    #[tokio::test]
    async fn a_start_loads_no_account() {
        let e = Env::new("lazy-start").await;
        drop(e.lazy());
        let dids = synthetic(&e.dir, 300);
        // Its log alone: where each account's entry is, is noted.
        let s = e.lazy();
        assert_eq!((loaded(&s), rows_read(&s)), (0, 0));
        assert_eq!(read(&s.directory).count(), 300);
        assert_eq!(s.account(&dids[3]).unwrap().handle(), "u3.test");
        assert_eq!(s.account("u250.test").unwrap().did, dids[250]);
        assert_eq!((loaded(&s), rows_read(&s)), (2, 0));
        s.log.compact().unwrap();
        assert_eq!((loaded(&s), rows_read(&s)), (2, 0));
        drop(s);
        // Compacted: a table, and a log that holds none of them.
        let s = e.lazy();
        assert_eq!((loaded(&s), rows_read(&s)), (0, 0));
        assert_eq!(read(&s.directory).count(), 300);
        call(&s, "com.atproto.server.describeServer", None, Json::Null)
            .await
            .unwrap();
        assert_eq!((loaded(&s), rows_read(&s)), (0, 0));
        assert_eq!(s.account(&dids[7]).unwrap().handle(), "u7.test");
        assert_eq!((loaded(&s), rows_read(&s)), (1, 1));
        assert_eq!(s.account("u120.test").unwrap().did, dids[120]);
        assert_eq!(s.by_email("u299@mail.com").unwrap().did, dids[299]);
        assert_eq!((loaded(&s), rows_read(&s)), (3, 3));
        // What names no account loads none.
        assert!(s.account("did:plc:nobody").is_none());
        assert!(s.account("nobody.test").is_none());
        assert!(s.by_email("nobody@mail.com").is_none());
        assert!(s.by_reset("AAAAA-AAAAA").is_none());
        assert_eq!((loaded(&s), rows_read(&s)), (3, 3));
        // Signing in loads the one account.
        let got = call(
            &s,
            LOGIN,
            None,
            json!({ "identifier": "u42@mail.com", "password": "pw" }),
        )
        .await
        .unwrap();
        assert_eq!(got["did"], json!(dids[42]));
        assert_eq!(loaded(&s), 4);
        // Its token names it to a server that has not loaded it.
        let token = got["accessJwt"].as_str().unwrap().to_string();
        drop(s);
        let s = e.lazy();
        assert_eq!(loaded(&s), 0);
        let session = call(
            &s,
            "com.atproto.server.getSession",
            Some(&token),
            Json::Null,
        )
        .await
        .unwrap();
        assert_eq!(session["handle"], json!("u42.test"));
        assert_eq!(loaded(&s), 1);
        // A page of `listRepos` loads its accounts, in the order they were made.
        let (page, cursor) = list(&s, Some("40"), 5).await;
        assert_eq!(page, dids[40..45]);
        assert_eq!(cursor.as_deref(), Some("45"));
        assert_eq!(loaded(&s), 5);
        // Asked for at once, by DID and by handle, it is one account.
        let asked: Vec<_> = (0..8)
            .map(|i| {
                let (s, did) = (s.clone(), dids[200].clone());
                std::thread::spawn(move || {
                    if i % 2 == 0 {
                        s.account(&did).unwrap()
                    } else {
                        s.account("u200.test").unwrap()
                    }
                })
            })
            .collect();
        let asked: Vec<Arc<Account>> = asked.into_iter().map(|t| t.join().unwrap()).collect();
        assert!(asked.iter().all(|a| Arc::ptr_eq(a, &asked[0])));
        assert_eq!(loaded(&s), 6);
        // A start that loads every account finds them all.
        drop(s);
        let s = e.eager();
        assert_eq!((loaded(&s), rows_read(&s)), (300, 300));
    }

    /// Accounts are found by DID, handle, email and password reset token
    /// whether they are in the table only, in the log only, or changed in
    /// the log after the table, with none loaded before it is asked for:
    /// an email changed, a handle changed, an account deleted. What an
    /// account no longer has names none, and is free for another to take.
    #[tokio::test]
    async fn lookups_find_accounts_in_the_table_and_the_log() {
        let plc = crate::identity::stub::Plc::start().await;
        let e = Env::at("lazy-lookups", &plc.url);
        let s = e.eager();
        let mut made = BTreeMap::new();
        for name in ["alice", "bob", "carol", "dave", "frank"] {
            made.insert(name, create(&s, name).await);
        }
        let reset = |s: &crate::Shared, email: &str| {
            let (s, body) = (s.clone(), json!({ "email": email }));
            async move { call(&s, REQUEST_RESET, None, body).await.unwrap() }
        };
        // A reset token in the table.
        reset(&s, "frank@mail.com").await;
        let frank_token = last_token(&e.sink);
        s.log.compact().unwrap();
        // In the log after it: an account made, an email and a handle
        // changed, an account deleted, a reset token.
        made.insert("erin", create(&s, "erin").await);
        call(
            &s,
            "com.atproto.server.updateEmail",
            Some(&made["alice"].1),
            json!({ "email": "alice2@mail.com" }),
        )
        .await
        .unwrap();
        call(
            &s,
            "com.atproto.identity.updateHandle",
            Some(&made["bob"].1),
            json!({ "handle": "bobby.test" }),
        )
        .await
        .unwrap();
        e.delete(&s, "carol", &made["carol"].0, &made["carol"].1)
            .await;
        reset(&s, "dave@mail.com").await;
        let dave_token = last_token(&e.sink);
        drop(s);
        let did = |name: &str| made[name].0.clone();
        let by_did = |s: &Service| {
            let handle = |name: &str| s.account(&did(name)).map(|a| a.handle());
            assert_eq!(handle("dave").as_deref(), Some("dave.test"));
            assert_eq!(handle("erin").as_deref(), Some("erin.test"));
            assert_eq!(handle("bob").as_deref(), Some("bobby.test"));
            assert_eq!(handle("carol"), None);
            let alice = s.account(&did("alice")).unwrap();
            assert_eq!(
                email::lock(&alice.mail).email.as_deref(),
                Some("alice2@mail.com")
            );
        };
        let by_handle = |s: &Service| {
            let of = |handle: &str| s.account(handle).map(|a| a.did.clone());
            assert_eq!(of("dave.test"), Some(did("dave")));
            assert_eq!(of("erin.test"), Some(did("erin")));
            assert_eq!(of("bobby.test"), Some(did("bob")));
            assert_eq!(of("bob.test"), None);
            assert_eq!(of("carol.test"), None);
        };
        let by_email = |s: &Service| {
            let of = |email: &str| s.by_email(email).map(|a| a.did.clone());
            assert_eq!(of("dave@mail.com"), Some(did("dave")));
            assert_eq!(of("erin@mail.com"), Some(did("erin")));
            assert_eq!(of("alice2@mail.com"), Some(did("alice")));
            assert_eq!(of("alice@mail.com"), None);
            assert_eq!(of("carol@mail.com"), None);
        };
        let by_token = |s: &Service| {
            let of = |token: &str| s.by_reset(token).map(|a| a.did.clone());
            assert_eq!(of(&frank_token), Some(did("frank")));
            assert_eq!(of(&dave_token), Some(did("dave")));
            assert_eq!(of("AAAAA-AAAAA"), None);
        };
        let all: [&dyn Fn(&Service); 4] = [&by_did, &by_handle, &by_email, &by_token];
        // The table and the log after it: each kind of lookup on a server
        // that has loaded none.
        for lookups in all {
            let s = e.lazy();
            assert_eq!(loaded(&s), 0);
            lookups(&s);
        }
        // The same once a compaction, with none loaded, has put a table
        // that holds it all in place: what was noted of the log is forgotten.
        let s = e.lazy();
        s.log.compact().unwrap();
        assert_eq!(loaded(&s), 0);
        for lookups in all {
            lookups(&s);
        }
        drop(s);
        // And of the table alone.
        for lookups in all {
            let s = e.lazy();
            assert_eq!(loaded(&s), 0);
            lookups(&s);
        }
        // What is taken is taken, of an account not loaded too, and what
        // was let go is free.
        let s = e.lazy();
        let account = |handle: &str, email: &str| {
            let (s, body) = (
                s.clone(),
                json!({ "handle": handle, "email": email, "password": "pw" }),
            );
            async move { call(&s, CREATE, None, body).await }
        };
        assert!(account("dave.test", "new@mail.com").await.is_err());
        assert!(account("bobby.test", "new@mail.com").await.is_err());
        assert!(account("new.test", "alice2@mail.com").await.is_err());
        assert!(account("new.test", "erin@mail.com").await.is_err());
        let erin = &made["erin"].1;
        assert_eq!(
            call(
                &s,
                "com.atproto.server.updateEmail",
                Some(erin),
                json!({ "email": "dave@mail.com" })
            )
            .await,
            Err("InvalidRequest".into())
        );
        assert_eq!(
            call(
                &s,
                "com.atproto.identity.updateHandle",
                Some(erin),
                json!({ "handle": "frank.test" })
            )
            .await,
            Err("HandleNotAvailable".into())
        );
        let new = account("bob.test", "alice@mail.com").await.unwrap();
        let new = new["did"].as_str().unwrap();
        assert_eq!(s.account("bob.test").unwrap().did, new);
        // A reset token works for an account not loaded, once.
        let s = e.lazy();
        let body = json!({ "token": frank_token, "password": "frank-new" });
        call(&s, "com.atproto.server.resetPassword", None, body.clone())
            .await
            .unwrap();
        assert!(
            call(&s, "com.atproto.server.resetPassword", None, body)
                .await
                .is_err()
        );
        let login = json!({ "identifier": "frank@mail.com", "password": "frank-new" });
        assert_eq!(
            call(&s, LOGIN, None, login).await.unwrap()["did"],
            json!(did("frank"))
        );
        drop(s);
        assert!(e.lazy().by_reset(&frank_token).is_none());
    }

    /// What is kept of an account, its sessions, app passwords,
    /// preferences, email and state, survives compactions and restarts
    /// during which it is never loaded: once with its entries in the log,
    /// once with its row in the table.
    #[tokio::test]
    async fn what_is_kept_of_an_account_not_loaded_survives_compaction() {
        let e = Env::new("lazy-kept").await;
        let s = e.eager();
        let (alice, token) = create(&s, "alice").await;
        let (bob, _) = create(&s, "bob").await;
        let app = call(
            &s,
            "com.atproto.server.createAppPassword",
            Some(&token),
            json!({ "name": "phone" }),
        )
        .await
        .unwrap();
        let login = json!({ "identifier": "alice.test", "password": app["password"] });
        let phone = call(&s, LOGIN, None, login.clone()).await.unwrap();
        let prefs = json!([{ "$type": "app.bsky.actor.defs#adultContentPref", "enabled": true }]);
        call(
            &s,
            "app.bsky.actor.putPreferences",
            Some(&token),
            json!({ "preferences": prefs }),
        )
        .await
        .unwrap();
        call(
            &s,
            "com.atproto.server.requestEmailConfirmation",
            Some(&token),
            Json::Null,
        )
        .await
        .unwrap();
        call(
            &s,
            "com.atproto.server.confirmEmail",
            Some(&token),
            json!({ "email": "alice@mail.com", "token": last_token(&e.sink) }),
        )
        .await
        .unwrap();
        call(
            &s,
            "com.atproto.server.deactivateAccount",
            Some(&token),
            json!({}),
        )
        .await
        .unwrap();
        let admin = format!(
            "Basic {}",
            base64::Engine::encode(
                &base64::engine::general_purpose::STANDARD,
                format!("admin:{ADMIN_PASSWORD}")
            )
        );
        let takedown = json!({ "subject": { "$type": "com.atproto.admin.defs#repoRef", "did": bob }, "takedown": { "applied": true, "ref": "kept" } });
        call_raw(
            &s,
            "com.atproto.admin.updateSubjectStatus",
            crate::sessions::Bearer::of(Some(&admin)),
            &[],
            &takedown.to_string(),
        )
        .await
        .unwrap();
        // Each account's sessions and app passwords, preferences, mail and state.
        let kept = |s: &Service| {
            let d = read(&s.directory);
            let accounts: BTreeMap<String, (email::Mail, crate::lifecycle::State, String)> = d
                .by_did
                .iter()
                .map(|(did, a)| {
                    let mail = email::lock(&a.mail).clone();
                    (did.clone(), (mail, read(&a.state).clone(), a.handle()))
                })
                .collect();
            (accounts, s.sessions.snapshot(), s.prefs.snapshot())
        };
        let want = kept(&s);
        assert_eq!(want.0.len(), 2);
        assert!(want.0[&alice].1.deactivated && want.0[&bob].1.takedown.is_some());
        assert!(want.0[&alice].0.confirmed && !want.2[&alice].is_empty());
        drop(s);
        let load = |s: &Service| {
            assert_eq!(loaded(s), 0);
            for did in [&alice, &bob] {
                s.account(did).unwrap();
            }
        };
        // In the log alone.
        let s = e.lazy();
        load(&s);
        assert!(kept(&s) == want);
        drop(s);
        for held in ["its log", "its table"] {
            let s = e.lazy();
            s.log.compact().unwrap();
            assert_eq!(loaded(&s), 0, "compacting {held}");
            // Read from the table just put in place.
            load(&s);
            assert!(kept(&s) == want, "after compacting {held}");
            drop(s);
            let s = e.lazy();
            load(&s);
            assert!(kept(&s) == want, "restarted after compacting {held}");
        }
        // And they work, for an account not loaded: its app password, the
        // session made with it, and its preferences.
        let s = e.lazy();
        assert_eq!(loaded(&s), 0);
        call(
            &s,
            "com.atproto.server.refreshSession",
            Some(phone["refreshJwt"].as_str().unwrap()),
            Json::Null,
        )
        .await
        .unwrap();
        let s = e.lazy();
        let got = call(
            &s,
            "app.bsky.actor.getPreferences",
            Some(&token),
            Json::Null,
        )
        .await
        .unwrap();
        assert_eq!(got["preferences"], prefs);
        let s = e.lazy();
        let listed = call(
            &s,
            "com.atproto.server.listAppPasswords",
            Some(&token),
            Json::Null,
        )
        .await
        .unwrap();
        assert_eq!(listed["passwords"][0]["name"], json!("phone"));
        let s = e.lazy();
        call(&s, LOGIN, None, login).await.unwrap();
        // The summary counts them as they are.
        let d = read(&s.directory);
        let summary = &d.table().unwrap().summary;
        assert_eq!(
            (summary.accounts, summary.deactivated, summary.takendown),
            (2, 1, 1)
        );
    }

    /// `listRepos` lists the accounts in the order they were made, and its
    /// cursors hold, across compactions and restarts and with accounts
    /// deleted, whose places stay theirs.
    #[tokio::test]
    async fn list_repos_keeps_its_order_and_cursors() {
        let e = Env::new("lazy-order").await;
        let s = e.eager();
        let made = std::cell::RefCell::new(Vec::new());
        for name in ["anna", "bert", "cleo", "dirk", "emma"] {
            let account = create(&s, name).await;
            made.borrow_mut().push((name, account));
        }
        let did = |name: &str| {
            let made = made.borrow();
            let (_, (did, _)) = made.iter().find(|(n, _)| *n == name).unwrap();
            did.clone()
        };
        let dids = |names: &[&str]| names.iter().map(|n| did(n)).collect::<Vec<_>>();
        let (page, after_bert) = list(&s, None, 2).await;
        assert_eq!(page, dids(&["anna", "bert"]));
        let (page, after_dirk) = list(&s, after_bert.as_deref(), 2).await;
        assert_eq!(page, dids(&["cleo", "dirk"]));
        let (after_bert, after_dirk) = (after_bert.unwrap(), after_dirk.unwrap());
        let (_, (cleo, cleo_token)) = made.borrow()[2].clone();
        e.delete(&s, "cleo", &cleo, &cleo_token).await;
        assert_eq!(
            list(&s, None, 10).await.0,
            dids(&["anna", "bert", "dirk", "emma"])
        );
        assert_eq!(
            list(&s, Some(&after_bert), 10).await.0,
            dids(&["dirk", "emma"])
        );
        s.log.compact().unwrap();
        assert_eq!(
            list(&s, Some(&after_bert), 10).await.0,
            dids(&["dirk", "emma"])
        );
        let finn = create(&s, "finn").await;
        made.borrow_mut().push(("finn", finn));
        assert_eq!(
            list(&s, Some(&after_dirk), 10).await.0,
            dids(&["emma", "finn"])
        );
        drop(s);
        // Restarted, with a table and a log after it, and none loaded.
        let s = e.lazy();
        assert_eq!(loaded(&s), 0);
        assert_eq!(read(&s.directory).count(), 5);
        let (page, cursor) = list(&s, Some(&after_dirk), 1).await;
        assert_eq!(page, dids(&["emma"]));
        assert_eq!(loaded(&s), 1);
        assert_eq!(list(&s, cursor.as_deref(), 10).await.0, dids(&["finn"]));
        assert_eq!(
            list(&s, None, 10).await.0,
            dids(&["anna", "bert", "dirk", "emma", "finn"])
        );
        assert_eq!(
            list(&s, Some(&after_bert), 10).await.0,
            dids(&["dirk", "emma", "finn"])
        );
        drop(s);
        // Compacted again with none loaded, one made and one deleted after.
        let s = e.lazy();
        s.log.compact().unwrap();
        let gwen = create(&s, "gwen").await;
        made.borrow_mut().push(("gwen", gwen));
        let (_, (emma, emma_token)) = made.borrow()[4].clone();
        e.delete(&s, "emma", &emma, &emma_token).await;
        let all = dids(&["anna", "bert", "dirk", "finn", "gwen"]);
        assert_eq!(list(&s, None, 10).await.0, all);
        drop(s);
        for compacted in [false, true] {
            let s = e.lazy();
            if compacted {
                s.log.compact().unwrap();
            }
            assert_eq!(read(&s.directory).count(), 5);
            assert_eq!(list(&s, None, 10).await.0, all, "compacted: {compacted}");
            assert_eq!(
                list(&s, Some(&after_bert), 10).await.0,
                all[2..],
                "compacted: {compacted}"
            );
            assert_eq!(
                list(&s, Some(&after_dirk), 10).await.0,
                all[3..],
                "compacted: {compacted}"
            );
            // Page by page, each cursor the next one's.
            let (mut paged, mut cursor) = (Vec::new(), None);
            loop {
                let (page, next) = list(&s, cursor.as_deref(), 2).await;
                if page.is_empty() {
                    break;
                }
                paged.extend(page);
                cursor = next;
            }
            assert_eq!(paged, all, "compacted: {compacted}");
            // The last cursor lists nothing, and one past every account is none.
            assert_eq!(list(&s, cursor.as_deref(), 2).await.0, Vec::<String>::new());
            let past = [("cursor".to_string(), "99".to_string())];
            assert_eq!(
                call_with(&s, LIST, None, &past, Json::Null).await,
                Err("InvalidRequest".into())
            );
        }
    }

    /// A start removes nothing from `repos/`; the sweep removes each
    /// directory of no account, and not that of an account loaded, not
    /// loaded, or being made.
    #[tokio::test]
    async fn the_sweep_removes_the_directories_of_no_account() {
        let e = Env::new("lazy-sweep").await;
        let s = e.eager();
        let (alice, _) = create(&s, "alice").await;
        let (bob, _) = create(&s, "bob").await;
        let repos = e.dir.join("repos");
        let stray = |did: &str| {
            std::fs::create_dir_all(repos.join(did)).unwrap();
            std::fs::write(repos.join(did).join("repo.0.log"), b"").unwrap();
        };
        let there = |did: &str| repos.join(did).exists();
        stray("did:plc:stray");
        stray("did:plc:making");
        crate::write(&s.directory).making("did:plc:making");
        s.sweep_repos();
        assert!(!there("did:plc:stray"));
        assert!(there("did:plc:making") && there(&alice) && there(&bob));
        // Made after all, or not: it is no account, and its directory goes.
        crate::write(&s.directory).made("did:plc:making");
        s.sweep_repos();
        assert!(!there("did:plc:making") && there(&alice) && there(&bob));
        s.log.compact().unwrap();
        let (carol, _) = create(&s, "carol").await;
        drop(s);
        // Of accounts not loaded: in the table, and in the log after it.
        stray("did:plc:stray");
        let s = e.lazy();
        assert!(there("did:plc:stray"));
        s.sweep_repos();
        assert_eq!(loaded(&s), 0);
        assert!(!there("did:plc:stray"));
        assert!(there(&alice) && there(&bob) && there(&carol));
    }

    /// A handle or an email that two accounts have is found as they are
    /// loaded: by a start that loads every account, which is refused.
    #[tokio::test]
    async fn a_handle_or_an_email_is_one_accounts() {
        for (name, what, entry) in [
            (
                "lazy-twice-handle",
                "is the handle of both",
                json!({ "handle": "u0.test", "email": "other@mail.com" }),
            ),
            (
                "lazy-twice-email",
                "a second account with the email of",
                json!({ "handle": "other.test", "email": "u0@mail.com" }),
            ),
        ] {
            let e = Env::new(name).await;
            drop(e.lazy());
            synthetic(&e.dir, 2);
            let log = crate::log::accounts_log(&e.dir);
            let mut frames = pds_store::scan(&std::fs::read(&log).unwrap()).unwrap().0;
            let mut twin = frames.last().unwrap()[0].clone();
            twin["did"] = json!("did:plc:twin");
            twin["handle"] = entry["handle"].clone();
            twin["email"] = entry["email"].clone();
            frames.push(vec![twin]);
            let bytes: Vec<u8> = frames.iter().flat_map(|f| pds_store::frame(f)).collect();
            std::fs::write(&log, bytes).unwrap();
            let mut c = config(&e.plc);
            let refused = Service::open(&e.dir, config(&e.plc)).err().unwrap();
            assert!(refused.contains(what), "{refused}");
            // One that loads none starts, and finds the others.
            c.load_all = false;
            let s = Service::open(&e.dir, c).unwrap();
            assert_eq!(
                s.account("u1.test").unwrap().did,
                "did:plc:000000000000000000000001"
            );
        }
    }

    /// What a start noted of the logs a table covers is forgotten when the
    /// table is put in place, and what it noted of the logs after stays.
    #[test]
    fn the_overlay_forgets_what_a_table_covers() {
        let mut d = Directory::new(Layout::new("/nowhere", "accounts"), None);
        let pos = |generation: u64, offset: u64| Pos {
            generation,
            offset,
            len: 10,
        };
        let account = |did: &str| json!({ "t": "account", "did": did, "handle": format!("{did}.test"), "email": format!("{did}@mail.com") });
        d.note(pos(1, 0), "a", &account("a")).unwrap();
        d.note(pos(1, 10), "b", &account("b")).unwrap();
        d.note(
            pos(2, 0),
            "a",
            &json!({ "t": "updateEmail", "did": "a", "email": "new@mail.com" }),
        )
        .unwrap();
        d.note_deleted("b", 2);
        d.note(pos(2, 10), "c", &account("c")).unwrap();
        d.note(pos(3, 0), "c", &json!({ "t": "confirmEmail", "did": "c" }))
            .unwrap();
        // An entry of no account, and a second account entry, are refused.
        assert!(
            d.note(pos(3, 10), "x", &json!({ "t": "confirmEmail", "did": "x" }))
                .is_err()
        );
        assert!(d.note(pos(3, 10), "c", &account("c")).is_err());
        // One of an account deleted is passed over.
        d.note(pos(3, 10), "b", &json!({ "t": "confirmEmail", "did": "b" }))
            .unwrap();
        assert_eq!((d.count(), d.next_ordinal()), (2, 3));
        assert_eq!(d.dids(), ["a", "c"]);
        assert!(d.may_be("a") && !d.may_be("b") && d.may_be("c") && !d.may_be("x"));
        let frames = |d: &Directory, did: &str| {
            d.overlay
                .accounts
                .get(did)
                .map(|n| n.frames.iter().map(|p| p.generation).collect::<Vec<_>>())
        };
        assert_eq!(frames(&d, "a"), Some(vec![1, 2]));
        // A table of generation 2 covers log 1.
        d.overlay.forget(2);
        assert_eq!(frames(&d, "a"), Some(vec![2]));
        assert!(d.is_noted_deleted("b"));
        assert_eq!(frames(&d, "c"), Some(vec![2, 3]));
        assert!(!d.overlay.handles.contains_key("a.test"));
        assert!(d.overlay.emails.contains_key("new@mail.com"));
        // One of generation 3 covers log 2 too: the deletion with it.
        d.overlay.forget(3);
        assert_eq!(frames(&d, "a"), None);
        assert!(!d.is_noted_deleted("b"));
        assert_eq!(frames(&d, "c"), Some(vec![3]));
        d.overlay.forget(4);
        assert!(d.overlay.accounts.is_empty() && d.overlay.emails.is_empty());
    }
}