Resolve TS shard discovery filters by schema alias
This commit is contained in:
parent
aeb7a96bd3
commit
94a8c5b104
2 changed files with 41 additions and 5 deletions
|
|
@ -47,11 +47,11 @@ export class CultNetShardCatalog {
|
|||
}
|
||||
|
||||
list(options: CultNetShardCatalogOptions = {}): CultNetShardDescriptor[] {
|
||||
const requestedSchemaIds = options.schemaIds ? new Set(options.schemaIds) : null;
|
||||
const requestedSchemaIds = options.schemaIds ?? [];
|
||||
const requestedRecordKeys = options.recordKeys ?? [];
|
||||
|
||||
return this.shards.filter((shard) => {
|
||||
if (requestedSchemaIds && !shard.schemaIds?.some((schemaId) => requestedSchemaIds.has(schemaId))) {
|
||||
if (requestedSchemaIds.length > 0 && !schemaIdsMatchAny(shard.schemaIds ?? [], requestedSchemaIds)) {
|
||||
return false;
|
||||
}
|
||||
if (requestedRecordKeys.length > 0 && !requestedRecordKeys.some((recordKey) => shardServes(shard, { recordKey }))) {
|
||||
|
|
@ -86,7 +86,12 @@ export function shardServes(
|
|||
shard: CultNetShardDescriptor,
|
||||
options: { schemaId?: string; recordKey?: string } = {},
|
||||
): boolean {
|
||||
if (options.schemaId !== undefined && shard.schemaIds !== undefined && shard.schemaIds.length > 0 && !shard.schemaIds.includes(options.schemaId)) {
|
||||
if (
|
||||
options.schemaId !== undefined &&
|
||||
shard.schemaIds !== undefined &&
|
||||
shard.schemaIds.length > 0 &&
|
||||
!schemaIdsMatchAny(shard.schemaIds, [options.schemaId])
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (options.recordKey !== undefined && shard.keyPrefix !== undefined && !options.recordKey.startsWith(shard.keyPrefix)) {
|
||||
|
|
@ -95,6 +100,35 @@ export function shardServes(
|
|||
return true;
|
||||
}
|
||||
|
||||
function schemaIdsMatchAny(
|
||||
advertisedSchemaIds: readonly string[],
|
||||
requestedSchemaIds: readonly string[],
|
||||
): boolean {
|
||||
return advertisedSchemaIds.some(advertised =>
|
||||
requestedSchemaIds.some(requested => schemaIdsMatch(advertised, requested)),
|
||||
);
|
||||
}
|
||||
|
||||
function schemaIdsMatch(left: string, right: string): boolean {
|
||||
if (left === right) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const leftName = inferSchemaName(left) ?? left;
|
||||
const rightName = inferSchemaName(right) ?? right;
|
||||
return leftName === rightName;
|
||||
}
|
||||
|
||||
function inferSchemaName(schemaId: string): string | undefined {
|
||||
const marker = schemaId.lastIndexOf(".v");
|
||||
if (marker <= 0 || marker + 2 >= schemaId.length) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const version = schemaId.slice(marker + 2);
|
||||
return /^\d+$/u.test(version) ? schemaId.slice(0, marker) : undefined;
|
||||
}
|
||||
|
||||
function cloneShardDescriptor(descriptor: CultNetShardDescriptor): CultNetShardDescriptor {
|
||||
return {
|
||||
shardId: descriptor.shardId,
|
||||
|
|
|
|||
|
|
@ -936,13 +936,15 @@ test("CultNet shard catalog filters descriptors and answers catalog requests", (
|
|||
|
||||
assert.equal(watchedShardId, "profiles-a");
|
||||
assert.equal(shardServes(catalog.get("notes-a")!, { schemaId: "note.v1", recordKey: "note:1" }), true);
|
||||
assert.equal(shardServes(catalog.get("notes-a")!, { schemaId: "note", recordKey: "note:1" }), true);
|
||||
assert.equal(shardServes(catalog.get("notes-a")!, { schemaId: "profile.v1", recordKey: "note:1" }), false);
|
||||
assert.deepEqual(catalog.list({ schemaIds: ["note.v1"], recordKeys: ["note:1"] }).map((shard) => shard.shardId), ["notes-a"]);
|
||||
assert.deepEqual(catalog.list({ schemaIds: ["note"], recordKeys: ["note:1"] }).map((shard) => shard.shardId), ["notes-a"]);
|
||||
|
||||
const response = catalog.createCatalogResponse({
|
||||
schemaVersion: "cultnet.shard_catalog_request.v0",
|
||||
messageId: "shards",
|
||||
schemaIds: ["note.v1"],
|
||||
schemaIds: ["note"],
|
||||
recordKeys: ["note:2"],
|
||||
});
|
||||
assert.equal(response.schemaVersion, "cultnet.shard_catalog_response.v0");
|
||||
|
|
@ -987,7 +989,7 @@ test("CultNet peer can request and sync shard catalogs by message id", async ()
|
|||
const synced = new CultNetShardCatalog();
|
||||
const applied = await requester.syncShardCatalog(synced, {
|
||||
messageId: "peer-shards",
|
||||
schemaIds: ["note.v1"],
|
||||
schemaIds: ["note"],
|
||||
recordKeys: ["note:local"],
|
||||
timeoutMs: 1_000,
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue