From f145202e63183c06fba7f5fa39b3bd757b94353b Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Wed, 18 Sep 2024 14:49:33 +0200 Subject: [PATCH] refac inode gen --- src/processor.ts | 20 +++++++------------- src/scripts/buildNode.ts | 37 +++++++++++++++++++++++++++++++++++++ src/scripts/rpcDatapod.ts | 2 ++ 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/scripts/buildNode.ts diff --git a/src/processor.ts b/src/processor.ts index 9291095..754c6eb 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 0000000..211449b --- /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 6933d6e..e6f4a9e 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() -- GitLab