C2a: mark the header by content, refuse an element twice in a list, route CultMesh single-file through the id rule
A store is written v3 (v5 for a directory) only when a record in it holds a non-empty element id. CultElementIds.Plan reports that as IdPlan.HoldsIds; the admission stamps it on the stored record and every store reads it. The descriptor's CarriesElementIds is deleted. A file already marked on disk stays marked. CultPersistedStoreSnapshot.FormatFor is the one header decision. Plan refuses one element object sitting twice in a list at write, naming the record and list, instead of writing both ids and refusing the store at load. CultMesh single-file write mints, refuses and marks as a cache write does; the read applies the load-time plan, so a write and a load refuse the same things. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e85b201bd2
commit
490f7b811f
7 changed files with 326 additions and 41 deletions
|
|
@ -98,12 +98,25 @@ namespace GameCult.Mesh
|
|||
|
||||
var resolvedRegistry = registry ?? CultDocumentRegistry.Shared;
|
||||
var descriptor = resolvedRegistry.GetRequired<TDocument>();
|
||||
WriteSingleFileDocumentPayload(
|
||||
path,
|
||||
key,
|
||||
descriptor.ToCatalogEntry(),
|
||||
storedAt,
|
||||
CultDocumentMessagePackSerialization.SerializeUntyped(document, typeof(TDocument)));
|
||||
// Element ids are decided as a cache write decides them, before anything is written: an unset id is minted, a
|
||||
// refused list refuses the write, and the file is marked when it holds an id. A failed write gives the ids back.
|
||||
var ids = CultElementIds.Plan(document, key.Value, deterministic: false);
|
||||
ids.Apply();
|
||||
try
|
||||
{
|
||||
WriteSingleFileDocumentPayload(
|
||||
path,
|
||||
key,
|
||||
descriptor.ToCatalogEntry(),
|
||||
storedAt,
|
||||
CultDocumentMessagePackSerialization.SerializeUntyped(document, typeof(TDocument)),
|
||||
ids.HoldsIds);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ids.Undo();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -118,7 +131,10 @@ namespace GameCult.Mesh
|
|||
var resolvedRegistry = registry ?? CultDocumentRegistry.Shared;
|
||||
var descriptor = resolvedRegistry.GetRequired<TDocument>();
|
||||
var payload = ReadSingleFileDocumentPayload(path, key, descriptor.SchemaId);
|
||||
return (TDocument)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(TDocument), payload);
|
||||
var document = (TDocument)CultDocumentMessagePackSerialization.DeserializeUntyped(typeof(TDocument), payload);
|
||||
// A load refuses what a write refuses, and mints the ids a cache load of the same file would mint.
|
||||
CultElementIds.Plan(document, key.Value, deterministic: true).Apply();
|
||||
return document;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -132,7 +148,7 @@ namespace GameCult.Mesh
|
|||
byte[] payload)
|
||||
{
|
||||
if (schema == null) throw new ArgumentNullException(nameof(schema));
|
||||
WriteSingleFileDocumentPayload(path, key, schema.ToCatalogEntry(payload), storedAt, payload);
|
||||
WriteSingleFileDocumentPayload(path, key, schema.ToCatalogEntry(payload), storedAt, payload, holdsIds: false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -214,7 +230,8 @@ namespace GameCult.Mesh
|
|||
CultRecordKey key,
|
||||
CultSchemaCatalogEntry catalogEntry,
|
||||
string? storedAt,
|
||||
byte[] payload)
|
||||
byte[] payload,
|
||||
bool holdsIds)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value must be non-empty.", nameof(path));
|
||||
if (catalogEntry == null) throw new ArgumentNullException(nameof(catalogEntry));
|
||||
|
|
@ -222,8 +239,11 @@ namespace GameCult.Mesh
|
|||
throw new ArgumentException("Catalog entry must include a schema id.", nameof(catalogEntry));
|
||||
|
||||
payload ??= Array.Empty<byte>();
|
||||
// A raw payload is opaque: whether it holds element ids is the typed caller's to say. A file already marked stays
|
||||
// marked, as it does in a cache's own store.
|
||||
var snapshot = new CultPersistedStoreSnapshot
|
||||
{
|
||||
FormatVersion = CultPersistedStoreSnapshot.FormatFor(holdsIds || IsMarkedOnDisk(path), holdsVariants: false),
|
||||
SchemaCatalog = new[] { catalogEntry },
|
||||
Records = new[]
|
||||
{
|
||||
|
|
@ -244,6 +264,20 @@ namespace GameCult.Mesh
|
|||
WriteFileAtomically(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
}
|
||||
|
||||
private static bool IsMarkedOnDisk(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
var reader = new MessagePackReader(File.ReadAllBytes(path));
|
||||
reader.ReadArrayHeader();
|
||||
return string.Equals(reader.ReadString(), CultPersistedStoreSnapshot.FormatV3, StringComparison.Ordinal);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or MessagePackSerializationException or EndOfStreamException or InvalidOperationException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static CultPersistedStoreSnapshot ReadSingleFileSnapshot(string path)
|
||||
{
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
|
|
|
|||
Loading…
Reference in a new issue