@peerbit/document 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1029 @@
1
+ import { AbstractType, field, serialize, variant } from "@dao-xyz/borsh";
2
+ import { asString, Keyable } from "./utils.js";
3
+ import { BORSH_ENCODING, Encoding, Entry } from "@peerbit/log";
4
+ import { equals } from "@peerbit/uint8arrays";
5
+ import { ComposableProgram } from "@peerbit/program";
6
+ import {
7
+ IntegerCompare,
8
+ ByteMatchQuery,
9
+ StringMatch,
10
+ Query,
11
+ ResultWithSource,
12
+ StateFieldQuery,
13
+ compare,
14
+ Context,
15
+ MissingField,
16
+ StringMatchMethod,
17
+ LogicalQuery,
18
+ And,
19
+ Or,
20
+ BoolQuery,
21
+ Sort,
22
+ CollectNextRequest,
23
+ AbstractSearchRequest,
24
+ SearchRequest,
25
+ SortDirection,
26
+ CloseIteratorRequest,
27
+ } from "./query.js";
28
+ import {
29
+ CanRead,
30
+ RPC,
31
+ RPCOptions,
32
+ RPCResponse,
33
+ queryAll,
34
+ MissingResponsesError,
35
+ } from "@peerbit/rpc";
36
+ import { Results } from "./query.js";
37
+ import { logger as loggerFn } from "@peerbit/logger";
38
+ import { Cache } from "@peerbit/cache";
39
+ import { PublicSignKey } from "@peerbit/crypto";
40
+ import { SharedLog } from "@peerbit/shared-log";
41
+
42
+ const logger = loggerFn({ module: "document-index" });
43
+
44
+ const stringArraysEquals = (a: string[] | string, b: string[] | string) => {
45
+ if (a.length !== b.length) {
46
+ return false;
47
+ }
48
+ for (let i = 0; i < a.length; i++) {
49
+ if (a[i] !== b[i]) {
50
+ return false;
51
+ }
52
+ }
53
+ return true;
54
+ };
55
+
56
+ @variant(0)
57
+ export class Operation<T> {}
58
+
59
+ export const BORSH_ENCODING_OPERATION = BORSH_ENCODING(Operation);
60
+
61
+ @variant(0)
62
+ export class PutOperation<T> extends Operation<T> {
63
+ @field({ type: "string" })
64
+ key: string;
65
+
66
+ @field({ type: Uint8Array })
67
+ data: Uint8Array;
68
+
69
+ _value?: T;
70
+
71
+ constructor(props?: { key: string; data: Uint8Array; value?: T }) {
72
+ super();
73
+ if (props) {
74
+ this.key = props.key;
75
+ this.data = props.data;
76
+ this._value = props.value;
77
+ }
78
+ }
79
+
80
+ get value(): T | undefined {
81
+ if (!this._value) {
82
+ throw new Error("Value not decoded, invoke getValue(...) once");
83
+ }
84
+ return this._value;
85
+ }
86
+
87
+ getValue(encoding: Encoding<T>): T {
88
+ if (this._value) {
89
+ return this._value;
90
+ }
91
+ this._value = encoding.decoder(this.data);
92
+ return this._value;
93
+ }
94
+ }
95
+
96
+ /* @variant(1)
97
+ export class PutAllOperation<T> extends Operation<T> {
98
+ @field({ type: vec(PutOperation) })
99
+ docs: PutOperation<T>[];
100
+
101
+ constructor(props?: { docs: PutOperation<T>[] }) {
102
+ super();
103
+ if (props) {
104
+ this.docs = props.docs;
105
+ }
106
+ }
107
+ }
108
+ */
109
+ @variant(2)
110
+ export class DeleteOperation extends Operation<any> {
111
+ @field({ type: "string" })
112
+ key: string;
113
+
114
+ constructor(props?: { key: string }) {
115
+ super();
116
+ if (props) {
117
+ this.key = props.key;
118
+ }
119
+ }
120
+ }
121
+
122
+ export interface IndexedValue<T> {
123
+ key: string;
124
+ value: Record<string, any> | T; // decrypted, decoded
125
+ context: Context;
126
+ }
127
+
128
+ export type RemoteQueryOptions<R> = RPCOptions<R> & { sync?: boolean };
129
+ export type QueryOptions<R> = {
130
+ remote?: boolean | RemoteQueryOptions<Results<R>>;
131
+ local?: boolean;
132
+ };
133
+ export type SearchOptions<R> = { size?: number } & QueryOptions<R>;
134
+ export type Indexable<T> = (
135
+ obj: T,
136
+ context: Context
137
+ ) => Record<string, any> | Promise<Record<string, any>>;
138
+
139
+ const extractFieldValue = <T>(doc: any, path: string[]): T => {
140
+ for (let i = 0; i < path.length; i++) {
141
+ doc = doc[path[i]];
142
+ }
143
+ return doc;
144
+ };
145
+
146
+ export type ResultsIterator<T> = {
147
+ close: () => Promise<void>;
148
+ next: (number: number) => Promise<T[]>;
149
+ done: () => boolean;
150
+ };
151
+
152
+ const sortCompare = (av: any, bv: any) => {
153
+ if (typeof av === "string" && typeof bv === "string") {
154
+ return av.localeCompare(bv);
155
+ }
156
+ if (av < bv) {
157
+ return -1;
158
+ } else if (av > bv) {
159
+ return 1;
160
+ }
161
+ return 0;
162
+ };
163
+ const extractSortCompare = (
164
+ a: Record<string, any>,
165
+ b: Record<string, any>,
166
+ sorts: Sort[]
167
+ ) => {
168
+ for (const sort of sorts) {
169
+ const av = extractFieldValue(a, sort.key);
170
+ const bv = extractFieldValue(b, sort.key);
171
+ const cmp = sortCompare(av, bv);
172
+ if (cmp != 0) {
173
+ if (sort.direction === SortDirection.ASC) {
174
+ return cmp;
175
+ } else {
176
+ return -cmp;
177
+ }
178
+ }
179
+ }
180
+ return 0;
181
+ };
182
+
183
+ const resolvedSort = async <T, Q extends { value: T; context: Context }>(
184
+ arr: Q[],
185
+ index: Indexable<T>,
186
+ sorts: Sort[]
187
+ ) => {
188
+ await Promise.all(
189
+ arr.map(
190
+ async (result) =>
191
+ (result[SORT_TMP_KEY] = await index(result.value, result.context))
192
+ )
193
+ );
194
+ arr.sort((a, b) =>
195
+ extractSortCompare(a[SORT_TMP_KEY], b[SORT_TMP_KEY], sorts)
196
+ );
197
+ return arr;
198
+ };
199
+ /*
200
+ const sortValueWithContext = async<T>(arr: {
201
+ value: T;
202
+ context: Context;
203
+ }[], index: Indexable<T>) => {
204
+
205
+
206
+ }
207
+ */
208
+
209
+ const SORT_TMP_KEY = "__sort_ref";
210
+
211
+ type QueryDetailedOptions<T> = QueryOptions<T> & {
212
+ onResponse?: (response: Results<T>, from?: PublicSignKey) => void;
213
+ };
214
+ const introduceEntries = async <T>(
215
+ responses: RPCResponse<Results<T>>[],
216
+ type: AbstractType<T>,
217
+ sync: (result: Results<T>) => Promise<void>,
218
+ options?: QueryDetailedOptions<T>
219
+ ): Promise<RPCResponse<Results<T>>[]> => {
220
+ return Promise.all(
221
+ responses.map(async (x) => {
222
+ x.response.results.forEach((r) => r.init(type));
223
+ if (typeof options?.remote !== "boolean" && options?.remote?.sync) {
224
+ await sync(x.response);
225
+ }
226
+ if (!x.from) {
227
+ logger.error("Missing from for response");
228
+ }
229
+ options?.onResponse && options.onResponse(x.response, x.from!);
230
+ return x;
231
+ })
232
+ );
233
+ };
234
+
235
+ const dedup = <T>(
236
+ allResult: T[],
237
+ dedupBy: (obj: any) => string | Uint8Array
238
+ ) => {
239
+ const unique: Set<Keyable> = new Set();
240
+ const dedup: T[] = [];
241
+ for (const result of allResult) {
242
+ const key = asString(dedupBy(result));
243
+ if (unique.has(key)) {
244
+ continue;
245
+ }
246
+ unique.add(key);
247
+ dedup.push(result);
248
+ }
249
+ return dedup;
250
+ };
251
+
252
+ const DEFAULT_INDEX_BY = "id";
253
+
254
+ export type OpenOptions<T> = {
255
+ type: AbstractType<T>;
256
+ log: SharedLog<Operation<T>>;
257
+ canRead: CanRead;
258
+ fields: Indexable<T>;
259
+ sync: (result: Results<T>) => Promise<void>;
260
+ indexBy?: string | string[];
261
+ };
262
+
263
+ @variant("documents_index")
264
+ export class DocumentIndex<T> extends ComposableProgram<OpenOptions<T>> {
265
+ @field({ type: RPC })
266
+ _query: RPC<AbstractSearchRequest, Results<T>>;
267
+
268
+ type: AbstractType<T>;
269
+
270
+ // Index key
271
+ private _indexBy: string | string[];
272
+ private _indexByArr: string[];
273
+
274
+ // Resolve doc value by index key
275
+ indexByResolver: (obj: any) => string | Uint8Array;
276
+
277
+ // Indexed (transforms an docuemnt into an obj with fields that ought to be indexed)
278
+ private _toIndex: Indexable<T>;
279
+
280
+ private _valueEncoding: Encoding<T>;
281
+
282
+ private _sync: (result: Results<T>) => Promise<void>;
283
+ private _index: Map<string, IndexedValue<T>>;
284
+
285
+ private _resultsCollectQueue: Cache<{ value: T; context: Context }[]>;
286
+
287
+ private _log: SharedLog<Operation<T>>;
288
+
289
+ constructor(properties?: { query?: RPC<SearchRequest, Results<T>> }) {
290
+ super();
291
+ this._query = properties?.query || new RPC();
292
+ }
293
+
294
+ get index(): Map<string, IndexedValue<T>> {
295
+ return this._index;
296
+ }
297
+
298
+ get valueEncoding() {
299
+ return this._valueEncoding;
300
+ }
301
+
302
+ get toIndex(): Indexable<T> {
303
+ return this._toIndex;
304
+ }
305
+
306
+ async open(properties: OpenOptions<T>) {
307
+ this._index = new Map();
308
+ this._log = properties.log;
309
+ this.type = properties.type;
310
+ this._sync = properties.sync;
311
+ this._toIndex = properties.fields;
312
+ this._indexBy = properties.indexBy || DEFAULT_INDEX_BY;
313
+ this._indexByArr = Array.isArray(this._indexBy)
314
+ ? this._indexBy
315
+ : [this._indexBy];
316
+
317
+ this.indexByResolver =
318
+ typeof this._indexBy === "string"
319
+ ? (obj) => obj[this._indexBy as string]
320
+ : (obj: any) => extractFieldValue(obj, this._indexBy as string[]);
321
+ this._valueEncoding = BORSH_ENCODING(this.type);
322
+ this._resultsCollectQueue = new Cache({ max: 10000 }); // TODO choose limit better
323
+
324
+ await this._query.open({
325
+ topic: this._log.log.idString + "/document",
326
+ canRead: properties.canRead,
327
+ responseHandler: async (query) => {
328
+ if (query instanceof CloseIteratorRequest) {
329
+ this.processCloseIteratorRequest(query);
330
+ } else {
331
+ const results = await this.processFetchRequest(
332
+ query as SearchRequest | SearchRequest | CollectNextRequest
333
+ );
334
+ return new Results({
335
+ // Even if results might have length 0, respond, because then we now at least there are no matching results
336
+ results: results.results.map(
337
+ (r) =>
338
+ new ResultWithSource({
339
+ source: serialize(r.value),
340
+ context: r.context,
341
+ })
342
+ ),
343
+ kept: BigInt(results.kept),
344
+ });
345
+ }
346
+ },
347
+ responseType: Results,
348
+ queryType: AbstractSearchRequest,
349
+ });
350
+ }
351
+
352
+ public async get(
353
+ key: Keyable,
354
+ options?: QueryOptions<T>
355
+ ): Promise<T | undefined> {
356
+ return (await this.getDetailed(key, options))?.[0]?.results[0]?.value;
357
+ }
358
+
359
+ public async getDetailed(
360
+ key: Keyable,
361
+ options?: QueryOptions<T>
362
+ ): Promise<Results<T>[] | undefined> {
363
+ let results: Results<T>[] | undefined;
364
+ if (key instanceof Uint8Array) {
365
+ results = await this.queryDetailed(
366
+ new SearchRequest({
367
+ query: [new ByteMatchQuery({ key: this._indexByArr, value: key })],
368
+ }),
369
+ options
370
+ );
371
+ } else {
372
+ const stringValue = asString(key);
373
+ results = await this.queryDetailed(
374
+ new SearchRequest({
375
+ query: [
376
+ new StringMatch({
377
+ key: this._indexByArr,
378
+ value: stringValue,
379
+ }),
380
+ ],
381
+ }),
382
+ options
383
+ );
384
+ }
385
+
386
+ return results;
387
+ }
388
+
389
+ get size(): number {
390
+ return this._index.size;
391
+ }
392
+
393
+ async getDocument(value: { context: { head: string } }): Promise<T> {
394
+ const payloadValue = await (await this._log.log.get(
395
+ value.context.head
396
+ ))!.getPayloadValue();
397
+ if (payloadValue instanceof PutOperation) {
398
+ return payloadValue.getValue(this.valueEncoding);
399
+ }
400
+ throw new Error("Unexpected");
401
+ }
402
+
403
+ async _queryDocuments(
404
+ filter: (doc: IndexedValue<T>) => boolean
405
+ ): Promise<{ context: Context; value: T }[]> {
406
+ // Whether we return the full operation data or just the db value
407
+ const results: { context: Context; value: T }[] = [];
408
+ for (const value of this._index.values()) {
409
+ if (filter(value)) {
410
+ results.push({
411
+ context: value.context,
412
+ value: await this.getDocument(value),
413
+ });
414
+ }
415
+ }
416
+ return results;
417
+ }
418
+
419
+ async processFetchRequest(
420
+ query: SearchRequest | CollectNextRequest
421
+ ): Promise<{ results: { context: Context; value: T }[]; kept: number }> {
422
+ // We do special case for querying the id as we can do it faster than iterating
423
+ if (query instanceof SearchRequest) {
424
+ if (
425
+ query.query.length === 1 &&
426
+ (query.query[0] instanceof ByteMatchQuery ||
427
+ query.query[0] instanceof StringMatch) &&
428
+ stringArraysEquals(query.query[0].key, this._indexByArr)
429
+ ) {
430
+ const firstQuery = query.query[0];
431
+ if (firstQuery instanceof ByteMatchQuery) {
432
+ const doc = this._index.get(asString(firstQuery.value)); // TODO could there be a issue with types here?
433
+ return doc
434
+ ? {
435
+ results: [
436
+ {
437
+ value: await this.getDocument(doc),
438
+ context: doc.context,
439
+ },
440
+ ],
441
+ kept: 0,
442
+ }
443
+ : { results: [], kept: 0 };
444
+ } else if (
445
+ firstQuery instanceof StringMatch &&
446
+ firstQuery.method === StringMatchMethod.exact &&
447
+ firstQuery.caseInsensitive === false
448
+ ) {
449
+ const doc = this._index.get(firstQuery.value); // TODO could there be a issue with types here?
450
+ return doc
451
+ ? {
452
+ results: [
453
+ {
454
+ value: await this.getDocument(doc),
455
+ context: doc.context,
456
+ },
457
+ ],
458
+ kept: 0,
459
+ }
460
+ : { results: [], kept: 0 };
461
+ }
462
+ }
463
+
464
+ const results = await this._queryDocuments((doc) => {
465
+ for (const f of query.query) {
466
+ if (!this.handleQueryObject(f, doc)) {
467
+ return false;
468
+ }
469
+ }
470
+ return true;
471
+ });
472
+
473
+ // Sort
474
+ await resolvedSort(results, this._toIndex, query.sort);
475
+
476
+ const batch = results.splice(0, query.fetch);
477
+ if (results.length > 0) {
478
+ this._resultsCollectQueue.add(query.idString, results); // cache resulst not returned
479
+ }
480
+
481
+ return { results: batch, kept: results.length }; // Only return 1 result since we are doing distributed sort, TODO buffer more initially
482
+ } else if (query instanceof CollectNextRequest) {
483
+ const results = this._resultsCollectQueue.get(query.idString);
484
+ if (!results) {
485
+ return {
486
+ results: [],
487
+ kept: 0,
488
+ };
489
+ }
490
+
491
+ const batch = results.splice(0, query.amount);
492
+
493
+ if (results.length === 0) {
494
+ this._resultsCollectQueue.del(query.idString); // TODO add tests for proper cleanup/timeouts
495
+ }
496
+
497
+ return { results: batch, kept: results.length };
498
+ }
499
+ throw new Error("Unsupported");
500
+ }
501
+
502
+ async processCloseIteratorRequest(
503
+ query: CloseIteratorRequest
504
+ ): Promise<void> {
505
+ this._resultsCollectQueue.del(query.idString);
506
+ }
507
+
508
+ private handleQueryObject(f: Query, doc: IndexedValue<T>) {
509
+ if (f instanceof StateFieldQuery) {
510
+ const fv: any = extractFieldValue(doc.value, f.key);
511
+
512
+ if (f instanceof StringMatch) {
513
+ let compare = f.value;
514
+ if (f.caseInsensitive) {
515
+ compare = compare.toLowerCase();
516
+ }
517
+
518
+ if (Array.isArray(fv)) {
519
+ for (const string of fv) {
520
+ if (this.handleStringMatch(f, compare, string)) {
521
+ return true;
522
+ }
523
+ }
524
+ return false;
525
+ } else {
526
+ if (this.handleStringMatch(f, compare, fv)) {
527
+ return true;
528
+ }
529
+ return false;
530
+ }
531
+ } else if (f instanceof ByteMatchQuery) {
532
+ if (fv instanceof Uint8Array === false) {
533
+ if (stringArraysEquals(f.key, this._indexByArr)) {
534
+ return f.valueString === fv;
535
+ }
536
+ return false;
537
+ }
538
+ return equals(fv, f.value);
539
+ } else if (f instanceof IntegerCompare) {
540
+ const value: bigint | number = fv;
541
+
542
+ if (typeof value !== "bigint" && typeof value !== "number") {
543
+ return false;
544
+ }
545
+ return compare(value, f.compare, f.value.value);
546
+ } else if (f instanceof MissingField) {
547
+ return fv == null; // null or undefined
548
+ } else if (f instanceof BoolQuery) {
549
+ return fv === f.value; // true/false
550
+ }
551
+ } else if (f instanceof LogicalQuery) {
552
+ if (f instanceof And) {
553
+ for (const and of f.and) {
554
+ if (!this.handleQueryObject(and, doc)) {
555
+ return false;
556
+ }
557
+ }
558
+ return true;
559
+ }
560
+
561
+ if (f instanceof Or) {
562
+ for (const or of f.or) {
563
+ if (this.handleQueryObject(or, doc)) {
564
+ return true;
565
+ }
566
+ }
567
+ return false;
568
+ }
569
+ return false;
570
+ }
571
+
572
+ logger.info("Unsupported query type: " + f.constructor.name);
573
+ return false;
574
+ }
575
+
576
+ private handleStringMatch(f: StringMatch, compare: string, fv: string) {
577
+ if (typeof fv !== "string") {
578
+ return false;
579
+ }
580
+ if (f.caseInsensitive) {
581
+ fv = fv.toLowerCase();
582
+ }
583
+ if (f.method === StringMatchMethod.exact) {
584
+ return fv === compare;
585
+ }
586
+ if (f.method === StringMatchMethod.prefix) {
587
+ return fv.startsWith(compare);
588
+ }
589
+ if (f.method === StringMatchMethod.contains) {
590
+ return fv.includes(compare);
591
+ }
592
+ throw new Error("Unsupported");
593
+ }
594
+
595
+ /**
596
+ * Query and retrieve results with most details
597
+ * @param queryRequest
598
+ * @param options
599
+ * @returns
600
+ */
601
+ public async queryDetailed(
602
+ queryRequest: SearchRequest,
603
+ options?: QueryDetailedOptions<T>
604
+ ): Promise<Results<T>[]> {
605
+ const local = typeof options?.local == "boolean" ? options?.local : true;
606
+ let remote: RemoteQueryOptions<Results<T>> | undefined = undefined;
607
+ if (typeof options?.remote === "boolean") {
608
+ if (options?.remote) {
609
+ remote = {};
610
+ } else {
611
+ remote = undefined;
612
+ }
613
+ } else {
614
+ remote = options?.remote || {};
615
+ }
616
+
617
+ const promises: Promise<Results<T> | Results<T>[] | undefined>[] = [];
618
+ if (!local && !remote) {
619
+ throw new Error(
620
+ "Expecting either 'options.remote' or 'options.local' to be true"
621
+ );
622
+ }
623
+ const allResults: Results<T>[] = [];
624
+
625
+ if (local) {
626
+ const results = await this.processFetchRequest(queryRequest);
627
+ if (results.results.length > 0) {
628
+ const resultsObject = new Results<T>({
629
+ results: await Promise.all(
630
+ results.results.map(async (r) => {
631
+ const payloadValue = await (
632
+ await this._log.log.get(r.context.head)
633
+ )?.getPayloadValue();
634
+ if (payloadValue instanceof PutOperation) {
635
+ return new ResultWithSource({
636
+ context: r.context,
637
+ value: r.value,
638
+ source: payloadValue.data,
639
+ });
640
+ }
641
+ throw new Error("Unexpected");
642
+ })
643
+ ),
644
+ kept: BigInt(results.kept),
645
+ });
646
+ options?.onResponse &&
647
+ options.onResponse(resultsObject, this.node.identity.publicKey);
648
+ allResults.push(resultsObject);
649
+ }
650
+ }
651
+
652
+ if (remote) {
653
+ const replicatorGroups = await this._log.replicators?.();
654
+ if (replicatorGroups) {
655
+ const fn = async () => {
656
+ const rs: Results<T>[] = [];
657
+ const responseHandler = async (
658
+ results: RPCResponse<Results<T>>[]
659
+ ) => {
660
+ await introduceEntries(
661
+ results,
662
+ this.type,
663
+ this._sync,
664
+ options
665
+ ).then((x) => x.forEach((y) => rs.push(y.response)));
666
+ };
667
+ try {
668
+ if (queryRequest instanceof CloseIteratorRequest) {
669
+ // don't wait for responses
670
+ await this._query.request(queryRequest, { to: remote!.to });
671
+ } else {
672
+ await queryAll(
673
+ this._query,
674
+ replicatorGroups,
675
+ queryRequest,
676
+ responseHandler,
677
+ remote
678
+ );
679
+ }
680
+ } catch (error) {
681
+ if (error instanceof MissingResponsesError) {
682
+ logger.error("Did not reciveve responses from all shard");
683
+ }
684
+ }
685
+ return rs;
686
+ };
687
+ promises.push(fn());
688
+ } else {
689
+ // TODO send without direction out to the world? or just assume we can insert?
690
+ /* promises.push(
691
+ this._query
692
+ .request(queryRequest, remote)
693
+ .then((results) => introduceEntries(results, this.type, this._sync, options).then(x => x.map(y => y.response)))
694
+ ); */
695
+ /* throw new Error(
696
+ "Missing remote replicator info for performing distributed document query"
697
+ ); */
698
+ }
699
+ }
700
+ const resolved = await Promise.all(promises);
701
+ for (const r of resolved) {
702
+ if (r) {
703
+ if (r instanceof Array) {
704
+ allResults.push(...r);
705
+ } else {
706
+ allResults.push(r);
707
+ }
708
+ }
709
+ }
710
+ return allResults;
711
+ }
712
+
713
+ /**
714
+ * Query and retrieve results
715
+ * @param queryRequest
716
+ * @param options
717
+ * @returns
718
+ */
719
+ public async search(
720
+ queryRequest: SearchRequest,
721
+ options?: SearchOptions<T>
722
+ ): Promise<T[]> {
723
+ // Set fetch to search size, or max value (default to max u32 (4294967295))
724
+ queryRequest.fetch = options?.size ?? 0xffffffff;
725
+
726
+ // So that the iterator is pre-fetching the right amount of entries
727
+ const iterator = this.iterate(queryRequest, options);
728
+
729
+ // So that this call will not do any remote requests
730
+ const allResult = await iterator.next(queryRequest.fetch);
731
+
732
+ await iterator.close();
733
+
734
+ //s Deduplicate and return values directly
735
+ return dedup(allResult, this.indexByResolver);
736
+ }
737
+
738
+ /**
739
+ * Query and retrieve documents in a iterator
740
+ * @param queryRequest
741
+ * @param options
742
+ * @returns
743
+ */
744
+ public iterate(
745
+ queryRequest: SearchRequest,
746
+ options?: QueryOptions<T>
747
+ ): ResultsIterator<T> {
748
+ let fetchPromise: Promise<any> | undefined = undefined;
749
+ const peerBufferMap: Map<
750
+ string,
751
+ {
752
+ kept: number;
753
+ buffer: { value: T; context: Context; from: PublicSignKey }[];
754
+ }
755
+ > = new Map();
756
+ const visited = new Set<string>();
757
+
758
+ let done = false;
759
+ let first = false;
760
+
761
+ // TODO handle join/leave while iterating
762
+ let stopperFns: (() => void)[] = [];
763
+
764
+ const peerBuffers = (): {
765
+ value: T;
766
+ from: PublicSignKey;
767
+ context: Context;
768
+ }[] => {
769
+ return [...peerBufferMap.values()].map((x) => x.buffer).flat();
770
+ };
771
+
772
+ const fetchFirst = async (n: number): Promise<boolean> => {
773
+ done = true; // Assume we are donne
774
+ queryRequest.fetch = n;
775
+ await this.queryDetailed(queryRequest, {
776
+ ...options,
777
+ onResponse: (response, from) => {
778
+ if (!from) {
779
+ logger.error("Missing response from");
780
+ return;
781
+ }
782
+
783
+ if (response.kept === 0n && response.results.length === 0) {
784
+ return;
785
+ }
786
+
787
+ if (response.kept > 0n) {
788
+ done = false; // we have more to do later!
789
+ }
790
+
791
+ peerBufferMap.set(from.hashcode(), {
792
+ buffer: response.results
793
+ .filter(
794
+ (x) => !visited.has(asString(this.indexByResolver(x.value)))
795
+ )
796
+ .map((x) => {
797
+ visited.add(asString(this.indexByResolver(x.value)));
798
+ return { from, value: x.value, context: x.context };
799
+ }),
800
+ kept: Number(response.kept),
801
+ });
802
+ },
803
+ });
804
+
805
+ return done;
806
+ };
807
+
808
+ const fetchAtLeast = async (n: number) => {
809
+ if (done && first) {
810
+ return;
811
+ }
812
+
813
+ await fetchPromise;
814
+
815
+ if (!first) {
816
+ first = true;
817
+ fetchPromise = fetchFirst(n);
818
+ return fetchPromise;
819
+ }
820
+
821
+ const promises: Promise<any>[] = [];
822
+ stopperFns = [];
823
+ let resultsLeft = 0;
824
+ for (const [peer, buffer] of peerBufferMap) {
825
+ if (buffer.buffer.length < n) {
826
+ if (buffer.kept === 0) {
827
+ if (peerBufferMap.get(peer)?.buffer.length === 0) {
828
+ peerBufferMap.delete(peer); // No more results
829
+ }
830
+ continue;
831
+ }
832
+
833
+ // TODO buffer more than deleted?
834
+ // TODO batch to multiple 'to's
835
+ const collectRequest = new CollectNextRequest({
836
+ id: queryRequest.id,
837
+ amount: 10, //n - buffer.buffer.length,
838
+ });
839
+ // Fetch locally?
840
+ if (peer === this.node.identity.publicKey.hashcode()) {
841
+ promises.push(
842
+ this.processFetchRequest(collectRequest)
843
+ .then((results) => {
844
+ resultsLeft += results.kept;
845
+
846
+ if (results.results.length === 0) {
847
+ if (peerBufferMap.get(peer)?.buffer.length === 0) {
848
+ peerBufferMap.delete(peer); // No more results
849
+ }
850
+ } else {
851
+ const peerBuffer = peerBufferMap.get(peer);
852
+ if (!peerBuffer) {
853
+ return;
854
+ }
855
+ peerBuffer.kept = results.kept;
856
+ peerBuffer.buffer.push(
857
+ ...results.results
858
+ .filter(
859
+ (x) =>
860
+ !visited.has(
861
+ asString(this.indexByResolver(x.value))
862
+ )
863
+ )
864
+ .map((x) => {
865
+ visited.add(asString(this.indexByResolver(x.value)));
866
+ return {
867
+ value: x.value,
868
+ context: x.context,
869
+ from: this.node.identity.publicKey,
870
+ };
871
+ })
872
+ );
873
+ }
874
+ })
875
+ .catch((e) => {
876
+ logger.error(
877
+ "Failed to collect sorted results self. " + e?.message
878
+ );
879
+ peerBufferMap.delete(peer);
880
+ })
881
+ );
882
+ } else {
883
+ // Fetch remotely
884
+ promises.push(
885
+ this._query
886
+ .request(collectRequest, {
887
+ ...options,
888
+ stopper: (fn) => stopperFns.push(fn),
889
+ to: [peer],
890
+ })
891
+ .then((response) =>
892
+ introduceEntries(response, this.type, this._sync, options)
893
+ .then((responses) => {
894
+ responses.map((response) => {
895
+ resultsLeft += Number(response.response.kept);
896
+ if (!response.from) {
897
+ logger.error("Missing from for sorted query");
898
+ return;
899
+ }
900
+
901
+ if (response.response.results.length === 0) {
902
+ if (peerBufferMap.get(peer)?.buffer.length === 0) {
903
+ peerBufferMap.delete(peer); // No more results
904
+ }
905
+ } else {
906
+ const peerBuffer = peerBufferMap.get(peer);
907
+ if (!peerBuffer) {
908
+ return;
909
+ }
910
+ peerBuffer.kept = Number(response.response.kept);
911
+ peerBuffer.buffer.push(
912
+ ...response.response.results
913
+ .filter(
914
+ (x) =>
915
+ !visited.has(
916
+ asString(this.indexByResolver(x.value))
917
+ )
918
+ )
919
+ .map((x) => {
920
+ visited.add(
921
+ asString(this.indexByResolver(x.value))
922
+ );
923
+ return {
924
+ value: x.value,
925
+ context: x.context,
926
+ from: response.from!,
927
+ };
928
+ })
929
+ );
930
+ }
931
+ });
932
+ })
933
+ .catch((e) => {
934
+ logger.error(
935
+ "Failed to collect sorted results from: " +
936
+ peer +
937
+ ". " +
938
+ e?.message
939
+ );
940
+ peerBufferMap.delete(peer);
941
+ })
942
+ )
943
+ );
944
+ }
945
+ } else {
946
+ resultsLeft += peerBufferMap.get(peer)?.kept || 0;
947
+ }
948
+ }
949
+ return (fetchPromise = Promise.all(promises).then(() => {
950
+ return resultsLeft === 0; // 0 results left to fetch and 0 pending results
951
+ }));
952
+ };
953
+
954
+ const next = async (n: number) => {
955
+ if (n < 0) {
956
+ throw new Error("Expecting to fetch a positive amount of element");
957
+ }
958
+
959
+ if (n === 0) {
960
+ return [];
961
+ }
962
+
963
+ // TODO everything below is not very optimized
964
+ const fetchedAll = await fetchAtLeast(n);
965
+
966
+ // get n next top entries, shift and pull more results
967
+ const results = await resolvedSort(
968
+ peerBuffers(),
969
+ this._toIndex,
970
+ queryRequest.sort
971
+ );
972
+ const pendingMoreResults = n < results.length;
973
+
974
+ const batch = results.splice(0, n);
975
+
976
+ for (const result of batch) {
977
+ const arr = peerBufferMap.get(result.from.hashcode());
978
+ if (!arr) {
979
+ logger.error("Unexpected empty result buffer");
980
+ continue;
981
+ }
982
+ const idx = arr.buffer.findIndex((x) => x == result);
983
+ if (idx >= 0) {
984
+ arr.buffer.splice(idx, 1);
985
+ }
986
+ }
987
+
988
+ done = fetchedAll && !pendingMoreResults;
989
+ return dedup(
990
+ batch.map((x) => x.value),
991
+ this.indexByResolver
992
+ );
993
+ };
994
+
995
+ const close = async () => {
996
+ for (const fn of stopperFns) {
997
+ fn();
998
+ }
999
+
1000
+ const closeRequest = new CloseIteratorRequest({ id: queryRequest.id });
1001
+ const promises: Promise<any>[] = [];
1002
+ for (const [peer, buffer] of peerBufferMap) {
1003
+ if (buffer.kept === 0) {
1004
+ peerBufferMap.delete(peer);
1005
+ continue;
1006
+ }
1007
+ // Fetch locally?
1008
+ if (peer === this.node.identity.publicKey.hashcode()) {
1009
+ promises.push(this.processCloseIteratorRequest(closeRequest));
1010
+ } else {
1011
+ // Fetch remotely
1012
+ promises.push(
1013
+ this._query.send(closeRequest, {
1014
+ ...options,
1015
+ to: [peer],
1016
+ })
1017
+ );
1018
+ }
1019
+ }
1020
+ await Promise.all(promises);
1021
+ };
1022
+
1023
+ return {
1024
+ close,
1025
+ next,
1026
+ done: () => done,
1027
+ };
1028
+ }
1029
+ }