1
1
import { BTree } from "../utils/btree.js"
2
- import { defaultComparator } from "../utils/comparison.js"
2
+ import { defaultComparator , normalizeValue } from "../utils/comparison.js"
3
3
import { BaseIndex } from "./base-index.js"
4
4
import type { BasicExpression } from "../query/ir.js"
5
5
import type { IndexOperation } from "./base-index.js"
@@ -71,15 +71,18 @@ export class BTreeIndex<
71
71
)
72
72
}
73
73
74
+ // Normalize the value for Map key usage
75
+ const normalizedValue = normalizeValue ( indexedValue )
76
+
74
77
// Check if this value already exists
75
- if ( this . valueMap . has ( indexedValue ) ) {
78
+ if ( this . valueMap . has ( normalizedValue ) ) {
76
79
// Add to existing set
77
- this . valueMap . get ( indexedValue ) ! . add ( key )
80
+ this . valueMap . get ( normalizedValue ) ! . add ( key )
78
81
} else {
79
82
// Create new set for this value
80
83
const keySet = new Set < TKey > ( [ key ] )
81
- this . valueMap . set ( indexedValue , keySet )
82
- this . orderedEntries . set ( indexedValue , undefined )
84
+ this . valueMap . set ( normalizedValue , keySet )
85
+ this . orderedEntries . set ( normalizedValue , undefined )
83
86
}
84
87
85
88
this . indexedKeys . add ( key )
@@ -101,16 +104,19 @@ export class BTreeIndex<
101
104
return
102
105
}
103
106
104
- if ( this . valueMap . has ( indexedValue ) ) {
105
- const keySet = this . valueMap . get ( indexedValue ) !
107
+ // Normalize the value for Map key usage
108
+ const normalizedValue = normalizeValue ( indexedValue )
109
+
110
+ if ( this . valueMap . has ( normalizedValue ) ) {
111
+ const keySet = this . valueMap . get ( normalizedValue ) !
106
112
keySet . delete ( key )
107
113
108
114
// If set is now empty, remove the entry entirely
109
115
if ( keySet . size === 0 ) {
110
- this . valueMap . delete ( indexedValue )
116
+ this . valueMap . delete ( normalizedValue )
111
117
112
118
// Remove from ordered entries
113
- this . orderedEntries . delete ( indexedValue )
119
+ this . orderedEntries . delete ( normalizedValue )
114
120
}
115
121
}
116
122
@@ -195,7 +201,8 @@ export class BTreeIndex<
195
201
* Performs an equality lookup
196
202
*/
197
203
equalityLookup ( value : any ) : Set < TKey > {
198
- return new Set ( this . valueMap . get ( value ) ?? [ ] )
204
+ const normalizedValue = normalizeValue ( value )
205
+ return new Set ( this . valueMap . get ( normalizedValue ) ?? [ ] )
199
206
}
200
207
201
208
/**
@@ -206,8 +213,10 @@ export class BTreeIndex<
206
213
const { from, to, fromInclusive = true , toInclusive = true } = options
207
214
const result = new Set < TKey > ( )
208
215
209
- const fromKey = from ?? this . orderedEntries . minKey ( )
210
- const toKey = to ?? this . orderedEntries . maxKey ( )
216
+ const normalizedFrom = normalizeValue ( from )
217
+ const normalizedTo = normalizeValue ( to )
218
+ const fromKey = normalizedFrom ?? this . orderedEntries . minKey ( )
219
+ const toKey = normalizedTo ?? this . orderedEntries . maxKey ( )
211
220
212
221
this . orderedEntries . forRange (
213
222
fromKey ,
@@ -240,7 +249,7 @@ export class BTreeIndex<
240
249
const keysInResult : Set < TKey > = new Set ( )
241
250
const result : Array < TKey > = [ ]
242
251
const nextKey = ( k ?: any ) => this . orderedEntries . nextHigherKey ( k )
243
- let key = from
252
+ let key = normalizeValue ( from )
244
253
245
254
while ( ( key = nextKey ( key ) ) && result . length < n ) {
246
255
const keys = this . valueMap . get ( key )
@@ -266,7 +275,8 @@ export class BTreeIndex<
266
275
const result = new Set < TKey > ( )
267
276
268
277
for ( const value of values ) {
269
- const keys = this . valueMap . get ( value )
278
+ const normalizedValue = normalizeValue ( value )
279
+ const keys = this . valueMap . get ( normalizedValue )
270
280
if ( keys ) {
271
281
keys . forEach ( ( key ) => result . add ( key ) )
272
282
}
0 commit comments