diff options
| author | Alexander_Kabui <alexanderkabui@proton.me> | 2026-11-16 13:28:55 +0300 |
|---|---|---|
| committer | Alexander_Kabui <alexanderkabui@proton.me> | 2026-11-17 14:06:50 +0300 |
| commit | 54b4deb1d45eb62dd89659033b958454f5dd4b10 (patch) | |
| tree | 37673b5312604e8fd796ce8d32bcef6f8125dc81 /src | |
| parent | ea3dd926211e8db4a0d7d3739a606440b888e8b0 (diff) | |
| download | genenetwork4-54b4deb1d45eb62dd89659033b958454f5dd4b10.tar.gz | |
* Cargo.toml, Cargo.lock, src/search (engine): link the extracted engine.
The engine now lives in the gn-search crate, pinned by revision as the xapian bindings already are. This adds the crate, a CrateEngine behind the existing Engine trait, and a "crate" arm in the dispatcher; the default stays "forward", so nothing changes for a deployment that does not ask for it. The local hit and error declarations are re-exports so the route keeps one vocabulary.
Diffstat (limited to 'src')
| -rw-r--r-- | src/search/gnindex.rs | 77 | ||||
| -rw-r--r-- | src/search/mod.rs | 65 |
2 files changed, 81 insertions, 61 deletions
diff --git a/src/search/gnindex.rs b/src/search/gnindex.rs new file mode 100644 index 0000000..07d53f7 --- /dev/null +++ b/src/search/gnindex.rs @@ -0,0 +1,77 @@ +//! The extracted search engine, linked as a crate. +//! +//! `CrateEngine` is the third [`Engine`](super::Engine) implementation beside +//! the gn3 proxy and the in-process native path: it answers the same four-tuple +//! and keeps the route, the envelope and every consumer-visible byte unchanged. +//! Nothing above this module learns that the engine now lives in another crate. +//! +//! The crate's calls are synchronous xapian work, so they run inside +//! `spawn_blocking` (the house pattern, cf. `native.rs`). Hits cross the seam as +//! JSON: both sides describe the same shape, and the crate's unknown-key +//! passthrough (`extra`) survives the round trip, which is the property the +//! proxy path relies on for gn3's `pubmed_link`. + +use super::{Engine, SearchError, SearchHit}; + +/// The engine that delegates to the `gn-search` crate. +pub struct CrateEngine { + /// The xapian index directory the crate opens per request. + pub index_path: String, +} + +impl CrateEngine { + /// The engine configured from `XAPIAN_DB_PATH`, as the native path is. + pub fn from_config() -> Self { + Self { + index_path: crate::config::Config::load().xapian_db_path, + } + } +} + +impl Engine for CrateEngine { + async fn search( + &self, + q: &str, + type_: &str, + limit: u64, + page: u64, + ) -> Result<Vec<SearchHit>, SearchError> { + let query = q.to_string(); + let search_type = type_.to_string(); + let index_path = self.index_path.clone(); + let hits = tokio::task::spawn_blocking(move || { + gn_search_engine::search(&index_path, &query, &search_type, limit, page) + }) + .await + .map_err(|e| SearchError::Internal(format!("crate search task failed to join: {e}")))? + .map_err(translate)?; + hits.into_iter().map(to_route_hit).collect() + } +} + +/// The crate's failures in this route's vocabulary, one arm per variant. +fn translate(error: gn_search_engine::SearchError) -> SearchError { + use gn_search_engine::SearchError as Crate; + match error { + Crate::InvalidQuery { + message, + xapian_error_type, + } => SearchError::InvalidQuery { + message, + xapian_error_type, + }, + Crate::Unreachable(detail) => SearchError::Unreachable(detail), + Crate::Misbehaving(detail) => SearchError::Misbehaving(detail), + Crate::Backend(detail) => SearchError::Backend(detail), + Crate::Internal(detail) => SearchError::Internal(detail), + } +} + +/// One crate hit as the route's `SearchHit`, via the shared JSON shape so an +/// unknown key cannot be dropped in translation. +fn to_route_hit(hit: gn_search_engine::SearchHit) -> Result<SearchHit, SearchError> { + let value = serde_json::to_value(hit) + .map_err(|e| SearchError::Internal(format!("crate hit not serialisable: {e}")))?; + serde_json::from_value(value) + .map_err(|e| SearchError::Misbehaving(format!("crate hit not a route hit: {e}"))) +} diff --git a/src/search/mod.rs b/src/search/mod.rs index 91d6f3f..f8797e7 100644 --- a/src/search/mod.rs +++ b/src/search/mod.rs @@ -92,6 +92,7 @@ use std::future::Future; pub mod gn3; +pub mod gnindex; pub mod native; /// Run a search through the configured engine: the gn3 forwarder by @@ -106,6 +107,7 @@ pub async fn search( ) -> 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, } } @@ -116,72 +118,13 @@ pub async fn search( /// `geno_chr`/`geno_mb`) for traits that don't have them. Fields gn3 /// adds in future pass through via `extra`, keeping the proxy /// transparent without freezing the schema to today's gn3. -#[derive(serde::Serialize, serde::Deserialize, utoipa::ToSchema)] -pub struct SearchHit { - pub name: Option<String>, - pub dataset: Option<String>, - pub dataset_fullname: Option<String>, - pub group: Option<String>, - pub species: Option<String>, - pub authors: Option<Vec<String>>, - pub year: Option<u16>, - pub description: Option<String>, - pub mean: Option<f64>, - pub additive: Option<f64>, - pub lrs: Option<f64>, - /// LOD score, LRS / `db::LRS_PER_LOD` (4.604); the route computes - /// it from `lrs`. - pub lod: Option<f64>, - /// Name of the peak marker (a Geno.Name such as "rsm10000012461"), - /// looked up in PublishXRef for phenotype hits; null for other - /// hits, or when the database is unavailable. - pub locus: Option<String>, - /// The trait's short name (e.g. "5HT_MB_M"), looked up like `locus`. - pub abbreviation: Option<String>, - pub geno_chr: Option<String>, - pub geno_mb: Option<f64>, - pub inbredsetcode: Option<String>, - pub pubmed_id: Option<u32>, - #[serde(flatten)] - #[schema(value_type = Option<std::collections::BTreeMap<String, String>>)] - pub extra: std::collections::BTreeMap<String, serde_json::Value>, -} +pub use gn_search_engine::SearchHit; /// A search-engine failure, in the vocabulary of the `/search` error /// contract (see the module doc comment). Each variant carries the /// detail text the route forwards in the `ApiError` body; the route /// supplies the HTTP status, `code`, and title for each. -#[derive(Debug)] -pub enum SearchError { - /// The backend rejected the query and supplied a diagnostic (gn3's - /// 4xx with a JSON `"error"` field, or the native engine's own - /// guards). Maps to 422 `invalid_query`. `xapian_error_type` is the - /// backend's own error classification when it has one (the xapian - /// exception class — `QueryParserError` for parser rejections, - /// `InvalidOperationError` for the Q42 invalid-location class); - /// the route forwards it inside the blessed typed 422 body (the - /// 2026-10-06 contract, AMENDMENT E). - InvalidQuery { - /// The human-readable diagnostic (gn3's `error` field / the - /// parser's message). - message: String, - /// The xapian exception class when known (`None` for a backend - /// that did not classify its rejection). - xapian_error_type: Option<String>, - }, - /// The backend could not be reached at all (transport failure). - /// Maps to 502 `backend_unreachable`. - Unreachable(String), - /// The backend answered but the response body could not be read or - /// decoded. Maps to 502 `backend_misbehaving`. - Misbehaving(String), - /// The backend returned a non-success status. Maps to 502 - /// `backend_error`. - Backend(String), - /// Local misconfiguration (e.g. an unparsable backend base URL), - /// not a backend fault. Maps to 500 `internal_error`. - Internal(String), -} +pub use gn_search_engine::SearchError; /// A search backend: resolves a validated `(q, type, limit)` into hits. /// |
