Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

distinct.ts

104 lines3.1 KB

1import { DifferenceStreamWriter, UnaryOperator } from '../graph.js'
2import { StreamBuilder } from '../d2.js'
3import { hash } from '../hashing/index.js'
4import { MultiSet } from '../multiset.js'
5import type { Hash } from '../hashing/index.js'
6import type { DifferenceStreamReader } from '../graph.js'
7import type { IStreamBuilder, KeyValue } from '../types.js'
8​
9type Multiplicity = number
10​
11type GetValue<T> = T extends KeyValue<any, infer V> ? V : never
12​
13/**
14 * Operator that removes duplicates
15 */
16export class DistinctOperator<
17 T extends KeyValue<any, any>,
18> extends UnaryOperator<T, KeyValue<number, GetValue<T>>> {
19 #by: (value: T) => any
20 #values: Map<Hash, Multiplicity> // keeps track of the number of times each value has been seen
21​
22 constructor(
23 id: number,
24 input: DifferenceStreamReader<T>,
25 output: DifferenceStreamWriter<KeyValue<number, GetValue<T>>>,
26 by: (value: T) => any = (value: T) => value,
27 ) {
28 super(id, input, output)
29 this.#by = by
30 this.#values = new Map()
31 }
32​
33 run(): void {
34 const updatedValues = new Map<Hash, [Multiplicity, T]>()
35​
36 // Compute the new multiplicity for each value
37 for (const message of this.inputMessages()) {
38 for (const [value, diff] of message.getInner()) {
39 const hashedValue = hash(this.#by(value))
40​
41 const oldMultiplicity =
42 updatedValues.get(hashedValue)?.[0] ??
43 this.#values.get(hashedValue) ??
44 0
45 const newMultiplicity = oldMultiplicity + diff
46 updatedValues.set(hashedValue, [newMultiplicity, value])
47 }
48 }
49​
50 const result: Array<[KeyValue<number, GetValue<T>>, number]> = []
51​
52 // Check which values became visible or disappeared
53 for (const [
54 hashedValue,
55 [newMultiplicity, value],
56 ] of updatedValues.entries()) {
57 const oldMultiplicity = this.#values.get(hashedValue) ?? 0
58​
59 if (newMultiplicity === 0) {
60 this.#values.delete(hashedValue)
61 } else {
62 this.#values.set(hashedValue, newMultiplicity)
63 }
64​
65 if (oldMultiplicity <= 0 && newMultiplicity > 0) {
66 // The value wasn't present in the stream
67 // but with this change it is now present in the stream
68 result.push([[hash(this.#by(value)), value[1]], 1])
69 } else if (oldMultiplicity > 0 && newMultiplicity <= 0) {
70 // The value was present in the stream
71 // but with this change it is no longer present in the stream
72 result.push([[hash(this.#by(value)), value[1]], -1])
73 }
74 }
75​
76 if (result.length > 0) {
77 this.output.sendData(new MultiSet(result))
78 }
79 }
81​
82/**
83 * Removes duplicate values
84 */
85export function distinct<T extends KeyValue<any, any>>(
86 by: (value: T) => any = (value: T) => value,
87) {
88 return (
89 stream: IStreamBuilder<T>,
90 ): IStreamBuilder<KeyValue<number, GetValue<T>>> => {
91 const output = new StreamBuilder<KeyValue<number, GetValue<T>>>(
92 stream.graph,
93 new DifferenceStreamWriter<KeyValue<number, GetValue<T>>>(),
94 )
95 const operator = new DistinctOperator<T>(
96 stream.graph.getNextOperatorId(),
97 stream.connectReader(),
98 output.writer,
99 by,
100 )
101 stream.graph.addOperator(operator)
102 return output
103 }