Skip to content

Commit e2b494e

Browse files
committed
optimise get flows
1 parent c1d41c6 commit e2b494e

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,26 @@ class Hyperblobs {
122122
})
123123
}
124124

125-
async get (id, opts) {
125+
async _getAll (id, opts) {
126126
if (id.blockLength === 1) return this.core.get(id.blockOffset, opts)
127127

128-
const res = []
128+
const promises = new Array(id.blockLength)
129+
for (let i = 0; i < id.blockLength; i++) {
130+
promises[i] = this.core.get(id.blockOffset + i, opts)
131+
}
129132

133+
const blocks = await Promise.all(promises)
134+
for (let i = 0; i < id.blockLength; i++) {
135+
if (blocks[i] === null) return null
136+
}
137+
return b4a.concat(blocks)
138+
}
139+
140+
async get (id, opts) {
141+
const all = !opts || (!opts.start && opts.length === undefined && opts.end === undefined && !opts.core)
142+
if (all) return this._getAll(id, opts)
143+
144+
const res = []
130145
try {
131146
for await (const block of this.createReadStream(id, opts)) {
132147
res.push(block)

0 commit comments

Comments
 (0)