diff --git a/packages/cultcache-rs/CHANGELOG.md b/packages/cultcache-rs/CHANGELOG.md index 23e217e8..af5bbd9e 100644 --- a/packages/cultcache-rs/CHANGELOG.md +++ b/packages/cultcache-rs/CHANGELOG.md @@ -13,6 +13,7 @@ only in git history. ### Changed +- `CultCache::delete::` of a key nothing holds now refuses, as `put` and `is_writable` do, when `T` has no home store ("No backing store is registered…") or when a store carries a foreign record at the key or another registered type holds it (before: `Ok(false)`). - `is_stored_at_stamp(&str)` (new, public) is the one shape a peer's `storedAt` must parse as, pinned by `tests/vectors/stored-at-stamp` (RFC 3339 `YYYY-MM-DDThh:mm:ss`, a 1 to 9 digit fraction, `Z` or `+hh:mm`/`-hh:mm`; uppercase `T`/`Z`, a real date, seconds 00-59, years 0001-9999), hand-checked because `chrono`'s RFC 3339 reader takes other shapes. `put_raw_envelope` and `put_prepared_batch` refuse a `stored_at` that is not a stamp (before: only a blank one) and mint nothing (variants:ruling:raw-put-without-stamp-refused). - The single-file store's `push`, `push_all` and `commit_if` stamp a replacing write strictly later than the record it replaces at the same key (a staged stamp stands only when it parses and is strictly later; otherwise the stamp is minted as the replaced stamp plus one microsecond, or now if later), with no identical-content exception, as C# does. A stamp with no successor (the replaced stamp is at year 9999's last microsecond) refuses the write and writes nothing, as C# `MintStoredAt` does. `commit_if` replaces only the record of the entry's own type under the key, so a retype is refused as `push` refuses it. The cache holds the stamp the store wrote. The redb stores and the store-level exact-envelope writes (`compare_and_swap_entry` and its family) still write the caller's envelope unchanged. - The single-file store re-checks for a link once the store lock is held (every write and the try-exchange), so a link put at the path while a writer waited on the lock is refused with `StoreLinkedError` and the lock released, as C# does, instead of being replaced. Pinned by a test that puts the link while a second holder owns the lock. diff --git a/packages/cultcache-rs/src/lib.rs b/packages/cultcache-rs/src/lib.rs index 0e60a0c2..614f7861 100644 --- a/packages/cultcache-rs/src/lib.rs +++ b/packages/cultcache-rs/src/lib.rs @@ -3487,6 +3487,8 @@ impl CultCache { /// The one decode behind every write entry and [`CultCache::is_writable`]: the held envelope at `(type_id, key)` /// read under the registered type's mark. + /// Each write over a held key pays one full typed decode of the held payload (about 40% of a `put` of a 200,000-element + /// record, measured in release); this cost is accepted deliberately, with no cached mark, so the check keeps no second state. fn held_finding(&self, type_id: &str, key: &str) -> Result { let Some(definition) = self.definitions.get(type_id) else { bail!("No entry type registered for CultCache envelope type {type_id:?}"); @@ -3985,15 +3987,22 @@ impl CultCache { self.require_loaded()?; self.require_entry_type::()?; self.refuse_unread_slots(T::TYPE, key)?; + let home = self.home_index(T::TYPE)?; let id = entry_id_parts(T::TYPE, key); let Some(entry) = self.entries.get(&id).cloned() else { - if let Some((_, foreign)) = self.foreign.iter().find(|(_, foreign)| foreign.key == key) { - return Err(foreign_conflict(foreign, T::TYPE)); - } + // The retype judge a write runs: a foreign record or another registered type holding the key refuses the removal. + let probe = CultCacheEnvelope { + key: key.to_string(), + r#type: T::TYPE.to_string(), + payload: Vec::new(), + stored_at: String::new(), + schema_id: None, + }; + self.refuse_retype(&self.entries, &self.foreign, &probe)?; return Ok(false); }; let released = self.projected_slots(&entry)?; - if let Some(home) = self.home_index(T::TYPE)? { + if let Some(home) = home { self.stores[home].store.delete(&entry)?; } self.entries.remove(&id); diff --git a/packages/cultcache-rs/tests/slot_readonly.rs b/packages/cultcache-rs/tests/slot_readonly.rs index 7851c818..1fb0909b 100644 --- a/packages/cultcache-rs/tests/slot_readonly.rs +++ b/packages/cultcache-rs/tests/slot_readonly.rs @@ -477,6 +477,21 @@ fn is_writable_is_false_for_a_type_with_no_home_store() { assert!(cache.is_writable(SlotUnit::TYPE, "k").unwrap()); } +// For a homeless type, is_writable, put and delete of an unheld key agree: removal is refused like the write, not answered false. +#[test] +fn delete_of_an_unheld_key_of_a_type_with_no_home_store_is_refused_like_the_put() { + let dir = tempfile::tempdir().unwrap(); + let mut cache = registered(); + cache.add_backing_store(SingleFileMessagePackBackingStore::new(dir.path().join("s.cc")), [SlotUnit::TYPE]).unwrap(); + cache.pull_all_backing_stores().unwrap(); + assert!(!cache.is_writable(UnionSurface::TYPE, "k").unwrap()); + let error = cache.delete::("k").unwrap_err(); + assert!(error.to_string().contains("No backing store is registered"), "{error:#}"); + // A homed type's unheld delete is still a plain false. + assert!(cache.is_writable(SlotUnit::TYPE, "k").unwrap()); + assert!(!cache.delete::("k").unwrap()); +} + #[test] fn load_soa_refuses_a_same_length_replacement_of_a_marked_record() { use cultcache_rs::CultCacheSoaTable; @@ -539,3 +554,53 @@ fn a_store_with_marked_records_and_a_tail_is_refused_and_its_cache_writes_nothin } assert_eq!(std::fs::read(&path).unwrap(), before); } + +// Attaching a store to a cache whose load was refused must keep the refusal: the write refusal still names why the load +// failed, not merely that the cache has not pulled. (Only a pulled cache falls back to unpulled when a store is attached.) +#[test] +fn a_store_attached_after_a_refused_load_keeps_the_refusal_reason() { + use cultcache_rs::CacheBackingStore; + use cultcache_rs::CultCacheLoadRefused; + let (_held, dir, path) = held_marked_and_clean(); + let mut tailed = vector("union-surface.msgpack"); + tailed.push(0xc0); + SingleFileMessagePackBackingStore::new(&path).push(&envelope("tailed", tailed)).unwrap(); + let mut cache = registered(); + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap(); + assert!(cache.pull_all_backing_stores().is_err()); + cache.add_backing_store(SingleFileMessagePackBackingStore::new(dir.path().join("routed.cc")), [SlotUnit::TYPE]).unwrap(); + let error = cache.put("new", &constructed()).unwrap_err(); + let refused = error.downcast_ref::().unwrap_or_else(|| panic!("{error:#}")); + assert!(refused.reason.contains("bytes after"), "refusal reason lost: {}", refused.reason); +} + +// A key held by another registered type, or carried as a foreign record: put, is_writable and typed delete all refuse, and +// delete refuses with the put's conflict (delete used to answer Ok(false)). This agreement is for those two cases; for a +// type with no home store delete's home-store error comes before the retype judge, and both still refuse. +#[test] +fn delete_of_a_key_held_by_another_registered_type_is_refused_like_the_put() { + let (mut cache, _dir, _path) = held_marked_and_clean(); + assert!(!cache.is_writable(SlotUnit::TYPE, "clean").unwrap()); + let put = cache.put("clean", &SlotUnit { name: "n".into(), outcome: UnitOutcome::Denied }).unwrap_err(); + let delete = cache.delete::("clean").unwrap_err(); + let (put, delete) = (conflict(&put), conflict(&delete)); + assert_eq!(delete.record_key, "clean"); + assert_eq!(delete.record_key, put.record_key); + assert_eq!(delete.schema_names, put.schema_names); + assert!(delete.schema_names.contains(&UnionSurface::TYPE.to_string()), "{:?}", delete.schema_names); + assert!(cache.snapshot_envelopes().iter().any(|held| held.key == "clean" && held.r#type == UnionSurface::TYPE)); + + // A foreign record at the key: only UnionSurface registered, the store's unit-arm record is foreign here. + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("extra-slot-store.cc"); + std::fs::write(&path, without_variant_row(&vector("extra-slot-store.cc"))).unwrap(); + let mut cache = CultCache::new(); + cache.register_entry_type::().unwrap(); + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap(); + cache.pull_all_backing_stores().unwrap(); + assert!(!cache.is_writable(UnionSurface::TYPE, "unit-arm").unwrap()); + let put = cache.put("unit-arm", &constructed()).unwrap_err(); + let delete = cache.delete::("unit-arm").unwrap_err(); + assert_eq!(conflict(&delete).record_key, "unit-arm"); + assert_eq!(conflict(&delete).schema_names, conflict(&put).schema_names); +}