This commit is contained in:
Zef Hemel
2023-02-28 11:13:18 +01:00
parent 520b5e91d6
commit ece4177e11
5 changed files with 57 additions and 6 deletions
+18 -1
View File
@@ -10,7 +10,7 @@ Deno.test("Test store", async () => {
const primary = new DiskSpacePrimitives(primaryPath);
const secondary = new DiskSpacePrimitives(secondaryPath);
const statusMap = new Map<string, SyncStatusItem>();
const sync = new SpaceSync(primary, secondary, statusMap);
const sync = new SpaceSync(primary, secondary, statusMap, {});
// Write one page to primary
await primary.writeFile("index", "utf8", "Hello");
@@ -129,6 +129,7 @@ Deno.test("Test store", async () => {
secondary,
ternary,
new Map<string, SyncStatusItem>(),
{},
);
console.log(
"N ops",
@@ -137,9 +138,25 @@ Deno.test("Test store", async () => {
await sleep(2);
assertEquals(await sync2.syncFiles(SpaceSync.primaryConflictResolver), 0);
// I had to look up what follows ternary (https://english.stackexchange.com/questions/25116/what-follows-next-in-the-sequence-unary-binary-ternary)
const quaternaryPath = await Deno.makeTempDir();
const quaternary = new DiskSpacePrimitives(quaternaryPath);
const sync3 = new SpaceSync(
secondary,
quaternary,
new Map<string, SyncStatusItem>(),
{
excludePrefixes: ["index"],
},
);
const selectingOps = await sync3.syncFiles(SpaceSync.primaryConflictResolver);
assertEquals(selectingOps, 1);
await Deno.remove(primaryPath, { recursive: true });
await Deno.remove(secondaryPath, { recursive: true });
await Deno.remove(ternaryPath, { recursive: true });
await Deno.remove(quaternaryPath, { recursive: true });
async function doSync() {
await sleep();
+20 -2
View File
@@ -20,14 +20,25 @@ class ConsoleLogger implements Logger {
}
}
export type SyncOptions = {
logger?: Logger;
excludePrefixes?: string[];
};
// Implementation of this algorithm https://unterwaditzer.net/2016/sync-algorithm.html
export class SpaceSync {
logger: ConsoleLogger;
excludePrefixes: string[];
constructor(
private primary: SpacePrimitives,
private secondary: SpacePrimitives,
readonly snapshot: Map<string, SyncStatusItem>,
readonly logger: Logger = new ConsoleLogger(),
) {}
readonly options: SyncOptions,
) {
this.logger = options.logger || new ConsoleLogger();
this.excludePrefixes = options.excludePrefixes || [];
}
async syncFiles(
conflictResolver: (
@@ -100,6 +111,13 @@ export class SpaceSync {
// console.log("Syncing", name, primaryHash, secondaryHash);
let operations = 0;
// Check if not matching one of the excluded prefixes
for (const prefix of this.excludePrefixes) {
if (name.startsWith(prefix)) {
return operations;
}
}
if (
primaryHash !== undefined && secondaryHash === undefined &&
!this.snapshot.has(name)