Skip to content

Commit 6b3fb65

Browse files
committed
fix: UNION performance
1 parent 362807d commit 6b3fb65

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

.changeset/few-carrots-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"rdf-web-access-control": minor
3+
---
4+
5+
Inline the `VALUES` clauses inside every `UNION` to improve performance when store does "bottom-up" query evaluation

packages/rdf-web-access-control/lib/instanceAuthorization.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@ export function instanceAuthorization(
1414
const check = { authorization, agent: variable('agent'), agentClass: variable('agentClass') }
1515
const term = variable('term')
1616

17-
const patternUnion = authorizationChecks.reduce((previous, buildPatterns) => {
17+
const values = sparql`
18+
VALUES ?mode { ${acl.Control} ${accessMode} }
19+
VALUES ?agent { ${agentTerm || '<>'} }
20+
VALUES ?agentClass { ${foaf.Agent} ${agentClasses(agent).filter(onlyNamedNodes)} }
21+
VALUES ?term { ${terms} }
22+
`
23+
24+
return ASK`${(authorizationChecks.reduce((previous, buildPatterns) => {
1825
return sparql`${previous}
1926
UNION
2027
{
28+
${values}
29+
2130
${authorization} a ${acl.Authorization} ;
2231
${acl.mode} ?mode ;
2332
${acl.accessTo} ${term} .
@@ -26,6 +35,8 @@ export function instanceAuthorization(
2635
}
2736
UNION
2837
{
38+
${values}
39+
2940
${term} a ?type .
3041
${authorization} a ${acl.Authorization} ;
3142
${acl.mode} ?mode ;
@@ -34,14 +45,8 @@ export function instanceAuthorization(
3445
${combinePatterns(additionalPatterns, authorization)}
3546
}`
3647
}, sparql`{
37-
${term} ${acl.owner} ?agent .
38-
}`)
48+
${values}
3949
40-
return ASK`
41-
VALUES ?mode { ${acl.Control} ${accessMode} }
42-
VALUES ?agent { ${agentTerm || '<>'} }
43-
VALUES ?agentClass { ${foaf.Agent} ${agentClasses(agent).filter(onlyNamedNodes)} }
44-
VALUES ?term { ${terms} }
45-
46-
${patternUnion}`
50+
${term} ${acl.owner} ?agent .
51+
}`))}`
4752
}

packages/rdf-web-access-control/lib/typeAuthorization.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@ export function typeAuthorization(
1313
const authorization = variable('authorization')
1414
const check = { authorization, agent: variable('agent'), agentClass: variable('agentClass') }
1515

16+
const values = sparql`
17+
VALUES ?mode { ${acl.Control} ${accessMode} }
18+
VALUES ?type { ${rdfs.Resource} ${types.filter(onlyNamedNodes)} }
19+
VALUES ?agent { ${agentTerm || '<>'} }
20+
VALUES ?agentClass { ${foaf.Agent} ${agentClasses(agent).filter(onlyNamedNodes)} }
21+
`
22+
1623
const patternUnion = authorizationChecks.reduce((previous: SparqlTemplateResult | string, buildPatterns) => {
1724
const next = sparql`{
25+
${values}
26+
1827
${authorization} a ${acl.Authorization} ;
1928
${acl.mode} ?mode ;
2029
${acl.accessToClass} ?type .
@@ -29,11 +38,6 @@ export function typeAuthorization(
2938
}, '')
3039

3140
return ASK`
32-
VALUES ?mode { ${acl.Control} ${accessMode} }
33-
VALUES ?type { ${rdfs.Resource} ${types.filter(onlyNamedNodes)} }
34-
VALUES ?agent { ${agentTerm || '<>'} }
35-
VALUES ?agentClass { ${foaf.Agent} ${agentClasses(agent).filter(onlyNamedNodes)} }
36-
3741
${combinePatterns(additionalPatterns, authorization)}
3842
3943
${patternUnion}`

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ ansi-regex@^4.1.0:
17721772
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.1.tgz#164daac87ab2d6f6db3a29875e2d1766582dabed"
17731773
integrity sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g==
17741774

1775-
ansi-regex@^5.0.0, ansi-regex@^5.0.1:
1775+
ansi-regex@^5.0.1:
17761776
version "5.0.1"
17771777
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
17781778
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==

0 commit comments

Comments
 (0)