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

Method delete

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

* Deletes a value that may or may not be in the topK. * IMPORTANT: this assumes that the value is present in the collection * if it's not the case it will remove the element * that is on the position where the provided `value` would be.

(value: V)

Source from the content-addressed store, hash-verified

202 * that is on the position where the provided `value` would be.
203 */
204 delete(value: V): TopKChanges<V> {
205 const result: TopKChanges<V> = { moveIn: null, moveOut: null }
206
207 // Lookup delete position
208 const index = this.#findIndex(value)
209 // Remove the value at that position
210 const [removedElem] = this.#sortedValues.splice(index, 1)
211
212 // Check if the topK changed
213 if (index < this.#topKEnd) {
214 // The removed element is either before the top K or within the top K
215 // If it is before the top K then the first element of the topK moves out of the topK
216 // If it is within the top K then the removed element moves out of the topK
217 result.moveOut = removedElem!
218 if (index < this.#topKStart) {
219 // The removed element is before the topK
220 // so actually, the first element of the topK moves out of the topK
221 // and not the element that we removed
222 // The first element of the topK is now at index topKStart - 1
223 // since we removed an element before the topK
224 const moveOutIndex = this.#topKStart - 1
225 if (moveOutIndex < this.#sortedValues.length) {
226 result.moveOut = this.#sortedValues[moveOutIndex]!
227 } else {
228 // No value is moving out of the topK
229 // because there are no elements in the topK
230 result.moveOut = null
231 }
232 }
233
234 // Since we removed an element that was before or in the topK
235 // the first element after the topK moved one position to the left
236 // and thus falls into the topK now
237 const moveInIndex = this.#topKEnd - 1
238 if (moveInIndex < this.#sortedValues.length) {
239 result.moveIn = this.#sortedValues[moveInIndex]!
240 }
241 }
242
243 return result
244 }
245
246 // TODO: see if there is a way to refactor the code for insert and delete in the topK above
247 // because they are very similar, one is shifting the topK window to the left and the other is shifting it to the right

Callers 15

ensureCollectionReadyMethod · 0.45
unloadSubsetMethod · 0.45
applyToCollectionMethod · 0.45
createWrappedSyncConfigFunction · 0.45
createRecordingAdapterFunction · 0.45
persisted.test.tsFile · 0.45
addValueMethod · 0.45
addValueMethod · 0.45

Calls 1

#findIndexMethod · 0.95

Tested by 1

createRecordingAdapterFunction · 0.36