CultCache: cacheless WriteRecord refuses unclassifiable index aliases; index values are culture-invariant canonical; pin the refusal across batch size, order, mixed types and loads

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Libby 2026-11-11 01:45:10 +00:00
commit d53778288b
5 changed files with 209 additions and 13 deletions

View file

@ -1,6 +1,7 @@
#nullable enable
using System;
using System.IO;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using GameCult.Caching;
@ -18,7 +19,7 @@ namespace GameCult.Caching.Tests
private static readonly CultDocumentRegistry Registry = CultDocumentRegistry.ForTypes(new[]
{
typeof(UqItem), typeof(UqTwin), typeof(UqTagged)
typeof(UqItem), typeof(UqTwin), typeof(UqTagged), typeof(UqDecimal), typeof(UqDouble), typeof(UqStamp)
});
// Emitted, so CultDocumentRegistry.Shared never scans them (one schema declared two ways, and a member Shared would refuse).
@ -276,12 +277,136 @@ namespace GameCult.Caching.Tests
entry,
registry: Registry));
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before), "the file is byte-identical");
Refused(() => store.WriteRecord(
new CultPersistedRecord { Key = "y", SchemaId = entry.SchemaId, StoredAt = DateTimeOffset.UtcNow.ToString("O"), Payload = new byte[] { 0x90 } },
entry));
Refused(() => store.WriteRecord(
new CultPersistedRecord { Key = "y", SchemaId = entry.SchemaId, StoredAt = DateTimeOffset.UtcNow.ToString("O"), Payload = new byte[] { 0x90 } },
entry,
registry: CultDocumentRegistry.ForTypes(new[] { typeof(UqTagged) })));
Assert.That(File.ReadAllBytes(path), Is.EqualTo(before), "the file is byte-identical, whichever registry (none, another) was lent");
store.WriteDocument(Key("t"), new UqTagged { Kind = "k" }, registry: Registry);
using var reopened = Open(path);
Assert.That(reopened.Get(Key("t")), Is.Not.Null, "a role-only type still writes cacheless");
}
// The rule is a function of the whole commit: it must not stop at a prefix, a mixed batch or a small load.
[TestCase(0, 1)]
[TestCase(0, 6)]
[TestCase(2, 5)]
[TestCase(5, 6)]
public void ADuplicateAnywhereInALargeMixedBatchIsRefused(int first, int second)
{
using var cache = Open(PathOf("items.cc"));
var refused = Refused(() => cache.Commit(batch =>
{
batch.Upsert(typeof(UqTagged), new UqTagged { Kind = "same" }, Key("tag-a"));
for (var i = 0; i < 7; i++)
batch.Upsert(typeof(UqItem), Item(i == first || i == second ? "dup" : "v" + i), Key("item-" + i));
batch.Upsert(typeof(UqTagged), new UqTagged { Kind = "same" }, Key("tag-b"));
}));
Assert.That(refused.Message, Does.Contain("item-" + first).And.Contain("item-" + second).And.Not.Contain("dup"));
Assert.That(cache.Get(Key("tag-a")), Is.Null, "nothing landed");
}
[Test]
public void ADuplicateBesideARecordOfAnotherTypeSharingTheAliasIsStillRefused()
{
using var cache = Open(PathOf("items.cc"));
var refused = Refused(() => cache.Commit(batch =>
{
batch.Upsert(typeof(UqTwin), new UqTwin { Sku = "same" }, Key("twin"));
batch.Upsert(typeof(UqItem), Item("same"), Key("item-1"));
batch.Upsert(typeof(UqItem), Item("same"), Key("item-2"));
}));
Assert.That(refused.Message, Does.Contain("item-1").And.Contain("item-2").And.Not.Contain("twin"));
}
[Test]
public void ValuesDifferingOnlyByCaseAreDifferentValues()
{
using var cache = Open(PathOf("items.cc"));
cache.Commit(batch =>
{
batch.Upsert(typeof(UqItem), Item("Sku"), Key("a"));
batch.Upsert(typeof(UqItem), Item("sku"), Key("b"));
});
Assert.That(cache.GetStored(Key("a"))!.Document, Is.SameAs(cache.GetByIndex<UqItem>("sku", "Sku")));
Assert.That(cache.GetStored(Key("b"))!.Document, Is.SameAs(cache.GetByIndex<UqItem>("sku", "sku")));
}
[Test]
public void ALoadOfManyRecordsRefusesAPairWhereverItSits([Values(0, 3, 6)] int pairAt)
{
var path = PathOf("load.cc");
var roleRegistry = CultDocumentRegistry.ForTypes(new[] { UqLoadRole });
using (var writer = Open(path, roleRegistry))
{
writer.Commit(batch =>
{
for (var i = 0; i < 8; i++)
batch.Upsert(UqLoadRole, Load(UqLoadRole, i == pairAt || i == pairAt + 1 ? "dup" : "v" + i), Key("rec-" + i));
});
}
var refused = Refused(() => Open(path, CultDocumentRegistry.ForTypes(new[] { UqLoadIndex })).Dispose());
Assert.That(refused.Message, Does.Contain("rec-" + pairAt).And.Contain("rec-" + (pairAt + 1)).And.Not.Contain("dup"));
}
[Test]
public void AMixedBatchOntoAMovedFileIsAConflictWhateverItsOrder()
{
var path = PathOf("items.cc");
using var seed = Open(path);
seed.Commit(batch => batch.Upsert(typeof(UqItem), Item("base"), Key("a")));
using var second = Open(path);
using (var first = Open(path))
first.Commit(batch => batch.Upsert(typeof(UqItem), Item("moved"), Key("b")));
Assert.Throws<CultWriteConflictException>(() => second.Commit(batch =>
{
batch.Upsert(typeof(UqTagged), new UqTagged { Kind = "k" }, Key("tag"));
batch.Upsert(typeof(UqItem), Item("fresh"), Key("c"));
}));
}
[Test]
public void ANumericIndexValueIsOneCanonicalValueInEveryCulture()
{
var culture = (CultureInfo)CultureInfo.InvariantCulture.Clone();
culture.NumberFormat.NumberDecimalSeparator = ",";
var previous = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = culture;
try
{
using var cache = Open(PathOf("numbers.cc"));
cache.Commit(batch => batch.Upsert(typeof(UqDecimal), new UqDecimal { Price = 1.0m }, Key("d1")));
Assert.That(cache.GetByIndex<UqDecimal>("price", "1"), Is.Not.Null, "1.0m is the canonical value 1");
var sameDecimal = Refused(() => cache.Commit(batch => batch.Upsert(typeof(UqDecimal), new UqDecimal { Price = 1.00m }, Key("d2"))));
Assert.That(sameDecimal.Message, Does.Contain("d1").And.Contain("d2"));
cache.Commit(batch => batch.Upsert(typeof(UqDecimal), new UqDecimal { Price = 1.5m }, Key("d3")));
Assert.That(cache.GetByIndex<UqDecimal>("price", "1.5"), Is.Not.Null, "lookups name the invariant spelling, not the current culture's");
Assert.That(cache.GetByIndex<UqDecimal>("price", "1,5"), Is.Null);
culture.DateTimeFormat.ShortDatePattern = "yyyy.dd.MM";
culture.DateTimeFormat.DateSeparator = ".";
cache.Commit(batch => batch.Upsert(typeof(UqStamp), new UqStamp { When = new DateTime(2026, 1, 2, 3, 4, 5) }, Key("t1")));
Assert.That(cache.GetByIndex<UqStamp>("when", "01/02/2026 03:04:05"), Is.Not.Null, "a date is its invariant spelling");
cache.Commit(batch => batch.Upsert(typeof(UqDouble), new UqDouble { Level = 0.0 }, Key("z1")));
var signedZero = Refused(() => cache.Commit(batch => batch.Upsert(typeof(UqDouble), new UqDouble { Level = -0.0 }, Key("z2"))));
Assert.That(signedZero.Message, Does.Contain("z1").And.Contain("z2"));
}
finally
{
CultureInfo.CurrentCulture = previous;
}
}
[Test]
public void AMemberWithBothAttributesIsRefusedAtRegistration()
{
@ -303,6 +428,27 @@ namespace GameCult.Caching.Tests
[Key(0)] [CultIndex("sku")] public string? Sku { get; set; }
}
[CultDocument("tests.uq_decimal", "tests.uq_decimal.v1")]
[MessagePackObject(AllowPrivate = true)]
public sealed class UqDecimal
{
[Key(0)] [CultIndex("price")] public decimal Price { get; set; }
}
[CultDocument("tests.uq_stamp", "tests.uq_stamp.v1")]
[MessagePackObject(AllowPrivate = true)]
public sealed class UqStamp
{
[Key(0)] [CultIndex("when")] public DateTime When { get; set; }
}
[CultDocument("tests.uq_double", "tests.uq_double.v1")]
[MessagePackObject(AllowPrivate = true)]
public sealed class UqDouble
{
[Key(0)] [CultIndex("level")] public double Level { get; set; }
}
[CultDocument("tests.uq_tagged", "tests.uq_tagged.v1")]
[MessagePackObject(AllowPrivate = true)]
public sealed class UqTagged

View file

@ -486,6 +486,39 @@ namespace GameCult.Networking.Tests
Assert.That(evaluation.Rows.Select(r => r.Key.Value), Is.EqualTo(new[] { "a" }));
}
// [CultIndex] means unique and is also a selection address: a unique member filters by equality and by number like a role.
[Test]
public void AUniqueIndexMemberIsAlsoSelectable()
{
var registry = CultDocumentRegistry.ForTypes(new[] { typeof(SelUnique) });
var rows = new[]
{
Row(registry, new SelUnique { Sku = "s1", Rank = 1 }, "a", 1),
Row(registry, new SelUnique { Sku = "s2", Rank = 7 }, "b", 2)
};
var byValue = CultNetSelectionEvaluator.Select(registry, rows,
new CultNetSelection { Fields = new[] { new CultNetFieldPredicate { Index = "sku", Op = "any_of", Values = new[] { "s2" } } } }, asOf: 2);
var byNumber = CultNetSelectionEvaluator.Select(registry, rows,
new CultNetSelection { Fields = new[] { new CultNetFieldPredicate { Index = "rank", Op = "gt", Number = "3" } } }, asOf: 2);
Assert.That(byValue.Rows.Select(r => r.Key.Value), Is.EqualTo(new[] { "b" }));
Assert.That(byNumber.Rows.Select(r => r.Key.Value), Is.EqualTo(new[] { "b" }));
}
[CultDocument("cultnet.selection-tests.unique", "cultnet.selection-tests.unique.v1")]
[MessagePackObject(AllowPrivate = true)]
public sealed class SelUnique
{
[Key(0)]
[CultIndex("sku")]
public string Sku = string.Empty;
[Key(1)]
[CultIndex("rank")]
public int Rank;
}
public abstract class SelFixtureMiddle
{
[Key(0)]