-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathInMemoryProvider.ts
More file actions
1061 lines (965 loc) · 29.6 KB
/
Copy pathInMemoryProvider.ts
File metadata and controls
1061 lines (965 loc) · 29.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* InMemoryProvider.ts
* Author: David de Regt
* Copyright: Microsoft 2015
*
* ObjectStoreProvider provider setup for a non-persisted in-memory database backing provider.
*/
import {
attempt,
isError,
each,
includes,
compact,
map,
find,
values,
flatten,
dropRight,
takeRight,
drop,
take,
} from "lodash";
import {
DbIndexFTSFromRangeQueries,
getFullTextIndexWordsForItem,
} from "./FullTextSearchHelpers";
import {
StoreSchema,
DbProvider,
DbSchema,
DbTransaction,
DbIndex,
IndexSchema,
DbStore,
QuerySortOrder,
ItemType,
KeyPathType,
IObjectStoreProviderLogger,
KeyType,
} from "./ObjectStoreProvider";
import {
arrayify,
serializeKeyToString,
formListOfSerializedKeys,
getSerializedKeyForKeypath,
getValueForSingleKeypath,
MAX_COUNT,
trimArray,
} from "./ObjectStoreProviderUtils";
import {
TransactionToken,
TransactionLockHelper,
} from "./TransactionLockHelper";
import { createOrderedMap, IOrderedMap, OrderedMapType } from "./ordered-map";
export interface StoreData {
data: Map<string, ItemType>;
indices: Map<string, InMemoryIndex>;
schema: StoreSchema;
mapType?: OrderedMapType;
}
export interface ILiveConsumerConfigs {
usePushForGetRange: boolean;
usePrimaryKeyForGetKeysForRange: boolean;
}
export type GetLiveConsumerConfigsFn = () => ILiveConsumerConfigs;
const defaultLiveConsumerConfigs: ILiveConsumerConfigs = {
usePushForGetRange: false,
usePrimaryKeyForGetKeysForRange: false,
};
export class InMemoryProvider extends DbProvider {
private _stores: Map<string, StoreData> = new Map();
private _lockHelper: TransactionLockHelper | undefined;
private readonly _mapType?: OrderedMapType;
private readonly _supportsRollback?: boolean;
private logger: IObjectStoreProviderLogger;
constructor(
mapType?: OrderedMapType,
supportsRollback = false,
logger?: IObjectStoreProviderLogger,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
super();
this._mapType = mapType;
this._supportsRollback = supportsRollback;
this.logger = logger ? logger : console;
}
open(
dbName: string,
schema: DbSchema,
wipeIfExists: boolean,
verbose: boolean
): Promise<void> {
super.open(dbName, schema, wipeIfExists, verbose);
each(this._schema!!!.stores, (storeSchema) => {
this._stores.set(storeSchema.name, {
schema: storeSchema,
data: new Map(),
indices: new Map(),
mapType: this._mapType,
});
});
this._lockHelper = new TransactionLockHelper(schema, true);
return Promise.resolve<void>(void 0);
}
protected _deleteDatabaseInternal() {
return Promise.resolve();
}
openTransaction(
storeNames: string[],
writeNeeded: boolean
): Promise<DbTransaction> {
return this._lockHelper!!!.openTransaction(storeNames, writeNeeded).then(
(token: any) =>
new InMemoryTransaction(
this,
this._lockHelper!!!,
token,
writeNeeded,
this._supportsRollback!,
this.logger,
this.getLiveConfigs
)
);
}
close(): Promise<void> {
return this._lockHelper!!!.closeWhenPossible().then(() => {
this._stores = new Map();
});
}
internal_getStore(name: string): StoreData {
return this._stores.get(name)!!!;
}
}
// Notes: Doesn't limit the stores it can fetch to those in the stores it was "created" with, nor does it handle read-only transactions
class InMemoryTransaction implements DbTransaction {
private _stores: Map<string, InMemoryStore> = new Map();
private _openTimer: number | undefined;
constructor(
private _prov: InMemoryProvider,
private _lockHelper: TransactionLockHelper,
private _transToken: TransactionToken,
private _writeNeeded: boolean,
private _supportsRollback: boolean,
private logger: IObjectStoreProviderLogger,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
// Close the transaction on the next tick. By definition, anything is completed synchronously here, so after an event tick
// goes by, there can't have been anything pending.
if (this._writeNeeded) {
this._openTimer = setTimeout(() => {
this._openTimer = undefined;
this._commitTransaction();
this._lockHelper.transactionComplete(this._transToken);
}, 0) as any as number;
} else {
// read-only
this._openTimer = undefined;
this._lockHelper.transactionComplete(this._transToken);
}
}
private _commitTransaction(): void {
this._stores.forEach((store) => {
store.internal_commitPendingData();
});
}
getCompletionPromise(): Promise<void> {
return this._transToken.completionPromise;
}
abort(): void {
if (!this._supportsRollback) {
this.logger.error(
"Unable to abort transaction since provider doesn't support rollback"
);
throw new Error(
"Unable to abort transaction since provider doesn't support rollback"
);
}
this._stores.forEach((store) => {
store.internal_rollbackPendingData();
});
if (this._openTimer) {
clearTimeout(this._openTimer);
this._openTimer = undefined;
}
this._lockHelper.transactionFailed(
this._transToken,
"InMemoryTransaction Aborted"
);
}
markCompleted(): void {
// noop
}
getStore(storeName: string): DbStore {
if (!includes(arrayify(this._transToken.storeNames), storeName)) {
throw new Error(
"Store not found in transaction-scoped store list: " + storeName
);
}
if (this._stores.has(storeName)) {
return this._stores.get(storeName)!!!;
}
const store = this._prov.internal_getStore(storeName);
if (!store) {
throw new Error("Store not found: " + storeName);
}
const ims = new InMemoryStore(
this,
store,
this._writeNeeded && this._supportsRollback,
this.logger,
this.getLiveConfigs
);
this._stores.set(storeName, ims);
return ims;
}
internal_isOpen() {
return !!this._openTimer;
}
}
class InMemoryStore implements DbStore {
private _committedStoreData?: Map<string, ItemType>;
private _mergedData: Map<string, ItemType>;
private _storeSchema: StoreSchema;
private _indices: Map<string, InMemoryIndex>;
private _mapType?: OrderedMapType;
constructor(
private _trans: InMemoryTransaction,
storeInfo: StoreData,
private _supportsRollback: boolean,
private logger: IObjectStoreProviderLogger,
private getLiveConfigs?: GetLiveConsumerConfigsFn
) {
this._storeSchema = storeInfo.schema;
if (this._supportsRollback) {
this._committedStoreData = new Map(storeInfo.data);
}
this._indices = storeInfo.indices;
this._mergedData = storeInfo.data;
this._mapType = storeInfo.mapType;
}
internal_commitPendingData(): void {
if (this._supportsRollback) {
this._committedStoreData = new Map(this._mergedData);
}
// Indices were already updated, theres no need to update them now.
}
internal_rollbackPendingData(): void {
if (!this._supportsRollback) {
throw new Error(
"Unable to rollback since InMemoryStore was created with supportsRollback = false"
);
}
this._mergedData.clear();
this._committedStoreData?.forEach((val, key) => {
this._mergedData.set(key, val);
});
// Recreate all indexes on a roll back.
each(this._storeSchema.indexes, (index) => {
this._indices.set(
index.name,
new InMemoryIndex(
this._mergedData,
index,
this._storeSchema.primaryKeyPath,
this._storeSchema.name,
this.logger,
this._mapType,
this.getLiveConfigs
)
);
});
}
get(key: KeyType): Promise<ItemType | undefined> {
const joinedKey = attempt(() => {
return serializeKeyToString(key, this._storeSchema.primaryKeyPath);
});
if (isError(joinedKey)) {
return Promise.reject(joinedKey);
}
return Promise.resolve(this._mergedData.get(joinedKey));
}
getMultiple(keyOrKeys: KeyType | KeyType[]): Promise<ItemType[]> {
const joinedKeys = attempt(() => {
return formListOfSerializedKeys(
keyOrKeys,
this._storeSchema.primaryKeyPath
);
});
if (isError(joinedKeys)) {
return Promise.reject(joinedKeys);
}
return Promise.resolve(
compact(map(joinedKeys, (key) => this._mergedData.get(key)))
);
}
put(itemOrItems: ItemType | ItemType[]): Promise<void> {
if (!this._trans.internal_isOpen()) {
return Promise.reject<void>("InMemoryTransaction already closed");
}
const err = attempt(() => {
each(arrayify(itemOrItems), (item) => {
let pk = getSerializedKeyForKeypath(
item,
this._storeSchema.primaryKeyPath
)!!!;
const existingItem = this._mergedData.get(pk);
if (existingItem) {
// We're going to overwrite the PK anyways - don't remove PK
this._removeFromIndices(
pk,
existingItem,
/** RemovePrimaryKey */ false
);
}
this._mergedData.set(pk, item);
(this.openPrimaryKey() as InMemoryIndex).put(item);
if (this._storeSchema.indexes) {
for (const index of this._storeSchema.indexes) {
(this.openIndex(index.name) as InMemoryIndex).put(item);
}
}
});
});
if (err) {
return Promise.reject<void>(err);
}
return Promise.resolve<void>(void 0);
}
remove(keyOrKeys: KeyType | KeyType[]): Promise<void> {
if (!this._trans.internal_isOpen()) {
return Promise.reject<void>("InMemoryTransaction already closed");
}
const joinedKeys = attempt(() => {
return formListOfSerializedKeys(
keyOrKeys,
this._storeSchema.primaryKeyPath
);
});
if (isError(joinedKeys)) {
return Promise.reject(joinedKeys);
}
return this._removeInternal(joinedKeys);
}
removeRange(
indexName: string,
keyLowRange: KeyType,
keyHighRange: KeyType,
lowRangeExclusive?: boolean,
highRangeExclusive?: boolean
): Promise<void> {
if (!this._trans.internal_isOpen()) {
return Promise.reject<void>("InMemoryTransaction already closed");
}
const index = attempt(() => {
return indexName ? this.openIndex(indexName) : this.openPrimaryKey();
});
if (!index || isError(index)) {
return Promise.reject<void>('Index "' + indexName + '" not found');
}
return index
.getKeysForRange(
keyLowRange,
keyHighRange,
lowRangeExclusive,
highRangeExclusive
)
.then((keys) => {
return this._removeInternal(keys);
});
}
openPrimaryKey(): DbIndex {
if (!this._indices.get("pk")) {
this._indices.set(
"pk",
new InMemoryIndex(
this._mergedData,
undefined as any,
this._storeSchema.primaryKeyPath,
this._storeSchema.name,
this.logger,
this._mapType,
this.getLiveConfigs
)
);
}
const index = this._indices.get("pk")!!!;
index.internal_SetTransaction(this._trans);
return index;
}
openIndex(indexName: string): DbIndex {
let indexSchema = find(
this._storeSchema.indexes,
(idx) => idx.name === indexName
);
if (!indexSchema) {
throw new Error("Index not found: " + indexName);
}
if (!this._indices.has(indexSchema.name)) {
this._indices.set(
indexSchema.name,
new InMemoryIndex(
this._mergedData,
indexSchema,
this._storeSchema.primaryKeyPath,
this._storeSchema.name,
this.logger,
this._mapType,
this.getLiveConfigs
)
);
}
const index = this._indices.get(indexSchema.name)!!!;
index.internal_SetTransaction(this._trans);
return index;
}
clearAllData(): Promise<void> {
if (!this._trans.internal_isOpen()) {
return Promise.reject<void>("InMemoryTransaction already closed");
}
this._mergedData = new Map();
each(this._storeSchema.indexes, (index) => {
this._indices.set(
index.name,
new InMemoryIndex(
this._mergedData,
index,
this._storeSchema.primaryKeyPath,
this._storeSchema.name,
this.logger,
this._mapType,
this.getLiveConfigs
)
);
});
return Promise.resolve<void>(void 0);
}
private _removeInternal(keys: string[]): Promise<void> {
if (!this._trans.internal_isOpen()) {
return Promise.reject<void>("InMemoryTransaction already closed");
}
each(keys, (key) => {
const existingItem = this._mergedData.get(key);
this._mergedData.delete(key);
if (existingItem) {
this._removeFromIndices(key, existingItem, /* RemovePK */ true);
}
});
return Promise.resolve<void>(void 0);
}
private _removeFromIndices(
key: string,
item: ItemType,
removePrimaryKey: boolean
) {
// Don't need to remove from primary key on Puts because set is enough
// 1. If it's an existing key then it will get overwritten
// 2. If it's a new key then we need to add it
if (removePrimaryKey) {
(this.openPrimaryKey() as InMemoryIndex).remove(key);
}
each(this._storeSchema.indexes, (index: IndexSchema) => {
const ind = this.openIndex(index.name) as InMemoryIndex;
const indexKeys = ind.internal_getKeysFromItem(item);
// when it's a unique index, value is the item.
// in case of a non-unique index, value is an array of items,
// and we want to only remove items that have the same primary key
if (ind.isUniqueIndex()) {
each(indexKeys, (indexKey: string) => ind.remove(indexKey));
} else {
each(indexKeys, (idxKey: string) =>
ind.remove({ idxKey, primaryKey: key })
);
}
});
}
}
// Note: Currently maintains nothing interesting -- rebuilds the results every time from scratch. Scales like crap.
class InMemoryIndex extends DbIndexFTSFromRangeQueries {
private _indexTree: IOrderedMap<string, ItemType[]>;
private _trans?: InMemoryTransaction;
private getLiveConfigs: GetLiveConsumerConfigsFn;
constructor(
_mergedData: Map<string, ItemType>,
indexSchema: IndexSchema,
primaryKeyPath: KeyPathType,
private tableName: string,
private logger: IObjectStoreProviderLogger,
mapType?: OrderedMapType,
getLiveConfigs?: GetLiveConsumerConfigsFn
) {
super(indexSchema, primaryKeyPath);
this._indexTree = createOrderedMap(mapType);
this.put(values(_mergedData), true);
this.getLiveConfigs = getLiveConfigs ?? (() => defaultLiveConsumerConfigs);
}
public internal_SetTransaction(trans: InMemoryTransaction) {
this._trans = trans;
}
public internal_getKeysFromItem(item: ItemType) {
let keys: string[] | undefined;
if (this._indexSchema && this._indexSchema!!!.fullText) {
keys = map(
getFullTextIndexWordsForItem(<string>this._keyPath, item),
(val) => serializeKeyToString(val, <string>this._keyPath)
);
} else if (this._indexSchema && this._indexSchema!!!.multiEntry) {
// Have to extract the multiple entries into this alternate table...
const valsRaw = getValueForSingleKeypath(item, <string>this._keyPath);
if (valsRaw) {
keys = map(arrayify(valsRaw), (val) =>
serializeKeyToString(val, <string>this._keyPath)
);
}
} else {
const keyFromKeyPath = getSerializedKeyForKeypath(item, this._keyPath);
if (keyFromKeyPath) {
keys = [keyFromKeyPath];
} else {
this.logger.warn(
`getSerializedKeyForKeypath returned undefined key in InMemoryIndex for table: ${this.tableName}, with index: ${this._indexSchema?.name}`
);
keys = [];
}
}
return keys;
}
// Warning: This function can throw, make sure to trap.
public put(
itemOrItems: ItemType | ItemType[],
skipTransactionOnCreation?: boolean
): void {
if (!skipTransactionOnCreation && !this._trans!.internal_isOpen()) {
throw new Error("InMemoryTransaction already closed");
}
const items = arrayify(itemOrItems);
// If it's not the PK index, re-pivot the data to be keyed off the key value built from the keypath
each(items, (item) => {
// Each item may be non-unique so store as an array of items for each key
const keys = this.internal_getKeysFromItem(item);
each(keys, (key) => {
// For non-unique indexes we want to overwrite
if (!this.isUniqueIndex() && this._indexTree.has(key)) {
const existingItems = this._indexTree.get(key)!!! as ItemType[];
existingItems.push(item);
this._indexTree.set(key, existingItems);
} else {
this._indexTree.set(key, [item]);
}
});
});
}
isUniqueIndex(): boolean {
// An index is unique if it's the primary key (undefined index schema)
// Or the index has defined itself as unique
return (
this._indexSchema === undefined ||
(this._indexSchema && this._indexSchema.unique === true)
);
}
getMultiple(keyOrKeys: KeyType | KeyType[]): Promise<ItemType[]> {
const joinedKeys = attempt(() => {
return formListOfSerializedKeys(keyOrKeys, this._keyPath);
});
if (isError(joinedKeys)) {
return Promise.reject(joinedKeys);
}
let values = [] as ItemType[];
for (const key of joinedKeys) {
values.push(this._indexTree.get(key) as ItemType[]);
}
return Promise.resolve(compact(flatten(values)));
}
/**
* Removes item from index. For non-unique indices, a pair of index value and a primary key is required.
* @param key a string, if it's a unique index, a pair of key value and a primary key, if it's a non-unique index
* @param skipTransactionOnCreation
* @returns
*/
public remove(
key: string | { primaryKey: string; idxKey: string },
skipTransactionOnCreation?: boolean
) {
if (!skipTransactionOnCreation && !this._trans!.internal_isOpen()) {
throw new Error("InMemoryTransaction already closed");
}
if (typeof key === "string") {
this._indexTree.delete(key);
} else {
const idxItems = this._indexTree.get(key.idxKey);
if (!idxItems) {
return;
}
const idxItemsWithoutItem = idxItems.filter((idxItem) => {
const idxItemPrimaryKeyVal = getSerializedKeyForKeypath(
idxItem,
this._primaryKeyPath
)!!!;
return idxItemPrimaryKeyVal !== key.primaryKey;
});
// if we removed all items, remove the index tree node.
// otherwise, update the index value with the new array
// sans the primary key item
if (idxItemsWithoutItem.length === 0) {
this._indexTree.delete(key.idxKey);
} else {
this._indexTree.set(key.idxKey, idxItemsWithoutItem);
}
}
}
getAll(
reverseOrSortOrder?: boolean | QuerySortOrder,
limit?: number,
offset?: number
): Promise<ItemType[]> {
const definedLimit = limit
? limit
: this.isUniqueIndex()
? this._indexTree.size
: MAX_COUNT;
let definedOffset = offset ? offset : 0;
const data = new Array<ItemType>(definedLimit);
const reverse =
reverseOrSortOrder === true ||
reverseOrSortOrder === QuerySortOrder.Reverse;
// when index is not unique, we cannot use offset as a starting index
let skip = this.isUniqueIndex() ? definedOffset : 0;
const iterator = reverse
? this._indexTree.entriesReversed()
: this._indexTree.entries();
let i = 0;
for (const item of iterator) {
if (item.key === undefined) {
continue;
}
// a hack to account for offset that b+tree library lacks
if (skip > 0) {
skip--;
continue;
}
// when index is not unique, each node may contain multiple items
if (!this.isUniqueIndex()) {
let count = item.value?.length || 0;
const minOffsetCount = Math.min(count, definedOffset);
count -= minOffsetCount;
definedOffset -= minOffsetCount;
// we have skipped all values in this index, go to the next one
if (count === 0) {
continue;
}
const values = this._getKeyValues(
item.key,
definedLimit - i,
(item.value?.length || 0) - count,
reverse
);
values.forEach((v, j) => {
data[i + j] = v;
});
i += values.length;
} else {
// in case of non-unique index, value will be an array of one element
data[i] = item.value?.[0] as Object;
i++;
}
if (i >= definedLimit) {
break;
}
}
// if index is not unique, trim the empty slots in data
// if we used MAX_COUNT to construct it.
if (!this.isUniqueIndex() && i !== definedLimit) {
return Promise.resolve(trimArray(data, i));
} else {
return Promise.resolve(data);
}
}
getOnly(
key: KeyType,
reverseOrSortOrder?: boolean | QuerySortOrder,
limit?: number,
offset?: number
): Promise<ItemType[]> {
return this.getRange(
key,
key,
false,
false,
reverseOrSortOrder,
limit,
offset
);
}
getRange(
keyLowRange: KeyType,
keyHighRange: KeyType,
lowRangeExclusive?: boolean,
highRangeExclusive?: boolean,
reverseOrSortOrder?: boolean | QuerySortOrder,
limit?: number,
offset?: number
): Promise<ItemType[]> {
const values = attempt(() => {
const reverse =
reverseOrSortOrder === true ||
reverseOrSortOrder === QuerySortOrder.Reverse;
limit = limit
? limit
: this.isUniqueIndex()
? this._indexTree.size
: MAX_COUNT;
offset = offset ? offset : 0;
const keyLow = serializeKeyToString(keyLowRange, this._keyPath);
const keyHigh = serializeKeyToString(keyHighRange, this._keyPath);
const iterator = reverse
? this._indexTree.entriesReversed()
: this._indexTree.entries();
let values = [] as ItemType[];
const { usePushForGetRange } = this.getLiveConfigs();
const pushValues = (values: ItemType[], newValues: ItemType[]) => {
newValues.forEach((v) => values.push(v));
return values;
};
const concatValues = (values: ItemType[], newValues: ItemType[]) => {
return values.concat(newValues);
};
const mergeFn = usePushForGetRange ? pushValues : concatValues;
for (const entry of iterator) {
const key = entry.key;
if (key === undefined) {
continue;
}
if (
(key > keyLow || (key === keyLow && !lowRangeExclusive)) &&
(key < keyHigh || (key === keyHigh && !highRangeExclusive))
) {
if (offset > 0) {
if (this.isUniqueIndex()) {
offset--;
continue;
} else {
const idxValues = this._indexTree.get(key) as ItemType[];
offset -= idxValues.length;
// if offset >= 0, we skipped just enough, or we still need to skip more
// if offset < 0, we need to get some of the values from the index
if (offset >= 0) {
continue;
}
}
}
if (values.length >= limit) {
break;
}
if (this.isUniqueIndex()) {
const newValues = this._indexTree.get(key) as ItemType[];
values = mergeFn(values, newValues);
} else {
const newValues = this._getKeyValues(
key,
limit - values.length,
Math.abs(offset),
reverse
);
values = mergeFn(values, newValues);
if (offset < 0) {
offset = 0;
}
}
}
}
return values;
});
if (isError(values)) {
return Promise.reject(values);
}
return Promise.resolve(values);
}
getKeysForRange(
keyLowRange: KeyType,
keyHighRange: KeyType,
lowRangeExclusive?: boolean,
highRangeExclusive?: boolean
): Promise<any[]> {
const keys = attempt(() => {
return this._getKeysForRange(
keyLowRange,
keyHighRange,
lowRangeExclusive,
highRangeExclusive
);
});
if (isError(keys)) {
return Promise.reject(keys);
}
return Promise.resolve(keys);
}
/**
* Utility function to simplify offset/limit checks and allow a negative offset. Retrieves values associated with the given key
* @param key primary key
* @param limit
* @param offset can be neagtive, treated the same way as 0
* @param reverse
* @returns value associated with given key, undefined if the key is not found.
*/
private _getKeyValues(
key: string,
limit: number,
offset: number,
reverse: boolean
): ItemType[] {
if (limit <= 0) {
return [];
}
const idxValues = this._indexTree.get(key) as ItemType[];
// get may return undefined, if the key is not found
if (!idxValues) {
return idxValues;
}
if (offset >= idxValues.length) {
return [];
}
// Perf optimisation. No items to skip, and limit is at least the number of items we have in the index.
// we know that we will need the whole index values array to fulfill the results,
// skip using take/drop, return the whole array immediately.
if (offset <= 0 && limit >= idxValues.length) {
return reverse ? idxValues.slice().reverse() : idxValues;
}
const itemsToDrop = Math.min(limit, offset);
const itemsToTake = Math.min(limit, idxValues.length - offset);
return reverse
? takeRight(dropRight(idxValues, itemsToDrop), itemsToTake)
: take(drop(idxValues, itemsToDrop), itemsToTake);
}
// Warning: This function can throw, make sure to trap.
private _getKeysForRange(
keyLowRange: KeyType,
keyHighRange: KeyType,
lowRangeExclusive?: boolean,
highRangeExclusive?: boolean
): string[] {
const usePrimaryKey = this.getLiveConfigs().usePrimaryKeyForGetKeysForRange;
const keyLow = serializeKeyToString(keyLowRange, this._keyPath);
const keyHigh = serializeKeyToString(keyHighRange, this._keyPath);
const iterator = this._indexTree.entries();
const keys: string[] = [];
for (const entry of iterator) {
let key = entry.key;
if (key === undefined) {
continue;
}
const isMatch =
(key > keyLow || (key === keyLow && !lowRangeExclusive)) &&
(key < keyHigh || (key === keyHigh && !highRangeExclusive));
if (!isMatch) {
continue;
}
// If the current index is not the primary one. We need to find primary key for each value and return that instead.
if (
usePrimaryKey &&
entry.value &&
this._keyPath !== this._primaryKeyPath
) {
for (const value of entry.value) {
key = getSerializedKeyForKeypath(value, this._primaryKeyPath) ?? key;
if (key === entry.key) {
this.logger.warn(
`getSerializedKeyForKeypath returned undefined key in InMemoryIndex for table: ${this.tableName}, with index: ${this._indexSchema?.name}`
);
}
if (!keys.includes(key)) {
keys.push(key);
}
}
}
// Otherwise, we can just use the key as is.
else {
keys.push(key);
}
}
return keys;
}
// Warning: This function can throw, make sure to trap.
private _getKeyCountForRange(
keyLowRange: KeyType,
keyHighRange: KeyType,
lowRangeExclusive?: boolean,
highRangeExclusive?: boolean
): number {
const keyLow = serializeKeyToString(keyLowRange, this._keyPath);
const keyHigh = serializeKeyToString(keyHighRange, this._keyPath);
const iterator = this._indexTree.entries();
let keyCount = 0;
for (const item of iterator) {
const key = item.key;
if (key === undefined) {
continue;
}
if (
(key > keyLow || (key === keyLow && !lowRangeExclusive)) &&
(key < keyHigh || (key === keyHigh && !highRangeExclusive))
) {
if (this.isUniqueIndex()) {
keyCount++;
} else {
keyCount += item.value?.length || 0;
}
}
}
return keyCount;
}
countAll(): Promise<number> {
if (this.isUniqueIndex()) {