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

multiset.ts

221 lines6.6 KB

1import {
2 DefaultMap,
3 chunkedArrayPush,
4 globalObjectIdGenerator,
5} from './utils.js'
6import { hash } from './hashing/index.js'
7​
8export type MultiSetArray<T> = Array<[T, number]>
9export type KeyedData<T> = [key: string, value: T]
10​
11/**
12 * A multiset of data.
13 */
14export class MultiSet<T> {
15 #inner: MultiSetArray<T>
16​
17 constructor(data: MultiSetArray<T> = []) {
18 this.#inner = data
19 }
20​
21 toString(indent = false): string {
22 return `MultiSet(${JSON.stringify(this.#inner, null, indent ? 2 : undefined)})`
23 }
24​
25 toJSON(): string {
26 return JSON.stringify(Array.from(this.getInner()))
27 }
28​
29 static fromJSON<U>(json: string): MultiSet<U> {
30 return new MultiSet(JSON.parse(json))
31 }
32​
33 /**
34 * Apply a function to all records in the collection.
35 */
36 map<U>(f: (data: T) => U): MultiSet<U> {
37 return new MultiSet(
38 this.#inner.map(([data, multiplicity]) => [f(data), multiplicity]),
39 )
40 }
41​
42 /**
43 * Filter out records for which a function f(record) evaluates to False.
44 */
45 filter(f: (data: T) => boolean): MultiSet<T> {
46 return new MultiSet(this.#inner.filter(([data, _]) => f(data)))
47 }
48​
49 /**
50 * Negate all multiplicities in the collection.
51 */
52 negate(): MultiSet<T> {
53 return new MultiSet(
54 this.#inner.map(([data, multiplicity]) => [data, -multiplicity]),
55 )
56 }
57​
58 /**
59 * Concatenate two collections together.
60 */
61 concat(other: MultiSet<T>): MultiSet<T> {
62 const out: MultiSetArray<T> = []
63 chunkedArrayPush(out, this.#inner)
64 chunkedArrayPush(out, other.getInner())
65 return new MultiSet(out)
66 }
67​
68 /**
69 * Produce as output a collection that is logically equivalent to the input
70 * but which combines identical instances of the same record into one
71 * (record, multiplicity) pair.
72 */
73 consolidate(): MultiSet<T> {
74 // Check if this looks like a keyed multiset (first item is a tuple of length 2)
75 if (this.#inner.length > 0) {
76 const firstItem = this.#inner[0]?.[0]
77 if (Array.isArray(firstItem) && firstItem.length === 2) {
78 return this.#consolidateKeyed()
79 }
80 }
81​
82 // Fall back to original method for unkeyed data
83 return this.#consolidateUnkeyed()
84 }
85​
86 /**
87 * Private method for consolidating keyed multisets where keys are strings/numbers
88 * and values are compared by reference equality.
89 *
90 * This method provides significant performance improvements over the hash-based approach
91 * by using WeakMap for object reference tracking and avoiding expensive serialization.
92 *
93 * Special handling for join operations: When values are tuples of length 2 (common in joins),
94 * we unpack them and compare each element individually to maintain proper equality semantics.
95 */
96 #consolidateKeyed(): MultiSet<T> {
97 const consolidated = new Map<string, number>()
98 const values = new Map<string, T>()
99​
100 // Use global object ID generator for consistent reference equality
101​
102 /**
103 * Special handler for tuples (arrays of length 2) commonly produced by join operations.
104 * Unpacks the tuple and generates an ID based on both elements to ensure proper
105 * consolidation of join results like ['A', null] and [null, 'X'].
106 */
107 const getTupleId = (tuple: Array<any>): string => {
108 if (tuple.length !== 2) {
109 throw new Error(`Expected tuple of length 2`)
110 }
111 const [first, second] = tuple
112 return `${globalObjectIdGenerator.getStringId(first)}|${globalObjectIdGenerator.getStringId(second)}`
113 }
114​
115 // Process each item in the multiset
116 for (const [data, multiplicity] of this.#inner) {
117 // Verify this is still a keyed item (should be [key, value] pair)
118 if (!Array.isArray(data) || data.length !== 2) {
119 // Found non-keyed item, fall back to unkeyed consolidation
120 return this.#consolidateUnkeyed()
121 }
122​
123 const [key, value] = data
124​
125 // Verify key is string or number as expected for keyed multisets
126 if (typeof key !== `string` && typeof key !== `number`) {
127 // Found non-string/number key, fall back to unkeyed consolidation
128 return this.#consolidateUnkeyed()
129 }
130​
131 // Generate value ID with special handling for join tuples
132 let valueId: string
133 if (Array.isArray(value) && value.length === 2) {
134 // Special case: value is a tuple from join operations
135 valueId = getTupleId(value)
136 } else {
137 // Regular case: use reference/value equality
138 valueId = globalObjectIdGenerator.getStringId(value)
139 }
140​
141 // Create composite key and consolidate
142 const compositeKey = key + `|` + valueId
143 consolidated.set(
144 compositeKey,
145 (consolidated.get(compositeKey) || 0) + multiplicity,
146 )
147​
148 // Store the original data for the first occurrence
149 if (!values.has(compositeKey)) {
150 values.set(compositeKey, data as T)
151 }
152 }
153​
154 // Build result array, filtering out zero multiplicities
155 const result: MultiSetArray<T> = []
156 for (const [compositeKey, multiplicity] of consolidated) {
157 if (multiplicity !== 0) {
158 result.push([values.get(compositeKey)!, multiplicity])
159 }
160 }
161​
162 return new MultiSet(result)
163 }
164​
165 /**
166 * Private method for consolidating unkeyed multisets using the original approach.
167 */
168 #consolidateUnkeyed(): MultiSet<T> {
169 const consolidated = new DefaultMap<string | number, number>(() => 0)
170 const values = new Map<string, any>()
171​
172 let hasString = false
173 let hasNumber = false
174 let hasOther = false
175 for (const [data, _] of this.#inner) {
176 if (typeof data === `string`) {
177 hasString = true
178 } else if (typeof data === `number`) {
179 hasNumber = true
180 } else {
181 hasOther = true
182 break
183 }
184 }
185​
186 const requireJson = hasOther || (hasString && hasNumber)
187​
188 for (const [data, multiplicity] of this.#inner) {
189 const key = requireJson ? hash(data) : (data as string | number)
190 if (requireJson && !values.has(key as string)) {
191 values.set(key as string, data)
192 }
193 consolidated.update(key, (count) => count + multiplicity)
194 }
195​
196 const result: MultiSetArray<T> = []
197 for (const [key, multiplicity] of consolidated.entries()) {
198 if (multiplicity !== 0) {
199 const parsedKey = requireJson ? values.get(key as string) : key
200 result.push([parsedKey as T, multiplicity])
201 }
202 }
203​
204 return new MultiSet(result)
205 }
206​
207 extend(other: MultiSet<T> | MultiSetArray<T>): void {
208 const otherArray = other instanceof MultiSet ? other.getInner() : other
209 chunkedArrayPush(this.#inner, otherArray)
210 }
211​
212 add(item: T, multiplicity: number): void {
213 if (multiplicity !== 0) {
214 this.#inner.push([item, multiplicity])
215 }
216 }
217​
218 getInner(): MultiSetArray<T> {
219 return this.#inner
220 }