uqa-storage-sqlite 0.5.0

SQLite catalog, indexes, compressed storage, graph and key/value providers
Documentation
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Namespace and slot reuse must not revive an old participant or release a live peer.

use super::*;

fn namespace(incarnation: u8) -> LeaseNamespace {
    LeaseNamespace {
        magic: *b"UQASSL01",
        database: DatabaseId::from_bytes([2; 16]),
        incarnation: Some([incarnation; 16]),
    }
}

fn tags(file: &NativeLeaseFile, control: &StorageReadControl) -> Vec<u64> {
    let mut tags = Vec::new();
    file.visit(control, &mut |tag| {
        tags.push(tag);
        Ok(())
    })
    .unwrap();
    tags.sort_unstable();
    tags
}

#[test]
fn closing_transports_releases_registered_descriptors_without_another_open() {
    let directory = tempfile::tempdir().unwrap();
    let files: Vec<_> = (0..8)
        .map(|id| {
            NativeLeaseFile::open(&directory.path().join(format!("leases-{id}")), namespace(1))
                .unwrap()
        })
        .collect();
    let retained: Vec<_> = files
        .iter()
        .map(|file| Arc::downgrade(file.0.shared()))
        .collect();
    drop(files);
    assert!(
        retained.iter().all(|state| state.upgrade().is_none()),
        "the process registry retained closed databases until another file opened"
    );
}

#[test]
fn the_final_lease_releases_its_registered_descriptor_after_transport_close() {
    let directory = tempfile::tempdir().unwrap();
    let control = StorageReadControl::with_limit(1 << 20);
    let file = NativeLeaseFile::open(&directory.path().join("leases"), namespace(1)).unwrap();
    let state = Arc::downgrade(file.0.shared());
    let admission = file.admit(&control).unwrap();
    let lease = file.retain(31, &control).unwrap();
    drop((admission, file));
    assert!(state.upgrade().is_some(), "a live lease lost its file");
    drop(lease);
    assert!(
        state.upgrade().is_none(),
        "the final lease left its file registered"
    );
    assert_eq!(control.memory().used(), 0);
}

#[test]
fn concurrent_handle_release_closes_the_last_registered_descriptor() {
    let directory = tempfile::tempdir().unwrap();
    let file = NativeLeaseFile::open(&directory.path().join("leases"), namespace(1)).unwrap();
    let state = Arc::downgrade(file.0.shared());
    let start = Arc::new(std::sync::Barrier::new(9));
    let workers: Vec<_> = (0..8)
        .map(|_| {
            let file = file.clone();
            let start = Arc::clone(&start);
            std::thread::spawn(move || {
                start.wait();
                drop(file);
            })
        })
        .collect();
    drop(file);
    start.wait();
    for worker in workers {
        worker.join().unwrap();
    }
    assert!(state.upgrade().is_none());
}

#[test]
fn path_aliases_share_one_descriptor_and_cannot_hide_live_leases() {
    let directory = tempfile::tempdir().unwrap();
    std::fs::create_dir(directory.path().join("nested")).unwrap();
    let path = directory.path().join("leases");
    let alias = directory.path().join("nested/../leases");
    let control = StorageReadControl::with_limit(1 << 20);
    let file = NativeLeaseFile::open(&path, namespace(1)).unwrap();
    let admission = file.admit(&control).unwrap();
    let retained = file.retain(81, &control).unwrap();
    drop(admission);
    let peer = NativeLeaseFile::open(&alias, namespace(1)).unwrap();
    assert!(file.shares_descriptor(&peer));
    let admission = peer.admit(&control).unwrap();
    assert_eq!(tags(&peer, &control), [81]);
    drop((admission, peer));
    let _admission = file.admit(&control).unwrap();
    assert_eq!(tags(&file, &control), [81]);
    drop(retained);
    assert!(tags(&file, &control).is_empty());
}

#[test]
fn reusing_a_released_slot_keeps_only_the_new_tag_and_live_peers() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("leases");
    let control = StorageReadControl::with_limit(1 << 20);
    let file = NativeLeaseFile::open(&path, namespace(1)).unwrap();
    let admission = file.admit(&control).unwrap();
    let first = file.retain(11, &control).unwrap();
    let peer = file.retain(22, &control).unwrap();
    drop(admission);
    let second = NativeLeaseFile::open(&path, namespace(1)).unwrap();
    assert!(file.shares_descriptor(&second));
    drop(file);
    let _admission = second.admit(&control).unwrap();
    drop(first);
    let replacement = second.retain(33, &control).unwrap();
    assert_eq!(tags(&second, &control), [22, 33]);
    let mut retained_peer = Some(peer);
    second
        .visit(&control, &mut |tag| {
            if tag == 22 {
                drop(retained_peer.take());
            }
            Ok(())
        })
        .unwrap();
    assert_eq!(tags(&second, &control), [33]);
    drop(replacement);
    assert!(tags(&second, &control).is_empty());
    assert_eq!(control.memory().used(), 0);
}

#[test]
fn a_new_incarnation_cannot_replace_a_live_namespace_or_inherit_old_tags() {
    let directory = tempfile::tempdir().unwrap();
    let path = directory.path().join("leases");
    let control = StorageReadControl::with_limit(1 << 20);
    let file = NativeLeaseFile::open(&path, namespace(1)).unwrap();
    let admission = file.admit(&control).unwrap();
    let retained = file.retain(11, &control).unwrap();
    drop((admission, file));
    assert!(matches!(
        NativeLeaseFile::open(&path, namespace(2)),
        Err(VersionError::WrongDatabase)
    ));
    drop(retained);
    let replacement = NativeLeaseFile::open(&path, namespace(2)).unwrap();
    let _admission = replacement.admit(&control).unwrap();
    assert!(tags(&replacement, &control).is_empty());
    let retained = replacement.retain(11, &control).unwrap();
    assert_eq!(tags(&replacement, &control), [11]);
    drop(retained);
    assert!(tags(&replacement, &control).is_empty());
}