Commit 0b8e3ca
committed
fix(index): correct doc-index alignment for Fuse<string> with blank docs
Fuse<string> collections containing blank strings produced records
whose .i values drifted from _docs indices through add, removeAt, and
remove/removeAll. Search results were silently misaligned with the
source array — e.g. removeAt(1) on ["", "apple", "banana"] could
return "banana" as a match for "apple".
Root cause: FuseIndex.add / removeAt / removeAll conflated
records-array offset with source-array doc-index. Blank docs produce
no record (per _createStringRecord), so records.length < _docs.length
and the two index spaces diverge.
- FuseIndex.add(doc, docIndex): IndexRecord | null — explicit doc-index
parameter; returns the appended record or null when doc is blank.
- FuseIndex.removeAt(idx) — operates on doc-index, order-independent.
- FuseIndex.removeAll(indices) — filter by doc-index, shift surviving
.i by count of removed indices strictly less than it.
- Fuse.add now only feeds the inverted index when a record was
appended; previously could re-ingest the prior doc on add("") under
useTokenSearch, or crash on an all-blank collection.
- Fuse.removeAt(idx) validates idx (integer, in-range) atomically
before any mutation.
- FuseIndex.add / removeAt / removeAll gain cheap input guards for
direct callers.
API note: FuseIndex.add gained a required docIndex parameter. The
public Fuse.add(doc) signature is unchanged; only direct callers of
FuseIndex.add are affected and will throw INVALID_DOC_INDEX if called
without it. The alternative — optional docIndex with this.docs.length
fallback — silently produces wrong indices when the FuseIndex is
shared with a Fuse instance that ran .remove (the docs reference
detaches), which is exactly the bug class this fix eliminates.
Migration: if you serialized a Fuse<string> index via
getIndex().toJSON() from a pre-fix instance whose collection contained
blanks and ran any add/remove/removeAt/removeAll before serialization,
those records may misrepresent the source array — rebuild via
new Fuse(docs, options). Indexes from clean string collections are
unaffected.
Surfaced during fuse-swift port planning; fuse-swift v1 inherits the
doc-index-canonical model from this fix.1 parent 8e55cae commit 0b8e3ca
16 files changed
Lines changed: 785 additions & 176 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
303 | 304 | | |
304 | 305 | | |
305 | 306 | | |
306 | | - | |
307 | | - | |
308 | | - | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
309 | 316 | | |
310 | | - | |
311 | | - | |
312 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
313 | 322 | | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
314 | 326 | | |
315 | | - | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
316 | 331 | | |
317 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
318 | 335 | | |
319 | | - | |
320 | | - | |
321 | | - | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
322 | 351 | | |
323 | 352 | | |
324 | | - | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
325 | 360 | | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
330 | 381 | | |
331 | 382 | | |
332 | 383 | | |
| |||
335 | 386 | | |
336 | 387 | | |
337 | 388 | | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | 389 | | |
348 | 390 | | |
349 | 391 | | |
| |||
1256 | 1298 | | |
1257 | 1299 | | |
1258 | 1300 | | |
1259 | | - | |
1260 | | - | |
1261 | | - | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
1262 | 1307 | | |
1263 | 1308 | | |
1264 | 1309 | | |
| |||
1291 | 1336 | | |
1292 | 1337 | | |
1293 | 1338 | | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
| 1343 | + | |
| 1344 | + | |
1294 | 1345 | | |
1295 | 1346 | | |
1296 | 1347 | | |
| |||
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
301 | 302 | | |
302 | 303 | | |
303 | 304 | | |
304 | | - | |
305 | | - | |
306 | | - | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
307 | 314 | | |
308 | | - | |
309 | | - | |
310 | | - | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
311 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
312 | 324 | | |
313 | | - | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
314 | 329 | | |
315 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
316 | 333 | | |
317 | | - | |
318 | | - | |
319 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
320 | 349 | | |
321 | 350 | | |
322 | | - | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
323 | 358 | | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
328 | 379 | | |
329 | 380 | | |
330 | 381 | | |
| |||
333 | 384 | | |
334 | 385 | | |
335 | 386 | | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | 387 | | |
346 | 388 | | |
347 | 389 | | |
| |||
1254 | 1296 | | |
1255 | 1297 | | |
1256 | 1298 | | |
1257 | | - | |
1258 | | - | |
1259 | | - | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
1260 | 1305 | | |
1261 | 1306 | | |
1262 | 1307 | | |
| |||
1289 | 1334 | | |
1290 | 1335 | | |
1291 | 1336 | | |
| 1337 | + | |
| 1338 | + | |
| 1339 | + | |
| 1340 | + | |
| 1341 | + | |
| 1342 | + | |
1292 | 1343 | | |
1293 | 1344 | | |
1294 | 1345 | | |
| |||
0 commit comments