Skip to content

Commit

Permalink
feat: use index claim (#10)
Browse files Browse the repository at this point in the history
This PR makes use of the `assert/index` content claim, allowing a [w3-index](https://github.com/w3s-project/specs/blob/main/w3-index.md) to be used to help serve the data, which yields the following perf improvements to the batching blob fetcher:

| Export 500MiB DAG | Sub-requests | Time |
| -- | -- | -- |
| without index |  535 | 11.960s |
| with index | 37 | 7.554s |
  • Loading branch information
Alan Shaw authored Jun 6, 2024
1 parent c17d9e1 commit 8876ec4
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 90 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,18 @@
"dependencies": {
"@ucanto/interface": "^10.0.1",
"@web3-storage/blob-index": "^1.0.2",
"@web3-storage/content-claims": "^5.0.0",
"@web3-storage/content-claims": "^5.1.0",
"multiformats": "^13.1.0",
"multipart-byte-range": "^3.0.1",
"p-defer": "^4.0.1",
"p-queue": "^8.0.1"
},
"devDependencies": {
"@ipld/unixfs": "^3.0.0",
"@web3-storage/public-bucket": "^1.0.0",
"@ucanto/principal": "^9.0.1",
"@web3-storage/public-bucket": "^1.1.0",
"c8": "^9.1.0",
"carstream": "^2.1.0",
"carstream": "^2.2.0",
"entail": "^2.1.2",
"ipfs-unixfs-exporter": "^13.5.0",
"standard": "^17.1.0",
Expand Down
56 changes: 20 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/fetcher/batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { resolveRange } from './lib.js'
* @typedef {Map<RangeKey, PendingBlobRequest[]>} RangedRequests
*/

const MAX_BATCH_SIZE = 12
const MAX_BATCH_SIZE = 16

/** @implements {API.Fetcher} */
class BatchingFetcher {
Expand Down
42 changes: 41 additions & 1 deletion src/locator/content-claims.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// eslint-disable-next-line
import * as API from '../api.js'
import * as Claims from '@web3-storage/content-claims/client'
import { DigestMap } from '@web3-storage/blob-index'
import { DigestMap, ShardedDAGIndex } from '@web3-storage/blob-index'
import { fetchBlob } from '../fetcher/simple.js'
import { NotFoundError } from '../lib.js'

/**
Expand Down Expand Up @@ -85,6 +86,45 @@ export class ContentClaimsLocator {
})
}
}

if (claim.type === 'assert/index') {
await this.#readClaims(claim.index.multihash)
const location = this.#cache.get(claim.index.multihash)
if (!location) continue

const fetchRes = await fetchBlob(location)
if (fetchRes.error) {
console.warn('failed to fetch index', fetchRes.error)
continue
}

const indexBytes = await fetchRes.ok.bytes()
const decodeRes = ShardedDAGIndex.extract(indexBytes)
if (decodeRes.error) {
console.warn('failed to decode index', decodeRes.error)
continue
}

const index = decodeRes.ok
await Promise.all([...index.shards].map(async ([shard, slices]) => {
await this.#readClaims(shard)
const location = this.#cache.get(shard)
if (!location) return

for (const [slice, pos] of slices) {
this.#cache.set(slice, {
digest: slice,
site: location.site.map(s => ({
location: s.location,
range: {
offset: s.range.offset + pos[0],
length: s.range.offset + pos[1]
}
}))
})
}
}))
}
}
this.#claimFetched.set(digest, true)
}
Expand Down
Loading

0 comments on commit 8876ec4

Please sign in to comment.