deno.land / x / replicache@v10.0.0-beta.0 / scan-iterator.ts

scan-iterator.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import {deepClone, ReadonlyJSONValue} from './json';import {Closed, throwIfClosed} from './transaction-closed-error';import { isScanIndexOptions, KeyTypeForScanOptions, normalizeScanOptionIndexedStartKey, ScanIndexOptions, ScanOptionIndexedStartKey, ScanOptions,} from './scan-options';import {asyncIterableToArray} from './async-iterable-to-array';import type {ReadonlyEntry} from './btree/node';import {encodeIndexScanKey, IndexKey} from './db/index.js';import {EntryForOptions, fromKeyForNonIndexScan} from './transactions.js';
type ScanKey = string | IndexKey;
type ToValue<Options extends ScanOptions, V> = ( entry: EntryForOptions<Options>,) => V;
type ShouldDeepClone = {shouldDeepClone: boolean};
/** * This class is used for the results of [[ReadTransaction.scan|scan]]. It * implements `AsyncIterable<JSONValue>` which allows you to use it in a `for * await` loop. There are also methods to iterate over the [[keys]], * [[entries]] or [[values]]. */export class ScanResultImpl< Options extends ScanOptions, V extends ReadonlyJSONValue,> implements ScanResult<KeyTypeForScanOptions<Options>, V>{ private readonly _iter: AsyncIterable<EntryForOptions<Options>>; private readonly _options: Options; private readonly _dbDelegateOptions: Closed & ShouldDeepClone; private readonly _onLimitKey: (inclusiveLimitKey: string) => void;
constructor( iter: AsyncIterable<EntryForOptions<Options>>, options: Options, dbDelegateOptions: Closed & ShouldDeepClone, onLimitKey: (inclusiveLimitKey: string) => void, ) { this._iter = iter; this._options = options; this._dbDelegateOptions = dbDelegateOptions; this._onLimitKey = onLimitKey; }
/** The default AsyncIterable. This is the same as [[values]]. */ [Symbol.asyncIterator](): AsyncIterableIteratorToArrayWrapper<V> { return this.values(); }
/** Async iterator over the values of the [[ReadTransaction.scan|scan]] call. */ values(): AsyncIterableIteratorToArrayWrapper<V> { const clone = this._dbDelegateOptions.shouldDeepClone ? deepClone : (x: ReadonlyJSONValue) => x; return new AsyncIterableIteratorToArrayWrapper( this._newIterator(e => clone(e[1])) as AsyncIterableIterator<V>, ); }
/** * Async iterator over the keys of the [[ReadTransaction.scan|scan]] * call. If the [[ReadTransaction.scan|scan]] is over an index the key * is a tuple of `[secondaryKey: string, primaryKey]` */ keys(): AsyncIterableIteratorToArrayWrapper<KeyTypeForScanOptions<Options>> { type K = KeyTypeForScanOptions<Options>; const toValue = (e: EntryForOptions<Options>) => e[0]; return new AsyncIterableIteratorToArrayWrapper( this._newIterator(toValue as ToValue<Options, K>), ); }
/** * Async iterator over the entries of the [[ReadTransaction.scan|scan]] * call. An entry is a tuple of key values. If the * [[ReadTransaction.scan|scan]] is over an index the key is a tuple of * `[secondaryKey: string, primaryKey]` */ entries(): AsyncIterableIteratorToArrayWrapper< readonly [KeyTypeForScanOptions<Options>, V] > { type K = KeyTypeForScanOptions<Options>; const clone = this._dbDelegateOptions.shouldDeepClone ? deepClone : (x: ReadonlyJSONValue) => x; const toValue = (e: EntryForOptions<Options>) => clone(e); return new AsyncIterableIteratorToArrayWrapper( this._newIterator(toValue as ToValue<Options, readonly [K, V]>), ); }
/** Returns all the values as an array. Same as `values().toArray()` */ toArray(): Promise<V[]> { return this.values().toArray(); }
private _newIterator<T>( toValue: ToValue<Options, T>, ): AsyncIterableIterator<T> { return scanIterator( toValue, this._iter, this._options, this._dbDelegateOptions, this._onLimitKey, ); }}
export interface ScanResult<K extends ScanKey, V extends ReadonlyJSONValue> extends AsyncIterable<V> { /** The default AsyncIterable. This is the same as [[values]]. */ [Symbol.asyncIterator](): AsyncIterableIteratorToArrayWrapper<V>;
/** Async iterator over the values of the [[ReadTransaction.scan|scan]] call. */ values(): AsyncIterableIteratorToArrayWrapper<V>;
/** * Async iterator over the keys of the [[ReadTransaction.scan|scan]] * call. If the [[ReadTransaction.scan|scan]] is over an index the key * is a tuple of `[secondaryKey: string, primaryKey]` */ keys(): AsyncIterableIteratorToArrayWrapper<K>;
/** * Async iterator over the entries of the [[ReadTransaction.scan|scan]] * call. An entry is a tuple of key values. If the * [[ReadTransaction.scan|scan]] is over an index the key is a tuple of * `[secondaryKey: string, primaryKey]` */ entries(): AsyncIterableIteratorToArrayWrapper<readonly [K, V]>;
/** Returns all the values as an array. Same as `values().toArray()` */ toArray(): Promise<V[]>;}
/** * A class that wraps an async iterable iterator to add a [[toArray]] method. * * Usage: * * ```ts * const keys: string[] = await rep.scan().keys().toArray(); * ``` */export class AsyncIterableIteratorToArrayWrapper<V> implements AsyncIterableIterator<V>{ private readonly _it: AsyncIterableIterator<V>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any readonly next: (v?: any) => Promise<IteratorResult<V>>; // eslint-disable-next-line @typescript-eslint/no-explicit-any readonly return?: (value?: any) => Promise<IteratorResult<V>>; // eslint-disable-next-line @typescript-eslint/no-explicit-any readonly throw?: (e?: any) => Promise<IteratorResult<V>>;
constructor(it: AsyncIterableIterator<V>) { this._it = it;
// eslint-disable-next-line @typescript-eslint/no-explicit-any this.next = (v: any) => it.next(v); // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion this.return = it.return ? (v: any) => it.return!(v) : undefined; // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion this.throw = it.throw ? (v: any) => it.throw!(v) : undefined; }
toArray(): Promise<V[]> { return asyncIterableToArray(this._it); }
[Symbol.asyncIterator](): AsyncIterableIterator<V> { return this._it[Symbol.asyncIterator](); }}
async function* scanIterator<Options extends ScanOptions, V>( toValue: ToValue<Options, V>, iter: AsyncIterable<EntryForOptions<Options>>, options: Options, closed: Closed, onLimitKey: (inclusiveLimitKey: string) => void,): AsyncIterableIterator<V> { throwIfClosed(closed);
let {limit = Infinity} = options; const {prefix = ''} = options; let exclusive = options.start?.exclusive;
const isIndexScan = isScanIndexOptions(options);
// iter has already been moved to the first entry for await (const entry of iter) { const key = entry[0]; const keyToMatch: string = isIndexScan ? key[0] : (key as string); if (!keyToMatch.startsWith(prefix)) { return; }
if (exclusive) { exclusive = true; if (isIndexScan) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if (shouldSkipIndexScan(key as IndexKey, options.start!.key)) { continue; } } else { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if (shouldSkipNonIndexScan(key as string, options.start!.key)) { continue; } } }
yield toValue(entry);
if (--limit === 0 && !isIndexScan) { onLimitKey(key as string); return; } }}
function shouldSkipIndexScan( key: IndexKey, startKey: ScanOptionIndexedStartKey,): boolean { const [secondaryStartKey, primaryStartKey] = normalizeScanOptionIndexedStartKey(startKey); const [secondaryKey, primaryKey] = normalizeScanOptionIndexedStartKey(key); if (secondaryKey !== secondaryStartKey) { return false; } if (primaryStartKey === undefined) { return true; } return primaryKey === primaryStartKey;}
function shouldSkipNonIndexScan(key: string, startKey: string): boolean { return key === startKey;}
/** * This is called when doing a [[ReadTransaction.scan|scan]] without an * `indexName`. * * @param fromKey The `fromKey` is computed by `scan` and is the key of the * first entry to return in the iterator. It is based on `prefix` and * `start.key` of the [[ScanNoIndexOptions]]. */export type GetScanIterator = ( fromKey: string,) => AsyncIterable<ReadonlyEntry<ReadonlyJSONValue>>;
/** * This is called when doing a [[ReadTransaction.scan|scan]] with an * `indexName`. * * @param indexName The name of the index we are scanning over. * @param fromSecondaryKey The `fromSecondaryKey` is computed by `scan` and is * the secondary key of the first entry to return in the iterator. It is based * on `prefix` and `start.key` of the [[ScanIndexOptions]]. * @param fromPrimaryKey The `fromPrimaryKey` is computed by `scan` and is the * primary key of the first entry to return in the iterator. It is based on * `prefix` and `start.key` of the [[ScanIndexOptions]]. */export type GetIndexScanIterator = ( indexName: string, fromSecondaryKey: string, fromPrimaryKey: string | undefined,) => AsyncIterable<readonly [key: IndexKey, value: ReadonlyJSONValue]>;
/** * A helper function that makes it easier to implement [[ReadTransaction.scan]] * with a custom backend */export function makeScanResult<Options extends ScanOptions>( options: Options, getScanIterator: Options extends ScanIndexOptions ? GetIndexScanIterator : GetScanIterator,): ScanResult<KeyTypeForScanOptions<Options>, ReadonlyJSONValue> { type AsyncIter = AsyncIterable<EntryForOptions<Options>>;
if (isScanIndexOptions(options)) { const [fromSecondaryKey, fromPrimaryKey] = fromKeyForIndexScan(options); const iter = (getScanIterator as GetIndexScanIterator)( options.indexName, fromSecondaryKey, fromPrimaryKey, ) as AsyncIter; return new ScanResultImpl( iter, options, {closed: false, shouldDeepClone: false}, _ => { // noop }, ); } const fromKey = fromKeyForNonIndexScan(options); const iter = (getScanIterator as GetScanIterator)(fromKey) as AsyncIter;
return new ScanResultImpl( iter, options, {closed: false, shouldDeepClone: false}, _ => { // noop }, );}export function fromKeyForIndexScan( options: ScanIndexOptions,): readonly [secondary: string, primary?: string] { const {prefix, start} = options; const prefixNormalized: [secondary: string, primary?: string] = [ prefix ?? '', undefined, ];
if (!start) { return prefixNormalized; }
const startKeyNormalized = normalizeScanOptionIndexedStartKey(start.key); if (startKeyNormalized[0] > prefixNormalized[0]) { return startKeyNormalized; } if ( startKeyNormalized[0] === prefixNormalized[0] && startKeyNormalized[1] !== undefined ) { return startKeyNormalized; }
return prefixNormalized;}
export function fromKeyForIndexScanInternal(options: ScanIndexOptions): string { const {prefix, start} = options; let prefix2 = ''; if (prefix !== undefined) { prefix2 = encodeIndexScanKey(prefix, undefined); } if (!start) { return prefix2; }
const {key} = start; const [secondary, primary] = normalizeScanOptionIndexedStartKey(key); const startKey = encodeIndexScanKey(secondary, primary);
if (startKey > prefix2) { return startKey; }
return prefix2;}
replicache

Version Info

Tagged at
2 years ago