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
//! The search service around the library: named indexes and subsets loaded from snapshot
//! directories, searched with the options and answered with the result objects of the service
//! contract, within limits checked before any work starts.
//!
//! It is the part `legit-hamming query` and `legit-hamming serve` share, with no Redis in it: the
//! command line and the stream worker give the same bytes for the same options and snapshot,
//! because both call [`Service::search`]. The crate's README, "Running it as a service", states
//! every field.
//!
//! - A [`Catalog`] holds the indexes and subsets named on the command line, each a directory of
//!   snapshot directories and a one-line `CURRENT` file naming the one to serve.
//!   [`Catalog::refresh`] reads each `CURRENT` and loads a new snapshot beside the one served,
//!   checking every checksum, then swaps it in; searches already running finish on the one they
//!   started on.
//! - A [`Service`] answers a task's `options` against the catalog, as a [`Lane::Fast`] request or
//!   a [`Lane::Bulk`] one, within its [`Limits`].
//! - With a [`LoadConfig::state_dir`], each index keeps an ordered change log ([`log`]):
//!   [`Service::change`] applies an `append`, `replace` or `remove` task, writing it to the log and
//!   making it durable before it is searchable and answered with its `seq`, and every snapshot
//!   swapped in has the changes logged above its `covers_seq` replayed over it. A search is exact
//!   for the snapshot and the `seq` it names.
//!
//! ```
//! use std::sync::Arc;
//!
//! use legit_hamming::service::{Catalog, Lane, LoadConfig, Service, SetSpec, Limits};
//! use legit_hamming::{Records, snapshot};
//!
//! let dir = std::env::temp_dir().join(format!("legit-hamming-service-doc-{}", std::process::id()));
//! let records = Records::from_records([(17, 0x9f3a_0000_0000_00ffu64), (40, 0x9f3a_0000_0000_00fe)])?;
//! snapshot::write(&dir.join("first"), &records, 1_000)?;
//! std::fs::write(dir.join("CURRENT"), "first\n")?;
//!
//! let catalog = Catalog::new(vec![SetSpec::index("images", &dir)], LoadConfig::default())?;
//! catalog.refresh(&mut |_| {});
//! let service = Service::new(Arc::new(catalog), Limits::default(), 1);
//!
//! let options = r#"{"format":1,"index":"images","width":64,"radius":1,"values":["9f3a0000000000ff"]}"#;
//! let answer = service.search(options, Lane::Fast).map_err(|error| error.to_json())?;
//! assert!(answer.json.starts_with(r#"{"format":1,"index":"images","snapshot":"first","seq":0,"exact":true"#));
//! assert!(answer.json.contains(r#"{"id":17,"value":"9f3a0000000000ff","distance":0}"#));
//! # std::fs::remove_dir_all(&dir).ok();
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

mod answer;
mod catalog;
mod error;
mod limits;
pub mod log;
mod memory;
mod request;
mod set;
mod time;

pub use answer::{Answer, Counts, Service};
pub use catalog::{Catalog, Event, LoadConfig, SetSpec, TableMemory, Totals, Verified, verify_snapshot};
pub use error::{CONFLICT_IDS, ErrorType, SUPPORTED_MODELS, ServiceError};
pub use limits::{Lane, Limits};
pub use memory::{available_memory, process_rss};
pub use request::{is_valid_name, shorten};
pub use time::rfc3339;

/// The version of the contract's `options` and `result` objects this service reads and writes:
/// `1`. A request's `options` must name it, and every result carries it.
pub const FORMAT: u32 = 1;

/// The name of the one-line file in an index or subset directory that names the snapshot to serve.
pub const CURRENT: &str = "CURRENT";