Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: perf: replace improve root entity lookup performance by replacing #307

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
32 changes: 25 additions & 7 deletions src/authorization/transformers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
ConditionalQueryNode,
EntitiesQueryNode,
EntityFromIdQueryNode,
FirstOfListQueryNode,
NullQueryNode,
PERMISSION_DENIED_ERROR,
RootEntityIDQueryNode,
RuntimeErrorQueryNode,
TransformListQueryNode,
VariableAssignmentQueryNode,
Expand All @@ -15,6 +17,7 @@ import { FlexSearchQueryNode } from '../../query-tree/flex-search';
import { AccessOperation, AuthContext } from '../auth-basics';
import { PermissionResult } from '../permission-descriptors';
import { getPermissionDescriptorOfRootEntityType } from '../permission-descriptors-in-model';
import { decapitalize } from '../../utils/utils';

export function transformEntitiesQueryNode(node: EntitiesQueryNode, authContext: AuthContext) {
const permissionDescriptor = getPermissionDescriptorOfRootEntityType(node.rootEntityType);
Expand Down Expand Up @@ -57,17 +60,32 @@ export function transformEntityFromIdQueryNode(
{ code: PERMISSION_DENIED_ERROR },
);
default:
const entityVar = new VariableQueryNode('entity');
// EntityFromIdQueryNode is converted to FIRST(FOR e in ... FILTER ... RETURN), so it's
// best to just add the condition there

const itemVariable = new VariableQueryNode(decapitalize(node.rootEntityType.name));
const condition = permissionDescriptor.getAccessCondition(
authContext,
AccessOperation.READ,
entityVar,
itemVariable,
);
const idEqualsNode = new BinaryOperationQueryNode(
new RootEntityIDQueryNode(itemVariable),
BinaryOperator.EQUAL,
node.idNode,
);
return new FirstOfListQueryNode(
new TransformListQueryNode({
listNode: new EntitiesQueryNode(node.rootEntityType),
itemVariable,
filterNode: new BinaryOperationQueryNode(
idEqualsNode,
BinaryOperator.AND,
condition,
),
maxCount: 1,
}),
);
return new VariableAssignmentQueryNode({
variableNode: entityVar,
variableValueNode: node,
resultNode: new ConditionalQueryNode(condition, entityVar, new NullQueryNode()),
});
}
}

Expand Down
20 changes: 18 additions & 2 deletions src/database/arangodb/aql-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,24 @@ register(WithPreExecutionQueryNode, (node, context) => {
});

register(EntityFromIdQueryNode, (node, context) => {
const collection = getCollectionForType(node.rootEntityType, AccessType.EXPLICIT_READ, context);
return aql`DOCUMENT(${collection}, ${processNode(node.idNode, context)})`;
// We previously used the DOCUMENT() function here, but that function is discouraged if the
// collection is statically known. https://docs.arangodb.com/3.11/aql/functions/miscellaneous/#document

const itemVariable = new VariableQueryNode(decapitalize(node.rootEntityType.name));
const transformed = new FirstOfListQueryNode(
new TransformListQueryNode({
listNode: new EntitiesQueryNode(node.rootEntityType),
itemVariable,
filterNode: new BinaryOperationQueryNode(
new RootEntityIDQueryNode(itemVariable),
BinaryOperator.EQUAL,
node.idNode,
),
maxCount: 1,
}),
);

return processNode(transformed, context);
});

register(PropertyAccessQueryNode, (node, context) => {
Expand Down
Loading