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

Method move

packages/db-ivm/src/operators/topKArray.ts:101–152  ·  view source on GitHub ↗

* Moves the topK window

({
    offset,
    limit,
  }: {
    offset?: number
    limit?: number
  })

Source from the content-addressed store, hash-verified

99 * Moves the topK window
100 */
101 move({
102 offset,
103 limit,
104 }: {
105 offset?: number
106 limit?: number
107 }): TopKMoveChanges<V> {
108 const oldOffset = this.#topKStart
109 const oldLimit = this.#topKEnd - this.#topKStart
110
111 // `this.#topKEnd` can be `Infinity` if it has no limit
112 // but `diffHalfOpen` expects a finite range
113 // so we restrict it to the size of the topK if topKEnd is infinite
114 const oldRange: HRange = [
115 this.#topKStart,
116 this.#topKEnd === Infinity ? this.#topKStart + this.size : this.#topKEnd,
117 ]
118
119 this.#topKStart = offset ?? oldOffset
120 this.#topKEnd = this.#topKStart + (limit ?? oldLimit) // can be `Infinity` if limit is `Infinity`
121
122 // Also handle `Infinity` in the newRange
123 const newRange: HRange = [
124 this.#topKStart,
125 this.#topKEnd === Infinity
126 ? Math.max(this.#topKStart + this.size, oldRange[1]) // since the new limit is Infinity we need to take everything (so we need to take the biggest (finite) topKEnd)
127 : this.#topKEnd,
128 ]
129 const { onlyInA, onlyInB } = diffHalfOpen(oldRange, newRange)
130
131 const moveIns: Array<IndexedValue<V>> = []
132 onlyInB.forEach((index) => {
133 const value = this.#sortedValues[index]
134 if (value) {
135 moveIns.push(value)
136 }
137 })
138
139 const moveOuts: Array<IndexedValue<V>> = []
140 onlyInA.forEach((index) => {
141 const value = this.#sortedValues[index]
142 if (value) {
143 moveOuts.push(value)
144 }
145 })
146
147 // It could be that there are changes (i.e. moveIns or moveOuts)
148 // but that the collection is lazy so we don't have the data yet that needs to move in/out
149 // so `moveIns` and `moveOuts` will be empty but `changes` will be true
150 // this will tell the caller that it needs to run the graph to load more data
151 return { moveIns, moveOuts, changes: onlyInA.length + onlyInB.length > 0 }
152 }
153
154 insert(value: V): TopKChanges<V> {
155 const result: TopKChanges<V> = { moveIn: null, moveOut: null }

Callers 2

#moveTopKMethod · 0.45
moveTopKMethod · 0.45

Calls 2

diffHalfOpenFunction · 0.85
forEachMethod · 0.45

Tested by

no test coverage detected