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

refac inode gen

parent d3b47dfd
Branches
No related tags found
No related merge requests found
...@@ -224,21 +224,12 @@ export async function mergeInodesSync( ...@@ -224,21 +224,12 @@ export async function mergeInodesSync(
const isAleaf = (nodeA as IndexLeaf).leaf != undefined const isAleaf = (nodeA as IndexLeaf).leaf != undefined
const isBleaf = (nodeB as IndexLeaf).leaf != undefined const isBleaf = (nodeB as IndexLeaf).leaf != undefined
const [leafA, leafB] = [nodeA as IndexLeaf, nodeB as IndexLeaf] const [leafA, leafB] = [nodeA as IndexLeaf, nodeB as IndexLeaf]
// these are not internal nodes, but leaves, and we should merge them
if (isAleaf && isBleaf) { if (isAleaf && isBleaf) {
// should only merge leaves with same key // should only merge leaves with same key
assert(leafA.key == leafB.key) assert(leafA.key == leafB.key)
// these are not internal nodes, but leaves, and we should merge them const allCIDs = (nodeA as IndexLeaf).leaf.concat((nodeB as IndexLeaf).leaf)
// problem: Set will not work on CIDs because they are objects and differ even if they have the same data const newLeaf = arrayToLeaf(leafA.key, allCIDs)
// 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
}
return kubo.dag.put(newLeaf).then((cid) => { return kubo.dag.put(newLeaf).then((cid) => {
// WIP pin des not work well // WIP pin des not work well
// kubo.pin.add(cid).catch((_e) => console.log(`📌📌 could not pin newly created leaf ${cid}`)) // 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 ...@@ -390,7 +381,10 @@ export function arrayToVinode(ctx: string, array: Array<[string, CID]>): IndexVi
/// transform array of cid to leaf object /// transform array of cid to leaf object
export function arrayToLeaf(key: string, array: CID[]): IndexLeaf { 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 /// 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() { ...@@ -27,6 +27,8 @@ async function doit() {
const enc = new TextEncoder() const enc = new TextEncoder()
await kubo.pubsub.publish('ddd', enc.encode("hello, I'm there\n")) await kubo.pubsub.publish('ddd', enc.encode("hello, I'm there\n"))
console.log('published ;)') console.log('published ;)')
// makes sure that pinning does not work
await kubo.dag.put(obj, { pin: true })
} }
doit() doit()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment