@@ -4,6 +4,21 @@ import { BaseIndex } from "./base-index.js"
4
4
import type { BasicExpression } from "../query/ir.js"
5
5
import type { IndexOperation } from "./base-index.js"
6
6
7
+ /**
8
+ * Normalizes values for use as Map keys to ensure proper equality comparison
9
+ * For Date objects, uses timestamp. For other objects, uses JSON serialization.
10
+ */
11
+ function normalizeMapKey ( value : any ) : any {
12
+ if ( value instanceof Date ) {
13
+ return value . getTime ( )
14
+ }
15
+ if ( typeof value === `object` && value !== null ) {
16
+ // For other objects, use JSON serialization as a fallback
17
+ // This ensures objects with same content are treated as equal
18
+ return JSON . stringify ( value )
19
+ }
20
+ return value
21
+ }
7
22
/**
8
23
* Options for Ordered index
9
24
*/
@@ -71,14 +86,17 @@ export class BTreeIndex<
71
86
)
72
87
}
73
88
89
+ // Normalize the value for Map key usage
90
+ const normalizedValue = normalizeMapKey ( indexedValue )
91
+
74
92
// Check if this value already exists
75
- if ( this . valueMap . has ( indexedValue ) ) {
93
+ if ( this . valueMap . has ( normalizedValue ) ) {
76
94
// Add to existing set
77
- this . valueMap . get ( indexedValue ) ! . add ( key )
95
+ this . valueMap . get ( normalizedValue ) ! . add ( key )
78
96
} else {
79
97
// Create new set for this value
80
98
const keySet = new Set < TKey > ( [ key ] )
81
- this . valueMap . set ( indexedValue , keySet )
99
+ this . valueMap . set ( normalizedValue , keySet )
82
100
this . orderedEntries . set ( indexedValue , undefined )
83
101
}
84
102
@@ -101,13 +119,16 @@ export class BTreeIndex<
101
119
return
102
120
}
103
121
104
- if ( this . valueMap . has ( indexedValue ) ) {
105
- const keySet = this . valueMap . get ( indexedValue ) !
122
+ // Normalize the value for Map key usage
123
+ const normalizedValue = normalizeMapKey ( indexedValue )
124
+
125
+ if ( this . valueMap . has ( normalizedValue ) ) {
126
+ const keySet = this . valueMap . get ( normalizedValue ) !
106
127
keySet . delete ( key )
107
128
108
129
// If set is now empty, remove the entry entirely
109
130
if ( keySet . size === 0 ) {
110
- this . valueMap . delete ( indexedValue )
131
+ this . valueMap . delete ( normalizedValue )
111
132
112
133
// Remove from ordered entries
113
134
this . orderedEntries . delete ( indexedValue )
@@ -195,7 +216,8 @@ export class BTreeIndex<
195
216
* Performs an equality lookup
196
217
*/
197
218
equalityLookup ( value : any ) : Set < TKey > {
198
- return new Set ( this . valueMap . get ( value ) ?? [ ] )
219
+ const normalizedValue = normalizeMapKey ( value )
220
+ return new Set ( this . valueMap . get ( normalizedValue ) ?? [ ] )
199
221
}
200
222
201
223
/**
@@ -266,7 +288,8 @@ export class BTreeIndex<
266
288
const result = new Set < TKey > ( )
267
289
268
290
for ( const value of values ) {
269
- const keys = this . valueMap . get ( value )
291
+ const normalizedValue = normalizeMapKey ( value )
292
+ const keys = this . valueMap . get ( normalizedValue )
270
293
if ( keys ) {
271
294
keys . forEach ( ( key ) => result . add ( key ) )
272
295
}
0 commit comments