Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/objectql-cascade-closure-memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@objectstack/objectql': patch
---

Clause-②: no
Metadata-protocol: none

`ObjectQL#getCascadeDeleteObjects` remembers its answer per object name until the schema registry's `objectRevision` moves, instead of walking every registered object on each call. The metadata read serves this list beside every `object` item (`cascadeDeleteObjects`), so an app bundle request no longer repeats the walk once per object it names. The answer is unchanged: every registry change that drops the registry's merged-object cache also moves `objectRevision`, and each call still gets its own copy of the list.
98 changes: 98 additions & 0 deletions packages/objectql/src/engine-cascade-delete-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,101 @@ describe('[#28001] the object read serves `cascadeDeleteObjects` beside the item
.toEqual(['cd_loner_line']);
});
});

/**
* [#34206] The closure is memoized per engine on the registry's
* `objectRevision`: the metadata read serves it beside every object, and an
* app bundle reads every object it names, so the walk over the whole registry
* ran once per object per bundle request. The pins: a move of the revision
* retires the memo; a registry change that does not move it (a view) keeps the
* memo and the answer stays right; the memoized answer equals the uncached
* one for every object; and a caller's copy is its own.
*/
describe('[#34206] getCascadeDeleteObjects is memoized on the registry object revision', () => {
/** How many relation judgements the closure made: the walk's own unit of work. */
function countWalk(engine: ObjectQL) {
return vi.spyOn(engine as any, 'cascadeRelationBehavior');
}

it('a second call at the same revision does no walk, and answers the same list', async () => {
const { engine } = await makeEngine();
const first = engine.getCascadeDeleteObjects('cd_parent');
const walk = countWalk(engine);
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(first);
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child', 'cd_grandchild']);
expect(walk).not.toHaveBeenCalled();
});

it('invalidation: registering a cascading child moves the revision, and the next call names it', async () => {
const { engine } = await makeEngine();
expect(engine.getCascadeDeleteObjects('cd_loner')).toEqual([]);
const before = engine.registry.objectRevision;
engine.registry.registerObject({
name: 'cd_loner_line', label: 'Loner line',
fields: { id, name, loner: { name: 'loner', type: 'master_detail', reference: 'cd_loner', required: true } },
} as any, '__test__');
expect(engine.registry.objectRevision).not.toBe(before);
expect(engine.getCascadeDeleteObjects('cd_loner')).toEqual(['cd_loner_line']);
});

it('invalidation: unregistering the cascading child drops it from the next answer', async () => {
const { engine } = await makeEngine();
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child', 'cd_grandchild']);
engine.registry.unregisterObject('cd_grandchild', { force: true });
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child']);
});

it('control: a registry change that leaves the object revision alone keeps the memo, and the answer is still right', async () => {
const { engine } = await makeEngine();
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child', 'cd_grandchild']);
const before = engine.registry.objectRevision;
engine.registry.registerItem('view', { name: 'cd_parent_all', object: 'cd_parent', type: 'grid' } as any, 'name', '__test__');
// Non-vacuity: the view really landed, and the counter did not move.
expect(engine.registry.getItem('view', 'cd_parent_all')).toBeDefined();
expect(engine.registry.objectRevision).toBe(before);
const walk = countWalk(engine);
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child', 'cd_grandchild']);
expect(walk).not.toHaveBeenCalled();
});

it('cached equals uncached for every object, the uncached reading taken from a registry with no counter', async () => {
const { engine } = await makeEngine();
const { ReferencesAndMasterDetailFacet } = await import('./engine-references-and-master-detail.js');
// The registry with its counter hidden: the memo declines and every call walks.
const registry = engine.registry;
const counterless = new Proxy(registry, {
get: (target, key) => (key === 'objectRevision' ? undefined : Reflect.get(target, key, target)),
});
const uncachedHost = {
_registry: counterless,
cascadeRelationBehavior: (...a: unknown[]) => (engine as any).cascadeRelationBehavior(...a),
resolveObjectName: (n: string) => (engine as any).resolveObjectName(n),
};
const names = [...OBJECTS.map((o) => o.name), 'cd_no_such_object'];
for (const n of names) {
const uncached = ReferencesAndMasterDetailFacet.getCascadeDeleteObjects.call(uncachedHost as any, n);
// First call fills the memo, the second reads it.
expect(engine.getCascadeDeleteObjects(n)).toEqual(uncached);
expect(engine.getCascadeDeleteObjects(n)).toEqual(uncached);
}
// Non-vacuity: the uncached path walked at least once.
const walk = countWalk(engine);
ReferencesAndMasterDetailFacet.getCascadeDeleteObjects.call(uncachedHost as any, 'cd_parent');
expect(walk).toHaveBeenCalled();
});

it('each call gets its own copy: editing one answer does not change the next', async () => {
const { engine } = await makeEngine();
const mine = engine.getCascadeDeleteObjects('cd_parent')!;
mine.push('cd_tag');
mine.reverse();
expect(engine.getCascadeDeleteObjects('cd_parent')).toEqual(['cd_child', 'cd_grandchild']);
});

it('a name the registry does not hold answers `undefined`, and its later registration is answered', async () => {
const { engine } = await makeEngine();
expect(engine.getCascadeDeleteObjects('cd_later')).toBeUndefined();
engine.registry.registerObject({ name: 'cd_later', label: 'Later', fields: { id, name } } as any, '__test__');
expect(engine.getCascadeDeleteObjects('cd_later')).toEqual([]);
});
});
87 changes: 79 additions & 8 deletions packages/objectql/src/engine-references-and-master-detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,14 +638,85 @@ export class ReferencesAndMasterDetailFacet {
return members;
}

/** The body of `ObjectQL#getCascadeDeleteObjects`; the method's docblock there is its contract. */
/**
* The body of `ObjectQL#getCascadeDeleteObjects`; the method's docblock there is its contract.
*
* [#34206] Memoized per engine on the registry's `objectRevision`
* ({@link cascadeDeleteObjectsMemo}): the answer is computed once per
* object name per revision and each call gets its own copy.
*/
static getCascadeDeleteObjects(this: Pick<ReferencesAndMasterDetailHost, '_registry' | 'cascadeRelationBehavior' | 'resolveObjectName'>, object: string): string[] | undefined {
if (!this._registry.getObject(object)) return undefined;
const host: ThisParameterType<typeof collectCascadeDeleteObjects> = {
cascadeRelationBehavior: (...a) => this.cascadeRelationBehavior(...a),
};
return collectCascadeDeleteObjects.call(
host, this.resolveObjectName(object), this._registry.getAllObjects(),
);
const revision = (this._registry as unknown as { objectRevision?: unknown }).objectRevision;
if (typeof revision !== 'number') return computeCascadeDeleteObjects.call(this, object);
let memo = cascadeDeleteObjectsMemo.get(this);
if (memo === undefined || memo.revision !== revision) {
memo = { revision, answers: new Map() };
cascadeDeleteObjectsMemo.set(this, memo);
}
let answer = memo.answers.get(object);
if (answer === undefined) {
const computed = computeCascadeDeleteObjects.call(this, object);
// A name the registry does not hold is not remembered: the miss is one
// index read, and remembering it would let a caller grow the memo with
// names that do not exist.
if (computed === undefined) return undefined;
answer = Object.freeze(computed);
memo.answers.set(object, answer);
}
return [...answer];
}
}

/**
* [#34206] The memo of {@link ReferencesAndMasterDetailFacet.getCascadeDeleteObjects},
* one per engine (keyed on the engine's facet host, which `FacetHost.of`
* builds once per engine and keeps for its life).
*
* WHY: the metadata read serves this closure beside every `object` item
* (`cascadeDeleteObjects`), and an app bundle reads every object it names, so
* the walk over every registered object ran once per object per bundle
* request: about a fifth of a Setup bundle's server time, a 304 included.
*
* WHY THE KEY IS SOUND: the closure reads the registered object set and
* nothing else. `resolveObjectName` and `getObject` read the registry's
* object index; `getAllObjects` reads the merged objects;
* `cascadeRelationBehavior` reads one field definition and resolves its
* target's name through the same index (`statesDeleteSemantics`,
* `deleteWalkTargetOf`, `isFederatedUnprovisionedInjectedColumn` read the
* definitions they are handed). No row, no package flag of its own, no
* caller, no datasource. The registry's `objectRevision` moves on every
* change to that set: every site that drops its merged-object cache bumps it
* (`registerObject`, `unregisterObject`, `unregisterObjectsByPackage`,
* `removeObjectOverlay`, `withdrawObjectExtension`, `invalidate`,
* `invalidateAll` (which every package enable and disable calls), `reset`,
* and a refused registration run's restore, which `invalidateAll` follows).
* So this memo is exactly as fresh as the registry's own merged-object cache,
* and the roll-up summary index and the record-currency parent lock already
* key on the same counter.
*
* A registry without the counter (a test double) declines the memo: every
* call computes. Only a name the registry holds is remembered, so the memo
* grows with the registered objects, never with what callers ask. The
* cached list is frozen and every call returns a copy, so a caller that edits
* its list cannot change the next caller's answer.
*
* What it does not see: a subclass or spy that changes
* `cascadeRelationBehavior`'s answer AFTER the first call at a revision. The
* rule is code, not state, and the engine's own delete walk reads the same
* rule fresh, so this only matters to a test that swaps the rule mid-run.
*/
const cascadeDeleteObjectsMemo = new WeakMap<object, {
revision: number;
answers: Map<string, readonly string[]>;
}>();

/** The uncached closure: the body {@link ReferencesAndMasterDetailFacet.getCascadeDeleteObjects} memoizes. */
function computeCascadeDeleteObjects(this: Pick<ReferencesAndMasterDetailHost, '_registry' | 'cascadeRelationBehavior' | 'resolveObjectName'>, object: string): string[] | undefined {
if (!this._registry.getObject(object)) return undefined;
const host: ThisParameterType<typeof collectCascadeDeleteObjects> = {
cascadeRelationBehavior: (...a) => this.cascadeRelationBehavior(...a),
};
return collectCascadeDeleteObjects.call(
host, this.resolveObjectName(object), this._registry.getAllObjects(),
);
}
Loading