Modern Apps git

← Modern-Apps.git

musicbrainz: one shared definition of indexable text

Commit
93b0d7fd0306a13ddf94f4935bc1a612f4b5ea6d
Author
vayun-mathur <[email protected]>
When
2026-08-21 00:28 UTC (3 months ago)
Parents
9b903cc

Diff

scripts/musicbrainz/mb_ingest/src/build.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/build.rs b/scripts/musicbrainz/mb_ingest/src/build.rsindex 6fdf163935..0b194ed16b 100644--- a/scripts/musicbrainz/mb_ingest/src/build.rs+++ b/scripts/musicbrainz/mb_ingest/src/build.rs@@ -476,18 +476,21 @@ fn credit_sym_of(credit_syms: &[Sym], credit_id: u32) -> Sym {     credit_syms.get(credit_id as usize).copied().unwrap_or(SYM_EMPTY) } -/// Tokenise `text` and spill one posting per term. The term must already be in-/// `terms` from the dictionary pass.+/// Tokenise an entity's indexable text and spill one posting per term.+///+/// The terms come from `pack::indexable_terms`, which the reader's verification+/// path also calls. That sharing is the point: two implementations of "what does+/// this entity contribute to the index" is precisely the bug that shipped. fn emit_postings(-    text: &str,+    primary: &str,+    credit: &str,     refv: u32,     terms: &StringPool,     ranks: &[u32],     buf: &mut Vec<String>,     spill: &mut PostingSpill, ) -> io::Result<()> {-    buf.clear();-    search_terms(text, buf);+    pack::indexable_terms(primary, credit, buf);     for t in buf.iter() {         let Some(sym) = terms.lookup(t) else {             return Err(other_err(format!(@@ -1474,6 +1477,7 @@ pub fn build<W: Write + Seek>(             let text = String::from_utf8_lossy(pool.get(sym)).into_owned();             emit_postings(                 &text,+                "",                 (pack::KIND_ARTIST << pack::KIND_SHIFT) | i as u32,                 &terms,                 &term_ranks,@@ -1484,35 +1488,40 @@ pub fn build<W: Write + Seek>(         for (i, &sym) in rg_title_syms.iter().enumerate() {             check_searchable(i as u32)?;             let refv = (pack::KIND_RELEASE_GROUP << pack::KIND_SHIFT) | i as u32;-            let text = String::from_utf8_lossy(pool.get(sym)).into_owned();-            emit_postings(&text, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;-            // Its artist credit, so "radiohead" finds Radiohead's albums. Duplicate-            // (term, entity) pairs between title and credit dedup at emit time.-            let csym = rg_credit_syms[i];-            if csym != SYM_EMPTY {-                let ctext = String::from_utf8_lossy(pool.get(csym)).into_owned();-                emit_postings(&ctext, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;-            }+            let title = String::from_utf8_lossy(pool.get(sym)).into_owned();+            let credit = match rg_credit_syms[i] {+                SYM_EMPTY => String::new(),+                csym => String::from_utf8_lossy(pool.get(csym)).into_owned(),+            };+            emit_postings(+                &title,+                &credit,+                refv,+                &terms,+                &term_ranks,+                &mut terms_buf,+                &mut post_spill,+            )?;         }         if opts.include_recording_search {             for (i, id) in rec_order.iter().enumerate() {                 check_searchable(i as u32)?;                 let refv = (pack::KIND_RECORDING << pack::KIND_SHIFT) | i as u32;-                let text =+                let title =                     String::from_utf8_lossy(pool.get(rec_title[*id as usize])).into_owned();-                emit_postings(&text, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;-                let csym = credit_sym_of(&credit_syms, rec_credit[*id as usize]);-                if csym != SYM_EMPTY {-                    let ctext = String::from_utf8_lossy(pool.get(csym)).into_owned();-                    emit_postings(-                        &ctext,-                        refv,-                        &terms,-                        &term_ranks,-                        &mut terms_buf,-                        &mut post_spill,-                    )?;-                }+                let credit = match credit_sym_of(&credit_syms, rec_credit[*id as usize]) {+                    SYM_EMPTY => String::new(),+                    csym => String::from_utf8_lossy(pool.get(csym)).into_owned(),+                };+                emit_postings(+                    &title,+                    &credit,+                    refv,+                    &terms,+                    &term_ranks,+                    &mut terms_buf,+                    &mut post_spill,+                )?;             }         }     }
scripts/musicbrainz/mb_ingest/src/pack.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/pack.rs b/scripts/musicbrainz/mb_ingest/src/pack.rsindex 3642d17ffa..bd23cad119 100644--- a/scripts/musicbrainz/mb_ingest/src/pack.rs+++ b/scripts/musicbrainz/mb_ingest/src/pack.rs@@ -1041,6 +1041,24 @@ pub fn search_terms(s: &str, out: &mut Vec<String>) {     } } +/// The terms an entity contributes to the search index.+///+/// **This is the ONE definition, called by both the builder and the reader's+/// verification path.** It exists because having two implementations of "what text+/// does this entity contribute" *was* a shipped bug: `build.rs` indexed title AND+/// artist credit while the reader's verifier read the title alone, so credit-only+/// matches were silently dropped for any token common enough to be deferred. A test+/// can only catch a divergence that has already happened; a single function makes+/// the divergence impossible to express.+///+/// `credit` is empty for entities that have none (artists are indexed on name+/// only, which is what WS/2 does).+pub fn indexable_terms(primary: &str, credit: &str, out: &mut Vec<String>) {+    out.clear();+    search_terms(primary, out);+    search_terms(credit, out);+}+ fn fold_char(c: char) -> char {     match c {         'à'..='å' | 'À'..='Å' => 'a',
scripts/musicbrainz/mb_ingest/src/reader.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/reader.rs b/scripts/musicbrainz/mb_ingest/src/reader.rsindex bd6a300b71..56334c038a 100644--- a/scripts/musicbrainz/mb_ingest/src/reader.rs+++ b/scripts/musicbrainz/mb_ingest/src/reader.rs@@ -1101,25 +1101,27 @@ impl<'a> MbPack<'a> {         out     } -    /// The text a given entity is searched by. This MUST yield the same terms-    /// `build.rs` emitted postings for: release groups and recordings are indexed-    /// on title AND artist credit, so verifying against the title alone would-    /// reject every credit-only match. It would do so only for queries expensive-    /// enough to defer, which is to say only for common artists, and no-    /// fixture-sized test can reach that threshold -- the same shape as the-    /// `MAX_POSTINGS_PER_TOKEN` truncation this replaced.-    ///-    /// The newline is a term separator, so this is exactly the union of the two-    /// texts' terms, not a third text with a seam term across the join.-    fn searchable_text(&self, kind: u32, idx: u32) -> Cow<'a, str> {-        match kind {-            KIND_ARTIST => self.artist(idx).map(|a| a.name),-            KIND_RELEASE_GROUP => {-                self.release_group(idx).map(|g| Cow::Owned(format!("{}\n{}", g.title, g.credit)))-            }-            _ => self.recording(idx).map(|r| Cow::Owned(format!("{}\n{}", r.title, r.credit))),-        }-        .unwrap_or(Cow::Borrowed(""))+    /// The terms this entity contributes to the index, via the ONE shared+    /// definition in `pack::indexable_terms`. The builder calls the same function,+    /// so the verifier cannot disagree with the index about what was indexed --+    /// which it did, once, and silently.+    fn indexed_terms_of(&self, kind: u32, idx: u32, out: &mut Vec<String>) {+        let empty = Cow::Borrowed("");+        let (primary, credit) = match kind {+            KIND_ARTIST => (+                self.artist(idx).map(|a| a.name).unwrap_or(Cow::Borrowed("")),+                empty,+            ),+            KIND_RELEASE_GROUP => match self.release_group(idx) {+                Some(g) => (g.title, g.credit),+                None => (Cow::Borrowed(""), empty),+            },+            _ => match self.recording(idx) {+                Some(r) => (r.title, r.credit),+                None => (Cow::Borrowed(""), empty),+            },+        };+        pack::indexable_terms(&primary, &credit, out);     }      /// Above this many postings, a token is verified against candidates' text@@ -1190,9 +1192,7 @@ impl<'a> MbPack<'a> {             let mut buf: Vec<String> = Vec::new();             let mut next: HashMap<u32, u32> = HashMap::with_capacity(cands.len());             for (&idx, &w) in cands.iter() {-                let text = self.searchable_text(kind, idx);-                buf.clear();-                pack::search_terms(&text, &mut buf);+                self.indexed_terms_of(kind, idx, &mut buf);                 let mut total = w;                 let mut all = true;                 for token in &deferred {@@ -1410,14 +1410,12 @@ mod tests {                 prev += read_uvarint(postings, &mut pos).expect("posting delta");                 let refv = prev as u32;                 let (kind, idx) = (refv >> KIND_SHIFT, refv & IDX_MASK);-                let text = pack.searchable_text(kind, idx);-                buf.clear();-                pack::search_terms(&text, &mut buf);+                pack.indexed_terms_of(kind, idx, &mut buf);                 assert_eq!(                     MbPack::weight_in_terms(&buf, term),                     Some(2),                     "term {term:?} is indexed for kind {kind} entity {idx} but its \-                     searchable_text {text:?} does not yield it, so any query that \+                     indexed_terms_of yields {buf:?}, so any query that \                      defers {term:?} silently drops this entity"                 );                 if kind != pack::KIND_ARTIST {