Some checks failed
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled
SingleFileMessagePackBackingStore::pull_schema_catalog reads the catalog under the shared lock pull_all takes; resolve_schema_entry resolves a record's schema id as the decoder does. Decoder, encoder and CultCacheEnvelope unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
125 lines
5.3 KiB
Rust
125 lines
5.3 KiB
Rust
//! A consumer of a single-file store reads the schema version a record was published under from the store's catalog, through the
|
|
//! public surface only: `pull_all`, `pull_schema_catalog` and `resolve_schema_entry`
|
|
//! (`cultlib-gaps:follow_up:rs-snapshot-schema-version`).
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use cultcache_rs::{
|
|
CacheBackingStore, CultCache, DatabaseEntry, SchemaCatalogEntry, SingleFileMessagePackBackingStore, resolve_schema_entry,
|
|
};
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
|
|
#[cultcache(type = "tests.sc_one", schema = "tests.sc_one")]
|
|
struct One {
|
|
#[cultcache(key = 0)]
|
|
text: String,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)]
|
|
#[cultcache(type = "tests.sc_two", schema = "tests.sc_two")]
|
|
struct Two {
|
|
#[cultcache(key = 0)]
|
|
text: String,
|
|
}
|
|
|
|
fn vectors(folder: &str) -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/vectors").join(folder)
|
|
}
|
|
|
|
/// The version a store publishes for each record, in key order: `(key, schema name, schema version)`.
|
|
fn versions(store: &SingleFileMessagePackBackingStore) -> Vec<(String, String, String)> {
|
|
let catalog = store.pull_schema_catalog().unwrap();
|
|
let mut rows: Vec<_> = store
|
|
.pull_all()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|envelope| {
|
|
let id = envelope.schema_id.as_deref().unwrap();
|
|
let entry = resolve_schema_entry(&catalog, id).unwrap_or_else(|| panic!("no entry for {id}"));
|
|
(envelope.key.clone(), entry.schema_name.clone(), entry.schema_version.clone())
|
|
})
|
|
.collect();
|
|
rows.sort();
|
|
rows
|
|
}
|
|
|
|
fn copy(from: PathBuf) -> (tempfile::TempDir, SingleFileMessagePackBackingStore) {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("store.cc");
|
|
std::fs::copy(from, &path).unwrap();
|
|
(dir, SingleFileMessagePackBackingStore::new(&path))
|
|
}
|
|
|
|
#[test]
|
|
fn a_consumer_reads_the_version_of_each_type_a_rust_store_holds() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("store.cc");
|
|
let mut cache = CultCache::new();
|
|
cache.register_entry_type::<One>().unwrap();
|
|
cache.register_entry_type::<Two>().unwrap();
|
|
cache.add_backing_store(SingleFileMessagePackBackingStore::new(&path), ["tests.sc_one", "tests.sc_two"]).unwrap();
|
|
cache.pull_all_backing_stores().unwrap();
|
|
cache.put("a", &One { text: "1".into() }).unwrap();
|
|
cache.put("b", &Two { text: "2".into() }).unwrap();
|
|
drop(cache);
|
|
|
|
let consumer = SingleFileMessagePackBackingStore::new(&path);
|
|
assert_eq!(
|
|
versions(&consumer),
|
|
vec![
|
|
("a".to_string(), "tests.sc_one".to_string(), "tests.sc_one.v1".to_string()),
|
|
("b".to_string(), "tests.sc_two".to_string(), "tests.sc_two.v1".to_string()),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_id_only_an_entry_lists_as_compatible_resolves_to_that_entry() {
|
|
let (_dir, store) = copy(vectors("document-variants-c2a/readability/compatible-id-only-v3.bin"));
|
|
let catalog = store.pull_schema_catalog().unwrap();
|
|
assert_eq!(catalog.len(), 1);
|
|
assert_eq!(catalog[0].schema_id, "vectors.item.next");
|
|
assert_eq!(catalog[0].compatible_schema_ids, vec!["vectors.item".to_string()]);
|
|
let entry = resolve_schema_entry(&catalog, "vectors.item").unwrap();
|
|
assert_eq!(entry.schema_id, "vectors.item.next");
|
|
assert_eq!(entry.schema_version, "vectors.item.v1");
|
|
assert_eq!(resolve_schema_entry(&catalog, "vectors.nowhere"), None);
|
|
}
|
|
|
|
#[test]
|
|
fn an_id_an_entry_owns_resolves_to_its_owner_not_to_an_entry_listing_it() {
|
|
for vector in ["own-id-over-compatible-v3.bin", "compatible-before-owner-v3.bin"] {
|
|
let (_dir, store) = copy(vectors("document-variants-c2a/readability").join(vector));
|
|
let catalog = store.pull_schema_catalog().unwrap();
|
|
assert_eq!(catalog.len(), 2, "{vector}");
|
|
let entry = resolve_schema_entry(&catalog, "vectors.item").unwrap();
|
|
assert_eq!((entry.schema_id.as_str(), entry.schema_version.as_str()), ("vectors.item", "vectors.item.v1"), "{vector}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_legacy_array_and_an_absent_file_publish_no_catalog() {
|
|
let (_dir, store) = copy(vectors("document-variants-c2a/readability/legacy-envelopes.bin"));
|
|
assert!(!store.pull_all().unwrap().is_empty());
|
|
assert_eq!(store.pull_schema_catalog().unwrap(), Vec::<SchemaCatalogEntry>::new());
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let absent = SingleFileMessagePackBackingStore::new(dir.path().join("none.cc"));
|
|
assert_eq!(absent.pull_schema_catalog().unwrap(), Vec::<SchemaCatalogEntry>::new());
|
|
assert!(!dir.path().join("none.cc").exists() && !dir.path().join("none.cc.lock").exists(), "reading an absent store creates nothing");
|
|
}
|
|
|
|
// `tests/vectors/key-identity/foreign-store.cc` is written by the C# reference: two types, each published with its version.
|
|
#[test]
|
|
fn a_store_the_csharp_reference_wrote_gives_its_versions() {
|
|
let (_dir, store) = copy(vectors("key-identity/foreign-store.cc"));
|
|
assert_eq!(
|
|
versions(&store),
|
|
vec![
|
|
("j".to_string(), "tests.ki_alpha".to_string(), "tests.ki_alpha.v1".to_string()),
|
|
("k".to_string(), "tests.ki_gamma".to_string(), "tests.ki_gamma.v1".to_string()),
|
|
]
|
|
);
|
|
let catalog = store.pull_schema_catalog().unwrap();
|
|
assert_eq!(catalog[0].content_hash, "sha256:02f0c8b7ea07c11705c1365fc29136ada08c8076209cacfb8d5526229461ce7a");
|
|
}
|