Skip to content

Commit

Permalink
perf: better insert performance
Browse files Browse the repository at this point in the history
  • Loading branch information
thesophiaxu committed Dec 19, 2021
1 parent 1ce5f4e commit de8fdfe
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if (account?.uid) {
await unigraph.updateObject(account.uid, {
access_token: token,
token_expires_in: (new Date((new Date()).getTime() + 3600 * 1000)).toISOString()
});
}, undefined, undefined, []);

let auth = new google.auth.OAuth2(
gmailClientId,
Expand All @@ -69,8 +69,8 @@ if (account?.uid) {
name: {type: {"unigraph.id": "$/schema/markdown"}, _value: cal.summary}
}
});
const uids = await unigraph.addObject(calObjs, '$/schema/calendar', undefined, []);
for (let i=0; i<calObjs.length; ++i) {
const uids = await unigraph.addObject(calObjs[i], '$/schema/calendar');
await unigraph.runExecutable('$/executable/sync-google-calendar-specific', {uid: uids[0]}, {calendar: calendar});
await unigraph.runExecutable('$/executable/sync-google-calendar-specific', {uid: uids[i]}, {calendar: calendar});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const toEod = (date) => {

// Process the changes, looping through all the events one by one
//if (cal['_value'].id?.['_value.%'] !== "c_5le2cv45o72utfkh4bcntpj2f4@group.calendar.google.com") return;

const toAdd = [];
for (let i=0; i<items.length; ++i) {
const ev = items[i];
if (ev.status === "cancelled") {
Expand Down Expand Up @@ -153,13 +153,13 @@ for (let i=0; i<items.length; ++i) {
identifier: el.email,
})),
...recurrenceObj
}
await unigraph.addObject(evObj, "$/schema/calendar_event")
};
toAdd.push(evObj);
}
}
}


await unigraph.updateObject(calUid, {
if (toAdd.length) await unigraph.addObject(toAdd, "$/schema/calendar_event");
if (syncToken !== nextSyncToken) await unigraph.updateObject(calUid, {
sync_token: nextSyncToken
});
5 changes: 3 additions & 2 deletions packages/unigraph-dev-backend/src/localUnigraphApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ export function getLocalUnigraphAPI(client: DgraphClient, states: {caches: Recor
else {prev.push(curr); return prev}
}, []);
},
addObject: async (object, schema, padded) => {
addObject: async (object, schema, padded, subIds) => {
clearEmpties(object);
//console.log(JSON.stringify(object, null, 4));
const objects = Array.isArray(object) ? object : [object];
if (objects.length === 0) return [];
const finalUnigraphObjects = objects.map((obj, index) => {
let unigraphObject = obj;
if (!padded) unigraphObject = buildUnigraphEntity(obj, schema, states.caches['schemas'].data, undefined, {globalStates: {nextUid: 100000 * index}} as any);
Expand All @@ -96,7 +97,7 @@ export function getLocalUnigraphAPI(client: DgraphClient, states: {caches: Recor
//console.log(JSON.stringify(finalUnigraphObject, null, 4));
const upsert = insertsToUpsert(finalUnigraphObjects, undefined, states.caches['schemas'].dataAlt![0]);
const uids = await client.createUnigraphUpsert(upsert);
callHooks(states.hooks, "after_object_changed", {subscriptions: states.subscriptions, caches: states.caches});
callHooks(states.hooks, "after_object_changed", {subscriptions: states.subscriptions, caches: states.caches, subIds});
return uids;
},
getNamespaceMapUid: (name) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/unigraph-dev-common/src/types/unigraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface Unigraph<TT = WebSocket | false> {
* @param object The object to be added.
* @param schema Schema of that object, must be valid. Such as: `$/schema/abc`
*/
addObject(object: any, schema: string, padded?: boolean): any;
addObject(object: any, schema: string, padded?: boolean, subIds?: any[]): any;
/**
* Reach into the namespace map cache and get a UID corresponding to the name.
*
Expand Down

0 comments on commit de8fdfe

Please sign in to comment.