-
Notifications
You must be signed in to change notification settings - Fork 14
/
folding.js
54 lines (48 loc) · 1.71 KB
/
folding.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Quads may be folded if
* - a quad is deleted after it has been inserted
* - a quad is inserted after it has been deleted
*
* Assumption: DELETE queries are always executed before INSERT queries.
* Therefore we handle deleted quads always before inserted quads.
*/
export function foldChangeSets(entry, changeSets) {
if (entry.options?.foldEffectiveChanges) {
// Create database
const foldedDelete = {};
const foldedInsert = {};
// Process changeSets
changeSets.forEach((changeSet) => {
const { effectiveDelete, effectiveInsert } = changeSet;
// Fold deleted quads if they have been inserted before
effectiveDelete.forEach((quad) => {
const key = JSON.stringify(quad);
if (foldedInsert[key])
delete foldedInsert[key];
else
foldedDelete[key] = quad;
});
// Do the inverse of effectiveDelete
effectiveInsert.forEach((quad) => {
const key = JSON.stringify(quad);
if (foldedDelete[key])
delete foldedDelete[key];
else
foldedInsert[key] = quad;
});
});
// Collect resulting values
const foldedChangeSets = [];
const foldedDeleteQuads = Object.values(foldedDelete);
if (foldedDeleteQuads.length)
foldedChangeSets.push({ delete: foldedDeleteQuads, insert: [] });
const foldedInsertQuads = Object.values(foldedInsert);
if (foldedInsertQuads.length)
foldedChangeSets.push({ delete: [], insert: foldedInsertQuads });
if (process.env["DEBUG_DELTA_FOLD"])
console.log(`Folded changeset from:\n ${JSON.stringify(changeSets)}\nto:\n ${JSON.stringify(foldedChangeSets)}`);
return foldedChangeSets;
} else {
return changeSets;
}
}