Expand description
One implementation of the legit_hashes definitions for callers in any language: requests in
and results out as JSON lines, from a command line or, with the serve feature, Redis Streams.
The legit-hasher binary is a thin shell around this library. The core is hash, one
Request in and one Outcome out, which never panics and turns every failure into an
error result. run drives a batch of requests on a pool of threads with bounded memory,
skipping those an earlier run’s output already answers (Finished). distances compares
stored hashes and signatures, and Info reports the definitions, the limits and the CPU.
use legit_hasher::{Request, Settings, Status, hash};
let line = br#"{"id": "greeting", "text": "hello there, world",
"options": {"format": 1, "definitions": [{"id": "minhash-v1", "shingle": 5, "band_keys": 16}]}}"#;
let request = Request::from_json(line).map_err(|outcome| outcome.to_json())?;
let outcome = hash(&request, Settings::default());
assert_eq!(outcome.status, Status::Success);
let result = &outcome.hashes.as_ref().expect("a successful result has hashes")[0];
assert!(result.value.as_deref().is_some_and(|value| value.starts_with("minhash-v1:")));
assert_eq!(result.band_keys.as_ref().map(Vec::len), Some(16));A definition’s output is the same on every machine. The runner detects the CPU only to report it; nothing it detects changes a hash.
Modules§
- serve
legit-hasher serve: the runner as a worker on the Redis Streams work contract oflegit_streams, so it can run on another machine than the application that wants hashes.
Structs§
- Comparison
- The comparison of one pair, as one JSON line.
- Cpu
- The CPU, as the standard library detects it at run time.
- Decode
Facts - How the bytes became pixels.
- Definition
Failure - A definition-level failure: the reason one entry of
definitionsgives no hash. - Definition
Info - One definition as
infolists it. - Entry
- One entry of
definitions: the id as given, and the plan or the reason there’s none. - Finished
- The requests an earlier run already answered, by id, read from its output file.
- Hash
Outcome - One entry of a result’s
hashes, in the order of the request’sdefinitions. - Info
- The report
legit-hasher infoprints, as JSON. - Limits
- The limits, in bytes and pixels.
- Options
- A request’s
options: which definitions to compute, and how. - Outcome
- The result of one request: one JSON line, with fields in a fixed order.
- Plan
- One definition of a request, with its parameters checked: what the runner computes.
- Request
- One request: an id, a source and the definitions to compute.
- RunConfig
- How a batch runs.
- Runner
- The program, hash library and decoder that made a result.
- Serve
Info - What
legit-hasher serveserves: the task kind, its streams and the fields of its tasks and results. - Settings
- Settings that apply to every request of a run.
- Source
Facts - What was hashed: the length and SHA-1 of an image source’s bytes.
- Summary
- The counts of a finished run.
- Timings
- Time spent on the request, in microseconds, reported with
--timings.
Enums§
- Definition
- A
legit-hashesdefinition the runner computes: a bit hash or a set signature. - Definition
Error - A definition-level error code: one entry of
definitionsgave no hash, and the others stand. - Distance
Error - Why a pair wasn’t compared.
- Error
- What stopped a run.
- Id
- The caller’s id for a request, echoed back untouched in its result: a JSON string or integer.
- Input
- What a definition reads.
- Malloc
- How
runandserveset up the memory allocator, which changes their speed and the memory they keep, never a result. - Orientation
- Whether to turn an image by its EXIF orientation tag before hashing.
- Request
Error - A request-level error code: the request as a whole gave no hashes.
- Source
- Where a request’s bytes or text come from.
- Status
- Whether a request was handled.
- Tokens
- How a text definition splits its text into features.
Constants§
- DEFAULT_
MAX_ LINE_ BYTES - The longest request line read by default: 8 MiB, room for about 6 MiB of inline image.
- FORMAT
- The version of the request and result formats:
1. Every result line andinfocarry it, and a request’soptionsmust name it. A breaking change to either format raises it. - MALLOC_
VARIABLE - The environment variable that keeps glibc’s own allocator settings when set to
default. - MAX_
DEFINITIONS - The most entries a request’s
definitionsmay hold,16. - MAX_
METADATA_ BYTES - With
--max-pixelsset, the most bytes the decoder is allowed for an image’s metadata while its size is read, 16 MiB: chiefly a PNG’s compressed ICC profile, which it inflates. A PNG whose profile doesn’t inflate within it, or not at all, is refused astoo_largebefore it’s decoded, since the decoder would inflate it again within its own, larger limit. A JPEG’s metadata is at most the file. Without--max-pixelsthe size isn’t read first, and the decoder’s own limit,DECODE_MAX_ALLOC, holds its metadata as it does its pixels. - MAX_
OPTIONS_ BYTES - The most bytes a request’s
optionsmay take as JSON text,65536: on the command line, as compact JSON; in a task, itsoptionsfield as sent. - MAX_
SHINGLE - The widest character shingle a text definition takes,
64. - PEAK_
BYTES_ PER_ PIXEL - Bytes a decoded pixel can cost a worker at its peak: four of RGBA, one of luma and eight of
pdq-v1’s twof32planes, all held at once, and one more for what the decoder holds. - RUNNER
- The runner that made every result of this build. The two releases are the ones
Cargo.lockpins, and a test fails when they differ from it.
Functions§
- compare
- Compare the pair on one line:
{"id": ..., "a": "<stored form>", "b": "<stored form>"}. - distances
- Compare every pair in
input, writing one line each tooutput, in order. Blank lines are skipped. Each line is flushed as it is written, so a caller that sends a pair and waits for its reply gets it while the input stays open. - hash
- Compute every definition a request asks for, and say what was hashed.
- run
- Run a batch: read requests from
input, write one result line per request tooutput, and skip the requestsfinishedalready holds.
Type Aliases§
- Result
- The result of a fallible operation in this crate.