Skip to content

Commit

Permalink
fix: propogate index build error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed Oct 12, 2022
1 parent e2c1ca2 commit fd6135b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/lib/blockstore.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class R2Blockstore {
}

export class BatchingR2Blockstore extends R2Blockstore {
/** @type {Map<string, Array<import('p-defer').DeferredPromise<Block|undefined>>} */
/** @type {Map<string, Array<import('p-defer').DeferredPromise<Block|undefined>>>} */
#batchBlocks = new Map()

/** @type {Map<CID, BlockBatch>} */
Expand Down
34 changes: 25 additions & 9 deletions src/lib/car-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,21 @@ export class MultiCarIndex {
async get (cid) {
const deferred = defer()
const idxEntries = Array.from(this.#idxs.entries())
await Promise.allSettled(idxEntries.map(async ([carCid, idx]) => {
const entry = await idx.get(cid)
if (entry) deferred.resolve([carCid, entry])
return entry
}))
deferred.resolve()

Promise
.allSettled(idxEntries.map(async ([carCid, idx]) => {
const entry = await idx.get(cid)
if (entry) deferred.resolve([carCid, entry])
}))
.then(results => {
// if not already resolved, check for rejections and reject
for (const r of results) {
if (r.status === 'rejected') return deferred.reject(r.reason)
}
// if no rejections, then entry was simply not found in any index
deferred.resolve()
})

return deferred.promise
}
}
Expand All @@ -55,6 +64,9 @@ export class StreamingCarIndex {
/** @type {boolean} */
#building = false

/** @type {Error?} */
#buildError = null

/** @param {AsyncIterable<Uint8Array>} stream */
constructor (stream) {
this.#buildIndex(stream)
Expand Down Expand Up @@ -97,17 +109,21 @@ export class StreamingCarIndex {
resolve()
})
}
} catch (err) {
this.#building = false
} catch (/** @type {any} */ err) {
console.error('failed to build index', err)
this.#building = false
this.#buildError = err
for (const promises of this.#promisedIdx.values()) {
promises.forEach(({ reject }) => reject(err))
promises.forEach(p => p.reject(new Error('failed to build index', { cause: err })))
}
}
}

/** @param {CID} cid */
async get (cid) {
if (this.#buildError) {
throw new Error('failed to build index', { cause: this.#buildError })
}
const key = mhToKey(cid.multihash.bytes)
const entry = this.#idx.get(key)
if (entry != null) return entry
Expand Down

0 comments on commit fd6135b

Please sign in to comment.