Store readability: one reader decides open, flush, commit and CultMesh writes
A rewrite replaces a file only when the runtime's own reader opens it. C# drops ReadStoreHeader (a second, header-only verdict that let a flush replace files the reader refuses) and asks DeserializeSnapshot from open, flush and commit; the reader now requires exactly one array, so C# refuses bytes after the store on read as TS and Python do. A zero-byte file is an empty store for all three paths. CultMesh.WriteSingleFileDocument reads the file it replaces through the Mesh reader before writing. Rust reads and rewrites through one classification (trailing bytes refused on read; a non-store first slot must be a legacy envelope array the reader accepts). TypeScript checks a legacy array with pullAll's own decoder before replacing it. Python already conformed. Shared byte vectors and per-runtime verdicts live in tests/vectors/document-variants-c2a/readability; every runtime's test reads them. Holds gains second-dictionary-value and second-list-item cases. The persistence format contract now marks v3 by content and states the readability rule. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
76e449f33e
commit
4028205ff1
28 changed files with 452 additions and 270 deletions
|
|
@ -177,8 +177,14 @@ public static class CultDocumentMessagePackSerialization
|
|||
return buffer.WrittenSpan.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a store file: exactly one complete MessagePack array. A truncated file, bytes after the array, a value that is not
|
||||
/// an array, or a first slot that is not a header string are refused, so a reader never takes part of a file for the whole.
|
||||
/// The rewriting paths use this same reader as their verdict on whether a file may be replaced.
|
||||
/// </summary>
|
||||
public static CultPersistedStoreSnapshot DeserializeSnapshot(byte[] payload)
|
||||
{
|
||||
RequireOneArray(payload);
|
||||
var reader = new MessagePackReader(payload);
|
||||
var fieldCount = reader.ReadArrayHeader();
|
||||
var snapshot = new CultPersistedStoreSnapshot();
|
||||
|
|
@ -217,6 +223,24 @@ public static class CultDocumentMessagePackSerialization
|
|||
return snapshot;
|
||||
}
|
||||
|
||||
private static void RequireOneArray(byte[] bytes)
|
||||
{
|
||||
var whole = new MessagePackReader(bytes);
|
||||
try
|
||||
{
|
||||
if (whole.NextMessagePackType != MessagePackType.Array)
|
||||
throw new NotSupportedException($"The store is a MessagePack {whole.NextMessagePackType}, not an array.");
|
||||
whole.Skip();
|
||||
}
|
||||
catch (Exception ex) when (ex is EndOfStreamException or MessagePackSerializationException or InvalidOperationException)
|
||||
{
|
||||
throw new NotSupportedException("The store is not one complete MessagePack array.", ex);
|
||||
}
|
||||
|
||||
if (!whole.End)
|
||||
throw new NotSupportedException("The store has bytes after its MessagePack array.");
|
||||
}
|
||||
|
||||
/// <summary>Refuses a single-file snapshot this runtime cannot read, naming the format or record it found.</summary>
|
||||
public static void RequireSingleFileFormat(CultPersistedStoreSnapshot snapshot)
|
||||
{
|
||||
|
|
@ -241,41 +265,6 @@ public static class CultDocumentMessagePackSerialization
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The format header of a store file about to be replaced by a writer that does not read the store's records: null for
|
||||
/// an empty file or a legacy envelope array (an array whose first slot is not a string), the header string otherwise.
|
||||
/// A file is a store only when it is exactly one complete MessagePack array: a truncated file, bytes after the array, a
|
||||
/// value that is not an array, or a first slot that is a string but not a header this runtime reads are all refused,
|
||||
/// never taken for a legacy file, so a writer cannot overwrite what it cannot see.
|
||||
/// </summary>
|
||||
public static string? ReadStoreHeader(byte[] bytes)
|
||||
{
|
||||
if (bytes.Length == 0)
|
||||
return null;
|
||||
var whole = new MessagePackReader(bytes);
|
||||
try
|
||||
{
|
||||
if (whole.NextMessagePackType != MessagePackType.Array)
|
||||
throw new NotSupportedException($"The store is a MessagePack {whole.NextMessagePackType}, not an array; it is not rewritten.");
|
||||
whole.Skip();
|
||||
}
|
||||
catch (Exception ex) when (ex is EndOfStreamException or MessagePackSerializationException or InvalidOperationException)
|
||||
{
|
||||
throw new NotSupportedException("The store is not one complete MessagePack array; it is not rewritten.", ex);
|
||||
}
|
||||
|
||||
if (!whole.End)
|
||||
throw new NotSupportedException("The store has bytes after its MessagePack array; it is not rewritten.");
|
||||
|
||||
var reader = new MessagePackReader(bytes);
|
||||
if (reader.ReadArrayHeader() == 0 || reader.NextMessagePackType != MessagePackType.String)
|
||||
return null;
|
||||
// A first slot that is a string is a header, and one this runtime does not read is refused, whatever the string says.
|
||||
var header = reader.ReadString() ?? string.Empty;
|
||||
RequireSingleFileFormat(new CultPersistedStoreSnapshot { FormatVersion = header });
|
||||
return header;
|
||||
}
|
||||
|
||||
private static void WritePersistedRecord(ref MessagePackWriter writer, CultPersistedRecord record)
|
||||
{
|
||||
writer.WriteArrayHeader(record.Variant == null ? PersistedRecordFieldCount : VariantRecordFieldCount);
|
||||
|
|
@ -568,8 +557,6 @@ public class SingleFileMessagePackBackingStore : SingleFileBackingStore
|
|||
return snapshot;
|
||||
}
|
||||
|
||||
protected override string? ReadStoreHeader(byte[] data) => CultDocumentMessagePackSerialization.ReadStoreHeader(data);
|
||||
|
||||
protected override byte[] SerializePayload(object document)
|
||||
{
|
||||
return CultDocumentMessagePackSerialization.SerializeUntyped(document, document.GetType(), Registry);
|
||||
|
|
|
|||
|
|
@ -219,12 +219,26 @@ A reader refuses any `cultcache.store.*` header it does not know and any record
|
|||
slots than its header allows, naming the version or the record's key and schema id. It
|
||||
never skips a slot it does not understand.
|
||||
|
||||
The headers are `v1` (no variant, no element ids), `v2` (holds a variant) and `v3` (can
|
||||
hold element ids, variants or not). A single-file store declares `v3` when any record it
|
||||
holds is of a type that reaches an object list, and a file already declared `v3` stays
|
||||
`v3` when a writer merges onto it. A directory store's manifest says
|
||||
`cultcache.store.v5.directory-content-addressed-pages` under the same rule, and
|
||||
`v4` otherwise.
|
||||
The headers are `v1` (no variant, no element ids), `v2` (holds a variant) and `v3` (holds
|
||||
an element id, variants or not). A single-file store declares `v3` when any record it
|
||||
holds carries an element id: a non-empty `[CultElementId]` value in a record's document,
|
||||
or in a variant's override values. A store that cannot read a variant's override values
|
||||
(no codec attached) counts the variant as holding one. A whole-store writer decides by
|
||||
what the store holds now, so the file drops back to `v1` (or `v2` with a variant) when
|
||||
the last id is taken away; a writer that touches only part of the file cannot see the
|
||||
rest, so a file already declared `v3` stays `v3` when it merges onto it. A directory
|
||||
store's manifest says `cultcache.store.v5.directory-content-addressed-pages` under the
|
||||
same rule, and `v4` otherwise.
|
||||
|
||||
A single-file store is exactly one MessagePack array. Every runtime refuses a file that is
|
||||
truncated, has bytes after the array (a second store appended included), is not an array, or
|
||||
whose first slot is not a header it reads; it refuses these on open and on rewrite alike. A
|
||||
zero-byte file is an empty store. A flush or a commit replaces a file only when the
|
||||
runtime's own reader opens it, so one verdict covers open, flush and commit, and a file the
|
||||
runtime cannot read is left as it is. Rust, TypeScript and Python also open the legacy
|
||||
envelope array (an array of `key`/`type`/`payload`/`storedAt` maps) and replace it; C# has
|
||||
no legacy reader and refuses it. The shared byte vectors and each runtime's verdict are
|
||||
`tests/vectors/document-variants-c2a/readability/manifest.txt`.
|
||||
|
||||
## Variant Records
|
||||
|
||||
|
|
@ -245,8 +259,8 @@ fifth slot:
|
|||
`elementId` is `""` unless the step enters an object-list element. `value` is the
|
||||
member's own MessagePack encoding, inline. A top-level Set has one step and `id` `""`;
|
||||
a reader that resolves only that shape refuses the others, naming the variant.
|
||||
- A store holding at least one variant declares `cultcache.store.v2` (`v3` when it can
|
||||
hold element ids); a store holding none declares `cultcache.store.v1` (or `v3`) and is
|
||||
- A store holding at least one variant declares `cultcache.store.v2` (`v3` when it holds an
|
||||
element id); a store holding none declares `cultcache.store.v1` (or `v3`) and is
|
||||
byte-identical to what a runtime without variants writes. A `v1` header over a variant
|
||||
record is refused.
|
||||
- A variant's resolved document is derived in memory when the cache admits the record
|
||||
|
|
@ -276,7 +290,7 @@ elements retargets nothing.
|
|||
written before ids existed loads, and its ids are minted from SHA-256 of the record
|
||||
key and the element's path, so every reader mints the same ids; they reach disk with
|
||||
the record's next write, or `CultCache.MintElementIds()`.
|
||||
- A store that can hold ids declares the `v3` header (above), so a reader older than ids
|
||||
- A store that holds an id declares the `v3` header (above), so a reader older than ids
|
||||
refuses it instead of skipping the id slot and rewriting the elements without ids.
|
||||
TypeScript, Python and Rust read `v3` and keep a `v3` header they found when they
|
||||
rewrite a store; they do not decide when a document carries ids.
|
||||
|
|
|
|||
|
|
@ -3416,15 +3416,13 @@ namespace GameCult.Caching
|
|||
|
||||
protected FileInfo FileInfo { get; }
|
||||
protected abstract byte[] SerializeSnapshot(CultPersistedStoreSnapshot snapshot);
|
||||
protected abstract CultPersistedStoreSnapshot DeserializeSnapshot(byte[] data);
|
||||
|
||||
/// <summary>
|
||||
/// The format header of a non-empty store file this store is about to replace: null for a legacy file that carries none,
|
||||
/// the header otherwise. It throws for bytes that are not exactly one store this runtime reads, so a rewrite never
|
||||
/// overwrites a file it cannot see. The default reads the whole snapshot; a codec that can read the header alone
|
||||
/// overrides it.
|
||||
/// This store's one reader of a non-empty store file, and so its one verdict on whether the file may be replaced: open,
|
||||
/// flush and commit all ask it. It must throw for anything that is not exactly one store this runtime reads (a
|
||||
/// truncated file, bytes after the store, a format it does not know), so a rewrite never overwrites a file it cannot see.
|
||||
/// </summary>
|
||||
protected virtual string? ReadStoreHeader(byte[] data) => DeserializeSnapshot(data).FormatVersion;
|
||||
protected abstract CultPersistedStoreSnapshot DeserializeSnapshot(byte[] data);
|
||||
|
||||
protected abstract byte[] SerializePayload(object document);
|
||||
protected abstract object DeserializePayload(Type documentType, byte[] payload);
|
||||
|
|
@ -3502,8 +3500,7 @@ namespace GameCult.Caching
|
|||
{
|
||||
using (AcquireLock(wait: true))
|
||||
{
|
||||
if (ReadDisk() is { Length: > 0 } onDisk)
|
||||
ReadStoreHeader(onDisk);
|
||||
ReadSnapshot();
|
||||
WriteSnapshot(
|
||||
Entries.Values.Select(entry => ToPersistedRecord(entry, SerializePayload)),
|
||||
Entries.Values.Select(entry => entry.Descriptor.ToCatalogEntry()),
|
||||
|
|
@ -3529,7 +3526,7 @@ namespace GameCult.Caching
|
|||
if (fileLock == null)
|
||||
return CultCommitOutcome.Contended;
|
||||
|
||||
var disk = ReadSnapshot(rewriting: true) ?? new CultPersistedStoreSnapshot();
|
||||
var disk = ReadSnapshot() ?? new CultPersistedStoreSnapshot();
|
||||
if (!request.ConditionsHold(disk.Records, Entries.Values))
|
||||
return CultCommitOutcome.Mismatch;
|
||||
|
||||
|
|
@ -3598,15 +3595,12 @@ namespace GameCult.Caching
|
|||
Judging!(arriving, departing);
|
||||
}
|
||||
|
||||
// A commit rewrites the file it read, so it reads it as a rewrite does: the header proves the bytes are one store first.
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot(bool rewriting = false)
|
||||
// Open, flush and commit read the file through this one call, so they agree on which files are stores. A file that is
|
||||
// gone or zero bytes is an empty store: nothing to read, nothing to lose.
|
||||
private CultPersistedStoreSnapshot? ReadSnapshot()
|
||||
{
|
||||
var bytes = ReadDisk();
|
||||
if (bytes == null)
|
||||
return null;
|
||||
if (rewriting && bytes.Length > 0)
|
||||
ReadStoreHeader(bytes);
|
||||
return DeserializeSnapshot(bytes);
|
||||
return bytes == null ? null : DeserializeSnapshot(bytes);
|
||||
}
|
||||
|
||||
private byte[]? ReadDisk()
|
||||
|
|
@ -3616,7 +3610,7 @@ namespace GameCult.Caching
|
|||
return null;
|
||||
try
|
||||
{
|
||||
return ReadAllBytesShared(FileInfo.FullName);
|
||||
return ReadAllBytesShared(FileInfo.FullName) is { Length: > 0 } bytes ? bytes : null;
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -241,11 +241,15 @@ namespace GameCult.Mesh
|
|||
throw new ArgumentException("Catalog entry must include a schema id.", nameof(catalogEntry));
|
||||
|
||||
payload ??= Array.Empty<byte>();
|
||||
// The file being replaced is read first, by the reader every read of it uses: one this runtime cannot read refuses the
|
||||
// write instead of being overwritten.
|
||||
var existing = File.Exists(path) ? ReadSingleFileSnapshot(path) : null;
|
||||
|
||||
// A typed write sees the document it replaces the file with, so its ids decide. A raw payload is opaque: the writer
|
||||
// cannot see whether it holds ids, so a file already marked stays marked.
|
||||
var snapshot = new CultPersistedStoreSnapshot
|
||||
{
|
||||
FormatVersion = CacheBackingStore.HeaderFor(holdsIds, contentKnown ? null : ExistingHeader(path), wholeStore: contentKnown, directoryStore: false),
|
||||
FormatVersion = CacheBackingStore.HeaderFor(holdsIds, existing?.FormatVersion, wholeStore: contentKnown, directoryStore: false),
|
||||
SchemaCatalog = new[] { catalogEntry },
|
||||
Records = new[]
|
||||
{
|
||||
|
|
@ -266,15 +270,11 @@ namespace GameCult.Mesh
|
|||
WriteFileAtomically(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
|
||||
}
|
||||
|
||||
private static string? ExistingHeader(string path)
|
||||
{
|
||||
// A file that is gone has no header; one that cannot be read refuses the write rather than being overwritten.
|
||||
return File.Exists(path) ? CultDocumentMessagePackSerialization.ReadStoreHeader(File.ReadAllBytes(path)) : null;
|
||||
}
|
||||
|
||||
private static CultPersistedStoreSnapshot ReadSingleFileSnapshot(string path)
|
||||
{
|
||||
var bytes = File.ReadAllBytes(path);
|
||||
if (bytes.Length == 0)
|
||||
return new CultPersistedStoreSnapshot();
|
||||
CultPersistedStoreSnapshot snapshot;
|
||||
try
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue