-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
nodes.ts
1218 lines (1064 loc) · 40.1 KB
/
nodes.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
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
import { store } from "./"
import { IGatsbyNode } from "./types"
import { createPageDependency } from "./actions/add-page-dependency"
import { IDbQueryElemMatch } from "../db/common/query"
// Only list supported ops here. "CacheableFilterOp"
export type FilterOp = // TODO: merge with DbComparator ?
| "$eq"
| "$ne"
| "$lt"
| "$lte"
| "$gt"
| "$gte"
| "$in"
| "$nin"
| "$regex" // Note: this includes $glob
// Note: `undefined` is an encoding for a property that does not exist
export type FilterValueNullable = // TODO: merge with DbComparatorValue
| string
| number
| boolean
| null
| undefined
| RegExp // Only valid for $regex
| Array<string | number | boolean | null | undefined>
// This is filter value in most cases
type FilterValue =
| string
| number
| boolean
| RegExp // Only valid for $regex
| Array<string | number | boolean>
export type FilterCacheKey = string
export interface IFilterCache {
op: FilterOp
// In this map `undefined` values represent nodes that did not have the path
// The individual arrays are ordered asc by internal.counter which will
// preserve insertion order and guarantee uniqueness and it's a number so our
// .sort() calls can subtract one from the other which is nice :)
// This arrays may contain duplicates (!) because those only get filtered in the
// last step.
// TODO: We might decide to make sure these buckets _are_ deduped for eq perf
byValue: Map<FilterValueNullable, Array<IGatsbyNode>>
meta: {
// Used by ne/nin, which will create a Set from this array and then remove another set(s) and sort
nodesUnordered?: Array<IGatsbyNode>
// Flat list of all nodes by requested types, ordered by counter (cached for empty filters)
orderedByCounter?: Array<IGatsbyNode>
// Ordered list of all values (by `<`) found by this filter. No null / undefs
valuesAsc?: Array<FilterValue>
// Flat list of nodes, ordered by valueAsc
nodesByValueAsc?: Array<IGatsbyNode>
// Ranges of nodes per value, maps to the nodesByValueAsc array
valueRangesAsc?: Map<FilterValue, [number, number]>
// Ordered list of all values (by `>`) found by this filter. No null / undefs
valuesDesc?: Array<FilterValue>
// Flat list of nodes, ordered by valueDesc
nodesByValueDesc?: Array<IGatsbyNode>
// Ranges of nodes per value, maps to the nodesByValueDesc array
valueRangesDesc?: Map<FilterValue, [number, number]>
}
}
export type FiltersCache = Map<FilterCacheKey, IFilterCache>
/**
* Get all nodes from redux store.
*/
export const getNodes = (): Array<IGatsbyNode> => {
const nodes = store.getState().nodes
if (nodes) {
return Array.from(nodes.values())
} else {
return []
}
}
/**
* Get node by id from store.
*/
export const getNode = (id: string): IGatsbyNode | undefined =>
store.getState().nodes.get(id)
/**
* Get all nodes of type from redux store.
*/
export const getNodesByType = (type: string): Array<IGatsbyNode> => {
const nodes = store.getState().nodesByType.get(type)
if (nodes) {
return Array.from(nodes.values())
} else {
return []
}
}
/**
* Get all type names from redux store.
*/
export const getTypes = (): Array<string> =>
Array.from(store.getState().nodesByType.keys())
/**
* Determine if node has changed.
*/
export const hasNodeChanged = (id: string, digest: string): boolean => {
const node = store.getState().nodes.get(id)
if (!node) {
return true
} else {
return node.internal.contentDigest !== digest
}
}
/**
* Get node and save path dependency.
*/
export const getNodeAndSavePathDependency = (
id: string,
path: string
): IGatsbyNode | undefined => {
const node = getNode(id)
if (!node) {
console.error(
`getNodeAndSavePathDependency failed for node id: ${id} as it was not found in cache`
)
return undefined
}
createPageDependency({ path, nodeId: id })
return node
}
type Resolver = (node: IGatsbyNode) => Promise<any> // TODO
export const saveResolvedNodes = async (
nodeTypeNames: Array<string>,
resolver: Resolver
): Promise<void> => {
for (const typeName of nodeTypeNames) {
const nodes = store.getState().nodesByType.get(typeName)
if (!nodes) continue
const resolvedNodes = new Map()
for (const node of nodes.values()) {
const resolved = await resolver(node)
resolvedNodes.set(node.id, resolved)
}
store.dispatch({
type: `SET_RESOLVED_NODES`,
payload: {
key: typeName,
nodes: resolvedNodes,
},
})
}
}
/**
* Get node and save path dependency.
*/
export const getResolvedNode = (
typeName: string,
id: string
): IGatsbyNode | null => {
const { nodesByType, resolvedNodesCache } = store.getState()
const nodes = nodesByType.get(typeName)
if (!nodes) {
return null
}
const node = nodes.get(id)
if (!node) {
return null
}
const resolvedNodes = resolvedNodesCache.get(typeName)
if (resolvedNodes) {
node.__gatsby_resolved = resolvedNodes.get(id)
}
return node
}
export function postIndexingMetaSetup(
filterCache: IFilterCache,
op: FilterOp
): void {
// Loop through byValue and make sure the buckets are sorted by counter
// Since we don't do insertion sort, we have to do it afterwards
for (const bucket of filterCache.byValue) {
bucket[1].sort((a, b) => a.internal.counter - b.internal.counter)
}
if (op === `$ne` || op === `$nin`) {
postIndexingMetaSetupNeNin(filterCache)
} else if ([`$lt`, `$lte`, `$gt`, `$gte`].includes(op)) {
postIndexingMetaSetupLtLteGtGte(filterCache, op)
}
}
function postIndexingMetaSetupNeNin(filterCache: IFilterCache): void {
// Note: edge cases regarding `null` and `undefined`. Here `undefined` signals
// that the property did not exist as the filters do not support actual
// `undefined` values.
// For $ne, `null` only returns nodes that actually have the property
// and in that case the property cannot be `null` either. For any other value,
// $ne will return all nodes where the value is not actually the needle,
// including nodes where the value is null.
// A $nin does the same as an $ne except it filters multiple values instead
// of just one.
// For `$ne` we will take the list of all targeted nodes and eliminate the
// bucket of nodes with a particular value, if it exists at all..
const arr: Array<IGatsbyNode> = []
filterCache.meta.nodesUnordered = arr
filterCache.byValue.forEach(v => {
v.forEach(node => {
arr.push(node)
})
})
}
function postIndexingMetaSetupLtLteGtGte(
filterCache: IFilterCache,
op: FilterOp
): void {
// Create an ordered array of individual nodes, ordered (grouped) by the
// value to which the filter resolves. Nodes per value are ordered by
// internal.counter, asc.
// This way non-eq ops can simply slice the array to get a range.
const entriesNullable: Array<[FilterValueNullable, Array<IGatsbyNode>]> = [
...filterCache.byValue.entries(),
]
// These range checks never return `null` or `undefined` so filter those out
// By filtering them out early, the sort should be faster. Could be ...
const entries: Array<[
FilterValue,
Array<IGatsbyNode>
]> = entriesNullable.filter(([v]) => v != null) as Array<
[FilterValue, Array<IGatsbyNode>]
>
// Sort all arrays by its value, asc. Ignore/allow potential type casting.
// Note: while `<` is the inverse of `>=`, the ordering might coerce values.
// This coercion makes the op no longer idempotent (normally the result of
// `a < b` is the opposite of `b >= a` for any a or b of the same type). The
// exception is a number that is `NaN`, which we're ignoring here as it's most
// likely a bug in the user code. However, when coercing the ops may end up
// comparing against `NaN`, too. For example: `("abc" <= 12) !== (12 > "abc")`
// which ends up doing `NaN <= 12` and `NaN > "abc"`, which will both yield
// false.
// So instead we potentially track two ordered lists; ascending and descending
// and the only difference when comparing the inverse of one to the other
// should be how these `NaN` cases end up getting ordered.
// It's fine for `lt` and `lte` to use the same ordered list. Same for gt/gte.
if (op === `$lt` || op === `$lte`) {
// Order ascending; first value is lowest
entries.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
} else if (op === `$gt` || op === `$gte`) {
// Order descending; first value is highest
entries.sort(([a], [b]) => (a > b ? -1 : a < b ? 1 : 0))
}
const orderedNodes: Array<IGatsbyNode> = []
const orderedValues: Array<FilterValue> = []
const offsets: Map<FilterValue, [number, number]> = new Map()
entries.forEach(([v, bucket]: [FilterValue, Array<IGatsbyNode>]) => {
// Record the range containing all nodes with as filter value v
// The last value of the range should be the offset of the next value
// (So you should be able to do `nodes.slice(start, stop)` to get them)
offsets.set(v, [orderedNodes.length, orderedNodes.length + bucket.length])
// We could do `arr.push(...bucket)` here but that's not safe with very
// large sets, so we use a regular loop
bucket.forEach(node => orderedNodes.push(node))
orderedValues.push(v)
})
if (op === `$lt` || op === `$lte`) {
filterCache.meta.valuesAsc = orderedValues
filterCache.meta.nodesByValueAsc = orderedNodes
// The nodesByValueAsc is ordered by value, but multiple nodes per value are
// not ordered. To make lt as fast as lte, we must know the start and stop
// index for each value. Similarly useful for for `ne`.
filterCache.meta.valueRangesAsc = offsets
} else if (op === `$gt` || op === `$gte`) {
filterCache.meta.valuesDesc = orderedValues
filterCache.meta.nodesByValueDesc = orderedNodes
// The nodesByValueDesc is ordered by value, but multiple nodes per value are
// not ordered. To make gt as fast as gte, we must know the start and stop
// index for each value. Similarly useful for for `ne`.
filterCache.meta.valueRangesDesc = offsets
}
}
/**
* Given a single non-elemMatch filter path, a list of node types, and a
* cache, create a cache that for each resulting value of the filter contains
* all the Nodes in a list.
* This cache is used for applying the filter and is a massive improvement over
* looping over all the nodes, when the number of pages (/nodes) scales up.
*/
export const ensureIndexByQuery = (
op: FilterOp,
filterCacheKey: FilterCacheKey,
filterPath: Array<string>,
nodeTypeNames: Array<string>,
filtersCache: FiltersCache
): void => {
const state = store.getState()
const resolvedNodesCache = state.resolvedNodesCache
const filterCache: IFilterCache = {
op,
byValue: new Map<FilterValueNullable, Array<IGatsbyNode>>(),
meta: {},
} as IFilterCache
filtersCache.set(filterCacheKey, filterCache)
// We cache the subsets of nodes by type, but only one type. So if searching
// through one node type we can prevent a search through all nodes, otherwise
// it's probably faster to loop through all nodes. Perhaps. Maybe.
if (nodeTypeNames.length === 1) {
getNodesByType(nodeTypeNames[0]).forEach(node => {
addNodeToFilterCache(node, filterPath, filterCache, resolvedNodesCache)
})
} else {
// Here we must first filter for the node type
// This loop is expensive at scale (!)
state.nodes.forEach(node => {
if (!nodeTypeNames.includes(node.internal.type)) {
return
}
addNodeToFilterCache(node, filterPath, filterCache, resolvedNodesCache)
})
}
postIndexingMetaSetup(filterCache, op)
}
export function ensureEmptyFilterCache(
filterCacheKey,
nodeTypeNames: Array<string>,
filtersCache: FiltersCache
): void {
// This is called for queries without any filters
// We want to cache the result since it's basically a list of nodes by type(s)
// There are sites that have multiple queries which are empty
const state = store.getState()
const resolvedNodesCache = state.resolvedNodesCache
const orderedByCounter: Array<IGatsbyNode> = []
filtersCache.set(filterCacheKey, {
op: `$eq`, // Ignore.
byValue: new Map<FilterValueNullable, Array<IGatsbyNode>>(),
meta: {
orderedByCounter, // This is what we want
},
})
if (nodeTypeNames.length === 1) {
getNodesByType(nodeTypeNames[0]).forEach(node => {
if (!node.__gatsby_resolved) {
const typeName = node.internal.type
const resolvedNodes = resolvedNodesCache.get(typeName)
const resolved = resolvedNodes?.get(node.id)
if (resolved !== undefined) {
node.__gatsby_resolved = resolved
}
}
orderedByCounter.push(node)
})
} else {
// Here we must first filter for the node type
// This loop is expensive at scale (!)
state.nodes.forEach(node => {
if (nodeTypeNames.includes(node.internal.type)) {
if (!node.__gatsby_resolved) {
const typeName = node.internal.type
const resolvedNodes = resolvedNodesCache.get(typeName)
const resolved = resolvedNodes?.get(node.id)
if (resolved !== undefined) {
node.__gatsby_resolved = resolved
}
}
orderedByCounter.push(node)
}
})
}
// Since each node can only have one type, we shouldn't have to be concerned
// about duplicates in this array. Just make sure they're sorted.
orderedByCounter.sort((a, b) => a.internal.counter - b.internal.counter)
}
function addNodeToFilterCache(
node: IGatsbyNode,
chain: Array<string>,
filterCache: IFilterCache,
resolvedNodesCache,
valueOffset: any = node
): void {
// There can be a filter that targets `__gatsby_resolved` so fix that first
if (!node.__gatsby_resolved) {
const typeName = node.internal.type
const resolvedNodes = resolvedNodesCache.get(typeName)
node.__gatsby_resolved = resolvedNodes?.get(node.id)
}
// - for plain query, valueOffset === node
// - for elemMatch, valueOffset is sub-tree of the node to continue matching
let v = valueOffset as any
let i = 0
while (i < chain.length && v) {
const nextProp = chain[i++]
v = v[nextProp]
}
if (
(typeof v !== `string` &&
typeof v !== `number` &&
typeof v !== `boolean` &&
v !== null) ||
i !== chain.length
) {
if (i === chain.length && Array.isArray(v)) {
// The op resolved to an array
// Add an entry for each element of the array. This would work for ops
// like eq and ne, but not sure about range ops like lt,lte,gt,gte.
v.forEach(v => markNodeForValue(filterCache, node, v))
return
}
// This means that either
// - The filter resolved to `undefined`, or
// - The filter resolved to something other than a primitive
// Set the value to `undefined` to mark "path does not (fully) exist"
v = undefined
}
markNodeForValue(filterCache, node, v)
}
function markNodeForValue(
filterCache: IFilterCache,
node: IGatsbyNode,
value: FilterValueNullable
): void {
let arr = filterCache.byValue.get(value)
if (!arr) {
arr = []
filterCache.byValue.set(value, arr)
}
arr.push(node)
}
export const ensureIndexByElemMatch = (
op: FilterOp,
filterCacheKey: FilterCacheKey,
filter: IDbQueryElemMatch,
nodeTypeNames: Array<string>,
filtersCache: FiltersCache
): void => {
// Given an elemMatch filter, generate the cache that contains all nodes that
// matches a given value for that sub-query
const state = store.getState()
const { resolvedNodesCache } = state
const filterCache: IFilterCache = {
op,
byValue: new Map<FilterValueNullable, Array<IGatsbyNode>>(),
meta: {},
} as IFilterCache
filtersCache.set(filterCacheKey, filterCache)
if (nodeTypeNames.length === 1) {
getNodesByType(nodeTypeNames[0]).forEach(node => {
addNodeToBucketWithElemMatch(
node,
node,
filter,
filterCache,
resolvedNodesCache
)
})
} else {
// Expensive at scale
state.nodes.forEach(node => {
if (!nodeTypeNames.includes(node.internal.type)) {
return
}
addNodeToBucketWithElemMatch(
node,
node,
filter,
filterCache,
resolvedNodesCache
)
})
}
postIndexingMetaSetup(filterCache, op)
}
function addNodeToBucketWithElemMatch(
node: IGatsbyNode,
valueAtCurrentStep: any, // Arbitrary step on the path inside the node
filter: IDbQueryElemMatch,
filterCache: IFilterCache,
resolvedNodesCache
): void {
// There can be a filter that targets `__gatsby_resolved` so fix that first
if (!node.__gatsby_resolved) {
const typeName = node.internal.type
const resolvedNodes = resolvedNodesCache.get(typeName)
node.__gatsby_resolved = resolvedNodes?.get(node.id)
}
const { path, nestedQuery } = filter
// Find the value to apply elemMatch to
let i = 0
while (i < path.length && valueAtCurrentStep) {
const nextProp = path[i++]
valueAtCurrentStep = valueAtCurrentStep[nextProp]
}
if (path.length !== i) {
// Found undefined before the end of the path
return
}
// `v` should now be an elemMatch target, probably an array (but maybe not)
if (!Array.isArray(valueAtCurrentStep)) {
// It's possible to `elemMatch` on a non-array so let's support that too
valueAtCurrentStep = [valueAtCurrentStep]
}
// Note: We need to check all elements because the node may need to be added
// to multiple buckets (`{a:[{b:3},{b:4}]}`, for `a.elemMatch.b/eq` that
// node ends up in buckets for value 3 and 4. This may lead to duplicate
// work when elements resolve to the same value, but that can't be helped.
valueAtCurrentStep.forEach(elem => {
if (nestedQuery.type === `elemMatch`) {
addNodeToBucketWithElemMatch(
node,
elem,
nestedQuery,
filterCache,
resolvedNodesCache
)
} else {
// Now take same route as non-elemMatch filters would take
addNodeToFilterCache(
node,
nestedQuery.path,
filterCache,
resolvedNodesCache,
elem
)
}
})
}
const binarySearchAsc = (
values: Array<FilterValue>, // Assume ordered asc
needle: FilterValue
): [number, number] | undefined => {
let min = 0
let max = values.length - 1
let pivot = Math.floor(values.length / 2)
while (min <= max) {
const value = values[pivot]
if (needle < value) {
// Move pivot to middle of nodes left of current pivot
// assert pivot < max
max = pivot
} else if (needle > value) {
// Move pivot to middle of nodes right of current pivot
// assert pivot > min
min = pivot
} else {
// This means needle === value
// TODO: except for NaN ... and potentially certain type casting cases
return [pivot, pivot]
}
if (max - min <= 1) {
// End of search. Needle not found (as expected). Use pivot as index.
// If the needle was not found, max-min==1 and max is returned.
return [min, max]
}
pivot = min + Math.floor((max - min) / 2)
}
// Shouldn't be reachable
return undefined
}
const binarySearchDesc = (
values: Array<FilterValue>, // Assume ordered desc
needle: FilterValue
): [number, number] | undefined => {
let min = 0
let max = values.length - 1
let pivot = Math.floor(values.length / 2)
while (min <= max) {
const value = values[pivot]
if (needle < value) {
// Move pivot to middle of nodes right of current pivot
// assert pivot < min
min = pivot
} else if (needle > value) {
// Move pivot to middle of nodes left of current pivot
// assert pivot > max
max = pivot
} else {
// This means needle === value
// TODO: except for NaN ... and potentially certain type casting cases
return [pivot, pivot]
}
if (max - min <= 1) {
// End of search. Needle not found (as expected). Use pivot as index.
// If the needle was not found, max-min==1 and max is returned.
return [min, max]
}
pivot = min + Math.floor((max - min) / 2)
}
// Shouldn't be reachable
return undefined
}
/**
* Given the cache key for a filter and a target value return the list of nodes
* that resolve to this value. The returned array should be ordered by id.
* This returns `undefined` if there is no such node
*
* Basically if the filter was {a: {b: {slug: {eq: "foo/bar"}}}} then it will
* return all the nodes that have `node.slug === "foo/bar"`. That usually (but
* not always) at most one node for slug, but this filter can apply to anything.
*
* Arrays returned by this function must be ordered by internal.counter and
* not contain duplicate nodes (!)
*/
export const getNodesFromCacheByValue = (
filterCacheKey: FilterCacheKey,
filterValue: FilterValueNullable,
filtersCache: FiltersCache,
wasElemMatch
): Array<IGatsbyNode> | undefined => {
const filterCache = filtersCache.get(filterCacheKey)
if (!filterCache) {
return undefined
}
const op = filterCache.op
if (op === `$eq`) {
// Arrays in byValue are assumed to be ordered by counter
if (filterValue == null) {
// Edge case for null; fetch all nodes for `null` and `undefined` because
// `$eq` also returns nodes without the path when searching for `null`.
// Not all ops do so, so we map non-existing paths to `undefined`.
const arrNull = filterCache.byValue.get(null) ?? []
const arrUndef = filterCache.byValue.get(undefined) ?? []
// Merge the two (ordered) arrays and return an ordered deduped array
// TODO: is there a reason left why we cant just cache this merged list?
return unionNodesByCounter(arrNull, arrUndef)
}
return filterCache.byValue.get(filterValue)
}
if (op === `$in`) {
if (!Array.isArray(filterValue)) {
throw new Error("The argument to the `in` comparator should be an array")
}
const filterValueArr: Array<FilterValueNullable> = filterValue
const set: Set<IGatsbyNode> = new Set()
// TODO: we can also mergeSort for every step. this may perform worse because of how memory in js works.
// For every value in the needle array, find the bucket of nodes for
// that value, add this bucket of nodes to one list, return the list.
filterValueArr.forEach((v: FilterValueNullable) =>
filterCache.byValue.get(v)?.forEach(v => set.add(v))
)
const arr = [...set] // this is bad for perf but will guarantee us a unique set :(
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// Note: it's very unlikely that the list of filter values is big so .includes should be fine here
if (filterValueArr.includes(null)) {
// Like all other ops, `in: [null]` behaves weirdly, allowing all nodes
// that do not actually have a (complete) path (v=undefined)
const nodes = filterCache.byValue.get(undefined)
if (nodes) {
// This will also dedupe so don't do that immediately
return unionNodesByCounter(nodes, arr)
}
}
// elemMatch can cause a node to appear in multiple buckets so we must dedupe first
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
if (op === `$nin`) {
// This is essentially the same as the $ne operator, just with multiple
// values to exclude.
if (!Array.isArray(filterValue)) {
throw new Error(`The $nin operator expects an array as value`)
}
const values: Set<FilterValueNullable> = new Set(filterValue)
const set = new Set(filterCache.meta.nodesUnordered)
// Do the action for "$ne" for each element in the set of values
values.forEach(filterValue => {
removeBucketFromSet(filterValue, filterCache, set)
})
// TODO: there's probably a more efficient algorithm to do set
// subtraction in such a way that we dont have to re-sort
return [...set].sort((A, B) => A.internal.counter - B.internal.counter)
}
if (op === `$ne`) {
const set = new Set(filterCache.meta.nodesUnordered)
removeBucketFromSet(filterValue, filterCache, set)
// TODO: there's probably a more efficient algorithm to do set
// subtraction in such a way that we dont have to resort here
return [...set].sort((A, B) => A.internal.counter - B.internal.counter)
}
if (op === `$regex`) {
// Note: $glob is converted to $regex so $glob filters go through here, too
// Aside from the input pattern format, further behavior is exactly the same.
// The input to the filter must be a string (including leading/trailing slash and regex flags)
// By the time the filter reaches this point, the filterValue has to be a regex.
if (!(filterValue instanceof RegExp)) {
throw new Error(
`The value for the $regex comparator must be an instance of RegExp`
)
}
const regex = filterValue
const arr: Array<IGatsbyNode> = []
filterCache.byValue.forEach((nodes, value) => {
// TODO: does the value have to be a string for $regex? Can we auto-ignore any non-strings? Or does it coerce.
// Note: for legacy reasons partial paths should also be included for regex
if (value !== undefined && regex.test(String(value))) {
nodes.forEach(node => arr.push(node))
}
})
// TODO: we _can_ cache this list as well. Might make sense if it turns out that $regex is mostly used with literals
// TODO: it may make sense to first collect all buckets and then to .concat them, or merge sort them
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
if (filterValue == null) {
if (op === `$lt` || op === `$gt`) {
// Nothing is lt/gt null
return undefined
}
// This is an edge case and this value should be directly indexed
// For `lte`/`gte` this should only return nodes for `null`, not a "range"
return filterCache.byValue.get(filterValue)
}
if (Array.isArray(filterValue)) {
throw new Error(
"Array is an invalid filter value for the `" + op + "` comparator"
)
}
if (filterValue instanceof RegExp) {
// This is most likely an internal error, although it is possible for
// users to talk to this API more directly.
throw new Error(
`A RegExp instance is only valid for $regex and $glob comparators`
)
}
if (op === `$lt`) {
// First try a direct approach. If a value is queried that also exists then
// we can prevent a binary search through the whole list, O(1) vs O(log n)
const ranges = filterCache.meta.valueRangesAsc
const nodes = filterCache.meta.nodesByValueAsc
const range = ranges!.get(filterValue)
if (range) {
const arr = nodes!.slice(0, range[0])
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
// Query may ask for a value that doesn't appear in the list, like if the
// list is [1, 2, 5, 6] and the query is <= 3. In that case we have to
// apply a search (we'll do binary) to determine the offset to slice from.
// Note: for lte, the valueAsc array must be list at this point
const values = filterCache.meta.valuesAsc as Array<FilterValue>
// It shouldn't find the targetValue (but it might) and return the index of
// the two value between which targetValue sits, or first/last element.
const point = binarySearchAsc(values, filterValue)
if (!point) {
return undefined
}
const [pivotMin, pivotMax] = point
// Each pivot index must have a value and a range
// The returned min/max index may include the lower/upper bound, so we still
// have to do lte checks for both values.
let pivotValue = values[pivotMax]
if (pivotValue > filterValue) {
pivotValue = values[pivotMin]
}
// Note: the pivot value _shouldnt_ match the filter value because that
// means the value was actually found, but those should have been indexed
// so should have yielded a result in the .get() above.
const [exclPivot, inclPivot] = ranges!.get(pivotValue) as [number, number]
// Note: technically, `5 <= "5" === true` but `5` would not be cached.
// So we have to consider weak comparison and may have to include the pivot
const until = pivotValue < filterValue ? inclPivot : exclPivot
const arr = nodes!.slice(0, until)
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
if (op === `$lte`) {
// First try a direct approach. If a value is queried that also exists then
// we can prevent a binary search through the whole list, O(1) vs O(log n)
const ranges = filterCache.meta.valueRangesAsc
const nodes = filterCache.meta.nodesByValueAsc
const range = ranges!.get(filterValue)
if (range) {
const arr = nodes!.slice(0, range[1])
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
// Query may ask for a value that doesn't appear in the list, like if the
// list is [1, 2, 5, 6] and the query is <= 3. In that case we have to
// apply a search (we'll do binary) to determine the offset to slice from.
// Note: for lte, the valueAsc array must be list at this point
const values = filterCache.meta.valuesAsc as Array<FilterValue>
// It shouldn't find the targetValue (but it might) and return the index of
// the two value between which targetValue sits, or first/last element.
const point = binarySearchAsc(values, filterValue)
if (!point) {
return undefined
}
const [pivotMin, pivotMax] = point
// Each pivot index must have a value and a range
// The returned min/max index may include the lower/upper bound, so we still
// have to do lte checks for both values.
let pivotValue = values[pivotMax]
if (pivotValue > filterValue) {
pivotValue = values[pivotMin]
}
// Note: the pivot value _shouldnt_ match the filter value because that
// means the value was actually found, but those should have been indexed
// so should have yielded a result in the .get() above.
const [exclPivot, inclPivot] = ranges!.get(pivotValue) as [number, number]
// Note: technically, `5 <= "5" === true` but `5` would not be cached.
// So we have to consider weak comparison and may have to include the pivot
const until = pivotValue <= filterValue ? inclPivot : exclPivot
const arr = nodes!.slice(0, until)
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
if (op === `$gt`) {
// First try a direct approach. If a value is queried that also exists then
// we can prevent a binary search through the whole list, O(1) vs O(log n)
const ranges = filterCache.meta.valueRangesDesc
const nodes = filterCache.meta.nodesByValueDesc
const range = ranges!.get(filterValue)
if (range) {
const arr = nodes!.slice(0, range[0]).reverse()
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
// Query may ask for a value that doesn't appear in the list, like if the
// list is [1, 2, 5, 6] and the query is <= 3. In that case we have to
// apply a search (we'll do binary) to determine the offset to slice from.
// Note: for gte, the valueDesc array must be list at this point
const values = filterCache.meta.valuesDesc as Array<FilterValue>
// It shouldn't find the targetValue (but it might) and return the index of
// the two value between which targetValue sits, or first/last element.
const point = binarySearchDesc(values, filterValue)
if (!point) {
return undefined
}
const [pivotMin, pivotMax] = point
// Each pivot index must have a value and a range
// The returned min/max index may include the lower/upper bound, so we still
// have to do gte checks for both values.
let pivotValue = values[pivotMax]
if (pivotValue < filterValue) {
pivotValue = values[pivotMin]
}
// Note: the pivot value _shouldnt_ match the filter value because that
// means the value was actually found, but those should have been indexed
// so should have yielded a result in the .get() above.
const [exclPivot, inclPivot] = ranges!.get(pivotValue) as [number, number]
// Note: technically, `5 >= "5" === true` but `5` would not be cached.
// So we have to consider weak comparison and may have to include the pivot
const until = pivotValue > filterValue ? inclPivot : exclPivot
const arr = nodes!.slice(0, until).reverse()
arr.sort((A, B) => A.internal.counter - B.internal.counter)
// elemMatch can cause a node to appear in multiple buckets so we must dedupe
if (wasElemMatch) {
expensiveDedupeInline(arr)
}
return arr
}
if (op === `$gte`) {
// First try a direct approach. If a value is queried that also exists then
// we can prevent a binary search through the whole list, O(1) vs O(log n)
const ranges = filterCache.meta.valueRangesDesc
const nodes = filterCache.meta.nodesByValueDesc