MCPcopy Create free account
hub / github.com/TanStack/db / createIndexUsageTracker

Function createIndexUsageTracker

packages/db/tests/query/indexes.test.ts:44–132  ·  view source on GitHub ↗
(collection: any)

Source from the content-addressed store, hash-verified

42}
43
44function createIndexUsageTracker(collection: any): {
45 stats: IndexUsageStats
46 restore: () => void
47} {
48 const stats: IndexUsageStats = {
49 rangeQueryCalls: 0,
50 fullScanCalls: 0,
51 indexesUsed: [],
52 queriesExecuted: [],
53 }
54
55 // Track rangeQuery calls on index objects (index usage)
56 const originalIndexes = new Map()
57
58 // Mock the indexes getter to intercept index access
59 const originalIndexesGetter = Object.getOwnPropertyDescriptor(
60 Object.getPrototypeOf(collection),
61 `indexes`,
62 )?.get
63 Object.defineProperty(collection, `indexes`, {
64 get: function () {
65 const indexes = originalIndexesGetter?.call(collection) || new Map()
66
67 // Mock each index's rangeQuery method
68 for (const [indexId, index] of indexes.entries()) {
69 if (!originalIndexes.has(indexId)) {
70 const originalLookup = index.lookup
71 originalIndexes.set(indexId, originalLookup)
72
73 index.lookup = function (operation: string, value: any) {
74 stats.rangeQueryCalls++
75 stats.indexesUsed.push(indexId)
76 stats.queriesExecuted.push({
77 type: `index`,
78 operation,
79 field: index.expression?.path?.join(`.`),
80 value,
81 })
82 return originalLookup.call(this, operation, value)
83 }
84 }
85 }
86
87 return indexes
88 },
89 configurable: true,
90 })
91
92 // Track full scan calls (entries() iteration)
93 const originalEntries = collection.entries
94 collection.entries = function* () {
95 // Only count as full scan if we're in a filtering context
96 // Check the call stack to see if we're inside createFilterFunction
97 const stack = new Error().stack || ``
98 if (
99 stack.includes(`createFilterFunction`) ||
100 stack.includes(`currentStateAsChanges`)
101 ) {

Callers 2

withIndexTrackingFunction · 0.70
indexes.test.tsFile · 0.70

Calls 4

entriesMethod · 0.45
hasMethod · 0.45
setMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected