|
1 | | -import { AsyncBuffer, ColumnData, parquetQuery } from 'hyparquet' |
| 1 | +import { AsyncBuffer, ColumnData, asyncBufferFromUrl, cachedAsyncBuffer, parquetQuery } from 'hyparquet' |
2 | 2 | import { compressors } from 'hyparquet-compressors' |
3 | | -import { asyncBufferFromUrl } from '../lib/utils.ts' |
| 3 | +// import { asyncBufferFromUrl } from '../lib/utils.ts' |
4 | 4 | import type { |
5 | | - AsyncBufferFrom, |
| 5 | + AsyncBufferFromUrl, |
6 | 6 | ChunkMessage, |
7 | 7 | ErrorMessage, |
8 | 8 | IndicesMessage, |
@@ -97,66 +97,12 @@ function compare<T>(a: T, b: T): number { |
97 | 97 | * Convert AsyncBufferFrom to AsyncBuffer and cache results. |
98 | 98 | */ |
99 | 99 | function asyncBufferFrom( |
100 | | - from: AsyncBufferFrom, |
| 100 | + from: AsyncBufferFromUrl, |
101 | 101 | ): Promise<AsyncBuffer> { |
102 | 102 | const key = JSON.stringify(from) |
103 | 103 | const cached = cache.get(key) |
104 | 104 | if (cached) return cached |
105 | | - const asyncBuffer = asyncBufferFromUrl(from).then(cachedAsyncBuffer) |
| 105 | + const asyncBuffer = asyncBufferFromUrl(from.url, from.byteLength).then(cachedAsyncBuffer) |
106 | 106 | cache.set(key, asyncBuffer) |
107 | 107 | return asyncBuffer |
108 | 108 | } |
109 | | - |
110 | | -type Awaitable<T> = T | Promise<T>; |
111 | | - |
112 | | -function cachedAsyncBuffer(asyncBuffer: AsyncBuffer): AsyncBuffer { |
113 | | - const cache = new Map<string, Awaitable<ArrayBuffer>>() |
114 | | - const { byteLength } = asyncBuffer |
115 | | - return { |
116 | | - byteLength, |
117 | | - /** |
118 | | - * @param {number} start |
119 | | - * @param {number} [end] |
120 | | - * @returns {Awaitable<ArrayBuffer>} |
121 | | - */ |
122 | | - slice(start: number, end?: number): Awaitable<ArrayBuffer> { |
123 | | - const key = cacheKey(start, end, byteLength) |
124 | | - const cached = cache.get(key) |
125 | | - if (cached) return cached |
126 | | - // cache miss, read from file |
127 | | - const promise = asyncBuffer.slice(start, end) |
128 | | - cache.set(key, promise) |
129 | | - return promise |
130 | | - }, |
131 | | - } |
132 | | -} |
133 | | - |
134 | | -/** |
135 | | - * Returns canonical cache key for a byte range 'start,end'. |
136 | | - * Normalize int-range and suffix-range requests to the same key. |
137 | | - * |
138 | | - * @param {number} start start byte of range |
139 | | - * @param {number} [end] end byte of range, or undefined for suffix range |
140 | | - * @param {number} [size] size of file, or undefined for suffix range |
141 | | - * @returns {string} |
142 | | - */ |
143 | | -function cacheKey(start: number, end?: number, size?: number): string { |
144 | | - if (start < 0) { |
145 | | - if (end !== undefined) |
146 | | - throw new Error( |
147 | | - `invalid suffix range [${start.toString()}, ${end.toString()}]`, |
148 | | - ) |
149 | | - if (size === undefined) return `${start.toString()},` |
150 | | - return `${(size + start).toString()},${size.toString()}` |
151 | | - } else if (end !== undefined) { |
152 | | - if (start > end) |
153 | | - throw new Error( |
154 | | - `invalid empty range [${start.toString()}, ${end.toString()}]`, |
155 | | - ) |
156 | | - return `${start.toString()},${end.toString()}` |
157 | | - } else if (size === undefined) { |
158 | | - return `${start.toString()},` |
159 | | - } else { |
160 | | - return `${start.toString()},${size.toString()}` |
161 | | - } |
162 | | -} |
0 commit comments