UNPKG

@dstanesc/o-o-o-o-o-o-o

Version:

O-O-O-O-O-O-O is a collection of content addressed persistent data structures

85 lines 4.14 kB
import { RootDecoder } from './serde'; import { chunkyStore } from '@dstanesc/store-chunky-bytes'; const blockIndexFactory = ({ linkCodec, blockStore, }) => { const { encode: linkEncode, decode: linkDecode, encodeString: encodeLinkString, parseString: parseLinkString, } = linkCodec; const { put: blockPut, get: blockGet } = blockStore; const { readIndex } = chunkyStore(); const buildChunkyIndex = async (root) => { return await readIndex(root, blockGet, linkDecode); }; const buildRootIndex = async (root) => { const bytes = await blockGet(root); const rootStruct = new RootDecoder(bytes, linkDecode).read(); const vertexIndex = await buildChunkyIndex(rootStruct.vertexRoot); const edgeIndex = await buildChunkyIndex(rootStruct.edgeRoot); const propIndex = await buildChunkyIndex(rootStruct.propRoot); const valueIndex = await buildChunkyIndex(rootStruct.valueRoot); const indexIndex = await buildChunkyIndex(rootStruct.indexRoot); const index = Object.assign({ vertexIndex, edgeIndex, propIndex, valueIndex, indexIndex }, rootStruct); return { root, index, indexBuffer: bytes }; }; const diffRootIndex = async ({ currentRoot, otherRoot, }) => { const currentRootIndex = await buildRootIndex(currentRoot); const otherRootIndex = await buildRootIndex(otherRoot); const addedLinks = []; const removedLinks = []; const { vertexIndex: currentVertexIndex, edgeIndex: currentEdgeIndex, propIndex: currentPropIndex, valueIndex: currentValueIndex, indexIndex: currentIndexIndex, } = currentRootIndex.index; const { vertexIndex: otherVertexIndex, edgeIndex: otherEdgeIndex, propIndex: otherPropIndex, valueIndex: otherValueIndex, indexIndex: otherIndexIndex, } = otherRootIndex.index; const vertexDiff = await diffChunkyIndex({ currentIndex: currentVertexIndex, otherIndex: otherVertexIndex, }); const edgeDiff = await diffChunkyIndex({ currentIndex: currentEdgeIndex, otherIndex: otherEdgeIndex, }); const propDiff = await diffChunkyIndex({ currentIndex: currentPropIndex, otherIndex: otherPropIndex, }); const valueDiff = await diffChunkyIndex({ currentIndex: currentValueIndex, otherIndex: otherValueIndex, }); const indexDiff = await diffChunkyIndex({ currentIndex: currentIndexIndex, otherIndex: otherIndexIndex, }); addedLinks.push(...vertexDiff.added); addedLinks.push(...edgeDiff.added); addedLinks.push(...propDiff.added); addedLinks.push(...valueDiff.added); addedLinks.push(...indexDiff.added); removedLinks.push(...vertexDiff.removed); removedLinks.push(...edgeDiff.removed); removedLinks.push(...propDiff.removed); removedLinks.push(...valueDiff.removed); removedLinks.push(...indexDiff.removed); return { added: addedLinks, removed: removedLinks }; }; const diffChunkyIndex = async ({ currentIndex, otherIndex, }) => { const addedLinks = []; const removedLinks = []; const currentLinks = new Set(Array.from(currentIndex.indexStruct.startOffsets.values(), (cid) => linkCodec.encodeString(cid))); const otherLinks = new Set(Array.from(otherIndex.indexStruct.startOffsets.values(), (cid) => linkCodec.encodeString(cid))); for (const cidString of otherLinks) { if (!currentLinks.has(cidString)) { addedLinks.push(linkCodec.parseString(cidString)); } } for (const cidString of currentLinks) { if (!otherLinks.has(cidString)) { removedLinks.push(linkCodec.parseString(cidString)); } } return { added: addedLinks, removed: removedLinks }; }; return { buildRootIndex, buildChunkyIndex, diffRootIndex, diffChunkyIndex, }; }; export { blockIndexFactory }; //# sourceMappingURL=block-index.js.map