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

Method addValue

packages/db-ivm/src/indexes.ts:300–356  ·  view source on GitHub ↗

* This method adds a value to the index. * @param key - The key to add the value to. * @param valueTuple - The value tuple [value, multiplicity] to add to the index.

(key: TKey, valueTuple: SingleValue<TValue>)

Source from the content-addressed store, hash-verified

298 * @param valueTuple - The value tuple [value, multiplicity] to add to the index.
299 */
300 addValue(key: TKey, valueTuple: SingleValue<TValue>) {
301 const [value, multiplicity] = valueTuple
302 // If the multiplicity is 0, do nothing
303 if (multiplicity === 0) return
304
305 // Update consolidated multiplicity tracking
306 const newConsolidatedMultiplicity =
307 (this.#consolidatedMultiplicity.get(key) || 0) + multiplicity
308 if (newConsolidatedMultiplicity === 0) {
309 this.#consolidatedMultiplicity.delete(key)
310 } else {
311 this.#consolidatedMultiplicity.set(key, newConsolidatedMultiplicity)
312 }
313
314 const mapOrSingleValue = this.#inner.get(key)
315
316 if (mapOrSingleValue === undefined) {
317 // First value for this key
318 this.#inner.set(key, valueTuple)
319 return
320 }
321
322 if (isSingleValue(mapOrSingleValue)) {
323 // Handle transition from single value to map
324 this.#handleSingleValueTransition(
325 key,
326 mapOrSingleValue,
327 value,
328 multiplicity,
329 )
330 return
331 }
332
333 if (mapOrSingleValue instanceof ValueMap) {
334 // Handle existing ValueMap
335 const prefix = getPrefix<TValue, TPrefix>(value)
336 if (prefix !== NO_PREFIX) {
337 // Convert ValueMap to PrefixMap since we have a prefixed value
338 const prefixMap = new PrefixMap<TValue, TPrefix>()
339 prefixMap.set(NO_PREFIX, mapOrSingleValue)
340 prefixMap.set(prefix, valueTuple)
341 this.#inner.set(key, prefixMap)
342 } else {
343 // Add to existing ValueMap
344 const isEmpty = mapOrSingleValue.addValue(value, multiplicity)
345 if (isEmpty) {
346 this.#inner.delete(key)
347 }
348 }
349 } else {
350 // Handle existing PrefixMap
351 const isEmpty = mapOrSingleValue.addValue(value, multiplicity)
352 if (isEmpty) {
353 this.#inner.delete(key)
354 }
355 }
356 }
357

Callers 5

fromMultiSetsMethod · 0.95
appendMethod · 0.95
addValueMethod · 0.45
runMethod · 0.45
indexes.test.tsFile · 0.45

Calls 6

isSingleValueFunction · 0.85
getPrefixFunction · 0.85
getMethod · 0.45
deleteMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected