Swift · Filtered Search

Filtered Search

import USearch 3.0.0-rc.1 · 4c3e5023

The predicate runs on the executor’s threads, so it must be @Sendable and safe for concurrent use; an error it throws rejects that key. Non-matching entries still route the traversal, so recall does not collapse at low selectivity. DenseSearchConfig(traversal: .scan) skips the walk and scores every entry exactly, on RAM and NVMe handles, the fallback for a filter too selective for a walk to cross.

let even = try index.on(executor).search(queries, wanted: 10, arena: arena, config: DenseSearchConfig(
    predicate: { key in key.integerIdentity.map { $0 % 2 == 0 } ?? false }))

Every long verb’s config, and every lifecycle config that reports, takes a progress handler, called with the units finished so far and the units the call finishes if it runs to the end; returning .cancel stops it with cancelled wherever the verb can stop, which an insert cannot. A handler never runs on two threads at once, so it may carry a counter or a deadline without a lock, but it may run on whichever thread advanced the work, hence @Sendable. A predicate, handler or value lookup that throws never unwinds into C; the first error stops the call where it can and is thrown once the call is final, a USearchError as it was and any other as the cause of a cancelled one.

let deadline = ContinuousClock.now + .seconds(30)
try index.on(executor).refine(config: DenseRefineConfig(
    expansionFactor: 128,
    progress: { _, _ in ContinuousClock.now < deadline ? .continue : .cancel }))

API

Types

ProgressA periodic report of a long call's advance - units finished and units in total, in whatever the verb consumes - and the caller's chance to stop it.
Types.swift:507 ↗
typealias Progress = @Sendable (_ completed: Int, _ total: Int) throws -> Continuation

Never invoked concurrently, however many threads perform the work, so the handler needs no synchronization of its own; an error it throws never unwinds into the engine, but stops the call as .cancel would and is thrown once the call is final. It runs on whichever thread advanced the work, hence @Sendable.

EnumContinuationWhether the operation a progress handler is watching carries on or unwinds.
Types.swift:493 ↗
enum Continuation

Values

continueCarry on to the next unit of work.
cancelStop; what the finished increments leave behind is each verb's own contract.

Conforms to

Equatable, Hashable, Sendable