legit-hamming 0.1.1

Exact Hamming radius search over 64-bit and 256-bit hashes in memory: a scan or multi-index hashing, chosen per query, checked against one reference, and a search service over snapshots
Documentation
//! Exact Hamming radius search over 64-bit and 256-bit hashes held in memory: every stored record
//! within distance `d` of a query, with its distance, and never a match missed.
//!
//! A plain Rust library with no framework dependency. A set of [`Records`] holds fixed-width
//! values, `u64` or `[u64; 4]`, each with an application's 64-bit id or without. Several variants
//! search it behind one trait, [`Search`], and every one returns exactly the matches of the plainest
//! of them:
//!
//! - [`Planned`]: the one to use. It owns the records and, within a memory budget, multi-index
//!   tables over them, and answers each query by a scan or a probe of the tables, whichever its
//!   estimate says costs less;
//! - [`Scan`]: a flat scan on a code path chosen at run time from what the CPU has ([`Isa`]):
//!   `popcnt`, AVX2, AVX-512 or NEON;
//! - [`MultiIndex`]: multi-index hashing with tables of 16-bit chunks, sized from the guarantee
//!   `m(r + 1) − 1`;
//! - [`Reference`]: the plainest scan, which every other variant is checked against;
//! - [`Mapped`]: a [snapshot] file searched in place, mapped into memory.
//!
//! [`search`] and [`search_batch`] run one, on as many threads as asked, and return [`Found`]
//! lists sorted by distance and then id, or position for a set without ids. A caller's limit keeps
//! the nearest matches, never the first found, a result says when anything cut it short, and
//! [`Found::plan`] says how a planned query ran. [`neighbour_table`] measures how many records sit
//! at each distance from a typical one.
//!
//! ```
//! use std::sync::Arc;
//!
//! use legit_hamming::{Options, Planned, Records, search};
//!
//! let records = Records::from_records([(1, 0b1011u64), (2, 0b1000), (5, u64::MAX)])?;
//! let index = Planned::new(Arc::new(records), 64 << 20, 1);
//!
//! let found = search(&index, 0b1001, &Options::new(1));
//! let ids: Vec<_> = found.matches.iter().map(|m| (m.id, m.distance)).collect();
//! assert_eq!(ids, [(Some(1), 1), (Some(2), 1)]);
//! assert!(found.is_complete());
//! # Ok::<(), legit_hamming::Error>(())
//! ```
//!
//! The [`service`] module puts named indexes and subsets loaded from snapshot directories behind
//! a search service, for the `legit-hamming` program's `query` command and, with the
//! `serve` feature, its `serve` worker on Redis Streams, in the `serve` module.
//!
//! `unsafe` code is confined to the vector kernels, behind a safe wrapper that checks the CPU at
//! run time and falls back to the scalar path, and the memory map, whose constructors are `unsafe
//! fn`s: a map is sound only while no process changes its files, which the caller promises. Every
//! other path, the service and the `verify` command included, reads files into memory it owns; the
//! rest of the crate denies `unsafe`.

#![deny(unsafe_code)]

mod cpu;
mod error;
mod kernel;
mod mapped;
mod multi_index;
mod neighbours;
mod planned;
mod records;
mod scan;
mod search;
#[cfg(feature = "serve")]
pub mod serve;
pub mod service;
pub mod snapshot;
mod value;

pub use cpu::{Cpu, Isa};
pub use error::{Error, Result};
pub use kernel::Hit;
pub use mapped::{Mapped, Verify};
pub use multi_index::{MultiIndex, chunk_radii, guaranteed_radius, probes};
pub use neighbours::{NeighbourTable, neighbour_table};
pub use planned::Planned;
pub use records::Records;
pub use scan::{Reference, Scan};
pub use search::{
    Cut, Estimate, Found, HitList, Match, Memory, Options, Plan, Planning, Search, Share, Stop, Strategy, TRIM_RUN,
    max_hits, search, search_batch, search_batch_excluding,
};
pub use value::Value;

/// The README's examples, run as tests.
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;