UNPKG

@atproto/repo

Version:

atproto repo and MST implementation

96 lines 2.54 kB
import { BlockMap } from './block-map.js'; import { CidSet } from './cid-set.js'; import { mstDiff } from './mst/index.js'; export class DataDiff { constructor() { this.adds = {}; this.updates = {}; this.deletes = {}; this.newMstBlocks = new BlockMap(); this.newLeafCids = new CidSet(); this.removedCids = new CidSet(); } static async of(curr, prev) { return mstDiff(curr, prev); } async nodeAdd(node) { if (node.isLeaf()) { this.leafAdd(node.key, node.value); } else { const data = await node.serialize(); this.treeAdd(data.cid, data.bytes); } } async nodeDelete(node) { if (node.isLeaf()) { const key = node.key; const cid = node.value; this.deletes[key] = { key, cid }; this.removedCids.add(cid); } else { const cid = await node.getPointer(); this.treeDelete(cid); } } leafAdd(key, cid) { this.adds[key] = { key, cid }; if (this.removedCids.has(cid)) { this.removedCids.delete(cid); } else { this.newLeafCids.add(cid); } } leafUpdate(key, prev, cid) { if (prev.equals(cid)) return; this.updates[key] = { key, prev, cid }; this.removedCids.add(prev); this.newLeafCids.add(cid); } leafDelete(key, cid) { this.deletes[key] = { key, cid }; if (this.newLeafCids.has(cid)) { this.newLeafCids.delete(cid); } else { this.removedCids.add(cid); } } treeAdd(cid, bytes) { if (this.removedCids.has(cid)) { this.removedCids.delete(cid); } else { this.newMstBlocks.set(cid, bytes); } } treeDelete(cid) { if (this.newMstBlocks.has(cid)) { this.newMstBlocks.delete(cid); } else { this.removedCids.add(cid); } } addList() { return Object.values(this.adds); } updateList() { return Object.values(this.updates); } deleteList() { return Object.values(this.deletes); } updatedKeys() { const keys = [ ...Object.keys(this.adds), ...Object.keys(this.updates), ...Object.keys(this.deletes), ]; return [...new Set(keys)]; } } //# sourceMappingURL=data-diff.js.map