Repository navigation
Conversation
`Index.add(None, vectors)` numbered new vectors from `len(index)`. After a `remove` or after explicit keys at or above `len`, that range reuses keys already in the index: a regular index raises "Duplicate keys not allowed in high-level wrappers", and a `multi=True` index silently files the new vector under another vector's key. When the default range collides with a present key, keys now continue past the largest key in the index. The append-only path keeps its `len`-based keys. A `ValueError` asks for explicit keys when no room is left below the `uint64` limit.
Member
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If you call
Index.add(None, vectors)after aremove(), or after adding an explicit key at or abovelen(index), the new keys collide with keys that are already in the index. A regular index raisesRuntimeError: Duplicate keys not allowed in high-level wrappers. Amulti=Trueindex raises nothing: it stores the new vector under another vector's key, so laterget,countand search results mix up the two. The docstring allowskeys=None, andpython/scripts/join.pyuses it.This targets
main-v2. The rewrite now onmaintakes explicit keys ininsert, so generated keys exist only in the 2.x line, which the current release (2.26.6) ships.The cause is in
_add_to_compiled, which numbers new keys fromlen(compiled). After a removal,lenis smaller than the largest live key, so the range overlaps live keys. This code has been there since 835581a.Before (
usearch2.26.6, sameindex.pyasmain-v2):After:
flowchart LR subgraph Before A1["add(None, v)"] --> B1["keys = arange(len, len + n)"] --> C1["add_many: duplicate error, or a shared key when multi=True"] end subgraph After A2["add(None, v)"] --> B2["keys = arange(len, len + n)"] --> D2{"contains_many(keys).any()?"} D2 -- no --> C2["add_many, same keys as before"] D2 -- yes --> E2["keys = arange(max key + 1, ...), or ValueError at the uint64 limit"] --> C2 endChanges:
python/usearch/index.py: when thelen-based range hits a present key, keys continue after the largest key in the index. Append-only use keeps its current keys and pays only onecontains_manylookup. If no keys are left below theuint64limit, aValueErrorasks for explicit keys, so the range never wraps around to 0.python/scripts/test_index.py:test_index_generated_keys_are_unique, parametrized overmultiand three collision cases (removed key, partly colliding batch, explicit key equal tolen), plus the near-uint64limit case.Concurrent
add(None)calls from several threads can still pick the same keys, both before and after this change. That would need state on the native side, so it is left out here.Tests, Windows 11, Python 3.11, prebuilt 2.26.6 extension with this
index.py:pytest python/scripts/test_index.py python/scripts/test_tooling.py python/scripts/test_sparse.py: 715 passed, 18 skipped. The 8 failures are intest_index_mini_floats_with_numkongand fail the same way without this change (the localnumkonghas noTensor).ruff check pythonandruff format --checkpass.