Skip to content
Snippets Groups Projects
Commit f145202e authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

refac inode gen

parent d3b47dfd
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
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()
......@@ -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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment