crates/kleene-index/tests/store.rs
//! Indexes are kept beside the set, keyed by its root hash: kept while the hash holds,
//! dropped when it changes, never written for a set without one.
use std::path::PathBuf;
use std::sync::Arc;
use arrow_array::{ArrayRef, UInt32Array};
use kleene_index::Store;
fn temp(name: &str) -> PathBuf {
let d = std::env::temp_dir().join(format!("kleene-index-{}-{name}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
d
}
fn col(v: Vec<u32>) -> ArrayRef {
Arc::new(UInt32Array::from(v))
}
#[test]
fn an_index_is_kept_while_the_root_hash_holds() {
let base = temp("keep");
let set = base.join("X.gar");
std::fs::create_dir_all(&set).unwrap();
std::fs::write(set.join("root"), [1u8; 32]).unwrap();
let store = Store::beside(&set).unwrap();
assert_eq!(store.dir(), Some(base.join("X.gar.kix").as_path()));
assert!(store.load("a").unwrap().is_none());
store
.save("a", vec![("x", col(vec![3, 1, 4])), ("y", col(vec![1, 5]))])
.unwrap();
let again = Store::beside(&set).unwrap();
let a = again.load("a").unwrap().expect("kept");
assert_eq!(&a.u32("x").unwrap()[..], &[3, 1, 4]);
assert_eq!(&a.u32("y").unwrap()[..], &[1, 5]);
// a new root hash drops every stored index
std::fs::write(set.join("root"), [2u8; 32]).unwrap();
let changed = Store::beside(&set).unwrap();
assert!(changed.load("a").unwrap().is_none(), "dropped");
assert_eq!(changed.root(), Some(&[2u8; 32][..]));
let _ = std::fs::remove_dir_all(&base);
}
#[test]
fn a_set_without_a_root_hash_keeps_nothing() {
let base = temp("none");
let set = base.join("Y.gar");
std::fs::create_dir_all(&set).unwrap();
let store = Store::beside(&set).unwrap();
assert!(store.dir().is_none());
store.save("a", vec![("x", col(vec![1]))]).unwrap();
assert!(store.load("a").unwrap().is_none());
assert!(!base.join("Y.gar.kix").exists());
let _ = std::fs::remove_dir_all(&base);
}