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

fix: use fanout "bits" #357

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('exporter sharded', function () {

const result = await last(importer(files, block, {
shardSplitThresholdBytes: 0,
shardFanoutBytes: 4,
shardFanoutBits: 4, // 2**4 = 16 children max
wrapWithDirectory: true
}))

Expand Down
8 changes: 5 additions & 3 deletions packages/ipfs-unixfs-importer/src/dir-sharded.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ async function hamtHashFn (buf: Uint8Array): Promise<Uint8Array> {
}

const HAMT_HASH_CODE = BigInt(0x22)
const DEFAULT_FANOUT_BITS = 8

export interface DirShardedOptions extends PersistOptions {
shardFanoutBytes: number
shardFanoutBits: number
}

class DirSharded extends Dir {
Expand All @@ -31,7 +32,7 @@ class DirSharded extends Dir {

this._bucket = createHAMT({
hashFn: hamtHashFn,
bits: options.shardFanoutBytes ?? 8
bits: options.shardFanoutBits ?? DEFAULT_FANOUT_BITS
})
}

Expand Down Expand Up @@ -196,6 +197,7 @@ function isDir (obj: any): obj is Dir {

function calculateSize (bucket: Bucket<any>, shardRoot: DirSharded | null, options: PersistOptions): number {
const children = bucket._children
const padLength = (bucket.tableSize() - 1).toString(16).length
const links: PBLink[] = []

for (let i = 0; i < children.length; i++) {
Expand All @@ -205,7 +207,7 @@ function calculateSize (bucket: Bucket<any>, shardRoot: DirSharded | null, optio
continue
}

const labelPrefix = i.toString(16).toUpperCase().padStart(2, '0')
const labelPrefix = i.toString(16).toUpperCase().padStart(padLength, '0')

if (child instanceof Bucket) {
const size = calculateSize(child, null, options)
Expand Down
10 changes: 6 additions & 4 deletions packages/ipfs-unixfs-importer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ export interface ImporterOptions extends ProgressOptions<ImporterProgressEvents>
shardSplitThresholdBytes?: number

/**
* The maximum number of bytes used as a HAMT prefix for shard entries. Default: 256
* The number of bits of a hash digest used at each level of sharding to
* the child index. 2**shardFanoutBits will dictate the maximum number of
* children for any shard in the HAMT. Default: 8
*/
shardFanoutBytes?: number
shardFanoutBits?: number

/**
* How many files to import concurrently. For large numbers of small files this
Expand Down Expand Up @@ -246,7 +248,7 @@ export async function * importer (source: ImportCandidateStream, blockstore: Wri

const wrapWithDirectory = options.wrapWithDirectory ?? false
const shardSplitThresholdBytes = options.shardSplitThresholdBytes ?? 262144
const shardFanoutBytes = options.shardFanoutBytes ?? 8
const shardFanoutBits = options.shardFanoutBits ?? 8
const cidVersion = options.cidVersion ?? 1
const rawLeaves = options.rawLeaves ?? true
const leafType = options.leafType ?? 'file'
Expand Down Expand Up @@ -275,7 +277,7 @@ export async function * importer (source: ImportCandidateStream, blockstore: Wri
const buildTree: TreeBuilder = options.treeBuilder ?? defaultTreeBuilder({
wrapWithDirectory,
shardSplitThresholdBytes,
shardFanoutBytes,
shardFanoutBits,
cidVersion,
onProgress: options.onProgress
})
Expand Down
2 changes: 1 addition & 1 deletion packages/ipfs-unixfs-importer/src/tree-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PersistOptions } from './utils/persist.js'

export interface AddToTreeOptions extends PersistOptions {
shardSplitThresholdBytes: number
shardFanoutBytes: number
shardFanoutBits: number
}

async function addToTree (elem: InProgressImportResult, tree: Dir, options: AddToTreeOptions): Promise<Dir> {
Expand Down