Mojo · Retrieval and Streaming

Retrieval and Streaming

from usearch import DenseVectors, DenseBatch, DenseDraws, … 3.0.0-rc.1 · 4c3e5023

get exports every entry stored under one key, one row per entry in insertion order, as DenseVectors, whose every row is exactly columns() values wide, as the engine reported them, however far apart the engine laid the rows.

var stored = index.get[DType.float32](Key(integer=42), arena)  # empty when the key is absent

A cursor walks the contents, writing each batch in the arena; next_keys walks the key column alone, and next_links lands the keys each entry links to beside it, while the vectors come from get. Without start keys it yields every entry as stored; cursor(starts) yields the starts’ own entries first, then the entries those link to breadth-first, each once.

var cursor = index.on(executor).cursor()
while True:
    var batch = cursor.next_links(1024, arena)  # empty once exhausted
    if len(batch) == 0:
        break
    print(batch.keys()[0], len(batch.links(0)))

A search stream is a resumable best-first traversal, so callers that post-filter aggressively pull until satisfied instead of guessing “k” upfront. No residency of the engine implements the stream yet, so it answers Status.UNSUPPORTED.

var stream = index.on(executor).search_stream(RowsView.of(query, 768))
while True:
    var pulled = stream.next(16, arena)  # one query of Matches, empty once drained
    if len(pulled.query_keys(0)) == 0:
        break

Cursors and streams open on the blocking receivers alone, the index and index.on(executor), and every pull blocks. Both borrow the index and the executor, so neither can be released while a cursor or a stream lives, and a drained one answers an empty view rather than raising.

§ Sampling

sample draws keys around focus, or across the whole index, each hanging from a shown or earlier draw and placed in three dimensions beside the shown keys already on screen, so a view grows one connected sample as it zooms. Equal seeds, shown keys and focus draw equally, and a larger wanted draws a superset.

var config = DenseSampleConfig(shown=shown, shown_coordinates=places, focus=near, seed=42)
var draws = index.on(executor).sample(256, arena, config)
var keys = draws.keys()
var entry_ordinals = draws.entry_ordinals()
var parents = draws.parents()
var coordinates = draws.coordinates()

API

StructDenseVectorsRows a call wrote into its arena - usearch_rows_view_t - read in place: the entries get exports, or the rows attend attends.3
struct DenseVectors[dtype: DType, origin: MutOrigin]

Rows are stride bytes apart and columns values wide, both as the engine reported them, so a padded row never reads as extra values.

Parameters

dtypeDTypecomptimeThe element type each row is read as.
originMutOrigincomptimeThe arena the rows live in, which this result borrows.

Fields

arenaPointer[Arena, origin]The arena holding the rows.
generationIntThe arena's call these rows came from, so a read after a later call is refused.
viewRowsViewArgumentsThe first row, the stride, the width and the kind, as the engine wrote them.
rowsIntRows written; zero when the key is absent.

Conforms to

Deinitable, Movable, Sized
__getitem__One row, borrowed from the arena this result reads. Binding-only.
def __getitem__(ref self, row: Int) -> Span[Scalar[dtype], origin_of(self)]

Parameters

rowIntWhich row, below len(self).

Returns

The row's elements - columns of them, or its packed bytes for a sub-byte kind.

Raises

Error with UNINITIALIZED once a later call rewound the arena, or DEVICE over VRAM.
__len__The rows written.
def __len__(self) -> Int

Returns

Zero when the key is absent.
columnsValues in each row, as the engine reported them.
def columns(self) -> Int

Returns

The width the rows were written at.
StructDenseBatchOne cursor pull, read in place from its arena: the keys, and the keys each links to when asked for.3
struct DenseBatch[origin: MutOrigin]

Parameters

originMutOrigincomptimeThe arena the batch lives in, which this result borrows.

Fields

arenaPointer[Arena, origin]The arena holding the batch.
generationIntThe arena's call this batch came from, so a read after a later call is refused.
keys_addressAddressThe first key, inside the arena.
linkedBoolWhether the pull exported links.
links_viewKeysTapeViewArgumentsThe keys each entry links to and their offsets, null for a pull of keys alone.
writtenIntEntries the pull wrote; zero when the walk is exhausted.

Conforms to

Deinitable, Movable, Sized
__len__The entries the pull wrote.
def __len__(self) -> Int

Returns

Zero when the walk is exhausted.
keysEvery entry's key, in walk order.
def keys(ref self) -> Span[Key, origin_of(self)]

Returns

The keys, borrowed from the arena this result reads.

Raises

Error with UNINITIALIZED once a later call rewound the arena, or DEVICE over VRAM.
StructDenseDrawsThe draws sample wrote into its arena, read in place: each draw's key, which of that key's entries it is, the shown or earlier draw it hangs from, and where it is placed.5
struct DenseDraws[origin: MutOrigin]

Parameters

originMutOrigincomptimeThe arena the draws live in, which this result borrows.

Fields

arenaPointer[Arena, origin]The arena holding the draws.
generationIntThe arena's call these draws came from, so a read after a later call is refused.
outputsDenseSampleOutputsWhere the engine wrote every output, inside the arena.

Conforms to

Deinitable, Movable, Sized
__len__The draws written.
def __len__(self) -> Int

Returns

Fewer than wanted only once the region is exhausted.
keysEvery draw's key.
def keys(ref self) -> Span[Key, origin_of(self)]

Returns

The keys, borrowed from the arena this result reads.

Raises

Error with UNINITIALIZED once a later call rewound the arena, or DEVICE over VRAM.
entry_ordinalsWhich of its key's entries each draw is, in the order get writes them.
def entry_ordinals(ref self) -> Span[UInt, origin_of(self)]

Returns

The entry ordinals, borrowed from the arena this result reads.

Raises

Error with UNINITIALIZED once a later call rewound the arena, or DEVICE over VRAM.
parentsEach draw's parent and its distance, one column per draw, a root holding none.
def parents(self) -> Matches[origin]

Returns

The parents, read in place from the same arena.
coordinatesWhere each draw is placed, one row of three values apiece.
def coordinates(self) -> DenseVectors[.float32, origin]

Returns

The places, read in place from the same arena.
StructDenseSampleConfigThe knobs of usearch_dense_sample_t; left alone, it draws across the whole index with nothing shown.1
struct DenseSampleConfig[shown_origin: ImmOrigin = ImmUntrackedOrigin, coordinates_origin: ImmOrigin = ImmUntrackedOrigin, focus_origin: ImmOrigin = ImmUntrackedOrigin]

Parameters

shown_originImmOrigincomptimeThe shown keys' origin, inferred where any are lent.
coordinates_originImmOrigincomptimeTheir coordinates' origin, inferred with them.
focus_originImmOrigincomptimeThe focus keys' origin, inferred where any are lent.

Fields

shownSpan[Key, shown_origin]The keys already shown, never drawn again; empty shows nothing yet.
shown_coordinatesSpan[Float32, coordinates_origin]Where each shown key sits, three values apiece.
focusSpan[Key, focus_origin]The keys to look around; empty looks around the whole index.
seedUInt64Equal seeds, shown keys and focus over an unchanged index draw equally.

Conforms to

Copyable, Deinitable, ImplicitlyCopyable, Movable
__init__Names only the knobs that differ from the defaults.
def __init__(
    out self,
    *,
    shown: Span[Key, shown_origin] = Span(),
    shown_coordinates: Span[Float32, coordinates_origin] = Span(),
    focus: Span[Key, focus_origin] = Span(),
    seed: UInt64 = UInt64(0)
)

Parameters

shownSpan[Key, shown_origin]The keys already shown, never drawn again; empty shows nothing yet.
shown_coordinatesSpan[Float32, coordinates_origin]Where each shown key sits, three values apiece.
focusSpan[Key, focus_origin]The keys to look around; empty looks around the whole index.
seedUInt64Equal seeds, shown keys and focus over an unchanged index draw equally.
StructDenseSearchStreamInitConfigThe knobs of usearch_dense_search_stream_init_t; zeroed ones derive what the C interface derives.1
struct DenseSearchStreamInitConfig

Fields

expansion_factorIntThe breadth of the candidates the walk explores; zero derives.

Conforms to

Copyable, Deinitable, ImplicitlyCopyable, Movable
__init__Names only the knobs that differ from the derived ones.
def __init__(out self, *, expansion_factor: Int = Int(0))

Parameters

expansion_factorIntThe breadth of the candidates the walk explores; zero derives.
StructDenseCursorA walk over the index contents - usearch_dense_cursor_t.4
struct DenseCursor[origin: ImmOrigin]

Export, migration, and visualization feed off it. It borrows the index and the executor its pulls run on, so neither can be released while it lives.

Parameters

originImmOrigincomptimeThe index and the executor, which the cursor borrows.

Fields

libraryLibraryThe loaded engine, held so the release can still reach usearch_dense_cursor_free.
indexAddressThe usearch_dense_index_t the cursor was opened on, which every pull names again.
executorAddressThe usearch_executor_t every pull runs on.
addressAddressThe usearch_dense_cursor_t this struct owns.

Conforms to

Deinitable, Movable
__init__Opens a cursor over index. Calls usearch_dense_cursor_init.
def __init__(out self, library: Library, index: Int, executor: Int, starts_count: Int, starts: Int)

Parameters

libraryLibraryThe loaded engine the cursor is opened through.
indexIntThe handle to scan.
executorIntThe executor every pull runs on.
starts_countIntThe start keys at starts; zero walks every entry as stored.
startsIntThe first start key, or NULL_ADDRESS for none.

Raises

Error if the residency has no cursor, as VRAM does not.
__deinit__Releases the cursor. Calls usearch_dense_cursor_free.
def __deinit__(deinit self)

The loader reference is surrendered only after the call: reading the entry point is the last use of library, so without the trailing move Mojo drops the last reference - and dlcloses the engine - between the load and the branch to it.

next_keysCopies up to wanted next keys into a lent arena, skipping their links. Calls usearch_dense_cursor_next.
def next_keys(self, wanted: Int, mut arena: Arena) -> DenseBatch[origin_of(arena)]

Parameters

wantedIntThe upper bound on keys this pull writes.
arenaArenamutThe arena to rewind and refill, which the batch borrows; a failed call leaves it valid.

Returns

The keys, read in place from the arena; an empty batch once the walk is exhausted.

Raises

Error if the engine refuses the copy.
StructDenseSearchStreamA resumable nearest-neighbor stream - usearch_dense_search_stream_t.3
struct DenseSearchStream[origin: ImmOrigin]

Best-first traversal is naturally pausable, so the stream keeps the walk's frontier and visited set between pulls. Callers that post-filter aggressively pull until satisfied instead of guessing "k" upfront. Ordering beyond the expansion horizon is best-effort nearest rather than globally exact.

Parameters

originImmOrigincomptimeThe index, the executor, and the query row, which the stream borrows.

Fields

libraryLibraryThe loaded engine, held so the release can still reach usearch_dense_search_stream_free.
indexAddressThe usearch_dense_index_t the stream was opened on, which every pull names again.
executorAddressThe usearch_executor_t every pull runs on.
addressAddressThe usearch_dense_search_stream_t this struct owns.

Conforms to

Deinitable, Movable
__init__Opens a stream over index for one query row. Calls usearch_dense_search_stream_init.
def __init__(
    out self,
    library: Library,
    index: Int,
    executor: Int,
    query: RowsViewArguments,
    expansion_factor: Int
)

Parameters

libraryLibraryThe loaded engine the stream is opened through.
indexIntThe handle to query.
executorIntThe executor every pull runs on.
queryRowsViewArgumentsThe single query row.
expansion_factorIntThe breadth of the candidates the walk explores; zero derives.

Raises

Error with UNSUPPORTED while no residency implements the stream.
__deinit__Releases the stream and the walk state it keeps. Calls usearch_dense_search_stream_free.
def __deinit__(deinit self)

The loader reference is surrendered only after the call: reading the entry point is the last use of library, so without the trailing move Mojo drops the last reference - and dlcloses the engine - between the load and the branch to it.

nextPulls up to wanted next-nearest matches into a lent arena, as the one query of a search. Calls usearch_dense_search_stream_next.
def next(self, wanted: Int, mut arena: Arena) -> Matches[origin_of(arena)]

Parameters

wantedIntThe upper bound on matches this pull writes.
arenaArenamutThe arena to rewind and refill, which the pull borrows; a failed call leaves it valid.

Returns

One query's matches, read in place from the arena; an empty query once the walk is drained.

Raises

Error if the engine refuses to advance.