Skip to main content

Crate legit_hasher

Crate legit_hasher 

Source
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 of legit_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.
DecodeFacts
How the bytes became pixels.
DefinitionFailure
A definition-level failure: the reason one entry of definitions gives no hash.
DefinitionInfo
One definition as info lists 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.
HashOutcome
One entry of a result’s hashes, in the order of the request’s definitions.
Info
The report legit-hasher info prints, 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.
ServeInfo
What legit-hasher serve serves: the task kind, its streams and the fields of its tasks and results.
Settings
Settings that apply to every request of a run.
SourceFacts
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-hashes definition the runner computes: a bit hash or a set signature.
DefinitionError
A definition-level error code: one entry of definitions gave no hash, and the others stand.
DistanceError
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 run and serve set 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.
RequestError
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 and info carry it, and a request’s options must 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 definitions may hold, 16.
MAX_METADATA_BYTES
With --max-pixels set, 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 as too_large before 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-pixels the 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 options may take as JSON text, 65536: on the command line, as compact JSON; in a task, its options field 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 two f32 planes, 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.lock pins, 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 to output, 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 to output, and skip the requests finished already holds.

Type Aliases§

Result
The result of a fallible operation in this crate.