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

indexes.test.ts

172 lines5.0 KB

1import { beforeEach, describe, expect, test } from "vitest"
2import { Index } from "../src/indexes.js"
3​
4describe(`Index`, () => {
5 let index: Index<string, number>
6 let createIndex: (name: string) => Index<string, number>
7​
8 beforeEach(() => {
9 createIndex = (_name: string) => new Index()
10 index = new Index()
11 })
12​
13 describe(`basic operations`, () => {
14 test(`should add and get values`, () => {
15 index.addValue(`key1`, [10, 1])
16 index.addValue(`key1`, [20, 2])
17​
18 const result = index.get(`key1`)
19 expect(result).toEqual([
20 [10, 1],
21 [20, 2],
22 ])
23 })
24​
25 test(`should return empty array for non-existent key`, () => {
26 const result = index.get(`nonexistent`)
27 expect(result).toEqual([])
28 })
29​
30 test(`should handle zero multiplicities`, () => {
31 index.addValue(`key1`, [10, 1])
32 index.addValue(`key1`, [10, -1])
33​
34 // Value is compacted on the fly
35 const result = index.get(`key1`)
36 expect(result).toEqual([])
37 })
38 })
39​
40 describe(`append`, () => {
41 test(`should append data from another index`, () => {
42 const other = createIndex(`other`)
43​
44 index.addValue(`key1`, [10, 1])
45 other.addValue(`key1`, [20, 1])
46 other.addValue(`key2`, [30, 1])
47​
48 index.append(other)
49​
50 expect(index.get(`key1`)).toEqual([
51 [10, 1],
52 [20, 1],
53 ])
54 expect(index.get(`key2`)).toEqual([[30, 1]])
55 })
56​
57 test(`should combine multiplicities for same values`, () => {
58 const other = createIndex(`other`)
59​
60 index.addValue(`key1`, [10, 2])
61 other.addValue(`key1`, [10, 3])
62​
63 index.append(other)
64​
65 expect(index.get(`key1`)).toEqual([[10, 5]])
66 })
67 })
68​
69 describe(`join`, () => {
70 test(`should join two indexes`, () => {
71 const other = createIndex(`other`)
72​
73 index.addValue(`key1`, [10, 2])
74 other.addValue(`key1`, [20, 3])
75​
76 const result = index.join(other)
77 const entries = result.getInner()
78 expect(entries).toHaveLength(1)
79 expect(entries[0]).toEqual([[`key1`, [10, 20]], 6])
80 })
81​
82 test(`should return empty multiset when no matching keys`, () => {
83 const other = createIndex(`other`)
84​
85 index.addValue(`key1`, [10, 1])
86 other.addValue(`key2`, [20, 1])
87​
88 const result = index.join(other)
89 expect(result.getInner()).toEqual([])
90 })
91​
92 test(`should handle multiple values with same key`, () => {
93 const other = createIndex(`other`)
94​
95 index.addValue(`key1`, [10, 2])
96 index.addValue(`key1`, [20, 3])
97 other.addValue(`key1`, [30, 1])
98 other.addValue(`key1`, [40, 2])
99​
100 const result = index.join(other)
101 const entries = result.getInner()
102 expect(entries).toHaveLength(4)
103 expect(entries).toContainEqual([[`key1`, [10, 30]], 2])
104 expect(entries).toContainEqual([[`key1`, [10, 40]], 4])
105 expect(entries).toContainEqual([[`key1`, [20, 30]], 3])
106 expect(entries).toContainEqual([[`key1`, [20, 40]], 6])
107 })
108​
109 test(`should handle multiple keys`, () => {
110 const other = createIndex(`other`)
111​
112 index.addValue(`key1`, [10, 2])
113 index.addValue(`key2`, [20, 3])
114 other.addValue(`key1`, [30, 1])
115 other.addValue(`key2`, [40, 2])
116​
117 const result = index.join(other)
118 const entries = result.getInner()
119 expect(entries).toHaveLength(2)
120 expect(entries).toContainEqual([[`key1`, [10, 30]], 2])
121 expect(entries).toContainEqual([[`key2`, [20, 40]], 6])
122 })
123​
124 test(`should handle zero multiplicities correctly`, () => {
125 const other = createIndex(`other`)
126​
127 index.addValue(`key1`, [10, 0])
128 other.addValue(`key1`, [20, 2])
129​
130 const result = index.join(other)
131 expect(result.getInner()).toEqual([])
132 })
133 })
134​
135 test(`should track and compact only changed keys`, () => {
136 // Add values to different keys
137 index.addValue(`key1`, [10, 1])
138 index.addValue(`key1`, [10, -1]) // This should cancel out when compacted
139 index.addValue(`key2`, [20, 1])
140 index.addValue(`key3`, [30, 1])
141​
142 // Create another index that we'll append from
143 const other = createIndex(`other`)
144 other.addValue(`key4`, [40, 1])
145 other.addValue(`key4`, [40, -1]) // This should cancel out when compacted
146 other.addValue(`key5`, [50, 1])
147​
148 // Append should also track changed keys
149 index.append(other)
150​
151 // Values should be consolidated and zero-multiplicity entries removed
152 expect(index.get(`key1`)).toEqual([]) // Cancelled out
153 expect(index.get(`key2`)).toEqual([[20, 1]])
154 expect(index.get(`key3`)).toEqual([[30, 1]])
155 expect(index.get(`key4`)).toEqual([]) // Cancelled out
156 expect(index.get(`key5`)).toEqual([[50, 1]])
157​
158 // Add more values
159 index.addValue(`key2`, [25, 1])
160 index.addValue(`key6`, [60, 1])
161​
162 expect(index.get(`key2`)).toEqual([
163 [20, 1],
164 [25, 1],
165 ]) // Both values preserved
166 expect(index.get(`key6`)).toEqual([[60, 1]])
167​
168 // Other keys should remain unchanged
169 expect(index.get(`key3`)).toEqual([[30, 1]])
170 expect(index.get(`key5`)).toEqual([[50, 1]])
171 })
172})