commit dca559d63bc71ac50fafaa6ed6f42977fa044786 Author: Meta Vens Date: Sun May 24 11:35:02 2026 +0100 Squashed 'crates/cultcache-rs/' content from commit 4e955437 git-subtree-dir: crates/cultcache-rs git-subtree-split: 4e95543714e8582a4e0a8962b32737a7df588e29 diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..96ef6c0b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..b11a909e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "cultcache-rs" +version = "0.1.0" +edition = "2024" +publish = false + +[workspace] +members = ["cultcache-rs-derive"] + +[lib] +name = "cultcache_rs" +path = "src/lib.rs" + +[dependencies] +anyhow = "1" +chrono = { version = "0.4", features = ["serde"] } +cultcache-rs-derive = { path = "cultcache-rs-derive" } +fs2 = "0.4" +rmp-serde = "1.3" +serde = { version = "1", features = ["derive"] } +serde_bytes = "0.11" +serde_json = "1" +uuid = { version = "1", features = ["v4"] } + +[dev-dependencies] +pretty_assertions = "1.4" +tempfile = "3" diff --git a/README.md b/README.md new file mode 100644 index 00000000..2d75d94e --- /dev/null +++ b/README.md @@ -0,0 +1,332 @@ +# CultCache Rust + +`cultcache-rs` is the Rust port of the useful part of GameCult's CultCache: +a polymorphism-aware in-memory cache that remembers through pluggable backing +stores. + +Consumer code should think in domain types: + +```rust +let player = cache.get_required::("player:meta")?; +cache.put("player:meta", &player)?; +``` + +The persistence layer should deal with envelopes, routing, MessagePack payload +bytes, and schema identity. Application code should not paw at loose JSON files +like a sad little bureaucrat with a clipboard. + +## What It Is + +CultCache is a domain cache with persistence adapters. + +- `CultCache` is the query and mutation surface. +- Domain structs implement `DatabaseEntry`. +- Entries are stored behind a `type::key` identity, so multiple entry types + can share a logical key without colliding. +- Each payload is encoded directly from the known `DatabaseEntry` type into + MessagePack bytes. +- Backing stores are adapters, not the public data model. +- Writes persist to the resolved backing store before the in-memory cache is + updated. +- Type-specific backing stores beat generic backing stores. +- `SingleFileMessagePackBackingStore` is the first concrete store and guards + file access with a sidecar lock file. + +This is not an ORM, not a database, and not distributed consensus in a novelty +hat. If multiple processes write the same backing file, use an external lock or +a coordinator. + +## Current API + +```rust +use cultcache_rs::{CultCache, DatabaseEntry, SingleFileMessagePackBackingStore}; + +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache(type = "settings")] +struct Settings { + #[cultcache(key = 0)] + theme: String, + + #[cultcache(key = 1, default)] + retries: u32, +} + +let mut cache = CultCache::new(); +cache.register_entry_type::()?; +cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.msgpack")); +cache.pull_all_backing_stores()?; + +cache.put("app", &Settings { + theme: "ash".to_string(), + retries: 3, +})?; + +let settings = cache.get_required::("app")?; +# Ok::<(), anyhow::Error>(()) +``` + +For a closed set of entries, generate a registry instead of repeating yourself: + +```rust +use cultcache_rs::cultcache_registry; + +cultcache_registry!(GameCultEntries { + Settings, + PlayerData, +}); + +let mut cache = CultCache::new(); +cache.register_registry(GameCultEntries)?; +``` + +The important part is that callers retrieve domain values directly: + +```rust +let settings: Settings = cache.get_required("app")?; +let all_settings: Vec = cache.get_all()?; +``` + +The cache is the ergonomic surface. The backing store is the memory prosthetic. + +## How This Maps From Original CultCache + +The original C# CultCache relies on runtime reflection: + +- cacheable models inherit from `DatabaseEntry` +- the cache scans child classes at startup +- `Get(id)` returns a `T` +- optional `INamedEntry` enables name lookup +- optional field/property indexes are registered by member name +- backing stores pull/push/delete `DatabaseEntry` values + +Rust does not have C#-style assembly scanning or runtime subclass discovery. +The closest honest Rust translation is: + +- domain structs implement a marker trait, `DatabaseEntry` +- the derive macro generates a positional array formatter from explicit integer + field slots +- the envelope carries the polymorphic type discriminator +- unknown persisted type ids fail closed instead of constructing arbitrary + runtime types +- a generated registry should eventually replace repetitive manual + registration + +Manual `register_entry_type::()` is still accepted, but entry identity no +longer needs a hand-written impl: `#[derive(DatabaseEntry)]` declares it beside +the domain type. `cultcache_registry!` provides the current generated resolver +surface for a closed entry set. + +## Intended Rust Ergonomics + +The target user-facing shape should be: + +```rust +#[derive(Clone, DatabaseEntry)] +#[cultcache(type = "player")] +pub struct PlayerData { + #[cultcache(key = 0)] + pub id: String, + + #[cultcache(key = 1)] + pub name: String, + + #[cultcache(key = 2)] + pub faction: String, +} + +let mut cache = CultCache::builder() + .with_generated_entries(game_entries()) + .with_generic_store(SingleFileMessagePackBackingStore::new("cache.msgpack")) + .build()?; + +cache.pull_all_backing_stores()?; +let player = cache.get_required::("player:ari")?; +``` + +The current `cultcache_registry!` macro is deliberately simple. The remaining +ergonomic endpoint is automatic inventory or build-script generation so users +do not have to maintain the registry list by hand. + +The registry is the Rust equivalent of C# reflection and MessagePack resolver +setup. It should know: + +- entry type id +- generated MessagePack/serde formatter path +- Rust type name / schema name +- optional store domain route +- optional name key extractor +- optional secondary indexes +- optional global singleton marker + +In other words: code generation should restore the original CultCache feeling +without pretending Rust has runtime reflection hiding under the floorboards. + +## Current Surface + +- `CultCache::new` +- `register_entry_type::` +- `add_generic_backing_store` +- `add_backing_store` +- `pull_all_backing_stores` +- `get::` +- `get_required::` +- `get_envelope::` +- `get_required_envelope::` +- `get_all::` +- `put::` +- `put_envelope::` +- `update::` +- `delete::` +- `snapshot` +- `SingleFileMessagePackBackingStore` + +## Backing Store Routing + +Generic store: + +```rust +cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new("cache.msgpack")); +``` + +Type-specific store: + +```rust +cache.add_backing_store( + SingleFileMessagePackBackingStore::new("players.msgpack"), + ["player"], +); +``` + +When writing a `PlayerData`, the cache checks type-specific stores first. If none +match, it writes to the first generic store. Later matching stores are mirrors. + +This mirrors the C# behavior: + +- specific domain stores own their domain +- the first generic store is the primary generic write target +- later generic stores mirror writes +- this is not multi-master + +## Persistence Semantics + +`put` persists before mutating the in-memory cache. If persistence fails, the +cache does not pretend the write succeeded. + +The envelope is MessagePack, and the payload inside each envelope is also raw +MessagePack bytes encoded from the registered concrete `DatabaseEntry` type. +That avoids the old bootstrap path where payloads were normalized through +`serde_json::Value`. + +For bit-compatible neighbors, that also enables a real fast lane: + +- `get_envelope::()` exports the canonical persisted bytes for a typed entry +- `put_envelope::()` ingests the same envelope into another cache instance + without re-encoding the payload first + +It still decodes once for validation and typed reads. We are not pretending one +`Vec` became metaphysically zero-copy because we believed in it harder. But +the stupid decode/re-encode loop is gone. + +The `DatabaseEntry` derive does not trust Rust source field order as the durable +schema. Every persisted member must declare a stable integer slot: + +```rust +#[derive(Clone, DatabaseEntry)] +#[cultcache(type = "player")] +pub struct PlayerData { + #[cultcache(key = 0)] + pub id: String, + + #[cultcache(key = 1)] + pub name: String, + + #[cultcache(key = 2, default)] + pub level: u32, +} +``` + +The derive emits a tuple/array formatter. Gaps serialize as nil. Missing fields +marked `default` use `Default::default()` when older payloads are read. Deleted +field slots should stay reserved until an explicit store migration rewrites the +data. + +The single-file MessagePack store rewrites an atomic snapshot. That is a sane +starting point for small typed state surfaces, settings, Epiphany agent memory, +heartbeat state, and other compact control-plane data. Large corpora should use +a sharded store or a real database instead of asking one file to become a +warehouse and then acting wounded when physics invoices us. + +The single-file store uses a sidecar lock file for shared reads and exclusive +writes. That protects ordinary multi-process access to the same file. It is +still not a multi-master replication protocol, and it is not a substitute for a +coordinator when higher-level write ordering matters. + +## Near-Term Ergonomic Improvements + +1. **Derive macro** + - `#[derive(DatabaseEntry)]` + - `#[cultcache(type = "...")]` + - `#[cultcache(key = N)]` on every persisted field + - tuple/array formatter generation landed + - optional `#[cultcache(name)]`, `#[cultcache(index)]`, and + `#[cultcache(global)]` are still future work + +2. **Generated registry** + - a `CultCacheRegistry` trait + - `cache.register_registry(GameCultEntries)` + - basic macro registry landed + - automatic inventory/build-script generation and store routes are future work + +3. **Name and index lookups** + - `cache.get_by_name::("Potion")` + - `cache.get_by_index::("faction", "Lucent")` + - generated extractors instead of reflection strings + +4. **Schema/version metadata** + - schema version in the envelope or payload metadata + - explicit migration hooks + - refusal on unknown persisted entry types unless a migration/resolver is + installed + +5. **Projection helpers** + - JSON projection for review and git diffs + - vector stripping / large-field elision for agent memory surfaces + +## Why Not Skip Registration Immediately? + +Rust can only deserialize polymorphic data into concrete types if something +maps the persisted `type` discriminator to the Rust type. In C#, reflection and +MessagePack resolvers can discover a closed `DatabaseEntry` inheritance tree at +runtime and route it through known formatters. In Rust, that map has to come from +somewhere: + +- explicit registration +- a generated registry +- a macro inventory crate +- a hand-written resolver + +Explicit registration is the simplest honest implementation. Generated registry +is the ergonomic destination. Hand-written resolver tables are the punishment +we give ourselves if we get lazy. + +## Security Model + +CultCache should never parse arbitrary structured data into arbitrary runtime +types. It only accepts persisted envelopes whose `type` discriminator resolves +to a known `DatabaseEntry` type registered in the cache or generated registry. + +That is the Rust version of the original trick: + +- C# refuses to store objects outside the `DatabaseEntry` root type. +- Runtime-visible `DatabaseEntry` subclasses get dedicated formatters. +- Unknown or unregistered types are rejected. +- MessagePack payload bytes deserialize through known concrete formatters, not + an open-world type loader or generic structured-value bridge. + +The derive/registry work should preserve that shape. Its job is to remove +manual registration ceremony, not to loosen the closed-world boundary. + +## License + +Private GameCult infrastructure for now. Public packaging can wait until the +API grows enough teeth to deserve strangers. diff --git a/cultcache-rs-derive/Cargo.toml b/cultcache-rs-derive/Cargo.toml new file mode 100644 index 00000000..74d52a4f --- /dev/null +++ b/cultcache-rs-derive/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "cultcache-rs-derive" +version = "0.1.0" +edition = "2024" +publish = false + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1" +quote = "1" +syn = { version = "2", features = ["derive", "parsing"] } diff --git a/cultcache-rs-derive/src/lib.rs b/cultcache-rs-derive/src/lib.rs new file mode 100644 index 00000000..f05a23f3 --- /dev/null +++ b/cultcache-rs-derive/src/lib.rs @@ -0,0 +1,243 @@ +use proc_macro::TokenStream; +use quote::format_ident; +use quote::quote; +use syn::Data; +use syn::DeriveInput; +use syn::Fields; +use syn::LitInt; +use syn::LitStr; +use syn::Type; +use syn::parse_macro_input; + +#[proc_macro_derive(DatabaseEntry, attributes(cultcache))] +pub fn derive_database_entry(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + let ident = input.ident.clone(); + let mut entry_type: Option = None; + let mut schema_name: Option = None; + + for attribute in input + .attrs + .iter() + .filter(|attr| attr.path().is_ident("cultcache")) + { + let parse_result = attribute.parse_nested_meta(|meta| { + if meta.path.is_ident("type") { + entry_type = Some(meta.value()?.parse()?); + Ok(()) + } else if meta.path.is_ident("schema") { + schema_name = Some(meta.value()?.parse()?); + Ok(()) + } else { + Err(meta.error("unsupported cultcache attribute")) + } + }); + if let Err(error) = parse_result { + return error.to_compile_error().into(); + } + } + + let Some(entry_type) = entry_type else { + return syn::Error::new_spanned( + ident, + "DatabaseEntry derive requires #[cultcache(type = \"...\")]", + ) + .to_compile_error() + .into(); + }; + let schema_name = schema_name.unwrap_or_else(|| LitStr::new(&ident.to_string(), ident.span())); + + let fields = match input.data { + Data::Struct(data) => match data.fields { + Fields::Named(fields) => fields.named, + _ => { + return syn::Error::new_spanned( + ident, + "DatabaseEntry derive requires a named struct", + ) + .to_compile_error() + .into(); + } + }, + _ => { + return syn::Error::new_spanned(ident, "DatabaseEntry derive requires a struct") + .to_compile_error() + .into(); + } + }; + + let mut slots: Vec = Vec::new(); + for field in fields { + let Some(field_ident) = field.ident else { + return syn::Error::new_spanned(field.ty, "DatabaseEntry derive requires named fields") + .to_compile_error() + .into(); + }; + let mut key: Option = None; + let mut default = false; + for attribute in field + .attrs + .iter() + .filter(|attr| attr.path().is_ident("cultcache")) + { + let parse_result = attribute.parse_nested_meta(|meta| { + if meta.path.is_ident("key") { + let value: LitInt = meta.value()?.parse()?; + key = Some(value.base10_parse()?); + Ok(()) + } else if meta.path.is_ident("default") { + default = true; + Ok(()) + } else { + Err(meta.error("unsupported field cultcache attribute")) + } + }); + if let Err(error) = parse_result { + return error.to_compile_error().into(); + } + } + let Some(key) = key else { + return syn::Error::new_spanned( + field_ident, + "DatabaseEntry fields require #[cultcache(key = N)]", + ) + .to_compile_error() + .into(); + }; + if slots.iter().any(|slot| slot.key == key) { + return syn::Error::new_spanned( + field_ident, + format!("duplicate DatabaseEntry slot {key}"), + ) + .to_compile_error() + .into(); + } + slots.push(CultCacheSlot { + key, + ident: field_ident, + ty: field.ty, + default, + }); + } + slots.sort_by_key(|slot| slot.key); + let max_slot = slots.iter().map(|slot| slot.key).max().unwrap_or(0); + + let serialize_elements = (0..=max_slot).map(|slot| { + if let Some(field) = slots.iter().find(|field| field.key == slot) { + let field_ident = &field.ident; + quote! { + tuple.serialize_element(&self.#field_ident)?; + } + } else { + quote! { + tuple.serialize_element(&())?; + } + } + }); + + let option_declarations = slots.iter().map(|field| { + let variable = format_ident!("slot_{}", field.key); + let ty = &field.ty; + quote! { + let mut #variable: ::std::option::Option<#ty> = ::std::option::Option::None; + } + }); + + let deserialize_match_arms = (0..=max_slot).map(|slot| { + if let Some(field) = slots.iter().find(|field| field.key == slot) { + let variable = format_ident!("slot_{}", field.key); + let ty = &field.ty; + quote! { + #slot => { + #variable = seq.next_element::<#ty>()?; + } + } + } else { + quote! { + #slot => { + let _ = seq.next_element::<::serde::de::IgnoredAny>()?; + } + } + } + }); + + let build_fields = slots.iter().map(|field| { + let field_ident = &field.ident; + let variable = format_ident!("slot_{}", field.key); + if field.default { + quote! { + #field_ident: #variable.unwrap_or_default() + } + } else { + let field_name = field_ident.to_string(); + quote! { + #field_ident: #variable.ok_or_else(|| ::serde::de::Error::missing_field(#field_name))? + } + } + }); + + quote! { + impl ::cultcache_rs::DatabaseEntry for #ident { + const TYPE: &'static str = #entry_type; + const SCHEMA_NAME: &'static str = #schema_name; + } + + impl ::serde::Serialize for #ident { + fn serialize(&self, serializer: S) -> ::std::result::Result + where + S: ::serde::Serializer, + { + use ::serde::ser::SerializeTuple; + let mut tuple = serializer.serialize_tuple(#max_slot + 1)?; + #(#serialize_elements)* + tuple.end() + } + } + + impl<'de> ::serde::Deserialize<'de> for #ident { + fn deserialize(deserializer: D) -> ::std::result::Result + where + D: ::serde::Deserializer<'de>, + { + struct CultCacheVisitor; + + impl<'de> ::serde::de::Visitor<'de> for CultCacheVisitor { + type Value = #ident; + + fn expecting(&self, formatter: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + formatter.write_str(concat!("a CultCache array for ", stringify!(#ident))) + } + + fn visit_seq(self, mut seq: A) -> ::std::result::Result + where + A: ::serde::de::SeqAccess<'de>, + { + #(#option_declarations)* + let mut index = 0usize; + while index <= #max_slot { + match index { + #(#deserialize_match_arms)* + _ => unreachable!(), + } + index += 1; + } + while let ::std::option::Option::Some(_) = seq.next_element::<::serde::de::IgnoredAny>()? {} + Ok(#ident { + #(#build_fields),* + }) + } + } + + deserializer.deserialize_tuple(#max_slot + 1, CultCacheVisitor) + } + } + } + .into() +} + +struct CultCacheSlot { + key: usize, + ident: syn::Ident, + ty: Type, + default: bool, +} diff --git a/examples/cultcache_interop.rs b/examples/cultcache_interop.rs new file mode 100644 index 00000000..4049acf8 --- /dev/null +++ b/examples/cultcache_interop.rs @@ -0,0 +1,121 @@ +use anyhow::Result; +use anyhow::anyhow; +use cultcache_rs::CultCache; +use cultcache_rs::DatabaseEntry; +use cultcache_rs::SingleFileMessagePackBackingStore; +use serde_json::json; +use std::collections::BTreeMap; + +const INTEROP_TYPE: &str = "cultcache.interop-note"; +const INTEROP_SCHEMA_VERSION: &str = "cultcache.interop_note.v1"; + +#[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] +#[cultcache(type = "cultcache.interop-note", schema = "CultCacheInteropNote")] +struct CultCacheInteropNote { + #[cultcache(key = 0)] + schema_version: String, + #[cultcache(key = 1)] + document_id: String, + #[cultcache(key = 2)] + author_runtime_id: String, + #[cultcache(key = 3)] + title: String, + #[cultcache(key = 4)] + body: String, + #[cultcache(key = 5, default)] + tags: Vec, +} + +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); + let mode = args + .next() + .ok_or_else(|| anyhow!("expected mode: write | read"))?; + let options = parse_args(args.collect()); + let file = require_arg(&options, "file")?; + + match mode.as_str() { + "write" => write_note(file, require_arg(&options, "runtime-id")?)?, + "read" => read_note(file)?, + _ => return Err(anyhow!("unknown mode {mode}")), + } + Ok(()) +} + +fn write_note(file: &str, runtime_id: &str) -> Result<()> { + let mut cache = build_cache(file)?; + cache.pull_all_backing_stores()?; + let note = CultCacheInteropNote { + schema_version: INTEROP_SCHEMA_VERSION.to_string(), + document_id: format!("note:{runtime_id}"), + author_runtime_id: runtime_id.to_string(), + title: format!("{runtime_id} wrote a CultCache note"), + body: "The v1 store format is the contract.".to_string(), + tags: vec![ + runtime_id.to_string(), + "rust".to_string(), + "interop".to_string(), + ], + }; + cache.put(¬e.document_id, ¬e)?; + print_note(¬e) +} + +fn read_note(file: &str) -> Result<()> { + let mut cache = build_cache(file)?; + cache.pull_all_backing_stores()?; + let notes = cache.get_all::()?; + let note = notes + .first() + .ok_or_else(|| anyhow!("no {INTEROP_TYPE} records found"))?; + print_note(note) +} + +fn build_cache(file: &str) -> Result { + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(file)); + Ok(cache) +} + +fn print_note(note: &CultCacheInteropNote) -> Result<()> { + println!( + "{}", + serde_json::to_string(&json!({ + "schemaVersion": note.schema_version, + "documentId": note.document_id, + "authorRuntimeId": note.author_runtime_id, + "title": note.title, + "body": note.body, + "tags": note.tags, + }))? + ); + Ok(()) +} + +fn parse_args(args: Vec) -> BTreeMap { + let mut parsed = BTreeMap::new(); + let mut index = 0; + while index < args.len() { + let token = &args[index]; + if !token.starts_with("--") { + index += 1; + continue; + } + let name = token.trim_start_matches("--").to_string(); + let value = args + .get(index + 1) + .cloned() + .unwrap_or_else(|| panic!("missing value for --{name}")); + parsed.insert(name, value); + index += 2; + } + parsed +} + +fn require_arg<'a>(options: &'a BTreeMap, name: &str) -> Result<&'a str> { + options + .get(name) + .map(String::as_str) + .ok_or_else(|| anyhow!("missing required argument --{name}")) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..cb56b8a8 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,885 @@ +use anyhow::Context; +use anyhow::Result; +use anyhow::anyhow; +use serde::Serialize; +use serde::de::DeserializeOwned; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use std::fs; +use std::fs::File; +use std::fs::OpenOptions; +use std::path::Path; +use std::path::PathBuf; + +extern crate self as cultcache_rs; + +pub use cultcache_rs_derive::DatabaseEntry; + +pub trait DatabaseEntry: Serialize + DeserializeOwned + Clone + Send + 'static { + const TYPE: &'static str; + const SCHEMA_NAME: &'static str = "DatabaseEntry"; +} + +pub trait CultCacheRegistry { + fn register_entries(&self, cache: &mut CultCache) -> Result<()>; +} + +#[macro_export] +macro_rules! cultcache_registry { + ($name:ident { $($entry:ty),* $(,)? }) => { + #[derive(Clone, Copy, Debug, Default)] + pub struct $name; + + impl $crate::CultCacheRegistry for $name { + fn register_entries(&self, cache: &mut $crate::CultCache) -> ::anyhow::Result<()> { + $( + cache.register_entry_type::<$entry>()?; + )* + Ok(()) + } + } + }; +} + +#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct CultCacheEnvelope { + pub key: String, + #[serde(rename = "type")] + pub r#type: String, + #[serde(with = "serde_bytes")] + pub payload: Vec, + pub stored_at: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub schema_id: Option, +} + +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] +struct PersistedStoreSnapshot( + String, + Vec, + Vec, +); + +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] +struct PersistedSchemaCatalogEntry( + String, + String, + String, + String, + String, + Vec, + Vec, +); + +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] +struct PersistedSchemaCatalogMember( + u32, + String, + String, + bool, + bool, + Option, + bool, + Option, +); + +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] +struct PersistedRecord( + String, + String, + String, + #[serde(with = "serde_bytes")] Vec, +); + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct PushAllOptions { + pub soft: bool, +} + +pub trait CacheBackingStore: Send { + fn pull_all(&self) -> Result>; + fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()>; + fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()>; + + fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> { + let existing = self.pull_all()?; + for entry in existing { + self.delete(&entry)?; + } + for entry in entries { + self.push(entry)?; + } + Ok(()) + } +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SingleFileMessagePackBackingStore { + path: PathBuf, +} + +impl SingleFileMessagePackBackingStore { + pub fn new(path: impl Into) -> Self { + Self { path: path.into() } + } + + pub fn path(&self) -> &Path { + &self.path + } + + fn read_all_unlocked(&self) -> Result> { + if !self.path.exists() { + return Ok(Vec::new()); + } + let bytes = fs::read(&self.path) + .with_context(|| format!("failed to read {}", self.path.display()))?; + if bytes.is_empty() { + return Ok(Vec::new()); + } + decode_store_snapshot(&bytes).or_else(|_| { + rmp_serde::from_slice(&bytes) + .with_context(|| format!("failed to decode MessagePack {}", self.path.display())) + }) + } + + fn write_all_unlocked(&self, entries: &[CultCacheEnvelope]) -> Result<()> { + if let Some(parent) = self.path.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("failed to create {}", parent.display()))?; + } + let bytes = rmp_serde::to_vec(&encode_store_snapshot(entries)) + .context("failed to encode MessagePack")?; + let tmp_path = temporary_path_for(&self.path); + fs::write(&tmp_path, bytes) + .with_context(|| format!("failed to write {}", tmp_path.display()))?; + if self.path.exists() { + fs::remove_file(&self.path) + .with_context(|| format!("failed to replace {}", self.path.display()))?; + } + fs::rename(&tmp_path, &self.path).with_context(|| { + format!( + "failed to move {} to {}", + tmp_path.display(), + self.path.display() + ) + })?; + Ok(()) + } + + fn with_shared_lock(&self, action: impl FnOnce() -> Result) -> Result { + let lock = self.open_lock_file()?; + fs2::FileExt::lock_shared(&lock) + .with_context(|| format!("failed to lock {}", self.lock_path().display()))?; + let result = action(); + fs2::FileExt::unlock(&lock) + .with_context(|| format!("failed to unlock {}", self.lock_path().display()))?; + result + } + + fn with_exclusive_lock(&self, action: impl FnOnce() -> Result) -> Result { + let lock = self.open_lock_file()?; + fs2::FileExt::lock_exclusive(&lock) + .with_context(|| format!("failed to lock {}", self.lock_path().display()))?; + let result = action(); + fs2::FileExt::unlock(&lock) + .with_context(|| format!("failed to unlock {}", self.lock_path().display()))?; + result + } + + fn open_lock_file(&self) -> Result { + let lock_path = self.lock_path(); + if let Some(parent) = lock_path.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("failed to create {}", parent.display()))?; + } + OpenOptions::new() + .create(true) + .read(true) + .truncate(false) + .write(true) + .open(&lock_path) + .with_context(|| format!("failed to open {}", lock_path.display())) + } + + fn lock_path(&self) -> PathBuf { + let mut lock_name = self + .path + .file_name() + .map(|value| value.to_os_string()) + .unwrap_or_else(|| "cultcache.msgpack".into()); + lock_name.push(".lock"); + self.path.with_file_name(lock_name) + } +} + +impl CacheBackingStore for SingleFileMessagePackBackingStore { + fn pull_all(&self) -> Result> { + self.with_shared_lock(|| self.read_all_unlocked()) + } + + fn push(&mut self, entry: &CultCacheEnvelope) -> Result<()> { + self.with_exclusive_lock(|| { + let mut entries = self.read_all_unlocked()?; + entries.retain(|candidate| entry_id(candidate) != entry_id(entry)); + entries.push(entry.clone()); + entries.sort_by_key(entry_id); + self.write_all_unlocked(&entries) + }) + } + + fn delete(&mut self, entry: &CultCacheEnvelope) -> Result<()> { + self.with_exclusive_lock(|| { + let mut entries = self.read_all_unlocked()?; + entries.retain(|candidate| entry_id(candidate) != entry_id(entry)); + self.write_all_unlocked(&entries) + }) + } + + fn push_all(&mut self, entries: &[CultCacheEnvelope], _options: PushAllOptions) -> Result<()> { + self.with_exclusive_lock(|| { + let mut entries = entries.to_vec(); + entries.sort_by_key(entry_id); + self.write_all_unlocked(&entries) + }) + } +} + +struct CultCacheStoreRegistration { + store: Box, + types: BTreeSet, +} + +pub struct CultCache { + definitions: BTreeMap, + entries: BTreeMap, + stores: Vec, +} + +impl CultCache { + pub fn new() -> Self { + Self { + definitions: BTreeMap::new(), + entries: BTreeMap::new(), + stores: Vec::new(), + } + } + + pub fn register_entry_type(&mut self) -> Result<()> { + if T::TYPE.trim().is_empty() { + return Err(anyhow!( + "CultCache entry types must declare a non-empty type" + )); + } + if let Some(existing_schema) = self.definitions.get(T::TYPE) + && *existing_schema != T::SCHEMA_NAME + { + return Err(anyhow!( + "CultCache already has a different definition registered for type {:?}", + T::TYPE + )); + } + self.definitions.insert(T::TYPE.to_string(), T::SCHEMA_NAME); + Ok(()) + } + + pub fn register_document_type(&mut self) -> Result<()> { + self.register_entry_type::() + } + + pub fn register_registry(&mut self, registry: R) -> Result<&mut Self> { + registry.register_entries(self)?; + Ok(self) + } + + pub fn add_backing_store( + &mut self, + store: impl CacheBackingStore + 'static, + types: impl IntoIterator>, + ) { + self.stores.push(CultCacheStoreRegistration { + store: Box::new(store), + types: types.into_iter().map(Into::into).collect(), + }); + } + + pub fn add_generic_backing_store(&mut self, store: impl CacheBackingStore + 'static) { + self.add_backing_store(store, Vec::::new()); + } + + pub fn pull_all_backing_stores(&mut self) -> Result<()> { + self.entries.clear(); + let known_types: BTreeSet = self.definitions.keys().cloned().collect(); + for registration in &mut self.stores { + for entry in registration.store.pull_all()? { + if !known_types.contains(&entry.r#type) { + return Err(anyhow!( + "No schema is registered for persisted entry type {:?}", + entry.r#type + )); + } + self.entries.insert(entry_id(&entry), entry); + } + } + Ok(()) + } + + pub fn get(&self, key: &str) -> Result> { + self.require_entry_type::()?; + let Some(entry) = self.entries.get(&entry_id_parts(T::TYPE, key)) else { + return Ok(None); + }; + let payload = rmp_serde::from_slice(&entry.payload).with_context(|| { + format!( + "failed to decode CultCache entry {:?} at key {:?} as {}", + T::TYPE, + key, + T::SCHEMA_NAME + ) + })?; + Ok(Some(payload)) + } + + pub fn get_required(&self, key: &str) -> Result { + self.get::(key)? + .ok_or_else(|| anyhow!("CultCache has no {:?} entry at key {:?}", T::TYPE, key)) + } + + pub fn get_envelope(&self, key: &str) -> Result> { + self.require_entry_type::()?; + Ok(self.entries.get(&entry_id_parts(T::TYPE, key)).cloned()) + } + + pub fn get_required_envelope(&self, key: &str) -> Result { + self.get_envelope::(key)? + .ok_or_else(|| anyhow!("CultCache has no {:?} envelope at key {:?}", T::TYPE, key)) + } + + pub fn get_all(&self) -> Result> { + self.require_entry_type::()?; + let mut values = Vec::new(); + for entry in self.entries.values() { + if entry.r#type != T::TYPE { + continue; + } + values.push(rmp_serde::from_slice(&entry.payload).with_context(|| { + format!( + "failed to decode CultCache entry {:?} at key {:?} as {}", + T::TYPE, + entry.key, + T::SCHEMA_NAME + ) + })?); + } + Ok(values) + } + + pub fn put(&mut self, key: impl Into, value: &T) -> Result { + self.require_entry_type::()?; + let key = key.into(); + let payload = rmp_serde::to_vec(value).with_context(|| { + format!( + "failed to encode CultCache entry {:?} at key {:?} as {}", + T::TYPE, + key, + T::SCHEMA_NAME + ) + })?; + let parsed: T = rmp_serde::from_slice(&payload).with_context(|| { + format!( + "failed to validate CultCache entry {:?} at key {:?} as {}", + T::TYPE, + key, + T::SCHEMA_NAME + ) + })?; + let entry = CultCacheEnvelope { + key: key.clone(), + r#type: T::TYPE.to_string(), + payload, + stored_at: now_utc_second(), + schema_id: Some(T::TYPE.to_string()), + }; + let route = self.resolve_route_indices(T::TYPE); + let Some(primary_index) = route.first().copied() else { + return Err(anyhow!( + "No backing store is registered for entry type {:?}", + T::TYPE + )); + }; + self.stores[primary_index].store.push(&entry)?; + for mirror_index in route.iter().skip(1).copied() { + self.stores[mirror_index].store.push(&entry)?; + } + self.entries.insert(entry_id(&entry), entry); + Ok(parsed) + } + + pub fn put_envelope(&mut self, entry: CultCacheEnvelope) -> Result { + self.require_entry_type::()?; + if entry.r#type != T::TYPE { + return Err(anyhow!( + "CultCache envelope type {:?} does not match registered Rust type {:?}", + entry.r#type, + T::TYPE + )); + } + if entry.key.trim().is_empty() { + return Err(anyhow!( + "CultCache envelope keys for type {:?} must be non-empty", + T::TYPE + )); + } + if entry.stored_at.trim().is_empty() { + return Err(anyhow!( + "CultCache envelope stored_at for type {:?} must be non-empty", + T::TYPE + )); + } + + let parsed: T = rmp_serde::from_slice(&entry.payload).with_context(|| { + format!( + "failed to validate CultCache envelope {:?} at key {:?} as {}", + T::TYPE, + entry.key, + T::SCHEMA_NAME + ) + })?; + let route = self.resolve_route_indices(T::TYPE); + let Some(primary_index) = route.first().copied() else { + return Err(anyhow!( + "No backing store is registered for entry type {:?}", + T::TYPE + )); + }; + self.stores[primary_index].store.push(&entry)?; + for mirror_index in route.iter().skip(1).copied() { + self.stores[mirror_index].store.push(&entry)?; + } + self.entries.insert(entry_id(&entry), entry); + Ok(parsed) + } + + pub fn update(&mut self, key: &str, updater: F) -> Result + where + T: DatabaseEntry, + F: FnOnce(Option) -> T, + { + let current = self.get::(key)?; + self.put::(key.to_string(), &updater(current)) + } + + pub fn delete(&mut self, key: &str) -> Result { + self.require_entry_type::()?; + let id = entry_id_parts(T::TYPE, key); + let Some(entry) = self.entries.get(&id).cloned() else { + return Ok(false); + }; + let route = self.resolve_route_indices(T::TYPE); + let Some(primary_index) = route.first().copied() else { + return Err(anyhow!( + "No backing store is registered for entry type {:?}", + T::TYPE + )); + }; + self.stores[primary_index].store.delete(&entry)?; + for mirror_index in route.iter().skip(1).copied() { + self.stores[mirror_index].store.delete(&entry)?; + } + self.entries.remove(&id); + Ok(true) + } + + pub fn snapshot(&self) -> Vec { + self.entries.values().cloned().collect() + } + + fn require_entry_type(&self) -> Result<()> { + match self.definitions.get(T::TYPE) { + Some(schema_name) if *schema_name == T::SCHEMA_NAME => Ok(()), + _ => Err(anyhow!( + "CultCache entry type {:?} is not registered on this cache instance", + T::TYPE + )), + } + } + + fn resolve_route_indices(&self, type_id: &str) -> Vec { + let type_specific: Vec = self + .stores + .iter() + .enumerate() + .filter_map(|(index, registration)| { + registration.types.contains(type_id).then_some(index) + }) + .collect(); + if !type_specific.is_empty() { + return type_specific; + } + self.stores + .iter() + .enumerate() + .filter_map(|(index, registration)| registration.types.is_empty().then_some(index)) + .collect() + } +} + +impl Default for CultCache { + fn default() -> Self { + Self::new() + } +} + +fn entry_id(entry: &CultCacheEnvelope) -> String { + entry_id_parts(&entry.r#type, &entry.key) +} + +fn entry_id_parts(r#type: &str, key: &str) -> String { + format!("{type}::{key}", type = r#type) +} + +fn now_utc_second() -> String { + chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true) +} + +fn encode_store_snapshot(entries: &[CultCacheEnvelope]) -> PersistedStoreSnapshot { + let mut schema_names = BTreeSet::::new(); + for entry in entries { + schema_names.insert( + entry + .schema_id + .clone() + .unwrap_or_else(|| entry.r#type.clone()), + ); + } + + let catalog = schema_names + .into_iter() + .map(|schema_id| { + PersistedSchemaCatalogEntry( + schema_id.clone(), + schema_id.clone(), + format!("{schema_id}.v1"), + schema_id.clone(), + format!( + "{{\"schemaName\":\"{}\",\"schemaVersion\":\"{}.v1\",\"members\":[]}}", + escape_json_string(&schema_id), + escape_json_string(&schema_id) + ), + vec![schema_id], + Vec::new(), + ) + }) + .collect(); + let records = entries + .iter() + .map(|entry| { + PersistedRecord( + entry.key.clone(), + entry + .schema_id + .clone() + .unwrap_or_else(|| entry.r#type.clone()), + entry.stored_at.clone(), + entry.payload.clone(), + ) + }) + .collect(); + + PersistedStoreSnapshot("cultcache.store.v1".to_string(), catalog, records) +} + +fn decode_store_snapshot(bytes: &[u8]) -> Result> { + let snapshot: PersistedStoreSnapshot = + rmp_serde::from_slice(bytes).context("failed to decode CultCache v1 snapshot")?; + if snapshot.0 != "cultcache.store.v1" { + return Err(anyhow!("unsupported CultCache snapshot {}", snapshot.0)); + } + + let catalog = snapshot + .1 + .into_iter() + .map(|entry| (entry.0, entry.1)) + .collect::>(); + snapshot + .2 + .into_iter() + .map(|record| { + let r#type = catalog.get(&record.1).cloned().ok_or_else(|| { + anyhow!( + "CultCache record {:?} references missing schema {:?}", + record.0, + record.1 + ) + })?; + Ok(CultCacheEnvelope { + key: record.0, + r#type, + stored_at: record.2, + payload: record.3, + schema_id: Some(record.1), + }) + }) + .collect() +} + +fn escape_json_string(value: &str) -> String { + value.replace('\\', "\\\\").replace('"', "\\\"") +} + +fn temporary_path_for(path: &Path) -> PathBuf { + let mut file_name = path + .file_name() + .map(|value| value.to_os_string()) + .unwrap_or_else(|| "cultcache.msgpack".into()); + file_name.push(format!(".{}.tmp", uuid::Uuid::new_v4())); + path.with_file_name(file_name) +} + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] + #[cultcache(type = "settings")] + struct Settings { + #[cultcache(key = 0)] + theme: String, + #[cultcache(key = 1, default)] + retries: u32, + } + + #[derive(Clone, Debug, PartialEq, Eq, DatabaseEntry)] + #[cultcache(type = "note")] + struct Note { + #[cultcache(key = 0)] + title: String, + #[cultcache(key = 1)] + body: String, + } + + cultcache_registry!(TestEntries { Settings, Note }); + + #[test] + fn familiar_cultcache_flow_persists_and_reloads_typed_documents() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let settings = Settings { + theme: "ash".to_string(), + retries: 3, + }; + + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + cache.pull_all_backing_stores()?; + cache.put("app", &settings)?; + assert_eq!(cache.get_required::("app")?, settings); + + let mut reloaded = CultCache::new(); + reloaded.register_entry_type::()?; + reloaded.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + reloaded.pull_all_backing_stores()?; + assert_eq!(reloaded.get_required::("app")?, settings); + Ok(()) + } + + #[test] + fn entry_identity_is_polymorphic_by_type_and_key() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let mut cache = CultCache::new(); + cache.register_registry(TestEntries)?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + + cache.put( + "shared", + &Settings { + theme: "green".to_string(), + retries: 1, + }, + )?; + cache.put( + "shared", + &Note { + title: "same key".to_string(), + body: "different type".to_string(), + }, + )?; + + assert_eq!(cache.snapshot().len(), 2); + assert_eq!(cache.get_required::("shared")?.title, "same key"); + assert_eq!(cache.get_required::("shared")?.theme, "green"); + Ok(()) + } + + #[test] + fn type_specific_store_routes_before_generic_store() -> Result<()> { + let temp = tempfile::tempdir()?; + let generic_path = temp.path().join("generic.msgpack"); + let settings_path = temp.path().join("settings.msgpack"); + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&generic_path)); + cache.add_backing_store( + SingleFileMessagePackBackingStore::new(&settings_path), + ["settings"], + ); + + cache.put( + "app", + &Settings { + theme: "ash".to_string(), + retries: 3, + }, + )?; + cache.put( + "memo", + &Note { + title: "hello".to_string(), + body: "world".to_string(), + }, + )?; + + let generic_entries = SingleFileMessagePackBackingStore::new(&generic_path).pull_all()?; + let settings_entries = SingleFileMessagePackBackingStore::new(&settings_path).pull_all()?; + assert_eq!(generic_entries[0].r#type, "note"); + assert_eq!(settings_entries[0].r#type, "settings"); + Ok(()) + } + + #[test] + fn update_and_delete_follow_the_cache_api() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + + let updated = cache.update::("app", |current| { + let mut current = current.unwrap_or(Settings { + theme: "ash".to_string(), + retries: 0, + }); + current.retries += 1; + current + })?; + assert_eq!(updated.retries, 1); + assert!(cache.delete::("app")?); + assert!(cache.get::("app")?.is_none()); + Ok(()) + } + + #[test] + fn pull_rejects_unregistered_persisted_entry_type() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let mut store = SingleFileMessagePackBackingStore::new(&store_path); + store.push(&CultCacheEnvelope { + key: "unknown".to_string(), + r#type: "unregistered".to_string(), + payload: rmp_serde::to_vec(&1_u8)?, + stored_at: now_utc_second(), + schema_id: Some("unregistered".to_string()), + })?; + + let mut cache = CultCache::new(); + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + let error = cache.pull_all_backing_stores().unwrap_err(); + assert!( + error + .to_string() + .contains("No schema is registered for persisted entry type") + ); + Ok(()) + } + + #[test] + fn payload_is_binary_messagepack_not_json_value() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + cache.put( + "app", + &Settings { + theme: "ash".to_string(), + retries: 3, + }, + )?; + + let entry = cache.snapshot().remove(0); + let decoded: Settings = rmp_serde::from_slice(&entry.payload)?; + assert_eq!(decoded.theme, "ash"); + assert!(!entry.payload.is_empty()); + Ok(()) + } + + #[test] + fn corrupted_payload_fails_during_typed_retrieval() -> Result<()> { + let temp = tempfile::tempdir()?; + let store_path = temp.path().join("cache.msgpack"); + let mut store = SingleFileMessagePackBackingStore::new(&store_path); + store.push(&CultCacheEnvelope { + key: "app".to_string(), + r#type: "settings".to_string(), + payload: vec![0xc1], + stored_at: now_utc_second(), + schema_id: Some("settings".to_string()), + })?; + + let mut cache = CultCache::new(); + cache.register_entry_type::()?; + cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&store_path)); + cache.pull_all_backing_stores()?; + let error = cache.get_required::("app").unwrap_err(); + assert!( + error + .to_string() + .contains("failed to decode CultCache entry") + ); + Ok(()) + } + + #[test] + fn put_envelope_reuses_existing_messagepack_payload() -> Result<()> { + let temp = tempfile::tempdir()?; + let origin_store = temp.path().join("origin.msgpack"); + let target_store = temp.path().join("target.msgpack"); + + let mut origin = CultCache::new(); + origin.register_entry_type::()?; + origin.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&origin_store)); + origin.put( + "app", + &Settings { + theme: "ash".to_string(), + retries: 3, + }, + )?; + + let envelope = origin.get_required_envelope::("app")?; + + let mut target = CultCache::new(); + target.register_entry_type::()?; + target.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&target_store)); + let applied = target.put_envelope::(envelope.clone())?; + + assert_eq!( + applied, + Settings { + theme: "ash".to_string(), + retries: 3, + } + ); + assert_eq!(target.get_required::("app")?, applied); + assert_eq!( + target.get_required_envelope::("app")?.payload, + envelope.payload + ); + Ok(()) + } +}