1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//! 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>>(())
//! ```
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use 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";