Skip to main content

legit_hamming/
lib.rs

1//! Exact Hamming radius search over 64-bit and 256-bit hashes held in memory: every stored record
2//! within distance `d` of a query, with its distance, and never a match missed.
3//!
4//! A plain Rust library with no framework dependency. A set of [`Records`] holds fixed-width
5//! values, `u64` or `[u64; 4]`, each with an application's 64-bit id or without. Several variants
6//! search it behind one trait, [`Search`], and every one returns exactly the matches of the plainest
7//! of them:
8//!
9//! - [`Planned`]: the one to use. It owns the records and, within a memory budget, multi-index
10//!   tables over them, and answers each query by a scan or a probe of the tables, whichever its
11//!   estimate says costs less;
12//! - [`Scan`]: a flat scan on a code path chosen at run time from what the CPU has ([`Isa`]):
13//!   `popcnt`, AVX2, AVX-512 or NEON;
14//! - [`MultiIndex`]: multi-index hashing with tables of 16-bit chunks, sized from the guarantee
15//!   `m(r + 1) − 1`;
16//! - [`Reference`]: the plainest scan, which every other variant is checked against;
17//! - [`Mapped`]: a [snapshot] file searched in place, mapped into memory.
18//!
19//! [`search`] and [`search_batch`] run one, on as many threads as asked, and return [`Found`]
20//! lists sorted by distance and then id, or position for a set without ids. A caller's limit keeps
21//! the nearest matches, never the first found, a result says when anything cut it short, and
22//! [`Found::plan`] says how a planned query ran. [`neighbour_table`] measures how many records sit
23//! at each distance from a typical one.
24//!
25//! ```
26//! use std::sync::Arc;
27//!
28//! use legit_hamming::{Options, Planned, Records, search};
29//!
30//! let records = Records::from_records([(1, 0b1011u64), (2, 0b1000), (5, u64::MAX)])?;
31//! let index = Planned::new(Arc::new(records), 64 << 20, 1);
32//!
33//! let found = search(&index, 0b1001, &Options::new(1));
34//! let ids: Vec<_> = found.matches.iter().map(|m| (m.id, m.distance)).collect();
35//! assert_eq!(ids, [(Some(1), 1), (Some(2), 1)]);
36//! assert!(found.is_complete());
37//! # Ok::<(), legit_hamming::Error>(())
38//! ```
39//!
40//! The [`service`] module puts named indexes and subsets loaded from snapshot directories behind
41//! a search service, for the `legit-hamming` program's `query` command and, with the
42//! `serve` feature, its `serve` worker on Redis Streams, in the `serve` module.
43//!
44//! `unsafe` code is confined to the vector kernels, behind a safe wrapper that checks the CPU at
45//! run time and falls back to the scalar path, and the memory map, whose constructors are `unsafe
46//! fn`s: a map is sound only while no process changes its files, which the caller promises. Every
47//! other path, the service and the `verify` command included, reads files into memory it owns; the
48//! rest of the crate denies `unsafe`.
49
50#![deny(unsafe_code)]
51
52mod cpu;
53mod error;
54mod kernel;
55mod mapped;
56mod multi_index;
57mod neighbours;
58mod planned;
59mod records;
60mod scan;
61mod search;
62#[cfg(feature = "serve")]
63pub mod serve;
64pub mod service;
65pub mod snapshot;
66mod value;
67
68pub use cpu::{Cpu, Isa};
69pub use error::{Error, Result};
70pub use kernel::Hit;
71pub use mapped::{Mapped, Verify};
72pub use multi_index::{MultiIndex, chunk_radii, guaranteed_radius, probes};
73pub use neighbours::{NeighbourTable, neighbour_table};
74pub use planned::Planned;
75pub use records::Records;
76pub use scan::{Reference, Scan};
77pub use search::{
78    Cut, Estimate, Found, HitList, Match, Memory, Options, Plan, Planning, Search, Share, Stop, Strategy, TRIM_RUN,
79    max_hits, search, search_batch, search_batch_excluding,
80};
81pub use value::Value;
82
83/// The README's examples, run as tests.
84#[doc = include_str!("../README.md")]
85#[cfg(doctest)]
86pub struct ReadmeDoctests;