1
0
silverbullet/webapp/spaces/sync.ts

209 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-04-05 15:02:17 +00:00
import { Space } from "./space";
2022-04-07 13:21:30 +00:00
import { PageMeta } from "../../common/types";
import { SpacePrimitives } from "./space_primitives";
2022-04-05 15:02:17 +00:00
export class SpaceSync {
constructor(
2022-04-07 13:21:30 +00:00
private primary: Space,
private secondary: Space,
public primaryLastSync: number,
public secondaryLastSync: number,
private trashPrefix: string
) {}
// Strategy: Primary wins
public static primaryConflictResolver(
2022-04-07 13:21:30 +00:00
primary: Space,
secondary: Space
): (pageMeta1: PageMeta, pageMeta2: PageMeta) => Promise<void> {
return async (pageMeta1, pageMeta2) => {
const pageName = pageMeta1.name;
const revisionPageName = `${pageName}.conflicted.${pageMeta2.lastModified}`;
// Copy secondary to conflict copy
let oldPageData = await secondary.readPage(pageName);
await secondary.writePage(revisionPageName, oldPageData.text);
// Write replacement on top
let newPageData = await primary.readPage(pageName);
await secondary.writePage(
pageName,
newPageData.text,
true,
newPageData.meta.lastModified
);
};
2022-04-05 15:02:17 +00:00
}
async syncablePages(
2022-04-07 13:21:30 +00:00
space: Space
): Promise<{ pages: PageMeta[]; nowTimestamp: number }> {
let fetchResult = await space.fetchPageList();
return {
pages: [...fetchResult.pages].filter(
(pageMeta) => !pageMeta.name.startsWith(this.trashPrefix)
),
nowTimestamp: fetchResult.nowTimestamp,
};
}
2022-04-07 13:21:30 +00:00
async trashPages(space: SpacePrimitives): Promise<PageMeta[]> {
return [...(await space.fetchPageList()).pages]
.filter((pageMeta) => pageMeta.name.startsWith(this.trashPrefix))
.map((pageMeta) => ({
...pageMeta,
name: pageMeta.name.substring(this.trashPrefix.length),
}));
}
async syncPages(
conflictResolver?: (
pageMeta1: PageMeta,
pageMeta2: PageMeta
) => Promise<void>
): Promise<number> {
let syncOps = 0;
let { pages: primaryAllPagesSet, nowTimestamp: primarySyncTimestamp } =
await this.syncablePages(this.primary);
let allPagesPrimary = new Map(primaryAllPagesSet.map((p) => [p.name, p]));
let { pages: secondaryAllPagesSet, nowTimestamp: secondarySyncTimestamp } =
await this.syncablePages(this.secondary);
2022-04-05 15:02:17 +00:00
let allPagesSecondary = new Map(
secondaryAllPagesSet.map((p) => [p.name, p])
);
let allTrashPrimary = new Map(
(await this.trashPages(this.primary))
// Filter out old trash
.filter((p) => p.lastModified > this.primaryLastSync)
.map((p) => [p.name, p])
);
let allTrashSecondary = new Map(
(await this.trashPages(this.secondary))
// Filter out old trash
.filter((p) => p.lastModified > this.secondaryLastSync)
.map((p) => [p.name, p])
2022-04-05 15:02:17 +00:00
);
// Iterate over all pages on the primary first
for (let [name, pageMetaPrimary] of allPagesPrimary.entries()) {
let pageMetaSecondary = allPagesSecondary.get(pageMetaPrimary.name);
if (!pageMetaSecondary) {
// New page on primary
// Let's check it's not on the deleted list
if (allTrashSecondary.has(name)) {
// Explicitly deleted, let's skip
continue;
}
2022-04-05 15:02:17 +00:00
// Push from primary to secondary
console.log("New page on primary", name, "syncing to secondary");
let pageData = await this.primary.readPage(name);
await this.secondary.writePage(
name,
pageData.text,
true,
secondarySyncTimestamp // The reason for this is to not include it in the next sync cycle, we cannot blindly use the lastModified date due to time skew
2022-04-05 15:02:17 +00:00
);
syncOps++;
2022-04-05 15:02:17 +00:00
} else {
// Existing page
if (pageMetaPrimary.lastModified > this.primaryLastSync) {
2022-04-05 15:02:17 +00:00
// Primary updated since last sync
if (pageMetaSecondary.lastModified > this.secondaryLastSync) {
2022-04-05 15:02:17 +00:00
// Secondary also updated! CONFLICT
if (conflictResolver) {
await conflictResolver(pageMetaPrimary, pageMetaSecondary);
} else {
throw Error(
`Sync conflict for ${name} with no conflict resolver specified`
);
}
2022-04-05 15:02:17 +00:00
} else {
// Ok, not changed on secondary, push it secondary
console.log(
"Changed page on primary",
name,
"syncing to secondary"
);
let pageData = await this.primary.readPage(name);
await this.secondary.writePage(
name,
pageData.text,
false,
secondarySyncTimestamp
2022-04-05 15:02:17 +00:00
);
syncOps++;
2022-04-05 15:02:17 +00:00
}
} else if (pageMetaSecondary.lastModified > this.secondaryLastSync) {
2022-04-05 15:02:17 +00:00
// Secondary updated, but not primary (checked above)
// Push from secondary to primary
console.log("Changed page on secondary", name, "syncing to primary");
let pageData = await this.secondary.readPage(name);
await this.primary.writePage(
name,
pageData.text,
false,
primarySyncTimestamp
2022-04-05 15:02:17 +00:00
);
syncOps++;
2022-04-05 15:02:17 +00:00
} else {
// Neither updated, no-op
}
}
}
// Now do a simplified version in reverse, only detecting new pages
for (let [name, pageMetaSecondary] of allPagesSecondary.entries()) {
if (!allPagesPrimary.has(pageMetaSecondary.name)) {
// New page on secondary
// Let's check it's not on the deleted list
if (allTrashPrimary.has(name)) {
// Explicitly deleted, let's skip
continue;
}
2022-04-05 15:02:17 +00:00
// Push from secondary to primary
console.log("New page on secondary", name, "pushing to primary");
let pageData = await this.secondary.readPage(name);
await this.primary.writePage(
name,
pageData.text,
false,
primarySyncTimestamp
);
syncOps++;
2022-04-05 15:02:17 +00:00
}
}
// And finally, let's trash some pages
for (let pageToDelete of allTrashPrimary.values()) {
console.log("Deleting", pageToDelete.name, "on secondary");
try {
await this.secondary.deletePage(
pageToDelete.name,
secondarySyncTimestamp
);
syncOps++;
} catch (e: any) {
console.log("Page already gone", e.message);
}
}
for (let pageToDelete of allTrashSecondary.values()) {
console.log("Deleting", pageToDelete.name, "on primary");
try {
await this.primary.deletePage(pageToDelete.name, primarySyncTimestamp);
syncOps++;
} catch (e: any) {
console.log("Page already gone", e.message);
}
}
// Setting last sync time to the timestamps we got back when fetching the page lists on each end
this.primaryLastSync = primarySyncTimestamp;
this.secondaryLastSync = secondarySyncTimestamp;
return syncOps;
2022-04-05 15:02:17 +00:00
}
}