C#: a document type declares the older schema ids its records may sit under
CultDocumentAttribute.CompatibleSchemaIds, as TypeScript and Python already name it. The registered catalog entry lists the ids after its own, the registry resolves a record under one to the type (its own id first, then the first registered type that declares it), and a condition on a record compares schema ids through that resolution, so a record still under a declared old id counts as the record the cache holds. The cache already stamps every record it writes with the descriptor's own id; the store writers still change no identity, so a record a commit does not rewrite stays under the id the file holds it under, published by the registered entry. Without the declaration such a commit is refused, typed, as before. Contract and XML docs describe the declaration. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5926804e2b
commit
186e5b29ae
5 changed files with 329 additions and 11 deletions
|
|
@ -178,7 +178,7 @@ public sealed class DirectoryMessagePackBackingStore : CacheBackingStore
|
|||
if (commitLease == null)
|
||||
return CultCommitOutcome.Contended;
|
||||
var manifest = ReadManifest();
|
||||
if (!request.ConditionsHold(manifest.Records, Entries.Values))
|
||||
if (!request.ConditionsHold(manifest.Records, Entries.Values, Registry.CanonicalSchemaId))
|
||||
return CultCommitOutcome.Mismatch;
|
||||
|
||||
var previousEntries = Entries.ToDictionary(pair => pair.Key, pair => pair.Value, StringComparer.Ordinal);
|
||||
|
|
|
|||
|
|
@ -129,6 +129,13 @@ record and when it puts one. The cache is the only owner of that stamp: a store
|
|||
changes the identity of a record it did not receive from its caller, so a record another writer
|
||||
changed since this cache read it is written back as that writer left it.
|
||||
|
||||
A schema type declares the older ids its records may sit under: `CultDocumentAttribute.CompatibleSchemaIds`
|
||||
(C#), `compatibleSchemaIds` (TypeScript, Python). The registered catalog entry lists them after its own id, and
|
||||
the cache restamps a record loaded under one to the type's own id, so the next write of that record carries the
|
||||
registered id. A record another writer left under a declared id, and that a commit does not rewrite, stays under it
|
||||
and the registered entry publishes it. Without the declaration, a C# commit onto a file that holds a record under an
|
||||
id only the file's entry lists is refused, typed, and the file is left as it was. Rust has no such declaration.
|
||||
|
||||
A cache resolves a record to a local type by the record's schema id: the registered type that owns
|
||||
the id, else one that lists it as compatible. The name the catalog entry carries is metadata. It
|
||||
names a local type only when no local type has the id, for a store written by a runtime whose
|
||||
|
|
@ -151,9 +158,8 @@ an own id and disagree on the schema name, when records of different types share
|
|||
write would change which type an existing record resolves to (the schema names of the records already
|
||||
in the store and of the records written differ), or when a record's id is published by no chosen entry.
|
||||
The retyping refusal has one exception, a rename: when the entries of both the existing records and
|
||||
the written ones own the id, the registered descriptor wins. (A C# cache stamps a record with its
|
||||
descriptor's own id, so it cannot write a record under an id its descriptor only lists, and the
|
||||
retyping refusal cannot arise there.) Each runtime refuses with one typed error naming the id, the
|
||||
the written ones own the id, the registered descriptor wins. (A C# cache stamps every record it
|
||||
writes with its descriptor's own id, so the retyping refusal cannot arise there.) Each runtime refuses with one typed error naming the id, the
|
||||
schema names and a record key: `CultSchemaConflictException` (C#), `SchemaConflictError`
|
||||
(Rust, TypeScript, Python).
|
||||
|
||||
|
|
|
|||
|
|
@ -241,6 +241,7 @@ namespace GameCult.Caching
|
|||
string schemaName,
|
||||
string schemaVersion,
|
||||
string schemaId,
|
||||
IReadOnlyList<string> compatibleSchemaIds,
|
||||
string contentHash,
|
||||
string canonicalSchemaJson,
|
||||
bool isGlobal,
|
||||
|
|
@ -254,6 +255,7 @@ namespace GameCult.Caching
|
|||
SchemaName = schemaName;
|
||||
SchemaVersion = schemaVersion;
|
||||
SchemaId = schemaId;
|
||||
CompatibleSchemaIds = compatibleSchemaIds;
|
||||
ContentHash = contentHash;
|
||||
CanonicalSchemaJson = canonicalSchemaJson;
|
||||
IsGlobal = isGlobal;
|
||||
|
|
@ -279,6 +281,9 @@ namespace GameCult.Caching
|
|||
public string SchemaName { get; }
|
||||
public string SchemaVersion { get; }
|
||||
public string SchemaId { get; }
|
||||
|
||||
/// <summary>The ids besides <see cref="SchemaId"/> this type declared through <see cref="CultDocumentAttribute.CompatibleSchemaIds"/>.</summary>
|
||||
public IReadOnlyList<string> CompatibleSchemaIds { get; }
|
||||
public string ContentHash { get; }
|
||||
public string CanonicalSchemaJson { get; }
|
||||
public bool IsGlobal { get; }
|
||||
|
|
@ -370,7 +375,7 @@ namespace GameCult.Caching
|
|||
SchemaVersion = SchemaVersion,
|
||||
ContentHash = ContentHash,
|
||||
CanonicalSchemaJson = CanonicalSchemaJson,
|
||||
CompatibleSchemaIds = [SchemaId],
|
||||
CompatibleSchemaIds = [SchemaId, .. CompatibleSchemaIds],
|
||||
Members = Members
|
||||
.OrderBy(member => member.Slot)
|
||||
.Select(member => new CultSchemaMemberCatalogEntry
|
||||
|
|
@ -578,6 +583,16 @@ namespace GameCult.Caching
|
|||
throw new InvalidOperationException($"Unknown CultCache schema id '{schemaId}'.");
|
||||
}
|
||||
|
||||
// The id a record under schemaId is stamped with: the own id of the type that owns it, else of the first registered
|
||||
// type that declares it compatible; an id no registered type claims is its own.
|
||||
internal string CanonicalSchemaId(string schemaId)
|
||||
{
|
||||
var indexes = _indexes;
|
||||
return indexes.BySchemaId.TryGetValue(schemaId, out var owner) || indexes.ByCompatibleSchemaId.TryGetValue(schemaId, out owner)
|
||||
? owner.SchemaId
|
||||
: schemaId;
|
||||
}
|
||||
|
||||
public CultDocumentDescriptor ResolvePersistedSchema(string schemaId, IReadOnlyCollection<CultSchemaCatalogEntry> catalog)
|
||||
{
|
||||
return ResolvePersistedSchemaDetailed(schemaId, catalog).Descriptor;
|
||||
|
|
@ -613,6 +628,11 @@ namespace GameCult.Caching
|
|||
throw new InvalidOperationException($"Persisted schema '{schemaId}' is not present in the embedded catalog.");
|
||||
}
|
||||
|
||||
if (indexes.ByCompatibleSchemaId.TryGetValue(schemaId, out var declared))
|
||||
{
|
||||
return BuildCompatibleResolutionResult(persisted, declared);
|
||||
}
|
||||
|
||||
foreach (var compatibleSchemaId in persisted.CompatibleSchemaIds.Where(candidate => !string.IsNullOrWhiteSpace(candidate)))
|
||||
{
|
||||
if (indexes.BySchemaId.TryGetValue(compatibleSchemaId, out var compatibleLocal))
|
||||
|
|
@ -687,6 +707,8 @@ namespace GameCult.Caching
|
|||
|
||||
indexes.ByType[descriptor.DocumentType] = descriptor;
|
||||
indexes.BySchemaId[descriptor.SchemaId] = descriptor;
|
||||
foreach (var compatibleSchemaId in descriptor.CompatibleSchemaIds)
|
||||
indexes.ByCompatibleSchemaId.TryAdd(compatibleSchemaId, descriptor);
|
||||
indexes.BySchemaName[descriptor.SchemaName] = schemaNameVersions == null
|
||||
? [descriptor]
|
||||
: [.. schemaNameVersions, descriptor];
|
||||
|
|
@ -717,6 +739,7 @@ namespace GameCult.Caching
|
|||
{
|
||||
ByType = new Dictionary<Type, CultDocumentDescriptor>();
|
||||
BySchemaId = new Dictionary<string, CultDocumentDescriptor>(StringComparer.Ordinal);
|
||||
ByCompatibleSchemaId = new Dictionary<string, CultDocumentDescriptor>(StringComparer.Ordinal);
|
||||
BySchemaName = new Dictionary<string, CultDocumentDescriptor[]>(StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
|
|
@ -724,11 +747,13 @@ namespace GameCult.Caching
|
|||
{
|
||||
ByType = new Dictionary<Type, CultDocumentDescriptor>(source.ByType);
|
||||
BySchemaId = new Dictionary<string, CultDocumentDescriptor>(source.BySchemaId, StringComparer.Ordinal);
|
||||
ByCompatibleSchemaId = new Dictionary<string, CultDocumentDescriptor>(source.ByCompatibleSchemaId, StringComparer.Ordinal);
|
||||
BySchemaName = new Dictionary<string, CultDocumentDescriptor[]>(source.BySchemaName, StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
public Dictionary<Type, CultDocumentDescriptor> ByType { get; }
|
||||
public Dictionary<string, CultDocumentDescriptor> BySchemaId { get; }
|
||||
public Dictionary<string, CultDocumentDescriptor> ByCompatibleSchemaId { get; }
|
||||
public Dictionary<string, CultDocumentDescriptor[]> BySchemaName { get; }
|
||||
}
|
||||
|
||||
|
|
@ -765,12 +790,19 @@ namespace GameCult.Caching
|
|||
var contentHash = Sha256(schemaJson);
|
||||
var semanticFingerprint = BuildSemanticFingerprint(attribute.SchemaName, attribute.SchemaVersion, descriptorMembers);
|
||||
var schemaId = Sha256(semanticFingerprint);
|
||||
if (attribute.CompatibleSchemaIds.Any(string.IsNullOrWhiteSpace))
|
||||
throw new InvalidOperationException($"Type {type.FullName} declares an empty compatible schema id.");
|
||||
var compatibleSchemaIds = attribute.CompatibleSchemaIds
|
||||
.Where(id => !string.Equals(id, schemaId, StringComparison.Ordinal))
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.ToArray();
|
||||
|
||||
return new CultDocumentDescriptor(
|
||||
type,
|
||||
attribute.SchemaName,
|
||||
attribute.SchemaVersion,
|
||||
schemaId,
|
||||
compatibleSchemaIds,
|
||||
contentHash,
|
||||
schemaJson,
|
||||
type.GetCustomAttribute<CultGlobalAttribute>() != null,
|
||||
|
|
@ -1783,7 +1815,7 @@ namespace GameCult.Caching
|
|||
|
||||
// durable is what the store holds on disk now; observed is what its cache last loaded or committed.
|
||||
// Identity is (schemaId, storedAt), sound because every write to a key mints a later storedAt.
|
||||
public bool ConditionsHold(IReadOnlyCollection<CultPersistedRecord> durable, IEnumerable<CultStoredDocument> observed)
|
||||
public bool ConditionsHold(IReadOnlyCollection<CultPersistedRecord> durable, IEnumerable<CultStoredDocument> observed, Func<string, string> canonicalSchemaId)
|
||||
{
|
||||
var byKey = durable.ToDictionary(record => record.Key, StringComparer.Ordinal);
|
||||
foreach (var (key, schemaId, storedAt) in Expected)
|
||||
|
|
@ -1791,13 +1823,13 @@ namespace GameCult.Caching
|
|||
byKey.TryGetValue(key.Value, out var record);
|
||||
var holds = schemaId == null
|
||||
? record == null
|
||||
: record != null && record.SchemaId == schemaId && record.StoredAt == storedAt;
|
||||
: record != null && canonicalSchemaId(record.SchemaId) == schemaId && record.StoredAt == storedAt;
|
||||
if (!holds)
|
||||
return false;
|
||||
}
|
||||
|
||||
return !ExpectUnchanged ||
|
||||
durable.Select(record => Identity(record.Key, record.SchemaId, record.StoredAt))
|
||||
durable.Select(record => Identity(record.Key, canonicalSchemaId(record.SchemaId), record.StoredAt))
|
||||
.OrderBy(identity => identity, StringComparer.Ordinal)
|
||||
.SequenceEqual(observed
|
||||
.Select(stored => Identity(stored.Key.Value, stored.Descriptor.SchemaId, stored.StoredAt))
|
||||
|
|
@ -2542,7 +2574,7 @@ namespace GameCult.Caching
|
|||
var inMemory = _entries.Values
|
||||
.Select(entry => new CultPersistedRecord { Key = entry.Key.Value, SchemaId = entry.Descriptor.SchemaId, StoredAt = entry.StoredAt })
|
||||
.ToArray();
|
||||
return request.ConditionsHold(inMemory, _entries.Values) ? CultCommitOutcome.Committed : CultCommitOutcome.Mismatch;
|
||||
return request.ConditionsHold(inMemory, _entries.Values, Registry.CanonicalSchemaId) ? CultCommitOutcome.Committed : CultCommitOutcome.Mismatch;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -3607,7 +3639,7 @@ namespace GameCult.Caching
|
|||
return CultCommitOutcome.Contended;
|
||||
|
||||
var disk = ReadSnapshot() ?? new CultPersistedStoreSnapshot();
|
||||
if (!request.ConditionsHold(disk.Records, Entries.Values))
|
||||
if (!request.ConditionsHold(disk.Records, Entries.Values, Registry.CanonicalSchemaId))
|
||||
return CultCommitOutcome.Mismatch;
|
||||
|
||||
// An unconditional commit writes what a flush would, this store's whole view plus the batch: last-writer-wins,
|
||||
|
|
@ -3664,7 +3696,7 @@ namespace GameCult.Caching
|
|||
{
|
||||
if (landing.Contains(record.Key) || removing.Contains(record.Key))
|
||||
continue;
|
||||
if (Entries.TryGetValue(record.Key, out var known) && known.StoredAt == record.StoredAt && known.Descriptor.SchemaId == record.SchemaId)
|
||||
if (Entries.TryGetValue(record.Key, out var known) && known.StoredAt == record.StoredAt && known.Descriptor.SchemaId == Registry.CanonicalSchemaId(record.SchemaId))
|
||||
continue;
|
||||
arriving.Add(ToStoredDocument(record, disk.SchemaCatalog, DeserializePayload, out _));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,14 @@ namespace GameCult.Caching
|
|||
public string SchemaName { get; }
|
||||
|
||||
public string SchemaVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Schema ids other than this type's own that a store may hold its records under, typically the ids an earlier
|
||||
/// version of the schema had. The registered catalog entry lists them, and a cache resolves a record stored under
|
||||
/// one of them to this type (its own id is tried first) and restamps it to the type's own id on load. TypeScript
|
||||
/// and Python name the same declaration <c>compatibleSchemaIds</c>.
|
||||
/// </summary>
|
||||
public string[] CompatibleSchemaIds { get; set; } = Array.Empty<string>();
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
|
|
|
|||
Loading…
Reference in a new issue