Caching: the attach lends the store no codec; a pulled variant carries none (Soul s3 codec-loan-pinned-by-reflection)

Deletes CacheBackingStore._codec and UseRegistry's codec parameter, the reflection LentCodec
test helper and the bare-store codec test. The cache re-resolves every pulled variant with its
own codec when it admits the load.

Not yet built or tested; the verify job at the head covers every commit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-25 20:07:34 +01:00
commit 295738fdd8
3 changed files with 18 additions and 54 deletions

View file

@ -2294,7 +2294,7 @@ namespace GameCult.Caching
"attach routed stores before the untyped store.");
}
store.UseRegistry(_registry, _codec);
store.UseRegistry(_registry);
store.Loaded = (loaded, dropped) => Admit(loaded, dropped, store, (_, _) => CultCommitOutcome.Committed);
_stores.Add(candidate);
try
@ -2305,7 +2305,7 @@ namespace GameCult.Caching
{
_stores.Remove(candidate);
store.Loaded = null;
store.UseRegistry(null, null);
store.UseRegistry(null);
throw;
}
store.Cache = this;
@ -3543,8 +3543,6 @@ namespace GameCult.Caching
public abstract class CacheBackingStore : IDisposable
{
private CultDocumentRegistry? _registry;
// Lent with the registry by UseRegistry: the attaching cache's codec, which a pull builds variants with. Null on a cacheless store.
private CultCodec? _codec;
private CultSchemaMigrationReport[] _lastSchemaMigrationReports = Array.Empty<CultSchemaMigrationReport>();
protected CacheBackingStore(bool readOnly = false)
@ -3675,14 +3673,14 @@ namespace GameCult.Caching
private protected void Cacheless(CultDocumentRegistry? registry, Action body) =>
Gate(() =>
{
UseRegistry(registry, null);
UseRegistry(registry);
try
{
body();
}
finally
{
UseRegistry(null, null);
UseRegistry(null);
}
return true;
}, cacheless: true);
@ -3690,14 +3688,10 @@ namespace GameCult.Caching
// The store's own gate, taken only here. Lock order: a cache's gate, then a store's; nothing takes a store's gate and then a cache's.
internal T UnderStoreGate<T>(Func<T> body) => CultCache.InStoreHold(_storeGate, body);
// The one writer of what an attach lends the store, the registry and the codec: a cacheless call for the call (no codec), an attach
// for as long as it holds the store, and a failed attach puts both back, so a cacheless store holds none between calls and a pull
// on one loads nothing. The pull reads the codec here, not from Cache, which is published only after the pull succeeds.
internal void UseRegistry(CultDocumentRegistry? registry, CultCodec? codec)
{
_registry = registry;
_codec = codec;
}
// The one writer of the lent registry: a cacheless call for the call, an attach for as long as it holds the store, and a failed
// attach puts it back, so a cacheless store holds none between calls and a pull on one loads nothing. Nothing else is lent, and the
// pull does not read Cache, which is published only after the pull succeeds.
internal void UseRegistry(CultDocumentRegistry? registry) => _registry = registry;
public abstract void PullAll();
public abstract void Push(CultStoredDocument entry);
@ -3782,14 +3776,16 @@ namespace GameCult.Caching
})).ToArray();
}
// The cache resolves it against its base when it admits the load.
// The store holds the delta and the cache resolves it against its base, with its own codec, when it admits the load. A
// codecless variant counts as holding ids, but a header reads HoldsIds only on staged entries, which are always the cache's
// pushed documents.
return new CultStoredDocument(
new CultRecordKey(record.Key),
record.StoredAt,
descriptor,
new CultVariantDelta(record.Variant.BaseKey, record.Variant.Overrides.Where(Has).ToArray()),
null,
_codec)
null)
{
StoredSchemaId = record.SchemaId,
StoredVersion = version

View file

@ -1123,13 +1123,10 @@ namespace GameCult.Caching.Tests
Assert.That(File.Exists(_path), Is.False);
}
private static CultCodec? LentCodec(CacheBackingStore store) =>
(CultCodec?)typeof(CacheBackingStore).GetField("_codec", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)!.GetValue(store);
// The attach lends the pull its registry and its codec, and publishes the cache only after the pull: while the pull runs the store
// holds both loans and no cache; once it returns the cache is there.
// The attach lends the pull its registry, and publishes the cache only after the pull: while the pull runs the store holds the
// loan and no cache; once it returns the cache is there.
[Test]
public void AnAttachLendsTheCodecAndPublishesTheCacheAfterThePull()
public void AnAttachLendsTheRegistryAndPublishesTheCacheAfterThePull()
{
var registry = CultDocumentRegistry.ForTypes(new[] { typeof(IdDeck) });
new SingleFileMessagePackBackingStore(_path).WriteDocument(new CultRecordKey("a"), new IdDeck { Name = "a" }, registry: registry);
@ -1138,14 +1135,12 @@ namespace GameCult.Caching.Tests
using var cache = new CultCache(registry, codec);
bool cacheDuringPull = true;
CultDocumentRegistry? registryDuringPull = null;
CultCodec? codecDuringPull = null;
var attach = OnThread("hook", () =>
{
RoleStore.RoleHook = () =>
{
cacheDuringPull = store.IsAttachedToACache;
registryDuringPull = store.Attached;
codecDuringPull = LentCodec(store);
};
try { cache.AddBackingStore(store); }
finally { RoleStore.RoleHook = null; }
@ -1155,13 +1150,12 @@ namespace GameCult.Caching.Tests
Assert.That(attach.Error(), Is.Null);
Assert.That(cacheDuringPull, Is.False, "the cache is not visible to the pull");
Assert.That(registryDuringPull, Is.SameAs(registry));
Assert.That(codecDuringPull, Is.SameAs(codec), "the pull reads the codec the attach lent");
Assert.That(store.IsAttachedToACache, Is.True, "published once the pull succeeded");
}
// A failed attach takes back both loans through the one call, and never shows the cache.
// A failed attach takes back the registry through the one call, and never shows the cache.
[Test]
public void AFailedAttachTakesBackTheRegistryAndTheCodecAndNeverShowsTheCache()
public void AFailedAttachTakesBackTheRegistryAndNeverShowsTheCache()
{
var registry = CultDocumentRegistry.ForTypes(new[] { typeof(IdDeck) });
new SingleFileMessagePackBackingStore(_path).WriteDocument(new CultRecordKey("a"), new IdDeck { Name = "a" }, registry: registry);
@ -1174,7 +1168,6 @@ namespace GameCult.Caching.Tests
Assert.That(attach.Error(), Is.Not.Null);
Assert.That(store.IsAttachedToACache, Is.False);
Assert.That(store.Attached, Is.Null, "the registry was taken back");
Assert.That(LentCodec(store), Is.Null, "the codec was taken back with it");
}
// The bound on a call's passes is structural: Cache goes from null to a cache once. This test writes it any other way (the

View file

@ -902,7 +902,7 @@ namespace GameCult.Caching.Tests
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v2"), "read with a codec, a name override holds no id");
var blind = new SingleFileMessagePackBackingStore(path);
blind.UseRegistry(Registry, null);
blind.UseRegistry(Registry);
blind.PullAll();
blind.PushAll();
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v2"), "a flush that staged nothing copies the variant and its header");
@ -918,31 +918,6 @@ namespace GameCult.Caching.Tests
Assert.That(HeaderOf(path), Is.EqualTo("cultcache.store.v3"), "read without a codec, the variant it writes cannot be seen, so it is marked");
}
// The pull builds a variant with the codec the attach lent the store, so a variant loaded during an attach (the cache is not
// published yet) reports what its values hold: a name override holds no id. Without the lent codec it would count as holding one.
[Test]
public void AVariantLoadedByAPullReadsItsValuesWithTheCodecTheAttachLent()
{
var path = PathOf("lent-codec-variant.cc");
using (var cache = Open(path))
{
cache.Commit(batch =>
{
batch.Upsert(typeof(IdDeck), EmptyDeck("base"), new CultRecordKey("base"));
batch.UpsertVariant(new CultRecordKey("v"), new CultRecordKey("base"), new[] { cache.Override<IdDeck>(nameof(IdDeck.Name), "v") });
});
}
CultStoredDocument? variant = null;
var store = new SingleFileMessagePackBackingStore(path);
store.Loaded = (loaded, _) => variant = loaded.Single(entry => entry.Variant != null);
store.UseRegistry(Registry, CultCacheMessagePack.CreateCodec(Registry));
store.PullAll();
Assert.That(variant, Is.Not.Null);
Assert.That(variant!.HoldsIds, Is.False, "read with the lent codec, a name override holds no id");
}
[Test]
public void ADirectoryStoreIsMarkedByTheIdsItWritesAndAnEmptyDeckDoesNotMarkIt()
{