Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions packages/core/src/sync/merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,22 +78,13 @@ export class MerkleDAG {
return dag;
}

public static compare(dag1: MerkleDAG, dag2: MerkleDAG): { added: string[], removed: string[], modified: string[] } {
const nodes1 = new Map(Array.from(dag1.getAllNodes()).map(n => [n.id, n]));
const nodes2 = new Map(Array.from(dag2.getAllNodes()).map(n => [n.id, n]));

const added = Array.from(nodes2.keys()).filter(k => !nodes1.has(k));
const removed = Array.from(nodes1.keys()).filter(k => !nodes2.has(k));

// For modified, we'll check if the data has changed for nodes that exist in both
const modified: string[] = [];
for (const [id, node1] of Array.from(nodes1.entries())) {
const node2 = nodes2.get(id);
if (node2 && node1.data !== node2.data) {
modified.push(id);
}
}
public static compare(dag1: MerkleDAG, dag2: MerkleDAG): { added: string[], removed: string[] } {
const nodes1 = new Set(Array.from(dag1.getAllNodes()).map(n => n.id));
const nodes2 = new Set(Array.from(dag2.getAllNodes()).map(n => n.id));

const added = Array.from(nodes2).filter(k => !nodes1.has(k));
const removed = Array.from(nodes1).filter(k => !nodes2.has(k));

return { added, removed, modified };
return { added, removed };
}
}
4 changes: 2 additions & 2 deletions packages/core/src/sync/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ export class FileSynchronizer {
// Compare the DAGs
const changes = MerkleDAG.compare(this.merkleDAG, newMerkleDAG);

// If there are any changes in the DAG, we should also do a file-level comparison
if (changes.added.length > 0 || changes.removed.length > 0 || changes.modified.length > 0) {
// If there are any changes in the DAG, do a file-level comparison
if (changes.added.length > 0 || changes.removed.length > 0) {
console.log('[Synchronizer] Merkle DAG has changed. Comparing file states...');
const fileChanges = this.compareStates(this.fileHashes, newFileHashes);

Expand Down