Skip to content

Commit 4510617

Browse files
committed
eng-1012 update query builder to use reified relations
1 parent 8c53cec commit 4510617

File tree

5 files changed

+489
-205
lines changed

5 files changed

+489
-205
lines changed

apps/roam/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"react-draggable": "4.4.5",
7070
"react-in-viewport": "1.0.0-alpha.20",
7171
"react-vertical-timeline-component": "3.5.2",
72-
"roamjs-components": "0.85.4",
72+
"roamjs-components": "0.85.6",
7373
"tldraw": "2.3.0",
7474
"use-sync-external-store": "1.5.0",
7575
"xregexp": "^5.0.0",

apps/roam/src/utils/createReifiedBlock.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const createReifiedBlock = async ({
8686
const RELATION_PAGE_TITLE = "roam/js/discourse-graph/relations";
8787
let relationPageUid: string | undefined = undefined;
8888

89-
const getRelationPageUid = async (): Promise<string> => {
89+
export const getOrCreateRelationPageUid = async (): Promise<string> => {
9090
if (relationPageUid === undefined) {
9191
relationPageUid = getPageUidByPageTitle(RELATION_PAGE_TITLE);
9292
if (relationPageUid === "") {
@@ -96,8 +96,16 @@ const getRelationPageUid = async (): Promise<string> => {
9696
return relationPageUid;
9797
};
9898

99+
export const getExistingRelationPageUid = (): string | undefined => {
100+
if (relationPageUid === undefined) {
101+
const uid = getPageUidByPageTitle(RELATION_PAGE_TITLE);
102+
if (uid !== "") relationPageUid = uid;
103+
}
104+
return relationPageUid;
105+
};
106+
99107
export const countReifiedRelations = async (): Promise<number> => {
100-
const pageUid = await getRelationPageUid();
108+
const pageUid = getExistingRelationPageUid();
101109
if (pageUid === undefined) return 0;
102110
const r = await window.roamAlphaAPI.data.async.q(
103111
`[:find (count ?c) :where [?p :block/children ?c] [?p :block/uid "${pageUid}"]]`,
@@ -117,7 +125,7 @@ export const createReifiedRelation = async ({
117125
const authorized = getSetting("use-reified-relations");
118126
if (authorized) {
119127
return await createReifiedBlock({
120-
destinationBlockUid: await getRelationPageUid(),
128+
destinationBlockUid: await getOrCreateRelationPageUid(),
121129
schemaUid: relationBlockUid,
122130
parameterUids: {
123131
sourceUid,

apps/roam/src/utils/fireQuery.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import conditionToDatalog from "./conditionToDatalog";
22
import type {
33
PullBlock,
4-
DatalogAndClause,
54
DatalogClause,
5+
DatalogAndClause,
66
} from "roamjs-components/types";
77
import compileDatalog from "./compileDatalog";
88
import { getNodeEnv } from "roamjs-components/util/env";
@@ -107,7 +107,8 @@ const optimizeQuery = (
107107
if (Array.from(allVars).every((v) => capturedVariables.has(v))) {
108108
score = 10;
109109
} else {
110-
score = 100002;
110+
// downgrade disjunction and negation
111+
score = c.type === "and-clause" ? 100002 : 100006;
111112
}
112113
} else if (c.type === "not-join-clause" || c.type === "or-join-clause") {
113114
if (c.variables.every((v) => capturedVariables.has(v.value))) {
@@ -156,6 +157,9 @@ const optimizeQuery = (
156157
.filter((v) => v.type === "variable")
157158
.forEach((v) => capturedVariables.add(v.value));
158159
}
160+
// Question: Should we not consider all variables in a complex clause captured?
161+
// const newVars = gatherDatalogVariablesFromClause(bestClause);
162+
// newVars.forEach((v) => capturedVariables.add(v));
159163
}
160164
return orderedClauses;
161165
};
@@ -197,7 +201,7 @@ export const getDatalogQuery = ({
197201
const whereClauses = optimizeQuery(
198202
getWhereClauses({ conditions, returnNode }),
199203
new Set([]),
200-
) as DatalogClause[];
204+
);
201205

202206
const defaultSelections: {
203207
mapper: PredefinedSelection["mapper"];

0 commit comments

Comments
 (0)