persistant 0.18.21

A thin persistence layer over Apache OpenDAL for blobs and records by key: declared capabilities, atomic replace, typed records, and a blocking facade for synchronous callers.
Documentation
// SPDX-License-Identifier: MIT OR Apache-2.0
//! `persistant::blocking::registry`: the registry for synchronous callers. Opened by id like the
//! async one, called from plain threads, from many threads at once and from inside a tokio runtime,
//! and refusing what the backend cannot give rather than approximating it.
#![cfg(feature = "registry")]

use persistant::blocking::registry::{self, KvHandle};
use persistant::capability::{CapabilityId, choose::Durability, provider::ProviderNeeds};
use persistant::kv::KvError;
use persistant::registry::{Endpoint, OpenError};
use std::sync::Arc;

fn needs(ids: &[&str], d: Durability) -> ProviderNeeds {
    ProviderNeeds::new(
        ids.iter()
            .map(|s| CapabilityId::new(*s).expect("a literal id"))
            .collect(),
        d,
    )
}

fn basic() -> ProviderNeeds {
    needs(
        &["kv.get", "kv.put", "kv.delete", "kv.prefix_list"],
        Durability::Process,
    )
}

/// The blob-backed in-process store: it opens on every platform.
fn open_memory() -> KvHandle {
    registry::open_named(
        "memory",
        &Endpoint::None,
        &needs(
            &["kv.get", "kv.put", "kv.delete", "kv.prefix_list"],
            Durability::Volatile,
        ),
    )
    .expect("memory opens")
}

#[test]
fn the_list_is_the_async_one_and_a_plain_thread_can_use_a_handle() {
    let listed = registry::providers(&basic()).expect("something qualifies");
    assert!(listed.iter().any(|l| l.id == "fs"), "{listed:?}");

    let kv = open_memory();
    assert_eq!(kv.id(), "memory");
    assert_eq!(kv.get("a").unwrap(), None);
    kv.put("svc/a", b"1".to_vec(), None).unwrap();
    kv.put("svc/b", b"2".to_vec(), None).unwrap();
    assert_eq!(kv.get("svc/a").unwrap().as_deref(), Some(&b"1"[..]));
    let page = kv.prefix_list("svc/", None, 10).unwrap();
    assert_eq!(page.keys.len(), 2);
    kv.delete("svc/a").unwrap();
    assert_eq!(kv.get("svc/a").unwrap(), None);
}

#[test]
fn what_the_backend_cannot_give_is_refused_not_approximated() {
    let kv = open_memory();
    // `memory` is a blob store: neither compare-and-delete nor a one-step count (the matrix says no).
    kv.put("k", b"v".to_vec(), None).unwrap();
    assert!(matches!(kv.take("k"), Err(KvError::Unsupported(_))));
    assert!(matches!(kv.count("k"), Err(KvError::Unsupported(_))));
    // And the value is still there: a refused take took nothing.
    assert_eq!(kv.get("k").unwrap().as_deref(), Some(&b"v"[..]));
}

#[test]
fn a_need_the_backend_does_not_meet_is_refused_at_open() {
    let dir = tempfile::tempdir().unwrap();
    // The registry's `fs` has no compare-and-swap: a blob store has no compare-and-delete.
    let wants = needs(
        &["kv.get", "kv.put", "kv.compare_and_swap"],
        Durability::Process,
    );
    let r = registry::open_named("fs", &Endpoint::path(dir.path().join("s")), &wants);
    assert!(matches!(r, Err(OpenError::Unmet(_))), "{r:?}");
    // The control: the same call with needs it does meet is not refused for them.
    let r = registry::open_named("fs", &Endpoint::path(dir.path().join("s")), &basic());
    assert!(r.is_ok(), "{r:?}");
}

#[test]
fn many_threads_share_one_handle_and_exactly_one_wins_a_race() {
    let kv = Arc::new(
        registry::open_named(
            "memory",
            &Endpoint::None,
            &needs(
                &["kv.get", "kv.put", "kv.put_if_absent"],
                Durability::Volatile,
            ),
        )
        .expect("memory opens"),
    );
    for round in 0..10 {
        let key = format!("race/{round}");
        let wins: usize = (0..8)
            .map(|i| {
                let (kv, key) = (kv.clone(), key.clone());
                std::thread::spawn(move || {
                    kv.put_if_absent(&key, vec![i as u8], None).unwrap() as usize
                })
            })
            .collect::<Vec<_>>()
            .into_iter()
            .map(|t| t.join().unwrap())
            .sum();
        assert_eq!(wins, 1, "round {round}: put_if_absent");
    }
}

#[tokio::test(flavor = "current_thread")]
async fn a_call_from_inside_a_tokio_runtime_does_not_deadlock() {
    // The calling thread never drives the handle's runtime, so this is safe (it blocks this task's
    // thread for the duration, as any blocking call does).
    let kv = open_memory();
    kv.put("in/rt", b"x".to_vec(), None).unwrap();
    assert_eq!(kv.get("in/rt").unwrap().as_deref(), Some(&b"x"[..]));
}

#[cfg(feature = "registry-sqlite")]
#[test]
fn sqlite_gives_take_and_count_through_the_blocking_handle() {
    let dir = tempfile::tempdir().unwrap();
    let wants = needs(
        &["kv.get", "kv.put", "kv.take", "kv.count"],
        Durability::Process,
    );
    let kv = registry::open_named("sqlite", &Endpoint::path(dir.path().join("kv.db")), &wants)
        .expect("sqlite opens");
    for i in 0..3 {
        kv.put(&format!("c/{i}"), vec![i], None).unwrap();
    }
    assert_eq!(kv.count("c/").unwrap(), 3);
    assert_eq!(kv.take("c/1").unwrap(), Some(vec![1]));
    assert_eq!(kv.take("c/1").unwrap(), None);
    assert_eq!(kv.count("c/").unwrap(), 2);
}

#[cfg(feature = "registry-sqlite")]
#[test]
fn eight_threads_taking_one_key_on_sqlite_get_it_once() {
    let dir = tempfile::tempdir().unwrap();
    let wants = needs(&["kv.get", "kv.put", "kv.take"], Durability::Process);
    let kv = Arc::new(
        registry::open_named("sqlite", &Endpoint::path(dir.path().join("kv.db")), &wants)
            .expect("sqlite opens"),
    );
    for round in 0..10 {
        let key = format!("take/{round}");
        kv.put(&key, b"v".to_vec(), None).unwrap();
        let got: usize = (0..8)
            .map(|_| {
                let (kv, key) = (kv.clone(), key.clone());
                std::thread::spawn(move || usize::from(kv.take(&key).unwrap().is_some()))
            })
            .collect::<Vec<_>>()
            .into_iter()
            .map(|t| t.join().unwrap())
            .sum();
        assert_eq!(got, 1, "round {round}");
    }
}

#[test]
fn the_blocking_blob_handle_exists_only_when_asked_for_and_refuses_the_reserved_keys() {
    let dir = tempfile::tempdir().unwrap();
    let kv_only = open_memory();
    assert!(kv_only.blob().is_none() && kv_only.rel().is_none());
    for (id, endpoint) in [
        ("memory", Endpoint::None),
        ("fs", Endpoint::path(dir.path().join("b"))),
    ] {
        let wants = needs(
            &[
                "kv.get",
                "kv.put",
                "blob.read",
                "blob.write",
                "blob.stat",
                "blob.list",
            ],
            Durability::Volatile,
        );
        let kv = registry::open_named(id, &endpoint, &wants).unwrap();
        let blob = kv.blob().expect("a blob need gets a blob handle");
        assert_eq!(blob.read("a").unwrap(), None);
        blob.write("a", b"1".to_vec()).unwrap();
        assert_eq!(blob.read("a").unwrap(), Some(b"1".to_vec()));
        assert_eq!(blob.stat("a").unwrap().unwrap().len, 1);
        let keys: Vec<String> = blob
            .list("", None, 10)
            .unwrap()
            .entries
            .into_iter()
            .map(|e| e.key)
            .collect();
        assert_eq!(keys, vec!["a".to_string()], "{id}");
        // Reserved keys are refused here as on the async handle; the key-value store is untouched.
        let e = blob.write("kvx/6b", b"x".to_vec()).unwrap_err();
        assert!(e.to_string().contains("key-value store"), "{e}");
        kv.put("k", b"v".to_vec(), None).unwrap();
        assert_eq!(kv.get("k").unwrap(), Some(b"v".to_vec()));
        blob.delete("a").unwrap();
        assert_eq!(blob.read("a").unwrap(), None);
    }
}

#[test]
fn the_blocking_blob_handle_reads_ranges_and_streams_writes_through_the_same_key_check() {
    let dir = tempfile::tempdir().unwrap();
    for (id, endpoint) in [
        ("memory", Endpoint::None),
        ("fs", Endpoint::path(dir.path().join("b"))),
    ] {
        let wants = needs(
            &[
                "kv.get",
                "kv.put",
                "blob.read",
                "blob.read_range",
                "blob.write_stream",
                "blob.write_stream.atomic",
            ],
            Durability::Volatile,
        );
        let kv = registry::open_named(id, &endpoint, &wants).unwrap();
        let blob = kv.blob().unwrap();
        // A streamed write is invisible until `finish`, whole after it; a range reads part of it.
        let mut w = blob.write_stream_atomic("big").unwrap();
        w.write(b"hello ").unwrap();
        w.write(b"world").unwrap();
        assert_eq!(blob.read("big").unwrap(), None, "{id}: not before finish");
        w.finish().unwrap();
        assert_eq!(blob.read("big").unwrap(), Some(b"hello world".to_vec()));
        assert_eq!(
            blob.read_range("big", 6, 5).unwrap(),
            Some(b"world".to_vec())
        );
        assert_eq!(blob.read_range("absent", 0, 1).unwrap(), None);
        // `abort` leaves the old value whole.
        let mut w = blob.write_stream("big").unwrap();
        w.write(b"junk").unwrap();
        w.abort().unwrap();
        assert_eq!(blob.read("big").unwrap(), Some(b"hello world".to_vec()));
        // Reserved keys are refused on every new operation, naming the key-value store; a lookalike
        // name is an ordinary key.
        for key in ["kv", "kvx", "kv/a", "kvx/6b/x"] {
            let e = blob.read_range(key, 0, 1).unwrap_err();
            assert!(e.to_string().contains("key-value store"), "{id} {key}: {e}");
            for atomic in [false, true] {
                let r = if atomic {
                    blob.write_stream_atomic(key)
                } else {
                    blob.write_stream(key)
                };
                let Err(e) = r else {
                    panic!("a reserved key opens no stream");
                };
                assert!(e.to_string().contains("key-value store"), "{id} {key}: {e}");
            }
        }
        let mut w = blob.write_stream("kvx2").unwrap();
        w.write(b"ok").unwrap();
        w.finish().unwrap();
        assert_eq!(blob.read_range("kvx2", 0, 2).unwrap(), Some(b"ok".to_vec()));
    }
}

#[cfg(feature = "registry-sqlite")]
#[test]
fn the_blocking_rel_handle_runs_statements_and_a_failed_transaction_keeps_nothing() {
    use persistant::relational::{Isolation, Value};

    let dir = tempfile::tempdir().unwrap();
    let wants = needs(&["rel.values", "rel.transaction"], Durability::Volatile);
    let kv =
        registry::open_named("sqlite", &Endpoint::path(dir.path().join("r.db")), &wants).unwrap();
    let rel = kv.rel().expect("a rel need gets a rel handle");
    assert_eq!(rel.backend().0, "sqlite");
    rel.execute("CREATE TABLE t (a INTEGER PRIMARY KEY)", &[])
        .unwrap();
    let n = rel
        .transaction(
            Isolation::default(),
            &[
                ("INSERT INTO t (a) VALUES (?)", &[Value::Int(1)]),
                ("INSERT INTO t (a) VALUES (?)", &[Value::Int(2)]),
            ],
        )
        .unwrap();
    assert_eq!(n, vec![1, 1]);
    // The second insert breaks the primary key: the first one of this transaction is rolled back.
    let e = rel.transaction(
        Isolation::default(),
        &[
            ("INSERT INTO t (a) VALUES (?)", &[Value::Int(3)]),
            ("INSERT INTO t (a) VALUES (?)", &[Value::Int(1)]),
        ],
    );
    assert!(e.is_err());
    assert_eq!(rel.query("SELECT a FROM t", &[]).unwrap().len(), 2);
    // Control: the same statement alone, without the duplicate, commits.
    rel.transaction(
        Isolation::default(),
        &[("INSERT INTO t (a) VALUES (?)", &[Value::Int(3)])],
    )
    .unwrap();
    assert_eq!(rel.query("SELECT a FROM t", &[]).unwrap().len(), 3);
}