diff options
| author | Alexander_Kabui <alexanderkabui@proton.me> | 2026-11-17 12:11:19 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabui@proton.me> | 2026-11-17 14:06:50 +0300 |
| commit | f76043bf82b6c86b5e844b27b53e957d770aeb33 (patch) | |
| tree | 5b4257c2523410f077e9fbc4f71f3fd797360b9f | |
| parent | e13bbd27d0b7addd277d0f50d9e90fee2a936211 (diff) | |
| download | genenetwork4-search/extract-engine.tar.gz | |
* src/search (engine): no default arm, so a wrong name is refused. search/extract-engine
An unrecognised SEARCH_ENGINE used to fall through to the gn3 proxy, which turned a typo into a silently different engine. The arms are now explicit - "crate"/"native" for the in-process engine, "forward" for the proxy - and anything else returns the value it was given. src/search/native.rs is deleted; the former name resolves through the same arm.
| -rw-r--r-- | src/search/mod.rs | 20 | ||||
| -rw-r--r-- | src/search/native.rs | 43 | ||||
| -rw-r--r-- | tests/search_crate_engine.rs | 16 | ||||
| -rw-r--r-- | tests/search_native.rs | 18 | ||||
| -rw-r--r-- | tests/search_native_dev_corpus.rs | 4 | ||||
| -rw-r--r-- | tests/search_native_shapes.rs | 4 |
6 files changed, 44 insertions, 61 deletions
diff --git a/src/search/mod.rs b/src/search/mod.rs index f8797e7..3e2611e 100644 --- a/src/search/mod.rs +++ b/src/search/mod.rs @@ -86,14 +86,13 @@ //! impl is speculation). The overnight schedule overrides that: the //! [`Engine`] trait is defined so the native engine can land behind //! it. The gn3-forwarding implementation ([`gn3::Gn3`]) remains the -//! default backend; [`native::Native`] is opt-in via `SEARCH_ENGINE` +//! default backend; the in-process engine is opt-in via `SEARCH_ENGINE` //! (see [`search`]). use std::future::Future; pub mod gn3; pub mod gnindex; -pub mod native; /// Run a search through the configured engine: the gn3 forwarder by /// default, the native xapian engine when `SEARCH_ENGINE=native` @@ -106,9 +105,20 @@ pub async fn search( page: u64, ) -> Result<Vec<SearchHit>, SearchError> { match crate::config::Config::load().search_engine.as_str() { - "native" => native::Native.search(q, type_, limit, page).await, - "crate" => gnindex::CrateEngine::from_config().search(q, type_, limit, page).await, - _ => gn3::Gn3.search(q, type_, limit, page).await, + // The in-process engine, in the crate; `native` is its former name. + "crate" | "native" => { + gnindex::CrateEngine::from_config() + .search(q, type_, limit, page) + .await + } + // The gn3 proxy, asked over HTTP. Deleted together with gn3. + "forward" => gn3::Gn3.search(q, type_, limit, page).await, + // No default arm: an unrecognised value is a misconfiguration, and a + // silent fall-through would hide it behind whichever engine happened to + // be last in the match. + other => Err(SearchError::Internal(format!( + "SEARCH_ENGINE={other:?} is not an engine: expected \"crate\", \"native\" or \"forward\"" + ))), } } diff --git a/src/search/native.rs b/src/search/native.rs deleted file mode 100644 index 667e786..0000000 --- a/src/search/native.rs +++ /dev/null @@ -1,43 +0,0 @@ -//! The native [`Engine`]: a thin wrapper over the extracted engine. -//! -//! The engine itself — the `QueryParser` configuration and every -//! prefix/slot registration, the checked parse, the Hs/Mm and numeric-range -//! guards, `open_database` and its pre-checks, the full-match-set fetch and -//! the doc-data decode — now lives in the `gn-search` crate -//! (`gn-search/src/native.rs`; before the move this file was the whole -//! 1003-line engine, and that code is unchanged there). -//! -//! What is left here is the [`Engine`] impl and nothing else: it delegates -//! to [`CrateEngine`], so `SEARCH_ENGINE=native` and `SEARCH_ENGINE=crate` -//! are the same engine, reached through the same seam, with the same error -//! classes — including the unset-index `Internal` that names -//! `XAPIAN_DB_PATH`. They cannot diverge, and no behaviour above this -//! module learns that the engine moved. -//! -//! The async shell stays here rather than in the crate because xapian calls -//! are synchronous C++ work: [`CrateEngine`] keeps them off the runtime's -//! workers with `spawn_blocking` (the house pattern, cf. every -//! `spawn_blocking` handler in `src/routes`), and that is the only thing -//! this module adds. - -use super::gnindex::CrateEngine; -use super::{Engine, SearchError, SearchHit}; - -/// The native xapian search engine. Configure the index directory with -/// `XAPIAN_DB_PATH` and select it with `SEARCH_ENGINE=native`; the -/// default remains the gn3 forwarder (see [`crate::search::search`]). -pub struct Native; - -impl Engine for Native { - async fn search( - &self, - q: &str, - type_: &str, - limit: u64, - page: u64, - ) -> Result<Vec<SearchHit>, SearchError> { - CrateEngine::from_config() - .search(q, type_, limit, page) - .await - } -} diff --git a/tests/search_crate_engine.rs b/tests/search_crate_engine.rs index 446bbf5..5273d9c 100644 --- a/tests/search_crate_engine.rs +++ b/tests/search_crate_engine.rs @@ -124,3 +124,19 @@ fn a_crate_hit_deserialises_into_the_route_hit_without_losing_keys() { // twin that could drift from it. let _: Option<SearchError> = None; } + +#[tokio::test] +async fn an_unknown_engine_name_is_refused_by_name() { + // A misconfiguration must not fall through to whichever engine sits last in + // the match, so the dispatcher has no default arm and names the value. + unsafe { std::env::set_var("SEARCH_ENGINE", "cratee") }; + let error = genenetwork4::search::search("alcohol", "phenotype", 5, 1) + .await + .expect_err("an unknown engine name is refused"); + let text = format!("{error:?}"); + assert!( + text.contains("SEARCH_ENGINE") && text.contains("cratee"), + "the refusal names the setting and the value it was given: {text}" + ); + unsafe { std::env::set_var("SEARCH_ENGINE", "crate") }; +} diff --git a/tests/search_native.rs b/tests/search_native.rs index 6c8f58e..3b75687 100644 --- a/tests/search_native.rs +++ b/tests/search_native.rs @@ -14,7 +14,7 @@ //! development index is never a recorded-count source, per //! AMENDMENT B — counts are printed as reference data only). -use genenetwork4::search::{self as seam, Engine, SearchError, SearchHit, native::Native}; +use genenetwork4::search::{self as seam, Engine, SearchError, SearchHit, gnindex::CrateEngine}; /// The name of a result's variant, so a failed assertion can name what /// it got without requiring `SearchHit: Debug`. @@ -47,7 +47,7 @@ async fn native_engine_error_contract_and_per_page_invariant() { // carries the parser's own classification. set("XAPIAN_DB_PATH", ""); set("SEARCH_ENGINE", "native"); - let result = Native.search("alcohol AND", "phenotype", 5, 1).await; + let result = CrateEngine::from_config().search("alcohol AND", "phenotype", 5, 1).await; assert_eq!(variant_name(&result), "InvalidQuery"); let Err(SearchError::InvalidQuery { message, @@ -65,12 +65,12 @@ async fn native_engine_error_contract_and_per_page_invariant() { assert_eq!(variant_name(&result), "InvalidQuery"); // 3. An unset index path is a local misconfiguration: Internal. - let result = Native.search("alcohol", "phenotype", 5, 1).await; + let result = CrateEngine::from_config().search("alcohol", "phenotype", 5, 1).await; assert_eq!(variant_name(&result), "Internal"); // 4. An index path that cannot be opened: Unreachable. set("XAPIAN_DB_PATH", "/nonexistent/gn4-native-test-index"); - let result = Native.search("alcohol", "phenotype", 5, 1).await; + let result = CrateEngine::from_config().search("alcohol", "phenotype", 5, 1).await; assert_eq!(variant_name(&result), "Unreachable"); // 5. The default engine stays the gn3 forwarder: with @@ -110,13 +110,13 @@ async fn native_engine_error_contract_and_per_page_invariant() { // Forward limit=10: the engine must ignore it and return the FULL // match set (PORT-DESIGN §5 — the recording compared complete // match sets; Q02's is 57 632 hits). - let small = Native.search("alcohol", "phenotype", 10, 1).await; + let small = CrateEngine::from_config().search("alcohol", "phenotype", 10, 1).await; let small = match small { Ok(hits) => hits, Err(e) => panic!("dev-index search failed: {e:?}"), }; // ... and the same query at a different limit returns the same set. - let large = Native + let large = CrateEngine::from_config() .search("alcohol", "phenotype", 100, 1) .await .expect("dev-index search with limit=100"); @@ -151,7 +151,7 @@ async fn native_engine_error_contract_and_per_page_invariant() { // 7. The typed search hits decode through the doc-data mapping: // a typed query through a type the index documents (gene) must // also come back Ok (the XT filter maps to real XT terms). - let genes = Native + let genes = CrateEngine::from_config() .search("insulin", "gene", 20, 1) .await .expect("dev-index insulin/gene search"); @@ -175,7 +175,7 @@ async fn native_engine_error_contract_and_per_page_invariant() { h.name.clone().unwrap_or_default(), ) }; - let page2 = Native + let page2 = CrateEngine::from_config() .search("alcohol", "phenotype", 10, 2) .await .expect("dev-index page-2 search"); @@ -195,7 +195,7 @@ async fn native_engine_error_contract_and_per_page_invariant() { page2.iter().all(|h| !head.contains(&hit_key(h))), "page 1 and page 2 must be disjoint" ); - let far = Native + let far = CrateEngine::from_config() .search("alcohol", "phenotype", 10, 100_000) .await .expect("dev-index far-page search"); diff --git a/tests/search_native_dev_corpus.rs b/tests/search_native_dev_corpus.rs index 66ed86a..de118fc 100644 --- a/tests/search_native_dev_corpus.rs +++ b/tests/search_native_dev_corpus.rs @@ -28,7 +28,7 @@ //! - `XAPIAN_DB_PATH` — the dev index directory; //! - `GN4_DEV_CORPUS_DIR` — output directory for the full corpus. -use genenetwork4::search::{Engine, SearchError, SearchHit, native::Native}; +use genenetwork4::search::{Engine, SearchError, SearchHit, gnindex::CrateEngine}; /// One row of the harness's shape table, parsed from its source. struct RecordedShape { @@ -172,7 +172,7 @@ async fn record_dev_corpus() { let mut recorded = 0usize; for shape in &shapes { let path = std::path::Path::new(&out_dir).join(format!("{}.json", shape.id)); - let search = Native + let search = CrateEngine::from_config() .search(&shape.raw_query, &shape.kind, RECORD_FETCH_LIMIT, 1) .await; match shape.class.as_str() { diff --git a/tests/search_native_shapes.rs b/tests/search_native_shapes.rs index 7358ed3..c0bcae7 100644 --- a/tests/search_native_shapes.rs +++ b/tests/search_native_shapes.rs @@ -47,7 +47,7 @@ //! set it to ~/spikes/index-self-build/xapian-index); the test skips //! loudly when unset so the hermetic suites stay green. -use genenetwork4::search::{Engine, SearchError, SearchHit, native::Native}; +use genenetwork4::search::{Engine, SearchError, SearchHit, gnindex::CrateEngine}; /// The recorded behavior class this gate asserts (see module doc). #[derive(Clone, Copy, PartialEq, Eq)] @@ -224,7 +224,7 @@ async fn native_engine_shape_class_gate() { let mut passed = 0usize; let mut failed = Vec::new(); for (id, query, type_, expected) in SHAPES { - let result = Native.search(query, type_, 100, 1).await; + let result = CrateEngine::from_config().search(query, type_, 100, 1).await; match (expected, &result) { (Class::Match200, Ok(hits)) => { // Dev-index counts are REFERENCE DATA ONLY (AMENDMENT B): |
