Skip to content

Commit

Permalink
refactor(batching): use generator for memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahhoward committed Dec 25, 2024
1 parent ae3e5f1 commit 80320a7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 10 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"@web3-storage/blob-index": "^1.0.2",
"@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",
"uint8arraylist": "^2.4.8",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

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

63 changes: 61 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
import { ByteView, MultihashDigest } from 'multiformats'
import { Failure, Result, URI, DID } from '@ucanto/interface'
import { Range } from 'multipart-byte-range'
import { QueryError } from '@storacha/indexing-service-client/api'

export { ByteView, MultihashDigest } from 'multiformats'
export { Failure, Result, URI, DID, Principal } from '@ucanto/interface'
export { Range, SuffixRange, AbsoluteRange } from 'multipart-byte-range'

/**
* An absolute byte range to extract - always an array of two values
* corresponding to the first and last bytes (both inclusive). e.g.
*
* ```
* [100, 200]
* ```
*/
export type AbsoluteRange = [first: number, last: number]

/**
* A suffix byte range - always an array of one value corresponding to the
* first byte to start extraction from (inclusive). e.g.
*
* ```
* [900]
* ```
*
* If it is unknown how large a resource is, the last `n` bytes
* can be requested by specifying a negative value:
*
* ```
* [-100]
* ```
*/
export type SuffixRange = [first: number]

/**
* Byte range to extract - an array of one or two values corresponding to the
* first and last bytes (both inclusive). e.g.
*
* ```
* [100, 200]
* ```
*
* Omitting the second value requests all remaining bytes of the resource. e.g.
*
* ```
* [900]
* ```
*
* Alternatively, if it's unknown how large a resource is, the last `n` bytes
* can be requested by specifying a negative value:
*
* ```
* [-100]
* ```
*/
export type Range = AbsoluteRange | SuffixRange

export type ByteGetter = (range: AbsoluteRange) => Promise<ReadableStream<Uint8Array>>

export interface EncoderOptions {
/** Mime type of each part. */
contentType?: string
/** Total size of the object in bytes. */
totalSize?: number
/** Stream queuing strategy. */
strategy?: QueuingStrategy<Uint8Array>
}

export interface Abortable {
signal: AbortSignal
Expand Down
4 changes: 2 additions & 2 deletions src/fetcher/batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class BatchingFetcher {
*/
export const create = (locator) => new BatchingFetcher(locator)

/** @typedef {{range: import('multipart-byte-range').AbsoluteRange, digest: API.MultihashDigest, orig: API.Range | undefined}} ResolvedRange */
/** @typedef {{range: API.AbsoluteRange, digest: API.MultihashDigest, orig: API.Range | undefined}} ResolvedRange */

/**
* Fetch blobs from the passed locations. The locations MUST share a common
Expand Down Expand Up @@ -188,7 +188,7 @@ export const fetchBlobs = withResultSpan('fetchBlobs',
let found = false
for (const l of s.location) {
if (l.toString() === url.toString()) {
/** @type {import('multipart-byte-range').AbsoluteRange} */
/** @type {API.AbsoluteRange} */
let resolvedRange = [s.range.offset, s.range.offset + s.range.length - 1]
if (range) {
const relRange = resolveRange(range, s.range.length)
Expand Down
4 changes: 2 additions & 2 deletions src/fetcher/lib.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @param {import('multipart-byte-range').Range} range
* @param {import('../api.js').Range} range
* @param {number} totalSize
* @returns {import('multipart-byte-range').AbsoluteRange}
* @returns {import('../api.js').AbsoluteRange}
*/
export const resolveRange = (range, totalSize) => {
let last = range[1]
Expand Down

0 comments on commit 80320a7

Please sign in to comment.