diff --git a/src/processor.ts b/src/processor.ts index 929109530480e7bdc18bcbf69c339a8de850db0a..754c6ebd4474f37c6dc70d978fbd4918c3214506 100644 --- a/src/processor.ts +++ b/src/processor.ts @@ -224,21 +224,12 @@ export async function mergeInodesSync( const isAleaf = (nodeA as IndexLeaf).leaf != undefined const isBleaf = (nodeB as IndexLeaf).leaf != undefined const [leafA, leafB] = [nodeA as IndexLeaf, nodeB as IndexLeaf] + // these are not internal nodes, but leaves, and we should merge them if (isAleaf && isBleaf) { // should only merge leaves with same key assert(leafA.key == leafB.key) - // these are not internal nodes, but leaves, and we should merge them - // problem: Set will not work on CIDs because they are objects and differ even if they have the same data - // const cidSet = new Set([...(nodeA as unknown as IndexLeaf).leaf, ...(nodeB as IndexLeaf).leaf]) - // const cidListUniqueSorted = Array.from(cidSet).sort() - const cidList = [...(nodeA as IndexLeaf).leaf, ...(nodeB as IndexLeaf).leaf] - const cidListUniqueSorted = uniqueby(cidList, (k) => k.toString()).sort((a, b) => - a.toString() < b.toString() ? -1 : 1 - ) - const newLeaf: IndexLeaf = { - leaf: cidListUniqueSorted, - key: leafA.key - } + const allCIDs = (nodeA as IndexLeaf).leaf.concat((nodeB as IndexLeaf).leaf) + const newLeaf = arrayToLeaf(leafA.key, allCIDs) return kubo.dag.put(newLeaf).then((cid) => { // WIP pin des not work well // kubo.pin.add(cid).catch((_e) => console.log(`📌📌 could not pin newly created leaf ${cid}`)) @@ -390,7 +381,10 @@ export function arrayToVinode(ctx: string, array: Array<[string, CID]>): IndexVi /// transform array of cid to leaf object export function arrayToLeaf(key: string, array: CID[]): IndexLeaf { - return { key, leaf: array.sort((a, b) => (a.toString() < b.toString() ? -1 : 1)) } + const cidListUniqueSorted = uniqueby<CID>(array, (k) => k.toString()).sort((a, b) => + a.toString() < b.toString() ? -1 : 1 + ) + return { key, leaf: cidListUniqueSorted } } /// concretize virtual node to CID diff --git a/src/scripts/buildNode.ts b/src/scripts/buildNode.ts new file mode 100644 index 0000000000000000000000000000000000000000..211449b5a9ef05c394896441f439f660730887c2 --- /dev/null +++ b/src/scripts/buildNode.ts @@ -0,0 +1,37 @@ +import { arrayToVinode, mergeInodesSyncCID } from '../processor' +import { kubo } from '../kubo' +import type { CID } from 'multiformats' +import { emptyInode } from '../types' +import { getAll } from '../interface' + +const obj = { index: 'request', fake: true } +const fakeRoot = emptyInode('fake_') + +// I should transform that into a unit test + +async function main() { + const rootCID = await kubo.dag.put(fakeRoot) + const cid = await kubo.dag.put(obj) + + const items: Array<[string, CID]> = [ + ['123', cid], + ['456', cid], + ['456', cid] + ] + + const root = arrayToVinode('fake_', items) + console.log(root) + console.log(JSON.stringify(root)) + + const [_, res] = await mergeInodesSyncCID(rootCID, root) + console.log(res) + + const collect = [] + for await (const item of getAll(res)) { + collect.push(item) + } + + console.log(collect) +} + +main() diff --git a/src/scripts/rpcDatapod.ts b/src/scripts/rpcDatapod.ts index 6933d6e89ce487c564479a649f7a9bcbb01a323c..e6f4a9e1aa2971b356e2d3a4441280df84808cd1 100644 --- a/src/scripts/rpcDatapod.ts +++ b/src/scripts/rpcDatapod.ts @@ -27,6 +27,8 @@ async function doit() { const enc = new TextEncoder() await kubo.pubsub.publish('ddd', enc.encode("hello, I'm there\n")) console.log('published ;)') + // makes sure that pinning does not work + await kubo.dag.put(obj, { pin: true }) } doit()