@@ -2261,7 +2261,7 @@ function loadDatabase(hooks) {
2261
2261
this . hashes = hashes ;
2262
2262
this . emptyset = emptyset ;
2263
2263
this . name = name ;
2264
- /** @type {{"hash": Uint8Array, "data": Promise< Uint8Array[]> ?, "end": number}[] } */
2264
+ /** @type {{"hash": Uint8Array, "data": Uint8Array[]?, "end": number}[] } */
2265
2265
this . buckets = [ ] ;
2266
2266
this . bucket_keys = [ ] ;
2267
2267
const l = counts . length ;
@@ -2295,64 +2295,75 @@ function loadDatabase(hooks) {
2295
2295
/**
2296
2296
* Look up a cell by row ID.
2297
2297
* @param {number } id
2298
- * @returns {Promise<Uint8Array| undefined> }
2298
+ * @returns {Promise<Uint8Array>|Uint8Array| undefined }
2299
2299
*/
2300
- async at ( id ) {
2300
+ at ( id ) {
2301
2301
if ( this . emptyset . contains ( id ) ) {
2302
- return Promise . resolve ( EMPTY_UINT8 ) ;
2302
+ return EMPTY_UINT8 ;
2303
2303
} else {
2304
2304
let idx = - 1 ;
2305
2305
while ( this . bucket_keys [ idx + 1 ] <= id ) {
2306
2306
idx += 1 ;
2307
2307
}
2308
2308
if ( idx === - 1 || idx >= this . bucket_keys . length ) {
2309
- return Promise . resolve ( undefined ) ;
2309
+ return undefined ;
2310
2310
} else {
2311
2311
const start = this . bucket_keys [ idx ] ;
2312
- const { hash , end } = this . buckets [ idx ] ;
2312
+ const bucket = this . buckets [ idx ] ;
2313
2313
let data = this . buckets [ idx ] . data ;
2314
2314
if ( data === null ) {
2315
- const dataSansEmptysetOrig = await registry . dataLoadByNameAndHash (
2316
- this . name ,
2317
- hash ,
2318
- ) ;
2319
- // After the `await` resolves, another task might fill
2320
- // in the data. If so, we should use that.
2321
- data = this . buckets [ idx ] . data ;
2322
- if ( data !== null ) {
2323
- return ( await data ) [ id - start ] ;
2324
- }
2325
- const dataSansEmptyset = [ ...dataSansEmptysetOrig ] ;
2326
- /** @type {(Uint8Array[])|null } */
2327
- let dataWithEmptyset = null ;
2328
- let pos = start ;
2329
- let insertCount = 0 ;
2330
- while ( pos < end ) {
2331
- if ( this . emptyset . contains ( pos ) ) {
2332
- if ( dataWithEmptyset === null ) {
2333
- dataWithEmptyset = dataSansEmptyset . splice ( 0 , insertCount ) ;
2334
- } else if ( insertCount !== 0 ) {
2335
- dataWithEmptyset . push (
2336
- ...dataSansEmptyset . splice ( 0 , insertCount ) ,
2337
- ) ;
2338
- }
2339
- insertCount = 0 ;
2340
- dataWithEmptyset . push ( EMPTY_UINT8 ) ;
2341
- } else {
2342
- insertCount += 1 ;
2343
- }
2344
- pos += 1 ;
2345
- }
2346
- data = Promise . resolve (
2347
- dataWithEmptyset === null ?
2348
- dataSansEmptyset :
2349
- dataWithEmptyset . concat ( dataSansEmptyset ) ,
2315
+ return this . atAsyncFetch ( id , start , bucket ) ;
2316
+ } else {
2317
+ return data [ id - start ] ;
2318
+ }
2319
+ }
2320
+ }
2321
+ }
2322
+ /**
2323
+ * Look up a cell by row ID.
2324
+ * @param {number } id
2325
+ * @param {number } start
2326
+ * @param {{hash: Uint8Array, data: Uint8Array[] | null, end: number} } bucket
2327
+ * @returns {Promise<Uint8Array> }
2328
+ */
2329
+ async atAsyncFetch ( id , start , bucket ) {
2330
+ const { hash, end} = bucket ;
2331
+ const dataSansEmptysetOrig = await registry . dataLoadByNameAndHash (
2332
+ this . name ,
2333
+ hash ,
2334
+ ) ;
2335
+ // After the `await` resolves, another task might fill
2336
+ // in the data. If so, we should use that.
2337
+ let data = bucket . data ;
2338
+ if ( data !== null ) {
2339
+ return data [ id - start ] ;
2340
+ }
2341
+ const dataSansEmptyset = [ ...dataSansEmptysetOrig ] ;
2342
+ /** @type {(Uint8Array[])|null } */
2343
+ let dataWithEmptyset = null ;
2344
+ let pos = start ;
2345
+ let insertCount = 0 ;
2346
+ while ( pos < end ) {
2347
+ if ( this . emptyset . contains ( pos ) ) {
2348
+ if ( dataWithEmptyset === null ) {
2349
+ dataWithEmptyset = dataSansEmptyset . splice ( 0 , insertCount ) ;
2350
+ } else if ( insertCount !== 0 ) {
2351
+ dataWithEmptyset . push (
2352
+ ...dataSansEmptyset . splice ( 0 , insertCount ) ,
2350
2353
) ;
2351
- this . buckets [ idx ] . data = data ;
2352
2354
}
2353
- return ( await data ) [ id - start ] ;
2355
+ insertCount = 0 ;
2356
+ dataWithEmptyset . push ( EMPTY_UINT8 ) ;
2357
+ } else {
2358
+ insertCount += 1 ;
2354
2359
}
2360
+ pos += 1 ;
2355
2361
}
2362
+ data = dataWithEmptyset === null ?
2363
+ dataSansEmptyset :
2364
+ dataWithEmptyset . concat ( dataSansEmptyset ) ;
2365
+ bucket . data = data ;
2366
+ return data [ id - start ] ;
2356
2367
}
2357
2368
/**
2358
2369
* Search by exact substring
0 commit comments