CultLib/packages/cultcache-rs/CHANGELOG.md

18 KiB

Changelog

All notable changes to cultcache-rs are recorded here, following Keep a Changelog. Earlier releases are recorded only in git history.

Unreleased

Added

  • CultCache::update_with_attempts and the conditional update: each attempt reads the record from its home store with its content version in one read, applies a function to a copy and commits only over that version, rerunning on a lost race (default five attempts). The result is CultUpdateResult<T>, outcome Updated, Declined (the function returned None) or LostRace, with the cache's document. Public types CultStoredRecord, CultCommitIf, CultUpdateOutcome, CultUpdateResult.
  • CacheBackingStore::read(key) and commit_if(entry, expected), provided methods whose defaults refuse with an error naming the store kind; SingleFileMessagePackBackingStore implements both. The content version is the lowercase hex SHA-256 of the record re-encoded as the store file writes it (key, schema id, stored_at, payload). It equals the SHA-256 of the record's bytes in the file, and so the C# reference's tests/vectors/content-version, whenever the file's MessagePack encodings are minimal, which every runtime's writer produces; a hand-edited file with a non-minimal encoding (for example a str8 key that fits a fixstr) hashes differently from its slice here. New dependency sha2 0.10 and eight new lockfile crates: sha2, digest, block-buffer, crypto-common, generic-array, typenum, cpufeatures, version_check.

Changed

  • CultCache::delete::<T> 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.
  • CultCache::is_writable does not look at store paths: a link at a store path is not predicted, and the write refuses it with StoreLinkedError. Pinned by a test through a cache attached to a linked store.

Breaking

Semver: this is a breaking change in the 0.x line (0.3.x to 0.4.0).

  • CultCache::update is replaced, not extended: update::<T, _>(key, apply) takes FnMut(Option<T>) -> Option<T> and returns Result<CultUpdateResult<T>>. The blind FnOnce(Option<T>) -> T form that returned T is gone; use put for a blind write. It reads the home store, not the cache's image, so a cache whose store has no read and commit_if (the redb stores, a custom store) refuses it before apply runs.
  • CacheBackingStore::push returns Result<String>, the stored_at written, and push_all returns Result<Vec<String>>, one stamp per entry given in order (a soft write that wrote nothing returns an empty vec). A custom store must return what it wrote; the redb stores return the entries' own stamps. Huginn implements CacheBackingStore in five test stores under a pinned git rev: they change at its next bump.
  • A write at a store path that is a symbolic link, live or dangling, is refused with the new public StoreLinkedError (error.downcast_ref::<StoreLinkedError>()) before the lock and the replace, and writes nothing: push, push_all, delete, the exchanges, and a soft push_all, which used to skip a link and now refuses. Before, a write at a live link replaced the link with a regular file and a dangling link failed with io::ErrorKind::NotFound. The redb stores (RedbMessagePackBackingStore, OwnedRedbMessagePackBackingStore) are exempt: they are ruled migrated and deleted (paged-store-redb-migrate-and-delete) and still open a live link's target. Configure the link's target as the store path. Reads through a live link work.
  • A record whose payload carries unread slots is read-only. A payload with a slot past its type's declared slots, at any depth (record, nested record, list element, union arm, unit arm), still decodes, and every write or removal of its key is now refused before any store is touched: put, put_envelope, put_raw_envelope, put_prepared_batch (the whole batch) and delete, so update too. The refusal is SchemaConflictError with the new unread_slots; its message names the type and both slot counts and never a slot value. Holders of such a record used to overwrite it and drop the extra slots silently. The payload nesting refusal still runs first. A held payload this build cannot read at all (an arm it lacks from a newer writer, or a corrupt record) is read-only too: every write or removal is refused with an error naming the type and key (a newer writer's record is read with that build; a corrupt one is restored from a backup or removed at the store itself, as no cache call removes it), and is_writable answers false. load_soa is a write of every held key it omits or changes, so it refuses to drop or replace a read-only record. is_writable also answers false for a key a store carries as a foreign record and for a type other than the one holding the key, as the writes do. Each write over a held key decodes the held payload once to ask (about 40% of a put of a 200,000-element record, measured in release); keys nothing holds pay nothing.
  • cultmesh-rs pull_rudp_catalog_snapshot applies a snapshot as one cache commit (put_prepared_batch), as C# does: a snapshot document for a key the node holds read-only, or any other refusal, applies nothing. It used to apply document by document, so a refusal ended the pull with the earlier documents kept. A snapshot whose types route to different backing stores is now refused whole, as a batch cannot span stores. C# CultCache.Commit refuses a cross-store commit the same way (The batch spans A and B; a commit lands in one home store), so the two agree; a test pins it.
  • SchemaConflictError gains the public field unread_slots: Option<UnreadSlots>; code that builds it with a struct literal must set it (None).
  • A unit arm with slots ([N, [extra...]]) decodes as the unit arm and marks the record, where the derive refused it.
  • The registration map's value changes from the schema name to a private struct holding the schema name and the unread-slots check.
  • A cache whose last pull_all_backing_stores was refused (a tail, a payload past the nesting bound, a duplicate unique-index value, a retype conflict, a record from the wrong store) refuses every put, put_envelope, put_raw_envelope, put_prepared_batch, update and delete with CultCacheLoadRefused until a pull succeeds. Before, it stayed writable over an empty image, and the next batch write replaced the store with only the new records.
  • CacheBackingStore::push_all writes the given entries onto the store as it is when the write lands, under the store's lock or transaction: a record under the same identity is replaced and no other record is removed. It used to replace everything the store held, so a cache whose image was stale (another writer had added a record since its pull) wiped that record with put_prepared_batch. put_prepared_batch now hands the store its staged records alone, as C# CommitBatch and Python push_all do; the lay-back of foreign records is gone because they are never rewritten. A custom CacheBackingStore must give push_all merge semantics. push_all reads the store, so through a dangling link it is refused like push and no longer replaces the link; a soft flush is unchanged.
  • A cache with a backing store also refuses every write and removal, with CultCacheLoadRefused, until it has pulled: a cache that never pulled, or that gained a store since its last pull, holds no image of its stores, and a batch from it replaced a populated store with only the new records. is_writable answers false meanwhile. A cache with no store needs no pull. Callers that attach a store and write (cultmesh nodes created with pull_on_start: false) must call pull_all_backing_stores first.
  • decode_payload, put_raw_envelope and prepared batches refuse a payload with bytes after its first MessagePack value (payload has {n} bytes after its first MessagePack value), by count; the tail is never walked or quoted. They used to read the first value and keep or drop the tail, so a record with a stray tail was served and rewritten without it. A store holding such a record (single-file, redb, load_soa) is refused at load, naming the type, the key and the count, and stays unreadable until the record is repaired; the same check now runs on every record at load, as the C# and Python runtimes do at open. The nesting bound is part of that check, so a stored payload nested past it is refused at load too, where it used to load and refuse on read.
  • A single-file store written as cultcache.store.v3 is read and written: a store the C# runtime marked v3 (it holds a record with an element id: an owned list element with a [CultElementId] member) keeps its header here, and is carried through every rewrite with its ids; this runtime never drops the mark. A runtime from before this change refuses a v3 store by name. Rolling back to an older release after a store has been written as v3 is one-way: there is no carry-back, so restore from a copy taken before upgrading.
  • A write over a file the reader refuses (a zero-byte file, a truncated store, a stamp this runtime does not read) refuses with CultCacheStoreUnreadable and leaves the file as it is. A whole-store write used to replace such a file. This reverses 0.3.1, where push_all replaced a file with no readable stamp (healing it); the stamp rule now has one owner, the reader every write asks under the lock, and it accepts v1 and v3 (0.3.1 refused v3).
  • #[cultcache(index)] means unique, as in C# RefuseIndexSharing: within one type and one alias no two records hold one non-blank value. A put, batch, push or load that would make two records hold a value is refused, naming both keys and the alias, and a store that already holds a duplicate does not open. A grouping index is no longer expressible; a consumer that relied on shared values must drop the index and look up by another way.
  • bool index and lookup text renders True / False, as C# does, where it rendered true / false.
  • DatabaseEntry::UNIQUE_INDEXES replaces cult_indexes as the one index declaration and changes type (its members are the unique aliases); a hand implementation overriding cult_indexes no longer compiles.
  • register_entry_type over a type that already holds records re-judges them under the new declaration (unique indexes and the global rule) and refuses, changing nothing, when they violate it. A declaration with an index also refuses when a held record cannot be decoded as the new type; a declaration without one does not read the records, so it does not.
  • A single-file store holds one record per key. The v1 and legacy readers refuse a store with two records under one key (CultCacheStoreUnreadable, the record Undecodable): the store does not open, and a write over it refuses and leaves the file. A consumer that holds such a file must repair it with the runtime that wrote it before opening it here.
  • Every single-file writer (push, push_all, insert_entry_if_absent, the compare-exchange and snapshot-append family, and the cache's puts and batches) refuses to put a second type under a key the store holds, with SchemaConflictError naming the key and both types, writing nothing. Retyping a key is a delete, then an add (one batch may hold both). A write owns the held record when its schema id equals the id the held record is stored under, as C# WrittenOwns asks; a type renamed under a stable id therefore replaces its own record, and a write of another type name under another id is refused (push of the same type name under another id still replaces the record, as before; compare_and_swap_entry, and a push_all batch holding one type under two ids, refuse it). Registered types here declare no compatible ids, so equality is the whole test. Writers used to leave two records under the key.
  • compare_and_swap_entry over a single file refuses an in-place retype (expected A/k, replacement B/k under another schema id) with the same SchemaConflictError, as compare_exchange does, where it used to retype the record and return true. A lost race still returns false first.
  • A cache holds one record per key. put, put_envelope, put_raw_envelope and a batch of a second type under a key the cache holds, or a key a store carries foreign, are refused with SchemaConflictError even with no store (a memory-only cache used to hold both), whatever schema id the envelope claims: another registered type's record is never owned. put_envelope, put_raw_envelope, put_prepared_batch and load_soa also refuse, at any key, an envelope whose schema id names another type registered in this cache (the id a pull would resolve the record by). An id this cache does not register is trusted as the envelope claims it: at a key this cache does not hold, the single-file store decides ownership by that id, so such an envelope can replace another writer's record stored under it, and a reader registering the type the id names serves the record as that type. Store-level writers called directly (push, insert_entry_if_absent, compare-and-swap) still trust the claimed id. A refused write or pull changes nothing. Two stores holding one key refuse the pull, naming the key and both types, and so does one store holding two records at a key that resolve to one type. A store holding a record no registered type claims now loads (it used to refuse with "No schema is registered for persisted entry type") and the record is carried, through put_prepared_batch too, which lays foreign records back byte for byte; delete at a foreign key is refused. load_soa refuses a second type at a key or a record at a foreign key, and still refuses an unregistered type. The redb stores still key by (type, key), and their own writers (push, insert_entry_if_absent, compare-and-swap) still put a second type at a held key: a cache now refuses to pull such a redb store with SchemaConflictError, where it used to load both records.
  • A write of the same type under a held key replaces the record by key, and in one push_all the last given of several entries under a key and a schema id is the record, whatever their type names. The redb stores are unchanged and still key by type and key.

Added

  • UnreadSlots { type_name, slot_count, declared_count, occurrences }, decode_payload_marked (the decode that returns the mark) and CultCache::is_writable(type, key). The derives' slot visitors note into a thread-local scope the decode entry opens (__enter_slots, __note_unread_slots, __UnitArmSlots, doc-hidden); a decode outside that entry notes nothing.
  • CultCache::foreign_records() and ForeignRecord { key, schema_id, schema_name, stored_at }: the records the last pull found that no registered type claims, sorted by key.
  • Lookups, matching the C# GetByName, GetByIndex and GetGlobal. #[cultcache(name)] and #[cultcache(index)] / #[cultcache(index = "alias")] on DatabaseEntry fields and #[cultcache(global)] on the struct give CultCache::get_by_name, get_by_index and get_global. An empty name and a blank index value are not found; records of one type sharing a name find the one with the smallest key (an index value is unique, below). A second record of a global type under another key is refused on every write path and on load with CultCacheSecondGlobal. Two name members, or two members with one index alias, are refused at compile time. A member's text comes from CultLookupValue (String, Option of one, integers, bool, char).
  • SchemaConflictError, the typed refusal of a write the catalog cannot describe: records of different types under one schema id, or an arrived schema and a registered one that share an id and disagree on the schema name. A whole-store write (push_all) refuses with it, names the schema id, the names involved and a record key, writes nothing and leaves the file as it is. A caller tells it apart with error.downcast_ref::<SchemaConflictError>().

[0.3.1]

Fixed

  • push_all reads the store's header under the lock and refuses a format this runtime does not read, with CultCacheStoreUnreadable UnsupportedFormat, leaving the file as it was. A file with no readable stamp is still replaced, so a whole write heals it.