cultcache-ts: store delete matches the stored schema id; cross-store owned rename and two-stores-one-foreign-key refused; walk 12 rows
All checks were successful
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Successful in 20m44s
All checks were successful
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Successful in 20m44s
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1993d3640a
commit
35c5834b6b
4 changed files with 109 additions and 9 deletions
|
|
@ -18,7 +18,7 @@ All notable changes to `@gamecult/cultcache-ts` are recorded here, following
|
|||
- A `put`, `push` or `pushAll` that would land a second type under a held key (against the file, or within one batch) is refused with `SchemaConflictError`, and nothing is written. The written type may replace the record when its catalog entry carries the stored schema id as its own or lists it compatible (a type renamed under a stable id restamps the record, as C# does); otherwise retyping a key is a delete, then a put. The C# allowance for an undeclared older id of the same schema name that reads whole is not ported: a same-named type keeps passing, including one that drops persisted slots, which C# refuses.
|
||||
- Same-type writes replace by key; in one `pushAll` batch the last entry for a key wins.
|
||||
- A store whose canonical-schema catalog slot is not a string is refused as `StoreUnreadableError`.
|
||||
- The cache holds one record per key (variants:ruling:cache-refuses-retype). A `put` or `putEnvelope` that lands a second type under a key the cache holds, or under a key a store carries foreign, is refused with `SchemaConflictError` naming the key, even with no store (a memory-only cache used to hold both types at the key). A type that owns the held record's schema id (as for the store) replaces it. Two stores holding one key refuse the load and admit nothing.
|
||||
- The cache holds one record per key (variants:ruling:cache-refuses-retype). A `put` or `putEnvelope` that lands a second type under a key the cache holds, or under a key a store carries foreign, is refused with `SchemaConflictError` naming the key, even with no store (a memory-only cache used to hold both types at the key). A type that owns the held record's schema id (as for the store) replaces it, unless the held record is in a store other than the written type's home: that rename is refused with `SchemaConflictError` (remove the record first). Two stores holding one key, registered or foreign, refuse the load and admit nothing. A store's `delete` removes the record whose stored schema id the removal's type owns, whatever the arrived record's `type` string.
|
||||
- A store holding a record no registered type claims now loads (it used to throw "No schema is registered..."): the cache does not serve the record, lists it in `foreignRecords()`, keeps its bytes through every write, and refuses a `put` or a `delete` at its key with `SchemaConflictError` (variants:ruling:unheld-removal-refused-at-commit). Registering a type that claims its schema id serves it at the next `pullAllBackingStores`.
|
||||
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -330,16 +330,22 @@ export class CultCache {
|
|||
const hydrated = this.#admitEntry(next, registered, admitted, value);
|
||||
const other = this.#heldUnderOtherType(next, type, hydrated.key);
|
||||
if (other !== undefined) {
|
||||
throw new Error(`CultCache record "${hydrated.key}" is held by two stores, as type "${other.type}" and as type "${type}".`);
|
||||
throw new Error(`Record ${hydrated.key} is held by two stores, as type "${other.type}" and as type "${type}".`);
|
||||
}
|
||||
this.#installEntry(next, registered, hydrated);
|
||||
}
|
||||
}
|
||||
|
||||
const foreignByKey = new Map<string, string>();
|
||||
for (const record of next.foreign) {
|
||||
const first = foreignByKey.get(record.key);
|
||||
if (first !== undefined) {
|
||||
throw new Error(`Record ${record.key} is held by two stores, as schema "${first}" and as schema "${record.schemaName}".`);
|
||||
}
|
||||
foreignByKey.set(record.key, record.schemaName);
|
||||
const other = [...this.#definitions.keys()].map((type) => next.entries.get(this.#entryId(type, record.key))).find((held) => held !== undefined);
|
||||
if (other !== undefined) {
|
||||
throw new Error(`CultCache record "${record.key}" is held by two stores, as type "${other.type}" and as schema "${record.schemaName}".`);
|
||||
throw new Error(`Record ${record.key} is held by two stores, as type "${other.type}" and as schema "${record.schemaName}".`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -694,6 +700,16 @@ export class CultCache {
|
|||
async #writeNow(registered: RegisteredDefinition, entry: CultCacheEnvelope, value: unknown): Promise<void> {
|
||||
const home = this.#homeStore(entry.type);
|
||||
const retyped = this.#refuseRetype(entry);
|
||||
if (retyped !== undefined && this.#homeStore(retyped.type) !== home) {
|
||||
// The write owns the held record's id and would replace it, but the held record is in another store; the push
|
||||
// reaches only this type's home, so the old record would stay in its own file and load as a second holder.
|
||||
throw new SchemaConflictError(
|
||||
schemaIdForEnvelope(entry),
|
||||
[retyped.type, entry.type],
|
||||
entry.key,
|
||||
`CultCache holds record "${entry.key}" as type "${retyped.type}" in another store than the home of type "${entry.type}", so the rename cannot replace it; remove the record first.`,
|
||||
);
|
||||
}
|
||||
const hydrated = this.#admitEntry(this.#hydrated, registered, entry, value);
|
||||
this.#refuseIndexSharing(this.#hydrated, entry.type, registered.catalogEntry.schemaName, entry.key, hydrated.lookups);
|
||||
const legacy = this.#hydrated.legacyGlobals.get(entry.type);
|
||||
|
|
|
|||
|
|
@ -71,8 +71,10 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore {
|
|||
const disk = this.#readDisk();
|
||||
this.#format = disk.format;
|
||||
const existing = disk.envelopes;
|
||||
// The record the file stores at the key is the one removed when the removal's schema owns its stored id: the
|
||||
// arrived record's type is its schema's name, which a type of another name does not carry.
|
||||
const filtered = existing.filter(
|
||||
(candidate) => !(candidate.type === entry.type && candidate.key === entry.key),
|
||||
(candidate) => !(candidate.key === entry.key && (schemaIdForEnvelope(candidate) === schemaIdForEnvelope(entry) || writtenOwns(entry, schemaIdForEnvelope(candidate)))),
|
||||
);
|
||||
await this.#writeAll([], filtered);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ const sameRecord = (left: CultCacheEnvelope | undefined, right: CultCacheEnvelop
|
|||
|
||||
test("vectors rows.msgpack: every row's refusal, held records, file, foreign list and kept bytes", async () => {
|
||||
const rows = decode(await readFile(join(vectors, "rows.msgpack"))) as Row[];
|
||||
assert.equal(rows.length, 11);
|
||||
assert.equal(rows.length, 12);
|
||||
let walked = 0;
|
||||
const failures: string[] = [];
|
||||
for (const [label, registered, ops, expect] of rows) {
|
||||
|
|
@ -139,7 +139,12 @@ test("vectors rows.msgpack: every row's refusal, held records, file, foreign lis
|
|||
assert.equal(failed, true, `${label}: the last op is refused`);
|
||||
const [key] = refusal;
|
||||
if (label === "key-in-two-stores-refused") {
|
||||
assert.ok((refused as Error).message.includes(key), `${label}: the refusal names the key`);
|
||||
// The key is read out of the refusal as a delimited token: the message opens "Record <key> is held by" and the key
|
||||
// occurs nowhere else in it (not inside a schema name).
|
||||
const message = (refused as Error).message;
|
||||
const named = /^Record (\S+) is held by/u.exec(message);
|
||||
assert.equal(named?.[1], key, `${label}: the refusal opens with the key`);
|
||||
assert.equal(message.split(/[^A-Za-z0-9_.]+/u).filter((token) => token === key).length, 1, `${label}: the key occurs only as a delimited token`);
|
||||
assert.deepEqual(cache.snapshot(), beforeCache, `${label}: the earlier pulled state survives`);
|
||||
assert.deepEqual(await Promise.all(attached.slice(0, beforeAttached.length).map((copy) => readFile(copy))), beforeAttached, `${label}: the attached files are byte-identical`);
|
||||
} else {
|
||||
|
|
@ -181,7 +186,7 @@ test("vectors rows.msgpack: every row's refusal, held records, file, foreign lis
|
|||
}
|
||||
}
|
||||
assert.deepEqual(failures, [], "every row holds its verdict");
|
||||
assert.equal(walked, 11);
|
||||
assert.equal(walked, 12);
|
||||
});
|
||||
|
||||
// Beyond the vector: what the walker's string-typed rows cannot say.
|
||||
|
|
@ -252,13 +257,13 @@ test("a foreign key one store carries and another holds under a registered type
|
|||
const catalog = join(dir, "catalog.cc");
|
||||
await copyFile(join(vectors, "catalog-store.cc"), catalog);
|
||||
const writer = CultCache.builder().withDocumentType(types.gamma!).withGenericStore(new SingleFileMessagePackBackingStore(foreign)).build();
|
||||
await writer.put(types.gamma!, "k", { text: "g" } as never);
|
||||
await writer.put(types.gamma!, "qx", { text: "g" } as never);
|
||||
const cache = CultCache.builder().withDocumentType(types.alpha!).build();
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(catalog), types.alpha!);
|
||||
await cache.pullAllBackingStores();
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(foreign));
|
||||
await assert.rejects(cache.pullAllBackingStores(), /held by two stores/u);
|
||||
assert.deepEqual(cache.snapshot().map((e) => e.key), ["k"], "the earlier pulled state survives");
|
||||
assert.deepEqual(cache.snapshot().map((e) => e.key), ["qx"], "the earlier pulled state survives");
|
||||
assert.deepEqual(cache.foreignRecords(), []);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
|
|
@ -293,3 +298,80 @@ test("a foreign key is refused by the cache before any store is asked, and forei
|
|||
assert.equal(await cache.delete(types.alpha!, "nobody"), false);
|
||||
assert.deepEqual(calls, []);
|
||||
});
|
||||
|
||||
// The store's delete matches the record the file stores by its schema id, not by the type string: a record read back is
|
||||
// typed by its schema's name, which a type of another name does not carry.
|
||||
test("a delete removes the record the file stores although the type differs from the schema name, and the key retypes across a reload", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "ki-"));
|
||||
try {
|
||||
const path = join(dir, "s.cc");
|
||||
const named = (type: string, schemaName: string, schemaId: string) =>
|
||||
defineDocumentType({ type, schemaId, schemaName, schemaVersion: `${schemaName}.v1`, schema: z.object({ v: z.string() }) });
|
||||
const first = named("app.first", "wire.first", "wire.first.v1");
|
||||
const second = named("app.second", "wire.second", "wire.second.v1");
|
||||
const open = () => CultCache.builder().withDocumentType(first).withDocumentType(second).withGenericStore(new SingleFileMessagePackBackingStore(path)).build();
|
||||
const writer = open();
|
||||
await writer.put(first, "k", { v: "a" });
|
||||
const reader = open();
|
||||
await reader.pullAllBackingStores();
|
||||
assert.equal(await reader.delete(first, "k"), true);
|
||||
assert.deepEqual([...stored(path).keys()], [], "the record is gone from the file");
|
||||
await reader.put(second, "k", { v: "b" });
|
||||
assert.deepEqual([...stored(path).values()].map((e) => [e.key, e.catalogEntry?.schemaName]), [["k", "wire.second"]]);
|
||||
const again = open();
|
||||
await again.pullAllBackingStores();
|
||||
assert.deepEqual(again.snapshot().map((e) => [e.type, e.key]), [["app.second", "k"]]);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("an owned rename whose two types home in different stores is refused, and neither store changes", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "ki-"));
|
||||
try {
|
||||
const oldPath = join(dir, "old.cc");
|
||||
const newPath = join(dir, "new.cc");
|
||||
const old = plain("rn.old", "rn.id.v1");
|
||||
const renamed = plain("rn.new", "rn.id.v2", ["rn.id.v2", "rn.id.v1"]);
|
||||
const cache = CultCache.builder().withDocumentType(old).withDocumentType(renamed).build();
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(oldPath), old);
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(newPath), renamed);
|
||||
await cache.put(old, "k", { v: "a" });
|
||||
const before = await readFile(oldPath);
|
||||
await assert.rejects(cache.put(renamed, "k", { v: CANARY }), (error: Error) => {
|
||||
assert.ok(error instanceof SchemaConflictError && error.recordKey === "k");
|
||||
assert.ok(!error.message.includes(CANARY));
|
||||
return true;
|
||||
});
|
||||
assert.deepEqual(await readFile(oldPath), before);
|
||||
assert.equal(existsSync(newPath), false, "the new home was never written");
|
||||
assert.deepEqual(cache.snapshot().map((e) => [e.type, e.key]), [["rn.old", "k"]]);
|
||||
// Same home: the rename replaces.
|
||||
const together = CultCache.builder().withDocumentType(old).withDocumentType(renamed).withGenericStore(new SingleFileMessagePackBackingStore(join(dir, "one.cc"))).build();
|
||||
await together.put(old, "k", { v: "a" });
|
||||
await together.put(renamed, "k", { v: "b" });
|
||||
assert.deepEqual(together.snapshot().map((e) => [e.type, e.key]), [["rn.new", "k"]]);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("two stores each carrying an unregistered record at one key refuse the load and admit nothing", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "ki-"));
|
||||
try {
|
||||
const paths = [join(dir, "a.cc"), join(dir, "b.cc")];
|
||||
for (const path of paths) {
|
||||
const writer = CultCache.builder().withDocumentType(types.gamma!).withGenericStore(new SingleFileMessagePackBackingStore(path)).build();
|
||||
await writer.put(types.gamma!, "k", { text: "g" } as never);
|
||||
}
|
||||
// Neither store's records are claimed: gamma is not registered here.
|
||||
const cache = CultCache.builder().withDocumentType(types.alpha!).withDocumentType(types.beta!).build();
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(paths[0]!), types.alpha!);
|
||||
await cache.addBackingStore(new SingleFileMessagePackBackingStore(paths[1]!), types.beta!);
|
||||
await assert.rejects(cache.pullAllBackingStores(), /^Error: Record k is held by two stores/u);
|
||||
assert.deepEqual(cache.foreignRecords(), []);
|
||||
assert.deepEqual(cache.snapshot(), []);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue