Write ownership by the written type: one per-key predicate, the registry leaves upserts
An upsert may replace the record at its key only when the type it writes there owns the record (CultDocumentRegistry.WrittenOwns): the entry declares the stored id, or reads the stored entry whole under its own schema name. Cache and cacheless writers, single-file and directory stores ask the same per-key judge (CacheBackingStore.UndeclaredAt); a cache no longer replaces another registered type's record. Registry.Owns is the removal/check rule only. Deleted: CatalogEntryLookup's registry half, CultDocumentRegistry.Lookup, Owns<T>'s mayClaim, the per-batch union and the per-id memo. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a4eae90561
commit
340c9507e4
5 changed files with 173 additions and 159 deletions
|
|
@ -185,7 +185,11 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
manifest.Records.ToDictionary(record => record.Key, record => VersionOf(record.Payload), StringComparer.Ordinal), Entries.Values, ForeignRecords))
|
||||
return CultCommitOutcome.Mismatch;
|
||||
|
||||
RefuseUndeclaredOnDisk(manifest, request.Upserts.Concat(request.Deletes).Select(entry => entry.Key.Value).Concat(request.Checks).Concat(StagedKeys));
|
||||
RefuseUndeclaredOnDisk(
|
||||
manifest,
|
||||
request.Upserts.Concat(request.Deletes).Select(entry => entry.Key.Value).Concat(request.Checks).Concat(StagedKeys),
|
||||
request.Upserts,
|
||||
request.Deletes);
|
||||
if (request.Upserts.Count == 0 && request.Deletes.Count == 0 && !IsDirty)
|
||||
{
|
||||
// Nothing to write: the batch only judged keys it does not hold.
|
||||
|
|
@ -243,7 +247,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
Directory.CreateDirectory(_manifestFile.DirectoryName!);
|
||||
using var commitLease = AcquireCommitLease(wait: true, create: true);
|
||||
var (manifest, catalogBytes) = ReadManifest();
|
||||
RefuseUndeclaredOnDisk(manifest, StagedKeys);
|
||||
RefuseUndeclaredOnDisk(manifest, StagedKeys, Array.Empty<CultStoredDocument>(), Array.Empty<CultStoredDocument>());
|
||||
if (IsDirty)
|
||||
WriteGeneration(manifest, catalogBytes);
|
||||
else
|
||||
|
|
@ -275,7 +279,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
using var commitLease = AcquireCommitLease(wait, create: true);
|
||||
if (commitLease == null)
|
||||
return CultCommitOutcome.Contended;
|
||||
RefuseUndeclaredOnDisk(ReadManifest().Snapshot, keys);
|
||||
RefuseUndeclaredOnDisk(ReadManifest().Snapshot, keys, Array.Empty<CultStoredDocument>(), Array.Empty<CultStoredDocument>());
|
||||
return CultCommitOutcome.Committed;
|
||||
});
|
||||
}
|
||||
|
|
@ -286,14 +290,22 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
|
||||
// The manifest's other records stay as they are, foreign and read-only ones included; a write that would replace or remove a
|
||||
// record the manifest holds that the writer does not own is refused before any page is written, by the one rule every
|
||||
// store shares (CacheBackingStore.RefuseUndeclaredOnDisk). The refused records reload as a pull loads them.
|
||||
private void RefuseUndeclaredOnDisk(CultPersistedStoreSnapshot currentManifest, IEnumerable<string> written)
|
||||
// store shares (CacheBackingStore.UndeclaredAt, CacheBackingStore.RefuseUndeclaredOnDisk). The refused records reload as a
|
||||
// pull loads them. A key is owned by the entry the write puts there: the batch's upsert, or the staged document. A key it
|
||||
// removes (the batch's deletes, the staged deletes) or only checks goes to the registry's removal rule.
|
||||
private void RefuseUndeclaredOnDisk(CultPersistedStoreSnapshot currentManifest, IEnumerable<string> written, IReadOnlyList<CultStoredDocument> upserts, IReadOnlyList<CultStoredDocument> deletes)
|
||||
{
|
||||
CultPersistedRecord? HeldAt(string key) =>
|
||||
currentManifest.Records.FirstOrDefault(record => string.Equals(record.Key, key, StringComparison.Ordinal));
|
||||
var batch = upserts.ToDictionary(entry => entry.Key.Value, entry => entry.Descriptor, StringComparer.Ordinal);
|
||||
var removed = deletes.Select(entry => entry.Key.Value).ToHashSet(StringComparer.Ordinal);
|
||||
CultSchemaCatalogEntry? WrittenAt(string key) =>
|
||||
batch.TryGetValue(key, out var descriptor) ? descriptor.ToCatalogEntry()
|
||||
: !removed.Contains(key) && _dirtyKeys.ContainsKey(key) && Entries.TryGetValue(key, out var staged) ? staged.Descriptor.ToCatalogEntry()
|
||||
: null;
|
||||
RefuseUndeclaredOnDisk(
|
||||
written,
|
||||
key => HeldAt(key) is { } held ? Undeclared(held, currentManifest.SchemaCatalog, Registry.Owns) : null,
|
||||
UndeclaredAt(HeldAt, currentManifest.SchemaCatalog, WrittenAt, Registry.Owns),
|
||||
key => HeldAt(key) is { } held && Foreign(held, currentManifest.SchemaCatalog, VersionOf(held.Payload)) == null
|
||||
? LoadPage(held, currentManifest.SchemaCatalog, out _, out _)
|
||||
: null,
|
||||
|
|
|
|||
Loading…
Reference in a new issue