-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add treeleaf strategy #15
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
Open
olizilla
wants to merge
1
commit into
main
Choose a base branch
from
treeleaf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { TreeleafCarSplitter } from './splitter.js' | ||
| import { TreeleafCarJoiner } from './joiner.js' | ||
|
|
||
| export { | ||
| TreeleafCarSplitter, | ||
| TreeleafCarJoiner | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { CarWriter } from '@ipld/car' | ||
|
|
||
| /** | ||
| * @typedef {import('@ipld/car/api').BlockReader & import('@ipld/car/api').RootsReader} ICarReader | ||
| */ | ||
|
|
||
| export class TreeleafCarJoiner { | ||
| /** | ||
| * @param {Iterable<ICarReader>} cars | ||
| */ | ||
| constructor (cars) { | ||
| /** @type {ICarReader[]} */ | ||
| this._cars = Array.from(cars) | ||
| if (!this._cars.length) throw new Error('missing CARs') | ||
| } | ||
|
|
||
| async * car () { | ||
| const reader = this._cars[0] | ||
| const roots = await reader.getRoots() | ||
| const { writer, out } = CarWriter.create(roots) | ||
| const writeCar = async () => { | ||
| try { | ||
| for await (const b of reader.blocks()) { | ||
| await writer.put(b) | ||
| } | ||
| for (const reader of this._cars.slice(1)) { | ||
| for await (const b of reader.blocks()) { | ||
| await writer.put(b) | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.error(err) // TODO: how to forward this on? | ||
| } finally { | ||
| await writer.close() | ||
| } | ||
| } | ||
|
|
||
| writeCar() | ||
| yield * out | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { CarReader, CarWriter } from '@ipld/car' | ||
| import * as Block from 'multiformats/block' | ||
| import { CID } from 'multiformats/cid' | ||
| import * as raw from 'multiformats/codecs/raw' | ||
| import * as cbor from '@ipld/dag-cbor' | ||
| import * as json from '@ipld/dag-json' | ||
| import * as pb from '@ipld/dag-pb' | ||
|
|
||
| /** | ||
| * @typedef {import('@ipld/car/api').BlockReader & import('@ipld/car/api').RootsReader} ICarReader | ||
| * @typedef {import('@ipld/car/api').WriterChannel} WriterChannel | ||
| * @typedef {import('multiformats/codecs/interface').BlockDecoder<any, any>} BlockDecoder | ||
| * @typedef {{ decoders?: BlockDecoder[] }} Options | ||
| * @typedef {number} Code | ||
| */ | ||
|
|
||
| /** | ||
| * A work-around for use-cases where the inclusion of a root CID is difficult | ||
| * but needing to be safely within the "at least one" recommendation is to use | ||
| * an empty CID: \x01\x55\x00\x00 (zero-length "identity" multihash with "raw" | ||
| * codec). Since current implementations for this version of the CAR | ||
| * specification don't check for the existence of root CIDs (see Root CID block | ||
| * existence), this will be safe as far as CAR implementations are concerned. | ||
| * However, there is no guarantee that applications that use CAR files will | ||
| * correctly consume (ignore) this empty root CID. | ||
| * | ||
| * https://github.com/ipld/specs/blob/master/block-layer/content-addressable-archives.md#number-of-roots | ||
| */ | ||
| const empty = CID.parse('bafkqaaa') | ||
|
|
||
| export class TreeleafCarSplitter { | ||
| /** | ||
| * @param {ICarReader} reader | ||
| * @param {number} targetSize | ||
| * @param {Options} [options] | ||
| */ | ||
| constructor (reader, targetSize, options = {}) { | ||
| if (typeof targetSize !== 'number' || targetSize <= 0) { | ||
| throw new Error('invalid target chunk size') | ||
| } | ||
| this._reader = reader | ||
| this._targetSize = targetSize | ||
| const codecs = [pb, raw, cbor, json, ...(options.decoders || [])] | ||
| /** @type Map<Code, BlockDecoder> */ | ||
| this._decoderMap = new Map(codecs.map(d => [d.code, d])) | ||
| } | ||
|
|
||
| async * cars () { | ||
| /** @type {(roots?: CID[]) => WriterChannel} */ | ||
| const createCarWriter = (roots = [empty]) => { | ||
| const car = CarWriter.create(roots) | ||
| Object.assign(car.out, { version: 1, getRoots: async () => roots }) | ||
| return car | ||
| } | ||
| const leaves = [] | ||
| const originRoots = await this._reader.getRoots() | ||
| const tree = createCarWriter(originRoots) | ||
|
|
||
| for await (const block of this._reader.blocks()) { | ||
| if (block.cid.code === raw.code) { | ||
| leaves.push(block) | ||
| continue | ||
| } | ||
| const codec = this._decoderMap.get(block.cid.code) | ||
| if (!codec) throw new Error(`missing decoder for ${block.cid.code}`) | ||
| const decoded = Block.createUnsafe({ cid: block.cid, bytes: block.bytes, codec }) | ||
| const hasLinks = Array.from(decoded.links()).length > 0 | ||
| if (hasLinks) { | ||
| tree.writer.put(block) | ||
| } else { | ||
| leaves.push(block) | ||
| } | ||
| } | ||
| tree.writer.close() | ||
| yield tree.out | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If DAG of 1 block, this is an empty CAR...what to do? Is it ok? |
||
|
|
||
| // finished tree car, now write multiple leaf cars | ||
|
|
||
| let size = 0 | ||
| let car = createCarWriter() | ||
| for (const block of leaves) { | ||
| if (size >= this._targetSize) { | ||
| car.writer.close() | ||
| yield car.out | ||
| car = createCarWriter() | ||
| } | ||
| car.writer.put(block) | ||
| size += block.bytes.length | ||
| } | ||
| car.writer.close() | ||
| yield car.out | ||
| } | ||
|
|
||
| /** | ||
| * @param {Blob} blob | ||
| * @param {number} targetSize | ||
| */ | ||
| static async fromBlob (blob, targetSize) { | ||
| const buffer = await blob.arrayBuffer() | ||
| const reader = await CarReader.fromBytes(new Uint8Array(buffer)) | ||
| return new TreeleafCarSplitter(reader, targetSize) | ||
| } | ||
|
|
||
| /** | ||
| * @param {AsyncIterable<Uint8Array>} iterable | ||
| * @param {number} targetSize | ||
| */ | ||
| static async fromIterable (iterable, targetSize) { | ||
| const reader = await CarReader.fromIterable(iterable) | ||
| return new TreeleafCarSplitter(reader, targetSize) | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this just the simple joiner?