Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe use the same Blockstore interface as @ipld/car #3

Closed
olizilla opened this issue Aug 24, 2022 · 2 comments · Fixed by #5
Closed

Maybe use the same Blockstore interface as @ipld/car #3

olizilla opened this issue Aug 24, 2022 · 2 comments · Fixed by #5

Comments

@olizilla
Copy link
Contributor

olizilla commented Aug 24, 2022

I was noodling around and wondered if I could re-use the dagula decode'n'walk logic on a CAR file. It was a success, but made slightly rougher by the different expectations of what a Block{store|Reader}'s get method signature should be.

dagular Blockstore
return a promise of the bytes or throw

get: (cid: CID, options?: { signal?: AbortSignal }) => Promise<Uint8Array>

source

@ipld/car BlockReader
return a promise of {cid: CID, bytes: Uint8Array} or undefined

get(key: CID): Promise<Block | undefined>

source

@olizilla
Copy link
Contributor Author

olizilla commented Aug 24, 2022

for fun, here is dagular using a CAR instead of the bit swap. Might be rad to be able to just pass in a CarReader and have it just work.

import { Dagula } from 'dagula'
import { CarIndexedReader } from '@ipld/car/indexed-reader'

export async function carWalk (input) {
  const carReader = await CarIndexedReader.fromFile(input)
  const blockstore = new CarBlockStore(carReader)
  const roots = await carReader.getRoots()
  const dagula = new Dagula(blockstore)
  try {
    for await (const block of dagula.get(roots[0])) {
      console.log(block.cid.toString())
    }
  } finally {
    await carReader.close()
  }
}

class CarBlockStore {
  #carReader
  constructor (carReader) {
    this.#carReader = carReader
  }
  async get (cid) {
    const block = await this.#carReader.get(cid)
    if (!block) {
      throw new Error('no such block')
    }
    return block.bytes
  }
}

console.log(`dang ${process.argv[2]}`)
carWalk(process.argv[2])

@alanshaw
Copy link
Member

👍 yes this is a good idea!

alanshaw added a commit that referenced this issue Oct 11, 2022
BREAKING CHANGE: The blockstore interface used by this library has been updated. If passing a blockstore to the constructor it should return `undefined` if the block is not present in the blockstore. If it is present, it should return a `{ bytes: Uint8Array, cid: CID }` object (a "Block"). Secondly the `getBlock` method now returns a `Block` instead of a `Uint8Array`.

resolves #3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants