musicbrainz: fix common-word search, cut peak RSS 6.1GB to 3.5GB
- Commit
332a506c7b494643e00785015015aa1cfce80a10- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-20 23:16 UTC (3 months ago)
- Parents
3d7db77
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 434f836109..6e2ea3d46b 100644--- a/scripts/musicbrainz/mb_ingest/src/build.rs+++ b/scripts/musicbrainz/mb_ingest/src/build.rs@@ -48,9 +48,15 @@ use crate::reader::encode_recording_credit; /// an absurd primary key. const MAX_ID: u32 = 400_000_000; -/// Every table this crate reads. All of these live in the core `mbdump.tar.bz2`-/// (each is marked `-- replicate` in `admin/sql/CreateTables.sql`, which is what-/// puts a table in that dump); none needs `mbdump-derived.tar.bz2`.+/// Every table this crate reads.+///+/// All of these are present in the core `mbdump.tar.bz2` — **verified by actually+/// extracting them**, which is not the same as what the schema suggests. Note the+/// absence of `release_group_meta`: it is marked `-- replicate` in+/// `admin/sql/CreateTables.sql`, but that marker controls replication packets, not+/// the dump split, and the table is NOT in the core dump. Its+/// `first_release_date_*` is derived here instead (see `derive_first_release_dates`),+/// which is what keeps this crate off `mbdump-derived.tar.bz2`. pub const TABLES: &[&str] = &[ "area", "iso_3166_1",@@ -63,7 +69,6 @@ pub const TABLES: &[&str] = &[ "artist_credit_name", "artist", "release_group",- "release_group_meta", "release_group_secondary_type_join", "release", "release_country",@@ -381,6 +386,112 @@ impl Drop for TrackSpill { } } +/// Bucket-sorts search postings by term rank through files on disk.+///+/// 157 M postings held as `Vec<(u32, u32)>` is 1.26 GB, which was the single+/// largest contributor to a measured 6.14 GB peak. Bucketing by rank means only+/// one bucket (~20 MB) is resident while it is sorted and emitted, and the ranks+/// are known before this runs so the buckets come out in output order.+struct PostingSpill {+ dir: PathBuf,+ writers: Vec<BufWriter<File>>,+ buckets: usize,+ terms: u32,+ written: u64,+}++impl PostingSpill {+ fn new(work_dir: Option<&Path>, terms: u32) -> io::Result<PostingSpill> {+ let buckets = if terms < 100_000 { 1 } else { 64 };+ let base = match work_dir {+ Some(p) => p.to_path_buf(),+ None => std::env::temp_dir(),+ };+ let seq = SPILL_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);+ let dir = base.join(format!("mb_ingest_postings_{}_{seq}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ std::fs::create_dir_all(&dir)?;+ let mut writers = Vec::with_capacity(buckets);+ for b in 0..buckets {+ writers.push(BufWriter::with_capacity(+ 1 << 16,+ File::create(dir.join(format!("p{b:03}")))?,+ ));+ }+ Ok(PostingSpill { dir, writers, buckets, terms, written: 0 })+ }++ fn bucket_of(&self, rank: u32) -> usize {+ if self.buckets == 1 || self.terms == 0 {+ return 0;+ }+ ((rank as u64 * self.buckets as u64) / self.terms as u64).min(self.buckets as u64 - 1)+ as usize+ }++ /// First rank NOT in bucket `b`. Must be the exact inverse of `bucket_of`.+ fn bucket_end(&self, b: usize) -> u32 {+ if self.buckets == 1 {+ return self.terms;+ }+ if b + 1 >= self.buckets {+ return self.terms;+ }+ let t = self.terms as u64;+ let n = self.buckets as u64;+ (((b as u64 + 1) * t).div_ceil(n)).min(t) as u32+ }++ fn push(&mut self, rank: u32, refv: u32) -> io::Result<()> {+ let b = self.bucket_of(rank);+ let mut rec = [0u8; 8];+ rec[0..4].copy_from_slice(&rank.to_le_bytes());+ rec[4..8].copy_from_slice(&refv.to_le_bytes());+ self.writers[b].write_all(&rec)?;+ self.written += 8;+ Ok(())+ }++ fn finish(&mut self) -> io::Result<Vec<PathBuf>> {+ for w in self.writers.iter_mut() {+ w.flush()?;+ }+ self.writers.clear();+ Ok((0..self.buckets).map(|b| self.dir.join(format!("p{b:03}"))).collect())+ }+}++impl Drop for PostingSpill {+ fn drop(&mut self) {+ self.writers.clear();+ let _ = std::fs::remove_dir_all(&self.dir);+ }+}++/// Tokenise `text` and spill one posting per term. The term must already be in+/// `terms` from the dictionary pass.+fn emit_postings(+ text: &str,+ refv: u32,+ terms: &StringPool,+ ranks: &[u32],+ buf: &mut Vec<String>,+ spill: &mut PostingSpill,+) -> io::Result<()> {+ buf.clear();+ search_terms(text, buf);+ for t in buf.iter() {+ let Some(sym) = terms.lookup(t) else {+ return Err(other_err(format!(+ "search term {t:?} was not interned in the dictionary pass; the two search \+ passes disagree, which would silently corrupt the index"+ )));+ };+ spill.push(ranks[sym as usize], refv)?;+ }+ Ok(())+}+ #[derive(Clone, Copy)] struct TrackRow { medium_idx: u32,@@ -638,18 +749,6 @@ pub fn build<W: Write + Seek>( for (i, r) in rgs.iter().enumerate() { rg_slot.set(r.id, i as u32)?; }- let n = require(- source,- "release_group_meta",- Shape { table: "release_group_meta", min_fields: 7, ints: &[0], uuids: &[] },- |row| {- if let Some(slot) = row.u32(0).and_then(|id| rg_slot.get(id)) {- rgs[slot as usize].first_date = pack_date(row.i64(2), row.i64(3), row.i64(4));- }- Ok(())- },- )?;- st.rows.push(("release_group_meta", n)); let n = require( source, "release_group_secondary_type_join",@@ -684,27 +783,6 @@ pub fn build<W: Write + Seek>( rg_idx.set(r.id, i as u32)?; } - // ARTIST_RGS: artist -> release groups, via the release group's credit.- let mut artist_rg_pairs: Vec<(u32, u32)> = Vec::new();- for (i, r) in rgs.iter().enumerate() {- let Some(artists_of) = credit_artists.get(r.credit_id as usize) else { continue };- for a in artists_of {- if let Some(ai) = artist_idx.get(*a) {- artist_rg_pairs.push((ai, i as u32));- }- }- }- // Newest first: the discography order MusicBrainzViewModel.kt:207 sorts into.- artist_rg_pairs.sort_unstable_by(|x, y| {- x.0.cmp(&y.0)- .then(rgs[y.1 as usize].first_date.cmp(&rgs[x.1 as usize].first_date))- .then(x.1.cmp(&y.1))- });- artist_rg_pairs.dedup();- let (artist_rgs, artist_rgs_idx) = to_csr(&artist_rg_pairs, artists.len());- drop(artist_rg_pairs);- drop(credit_artists);- // --- releases ------------------------------------------------------------- step!("pass 5: releases"); let mut releases: Vec<ReleaseBuild> = Vec::new();@@ -808,6 +886,46 @@ pub fn build<W: Write + Seek>( if dangling_rg > 0 { st.skipped.push(("releases whose release group is missing", dangling_rg)); }++ // A release group's first release date. MusicBrainz materialises this in+ // `release_group_meta`, which is NOT in the core dump, so it is derived here as+ // the earliest known date across the group's releases -- which is how upstream+ // computes it in the first place, so this is equivalent rather than approximate.+ // It is load-bearing: the RG screen header, the discography sort+ // (MusicBrainzViewModel.kt:207) and the release-date fallback (:475-476).+ for &(g, ri) in &rg_release_pairs {+ let d = releases[ri as usize].date;+ if d == 0 {+ continue;+ }+ let cur = &mut rgs[g as usize].first_date;+ if *cur == 0 || d < *cur {+ *cur = d;+ }+ }++ // ARTIST_RGS: artist -> release groups, via the release group's credit. Built+ // after the dates are derived, because it is sorted on them.+ let mut artist_rg_pairs: Vec<(u32, u32)> = Vec::new();+ for (i, r) in rgs.iter().enumerate() {+ let Some(artists_of) = credit_artists.get(r.credit_id as usize) else { continue };+ for a in artists_of {+ if let Some(ai) = artist_idx.get(*a) {+ artist_rg_pairs.push((ai, i as u32));+ }+ }+ }+ // Newest first: the discography order MusicBrainzViewModel.kt:207 sorts into.+ artist_rg_pairs.sort_unstable_by(|x, y| {+ x.0.cmp(&y.0)+ .then(rgs[y.1 as usize].first_date.cmp(&rgs[x.1 as usize].first_date))+ .then(x.1.cmp(&y.1))+ });+ artist_rg_pairs.dedup();+ let (artist_rgs, artist_rgs_idx) = to_csr(&artist_rg_pairs, artists.len());+ drop(artist_rg_pairs);+ drop(credit_artists);+ rg_release_pairs.sort_unstable_by(|x, y| { let (a, b) = (&releases[x.1 as usize], &releases[y.1 as usize]); x.0.cmp(&y.0)@@ -994,7 +1112,14 @@ pub fn build<W: Write + Seek>( } } let mut order: Vec<u32> = (0..rows.len() as u32).collect();- order.sort_unstable_by_key(|&i| (rows[i as usize].medium_idx, rows[i as usize].position));+ // The trailing `i` matters: MusicBrainz does contain more than one track at+ // the same position on the same medium, and without a total order+ // `sort_unstable_by_key` may order those two arbitrarily between runs. That+ // produced two same-sized but byte-different packs from identical input, and+ // the fixture never caught it because it has no tied positions.+ order.sort_unstable_by_key(|&i| {+ (rows[i as usize].medium_idx, rows[i as usize].position, i)+ }); let mut cursor = 0usize; // Every medium up to the last one in this bucket gets its offset written, // including the ones with no tracks at all.@@ -1100,30 +1225,6 @@ pub fn build<W: Write + Seek>( } st.standalone_recordings = rec_order.len() as u64 - tracked; - // --- recording MBIDs, scattered in clustered order ------------------------- let mut recording_mbids: Vec<u8> = Vec::new();- if opts.include_recording_mbids {- step!("pass 11: recording MBIDs");- recording_mbids = vec![0u8; rec_order.len() * 16];- require(- source,- "recording",- Shape { table: "recording", min_fields: 9, ints: &[0, 3], uuids: &[1] },- |row| {- let Some(id) = row.u32(0) else { return Ok(()) };- let Some(&new) = rec_new.get(id as usize) else { return Ok(()) };- if new == pack::NONE {- return Ok(());- }- if let Some(gid) = parse_mbid(&row.str(1)) {- let at = new as usize * 16;- recording_mbids[at..at + 16].copy_from_slice(&gid);- }- Ok(())- },- )?;- }- // --- ISRCs ---------------------------------------------------------------- let mut isrcs: Vec<(u32, [u8; 7])> = Vec::new(); if opts.include_isrcs {@@ -1153,105 +1254,203 @@ pub fn build<W: Write + Seek>( } // --- search index ---------------------------------------------------------- step!("pass 13: search index");- let mut term_dict: HashMap<String, u32> = HashMap::new();- let mut postings: Vec<(u32, u32)> = Vec::new();+ // Two passes over the in-memory entity texts rather than one, because the+ // one-pass version was the peak of the whole build: a `HashMap<String, u32>`+ // dictionary plus the `Vec<(String, u32)>` it is drained into cost ~1.5 GB of+ // duplicated allocations, and 157 M postings held as `Vec<(u32, u32)>` cost+ // another 1.26 GB. Pass A interns terms into a symbol pool (the same+ // open-addressed structure the string pool uses); pass B emits postings with+ // their final sorted term ranks straight into rank-keyed spill buckets. The+ // texts are already in memory, so the second pass costs CPU and no I/O.+ step!("pass 13a: search dictionary");+ let mut terms = StringPool::new(); let mut terms_buf: Vec<String> = Vec::new();- {- let mut add = |text: &str,- refv: u32,- dict: &mut HashMap<String, u32>,- post: &mut Vec<(u32, u32)>| {+ for a in artists.iter() {+ let text = String::from_utf8_lossy(pool.get(a.name)).into_owned();+ terms_buf.clear();+ search_terms(&text, &mut terms_buf);+ for t in terms_buf.iter() {+ terms.intern(t);+ }+ }+ for r in rgs.iter() {+ let text = String::from_utf8_lossy(pool.get(r.title)).into_owned();+ terms_buf.clear();+ search_terms(&text, &mut terms_buf);+ for t in terms_buf.iter() {+ terms.intern(t);+ }+ }+ if opts.include_recording_search {+ for id in rec_order.iter() {+ let text = String::from_utf8_lossy(pool.get(rec_title[*id as usize])).into_owned(); terms_buf.clear();- search_terms(text, &mut terms_buf);+ search_terms(&text, &mut terms_buf); for t in terms_buf.iter() {- let next = dict.len() as u32;- let id = *dict.entry(t.clone()).or_insert(next);- post.push((id, refv));+ terms.intern(t); }- };+ }+ }+ // `finalize` would consume the pool, and pass B still needs to look terms up,+ // so the sorted order is taken without consuming it. Alphabetical order is+ // exactly what SEARCH_TERMS needs for the reader's binary search.+ let term_count = terms.distinct();+ let term_order = terms.sorted_order();+ let mut term_ranks = vec![0u32; term_count];+ for (rank, &sym) in term_order.iter().enumerate() {+ term_ranks[sym as usize] = rank as u32;+ }++ step!("pass 13b: search postings ({term_count} terms)");+ let mut post_spill = PostingSpill::new(opts.work_dir.as_deref(), term_count as u32)?;+ { for (i, a) in artists.iter().enumerate() { check_searchable(i as u32)?; let text = String::from_utf8_lossy(pool.get(a.name)).into_owned();- add(+ emit_postings( &text, (pack::KIND_ARTIST << pack::KIND_SHIFT) | i as u32,- &mut term_dict,- &mut postings,- );+ &terms,+ &term_ranks,+ &mut terms_buf,+ &mut post_spill,+ )?; } for (i, r) in rgs.iter().enumerate() { check_searchable(i as u32)?; let text = String::from_utf8_lossy(pool.get(r.title)).into_owned();- add(+ emit_postings( &text, (pack::KIND_RELEASE_GROUP << pack::KIND_SHIFT) | i as u32,- &mut term_dict,- &mut postings,- );+ &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 text = String::from_utf8_lossy(pool.get(rec_title[*id as usize])).into_owned();- add(+ emit_postings( &text, (pack::KIND_RECORDING << pack::KIND_SHIFT) | i as u32,- &mut term_dict,- &mut postings,- );+ &terms,+ &term_ranks,+ &mut terms_buf,+ &mut post_spill,+ )?; } } }- st.search_postings = postings.len() as u64;- // Sort the dictionary so the reader can binary search it, then renumber.- let mut sorted_terms: Vec<(String, u32)> = term_dict.into_iter().collect();- sorted_terms.sort_unstable_by(|a, b| a.0.cmp(&b.0));- {- let mut remap = vec![0u32; sorted_terms.len()];- for (new, (_, old)) in sorted_terms.iter().enumerate() {- remap[*old as usize] = new as u32;- }- for p in postings.iter_mut() {- p.0 = remap[p.0 as usize];+ drop(term_ranks);+ st.search_postings = post_spill.written / 8;+ let post_buckets = post_spill.finish()?;++ // The pack writer opens here rather than after the search index, so+ // SEARCH_POSTINGS can be STREAMED bucket by bucket instead of accumulating a+ // ~300 MB buffer while the string pool is still resident. Section order in the+ // file is irrelevant -- the directory carries absolute offsets.+ step!("pass 14: writing");+ // Captured before the sources are dropped further down.+ let rec_max_id = rec_present.len().saturating_sub(1) as u64;+ let rec_live = rec_present.iter().filter(|&&p| p).count() as u64;+ let credit_live = credit_syms.iter().filter(|&&s| s != SYM_EMPTY).count() as u64;+ let mut w = PackWriter::new(out)?;++ // SEARCH_TERMS, streamed straight out of the term pool in rank order.+ w.begin(pack::S_SEARCH_TERMS)?;+ let mut chunk: Vec<u8> = Vec::with_capacity(1 << 16);+ for &sym in &term_order {+ chunk.extend_from_slice(terms.get(sym));+ chunk.push(0);+ if chunk.len() >= (1 << 16) {+ w.write(&chunk)?;+ chunk.clear(); } }- postings.sort_unstable();- postings.dedup();- let term_count = sorted_terms.len();- let mut search_terms_bytes: Vec<u8> = Vec::new();- let mut postings_bytes: Vec<u8> = Vec::new();+ w.write(&chunk)?;+ w.end()?;++ // SEARCH_POSTINGS, streamed; SEARCH_TERM_IDX accumulates the two offsets per+ // term as we go, which is only 8 bytes a term. let mut term_index: Vec<u8> = Vec::with_capacity((term_count + 1) * 8);- let mut p = 0usize;- for (new_id, (t, _)) in sorted_terms.iter().enumerate() {- term_index.extend_from_slice(&(search_terms_bytes.len() as u32).to_le_bytes());- term_index.extend_from_slice(&(postings_bytes.len() as u32).to_le_bytes());- search_terms_bytes.extend_from_slice(t.as_bytes());- search_terms_bytes.push(0);- let start = p;- while p < postings.len() && postings[p].0 == new_id as u32 {- p += 1;- }- pack::write_uvarint(&mut postings_bytes, (p - start) as u64);- let mut prev = 0u32;- for &(_, refv) in &postings[start..p] {- pack::write_uvarint(&mut postings_bytes, (refv - prev) as u64);- prev = refv;+ let mut term_off = 0u32;+ let mut post_off = 0u32;+ let mut rank = 0u32;+ let mut spill_buf: Vec<u8> = Vec::new();+ w.begin(pack::S_SEARCH_POSTINGS)?;+ for (b, bucket) in post_buckets.iter().enumerate() {+ spill_buf.clear();+ File::open(bucket)?.read_to_end(&mut spill_buf)?;+ let mut pairs: Vec<(u32, u32)> = spill_buf+ .chunks_exact(8)+ .map(|c| {+ (+ u32::from_le_bytes([c[0], c[1], c[2], c[3]]),+ u32::from_le_bytes([c[4], c[5], c[6], c[7]]),+ )+ })+ .collect();+ pairs.sort_unstable();+ pairs.dedup();+ let bucket_end = post_spill.bucket_end(b);+ let mut out_bytes: Vec<u8> = Vec::new();+ let mut p = 0usize;+ while rank < bucket_end {+ term_index.extend_from_slice(&term_off.to_le_bytes());+ term_index.extend_from_slice(&post_off.to_le_bytes());+ term_off += terms.get(term_order[rank as usize]).len() as u32 + 1;+ let start = p;+ while p < pairs.len() && pairs[p].0 == rank {+ p += 1;+ }+ let before = out_bytes.len();+ pack::write_uvarint(&mut out_bytes, (p - start) as u64);+ let mut prev = 0u32;+ for &(_, refv) in &pairs[start..p] {+ pack::write_uvarint(&mut out_bytes, (refv - prev) as u64);+ prev = refv;+ }+ post_off += (out_bytes.len() - before) as u32;+ rank += 1; }+ w.write(&out_bytes)?; }- term_index.extend_from_slice(&(search_terms_bytes.len() as u32).to_le_bytes());- term_index.extend_from_slice(&(postings_bytes.len() as u32).to_le_bytes());- drop(postings);- drop(sorted_terms);+ w.end()?;+ term_index.extend_from_slice(&term_off.to_le_bytes());+ term_index.extend_from_slice(&post_off.to_le_bytes());+ w.section(pack::S_SEARCH_TERM_IDX, &term_index)?;+ drop(term_index);+ drop(post_spill);+ drop(spill_buf);+ drop(term_order);+ drop(terms); // --- assemble -------------------------------------------------------------- step!("pass 14: writing");- let mut w = PackWriter::new(out)?;+ // Every buffer is dropped the moment its section is on disk. That is not+ // tidiness: holding them all to the end is what put the measured peak at+ // 4.24 GB against a 4 GB budget, and the entity vectors plus the recording+ // arrays are ~1.1 GB of it. Counts are captured before the sources go.+ let counts = HeaderCounts {+ artists: artists.len() as u32,+ credits: credit_count,+ release_groups: rgs.len() as u32,+ releases: releases.len() as u32,+ media: media.len() as u32,+ tracks: track_count,+ recordings: rec_order.len() as u32,+ isrcs: isrcs.len() as u32,+ search_terms: term_count as u32,+ enums: enums.syms().len() as u32,+ }; let enum_offsets: Vec<u32> = enums.syms().iter().map(|&s| pool.offset(s)).collect(); w.section_u32(pack::S_ENUM_POOL, &enum_offsets)?;+ drop(enum_offsets); let credit_offsets: Vec<u32> = credit_syms.iter().map(|&s| pool.offset(s)).collect(); w.section_u32(pack::S_CREDITS, &credit_offsets)?;+ drop(credit_offsets); drop(credit_syms); buf.clear();@@ -1269,6 +1468,7 @@ pub fn build<W: Write + Seek>( w.section(pack::S_ARTIST_MBID, &recs)?; w.section_u32(pack::S_ARTIST_MBID_HI, &hi)?; drop(gids);+ drop(artists); buf.clear(); for r in &rgs {@@ -1284,8 +1484,11 @@ pub fn build<W: Write + Seek>( w.section(pack::S_RG_MBID, &recs)?; w.section_u32(pack::S_RG_MBID_HI, &hi)?; drop(gids);+ drop(rgs); w.section_u32(pack::S_ARTIST_RGS, &artist_rgs)?;+ drop(artist_rgs); w.section_u32(pack::S_ARTIST_RGS_IDX, &artist_rgs_idx)?;+ drop(artist_rgs_idx); buf.clear(); for r in &releases {@@ -1303,17 +1506,23 @@ pub fn build<W: Write + Seek>( w.section(pack::S_RELEASE_MBID, &recs)?; w.section_u32(pack::S_RELEASE_MBID_HI, &hi)?; drop(gids);+ drop(releases); w.section_u32(pack::S_RG_RELEASES, &rg_releases)?;+ drop(rg_releases); w.section_u32(pack::S_RG_RELEASES_IDX, &rg_releases_idx)?;+ drop(rg_releases_idx); w.section_u32(pack::S_MEDIA_IDX, &media_idx_csr)?;+ drop(media_idx_csr); buf.clear(); for m in &media { buf.extend_from_slice(&m.format.to_le_bytes()); buf.extend_from_slice(&(m.track_count.min(u16::MAX as u32) as u16).to_le_bytes()); } w.section(pack::S_MEDIA, &buf)?;+ drop(media); w.section_u32(pack::S_TRACK_IDX, &track_idx)?;+ drop(track_idx); assert!( tracks_stream.len() <= u32::MAX as usize, "TRACKS is {} bytes, past the u32 ceiling TRACK_IDX addresses it with",@@ -1336,11 +1545,47 @@ pub fn build<W: Write + Seek>( buf.push(hi); } w.section(pack::S_RECORDINGS, &buf)?;+ buf.clear();+ buf.shrink_to_fit();+ // Dead from here: RECORDINGS was their only remaining consumer, and the search+ // index is already written.+ drop(rec_title);+ drop(rec_credit);+ drop(rec_dur);+ drop(rec_present);+ let recording_count = rec_order.len();+ drop(rec_order); if opts.include_recording_mbids {+ // Deliberately built HERE rather than earlier: this is a 638 MB buffer, and+ // running the pass at its natural place in the order would have kept it+ // resident alongside the search index. A second scan of `recording` is far+ // cheaper than that overlap -- and cheaper still than holding 16 B per+ // recording id (~760 MB) through the whole build, which is what scattering+ // into this buffer avoids.+ step!("pass 14b: recording MBIDs");+ let mut recording_mbids = vec![0u8; recording_count * 16];+ require(+ source,+ "recording",+ Shape { table: "recording", min_fields: 9, ints: &[0, 3], uuids: &[1] },+ |row| {+ let Some(id) = row.u32(0) else { return Ok(()) };+ let Some(&new) = rec_new.get(id as usize) else { return Ok(()) };+ if new == pack::NONE {+ return Ok(());+ }+ if let Some(gid) = parse_mbid(&row.str(1)) {+ let at = new as usize * 16;+ recording_mbids[at..at + 16].copy_from_slice(&gid);+ }+ Ok(())+ },+ )?; w.section(pack::S_RECORDING_MBID, &recording_mbids)?; }- drop(recording_mbids); w.section_u32(pack::S_REC_FIRST_RELEASE, &rec_first_release)?;+ drop(rec_first_release);+ drop(rec_new); if opts.include_isrcs { buf.clear();@@ -1350,10 +1595,11 @@ pub fn build<W: Write + Seek>( } w.section(pack::S_ISRCS, &buf)?; }-- w.section(pack::S_SEARCH_TERMS, &search_terms_bytes)?;- w.section(pack::S_SEARCH_POSTINGS, &postings_bytes)?;- w.section(pack::S_SEARCH_TERM_IDX, &term_index)?;+ let isrc_count = isrcs.len();+ let _ = isrc_count;+ drop(isrcs);+ buf.clear();+ buf.shrink_to_fit(); // STRINGS goes last: it is only complete now, and streaming it here means the // sorted pool is never materialised as a second buffer.@@ -1383,18 +1629,6 @@ pub fn build<W: Write + Seek>( st.string_pool_stored = w.end()?; } - let counts = HeaderCounts {- artists: artists.len() as u32,- credits: credit_count,- release_groups: rgs.len() as u32,- releases: releases.len() as u32,- media: media.len() as u32,- tracks: track_count,- recordings: rec_order.len() as u32,- isrcs: isrcs.len() as u32,- search_terms: term_count as u32,- enums: enum_offsets.len() as u32,- }; w.set_header(&counts, opts.flags(), source.dump_date(), string_blocks); st.string_blocks = string_blocks; st.sections = w@@ -1406,15 +1640,11 @@ pub fn build<W: Write + Seek>( artist_idx.bytes() + rg_idx.bytes() + release_idx.bytes() + medium_idx.bytes(); st.max_ids = vec![ ("artist", artist_idx.max_id(), artist_idx.live()),- ("artist_credit", credit_count.saturating_sub(1) as u64, credit_count as u64),+ ("artist_credit", credit_count.saturating_sub(1) as u64, credit_live), ("release_group", rg_idx.max_id(), rg_idx.live()), ("release", release_idx.max_id(), release_idx.live()), ("medium", medium_idx.max_id(), medium_idx.live()),- (- "recording",- rec_present.len().saturating_sub(1) as u64,- rec_present.iter().filter(|&&p| p).count() as u64,- ),+ ("recording", rec_max_id, rec_live), ]; st.total_bytes = w.finish()?; Ok(st)@@ -1593,6 +1823,29 @@ mod tests { } } + #[test]+ fn posting_buckets_and_ends_are_exact_inverses() {+ // If `bucket_of` and `bucket_end` disagree by even one rank, the emit loop+ // either skips a term's postings or attributes them to the wrong term.+ for terms in [1u32, 2, 63, 64, 65, 100_000, 4_332_477] {+ let s = PostingSpill::new(None, terms).unwrap();+ let mut expected = 0u32;+ for b in 0..s.buckets {+ let end = s.bucket_end(b);+ assert!(end >= expected, "bucket ends must not go backwards");+ for rank in expected..end {+ assert_eq!(+ s.bucket_of(rank),+ b,+ "terms={terms} rank={rank} belongs in bucket {b}"+ );+ }+ expected = end;+ }+ assert_eq!(expected, terms, "buckets must cover every rank exactly once");+ }+ }+ #[test] fn concurrent_spills_do_not_share_a_directory() { let a = TrackSpill::new(None, 10, TRACK_SPILL_LEN).unwrap();
scripts/musicbrainz/mb_ingest/src/fixture.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/fixture.rs b/scripts/musicbrainz/mb_ingest/src/fixture.rsindex 5c1046bce3..71ac83f1ef 100644--- a/scripts/musicbrainz/mb_ingest/src/fixture.rs+++ b/scripts/musicbrainz/mb_ingest/src/fixture.rs@@ -10,6 +10,11 @@ //! releases (so `d_recording` is not always 1), a standalone recording with no //! track, a release with no status, a release group with a secondary type, and a //! malformed ISRC that must be rejected rather than stored.+//!+//! There is deliberately **no `release_group_meta`**: that table is not in the+//! core dump, so first-release dates are derived from release dates. The fixture+//! therefore also exercises that derivation, including a release group whose only+//! release has no date at all. use std::fs; use std::io::{self, Write};@@ -153,17 +158,6 @@ pub fn write_fixture(dir: &Path) -> io::Result<()> { &["4", RG_WEIRD, "Untitled\\tWeird\\nTitle", "3", N, "", "0", TS], ], )?;- // release_group_meta: id, release_count, first y/m/d, rating, rating_count- table(- dir,- "release_group_meta",- &[- &["1", "2", "1973", "3", "1", N, N],- &["2", "1", "1997", "9", "22", N, N],- &["3", "1", "2001", "11", "5", N, N],- &["4", "1", N, N, N, N, N],- ],- )?; // release_group_secondary_type_join: release_group, secondary_type, created table(dir, "release_group_secondary_type_join", &[&["3", "1", TS], &["3", "3", TS]])?;
scripts/musicbrainz/mb_ingest/src/main.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/main.rs b/scripts/musicbrainz/mb_ingest/src/main.rsindex ec19b827e3..ed45bbf113 100644--- a/scripts/musicbrainz/mb_ingest/src/main.rs+++ b/scripts/musicbrainz/mb_ingest/src/main.rs@@ -8,6 +8,7 @@ //! [--work-dir <dir>] [-q] //! mb_ingest fixture <dir> write the synthetic test dump //! mb_ingest inspect <out.pack> header, section sizes and a smoke query+//! mb_ingest query <out.pack> <text> run all three searches and dump one result each //! ``` use std::io::{self, Write};@@ -37,7 +38,8 @@ fn usage() -> io::Error { [--no-isrcs] [--no-recording-search] [--no-recording-mbids] [--raw-strings] \ [--work-dir <dir>] [-q]\n \ mb_ingest fixture <dir>\n \- mb_ingest inspect <pack>",+ mb_ingest inspect <pack>\n \+ mb_ingest query <pack> <text>", ) } @@ -52,6 +54,10 @@ fn run() -> io::Result<()> { Ok(()) } Some("inspect") => cmd_inspect(args.get(1).ok_or_else(usage)?),+ Some("query") => cmd_query(+ args.get(1).ok_or_else(usage)?,+ args.get(2).ok_or_else(usage)?,+ ), _ => Err(usage()), } }@@ -117,6 +123,100 @@ fn cmd_build(args: &[String]) -> io::Result<()> { Ok(()) } +fn cmd_query(path: &str, text: &str) -> io::Result<()> {+ let bytes = std::fs::read(path)?;+ let pack = MbPack::open(&bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;+ let t0 = std::time::Instant::now();+ let artists = pack.search_artists(text, 5);+ let artist_ms = t0.elapsed().as_secs_f64() * 1000.0;+ println!("search_artists({text:?}) -> {} hits in {artist_ms:.1} ms", artists.len());+ for h in &artists {+ let a = pack.artist(h.idx).expect("artist row");+ println!(+ " [{}] {} ({}) {} {} — {} release groups",+ h.score,+ a.name,+ a.kind,+ a.country,+ a.begin_date.to_ws2(),+ pack.artist_release_groups(h.idx).len()+ );+ }+ let t0 = std::time::Instant::now();+ let rgs = pack.search_release_groups(text, 5);+ println!(+ "search_release_groups({text:?}) -> {} hits in {:.1} ms",+ rgs.len(),+ t0.elapsed().as_secs_f64() * 1000.0+ );+ for h in &rgs {+ let g = pack.release_group(h.idx).expect("rg row");+ println!(+ " [{}] {} — {} ({} {}) {} releases",+ h.score,+ g.title,+ g.credit,+ g.primary_type,+ g.first_release_date.to_ws2(),+ pack.release_group_releases(h.idx).len()+ );+ }+ let t0 = std::time::Instant::now();+ let recs = pack.search_recordings(text, 5);+ println!(+ "search_recordings({text:?}) -> {} hits in {:.1} ms",+ recs.len(),+ t0.elapsed().as_secs_f64() * 1000.0+ );+ for h in &recs {+ let r = pack.recording(h.idx).expect("recording row");+ let isrcs: Vec<String> = pack+ .recording_isrcs(h.idx)+ .iter()+ .map(|i| String::from_utf8_lossy(i).into_owned())+ .collect();+ println!(+ " [{}] {} — {} ({}s) {} {}",+ h.score,+ r.title,+ r.credit,+ r.length_secs,+ pack.recording_mbid(h.idx).map(|m| format_mbid(&m)).unwrap_or_default(),+ isrcs.join(",")+ );+ }+ // Walk the top release-group hit end to end: the exact path+ // GET /api/mb/release-group/:mbid then /api/mb/release/:mbid takes.+ if let Some(h) = rgs.first() {+ let t0 = std::time::Instant::now();+ let editions = pack.release_group_releases(h.idx);+ println!("\ntop release group has {} editions:", editions.len());+ for &r in editions.iter().take(3) {+ let rel = pack.release(r).expect("release");+ println!(+ " {} [{}] {} {} — {} tracks across {} media",+ rel.title,+ rel.status,+ rel.date.to_ws2(),+ rel.country,+ pack.release_track_count(r),+ pack.release_media(r).len()+ );+ }+ if let Some(&r) = editions.first() {+ let list = pack.release_tracklist(r);+ for (medium, tracks) in list.iter().take(1) {+ println!(" medium {} {}:", medium.position, medium.format);+ for t in tracks.iter().take(3) {+ println!(" {:>3}. {} — {} ({}s)", t.position, t.title, t.credit, t.length_secs);+ }+ }+ }+ println!(" walked in {:.1} ms", t0.elapsed().as_secs_f64() * 1000.0);+ }+ Ok(())+}+ fn cmd_inspect(path: &str) -> io::Result<()> { let bytes = std::fs::read(path)?; let pack = MbPack::open(&bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
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 9184232abd..7a86448d88 100644--- a/scripts/musicbrainz/mb_ingest/src/pack.rs+++ b/scripts/musicbrainz/mb_ingest/src/pack.rs@@ -540,6 +540,40 @@ impl StringPool { } } + /// Look a string up without interning it.+ pub fn lookup(&self, s: &str) -> Option<Sym> {+ if s.is_empty() {+ return Some(SYM_EMPTY);+ }+ let mask = self.slots.len() - 1;+ let mut j = (Self::hash(s.as_bytes()) as usize) & mask;+ loop {+ let v = self.slots[j];+ if v == 0 {+ return None;+ }+ if self.get(v - 1) == s.as_bytes() {+ return Some(v - 1);+ }+ j = (j + 1) & mask;+ }+ }++ /// Symbols in alphabetical order, without consuming the pool. Index into the+ /// result is a symbol's rank.+ pub fn sorted_order(&self) -> Vec<Sym> {+ let mut order: Vec<Sym> = (0..self.starts.len() as u32).collect();+ let bytes = &self.bytes;+ let starts = &self.starts;+ let key = |sym: &u32| -> &[u8] {+ let start = starts[*sym as usize] as usize;+ let end = bytes[start..].iter().position(|&b| b == 0).unwrap() + start;+ &bytes[start..end]+ };+ order.sort_unstable_by(|a, b| key(a).cmp(key(b)));+ order+ }+ /// Sort the distinct strings alphabetically and assign final byte offsets. /// /// The sorted bytes are deliberately **not** materialised — that would need 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 9e28caa859..47e397c139 100644--- a/scripts/musicbrainz/mb_ingest/src/reader.rs+++ b/scripts/musicbrainz/mb_ingest/src/reader.rs@@ -1017,20 +1017,33 @@ impl<'a> MbPack<'a> { (lower, hi) } - /// Terms scanned per query token. A one-letter query can match hundreds of- /// thousands of terms; the cap keeps a pathological query bounded instead of- /// walking the whole dictionary. Hits are still ranked, and exact matches sort- /// first, so the truncation is invisible for any realistic query.+ /// Terms scanned when expanding one query token's prefix. A token like "s"+ /// matches a great many dictionary terms; this bounds the expansion. const MAX_TERMS_PER_TOKEN: usize = 4096;- /// Postings decoded per query token, same reasoning.- const MAX_POSTINGS_PER_TOKEN: usize = 400_000;+ /// Candidates materialised from the most selective token. Only reached by a+ /// query made entirely of very common words, where any answer is arbitrary.+ const MAX_CANDIDATES: usize = 50_000; - fn matches_for_token(&self, token: &[u8], kind: u32) -> HashMap<u32, u32> {+ /// How many postings a term has, without decoding them: the count is the first+ /// uvarint of its block.+ fn postings_len(&self, term: usize) -> u64 {+ let postings = self.sec(pack::S_SEARCH_POSTINGS);+ let Some((_, start)) = self.term_idx(term) else { return 0 };+ let mut pos = start as usize;+ read_uvarint(postings, &mut pos).unwrap_or(0)+ }++ /// Total postings across a token's prefix expansion. Used to order tokens by+ /// selectivity, which is what makes the intersection both correct and cheap.+ fn token_cost(&self, lo: usize, hi: usize) -> u64 {+ (lo..hi).map(|t| self.postings_len(t)).sum()+ }++ /// Materialise the entities matching one token, from its postings.+ fn candidates_for_token(&self, lo: usize, hi: usize, token: &[u8], kind: u32) -> HashMap<u32, u32> { let mut out: HashMap<u32, u32> = HashMap::new();- let (lo, hi) = self.prefix_range(token); let postings = self.sec(pack::S_SEARCH_POSTINGS);- let mut budget = Self::MAX_POSTINGS_PER_TOKEN;- for t in lo..hi.min(lo + Self::MAX_TERMS_PER_TOKEN) {+ for t in lo..hi { let exact = self.term_bytes(t) == Some(token); let weight = if exact { 2 } else { 1 }; let (Some((_, start)), Some((_, end))) = (self.term_idx(t), self.term_idx(t + 1)) else {@@ -1043,26 +1056,35 @@ impl<'a> MbPack<'a> { let Some(n) = read_uvarint(postings, &mut pos) else { continue }; let mut prev: u64 = 0; for _ in 0..n {- if budget == 0 {- break;- }- budget -= 1; let Some(d) = read_uvarint(postings, &mut pos) else { break }; prev += d; let refv = prev as u32; if refv >> KIND_SHIFT == kind {- let idx = refv & IDX_MASK;- let e = out.entry(idx).or_insert(0);+ let e = out.entry(refv & IDX_MASK).or_insert(0); *e = (*e).max(weight); } }- if budget == 0 {+ if out.len() >= Self::MAX_CANDIDATES { break; } } out } + /// The text a given entity is searched by.+ 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| g.title),+ _ => self.recording(idx).map(|r| r.title),+ }+ .unwrap_or(Cow::Borrowed(""))+ }++ /// Above this many postings, a token is verified against candidates' text+ /// instead of by decoding its list. Below it, decoding is cheaper and exact.+ const POSTING_INTERSECT_MAX: u64 = 2_000_000;+ fn search(&self, q: &str, limit: usize, kind: u32, count: u32) -> Vec<SearchHit> { if limit == 0 || count == 0 || self.term_count() == 0 { return Vec::new();@@ -1072,30 +1094,86 @@ impl<'a> MbPack<'a> { if tokens.is_empty() { return Vec::new(); }- // AND across tokens: "dark side" should not return everything matching- // "side". Intersecting the smallest set first keeps the work down.- let mut acc: Option<HashMap<u32, u32>> = None;- for token in &tokens {- let m = self.matches_for_token(token.as_bytes(), kind);- if m.is_empty() {+ tokens.sort();+ tokens.dedup();++ // Expand each token's prefix and cost it. A token matching no term at all+ // means the AND can never be satisfied.+ let mut expanded: Vec<(String, usize, usize, u64)> = Vec::with_capacity(tokens.len());+ for t in &tokens {+ let (lo, hi) = self.prefix_range(t.as_bytes());+ if lo >= hi { return Vec::new(); }- acc = Some(match acc {- None => m,- Some(prev) => {- let (small, large) = if prev.len() <= m.len() { (prev, m) } else { (m, prev) };- small- .into_iter()- .filter_map(|(k, v)| large.get(&k).map(|w| (k, v + w)))- .collect()+ let cost = self.token_cost(lo, hi);+ expanded.push((t.clone(), lo, hi, cost));+ }+ // Most selective first. This is the whole trick, and it is what makes a+ // query containing a common word ("of", "the") work at all: capping those+ // posting lists silently broke the intersection before.+ expanded.sort_by_key(|e| e.3);++ let (ref rarest, lo, hi, _) = expanded[0];+ let mut cands = self.candidates_for_token(lo, hi, rarest.as_bytes(), kind);+ if cands.is_empty() {+ return Vec::new();+ }++ // Cheap tokens narrow the set by intersecting posting lists. Expensive ones+ // are deferred to a single text-verification pass, so a high-frequency token+ // costs O(candidates) rather than O(its posting list) -- and the candidate+ // set is already small by the time we get there.+ let mut deferred: Vec<&str> = Vec::new();+ for (token, tlo, thi, cost) in expanded.iter().skip(1) {+ if *cost > Self::POSTING_INTERSECT_MAX {+ deferred.push(token);+ continue;+ }+ let other = self.candidates_for_token(*tlo, *thi, token.as_bytes(), kind);+ cands.retain(|idx, w| match other.get(idx) {+ Some(ow) => {+ *w += *ow;+ true }+ None => false, });- if acc.as_ref().is_some_and(|a| a.is_empty()) {+ if cands.is_empty() { return Vec::new(); } }- let acc = acc.unwrap_or_default();- let mut hits: Vec<SearchHit> = acc++ if !deferred.is_empty() {+ // One text fetch per surviving candidate, checking every deferred token+ // against its terms at once. Fetching the text is what costs (a zstd+ // block decompression), so it must not happen per token.+ 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);+ let mut total = w;+ let mut all = true;+ for token in &deferred {+ match Self::weight_in_terms(&buf, token) {+ Some(tw) => total += tw,+ None => {+ all = false;+ break;+ }+ }+ }+ if all {+ next.insert(idx, total);+ }+ }+ cands = next;+ if cands.is_empty() {+ return Vec::new();+ }+ }++ let mut hits: Vec<SearchHit> = cands .into_iter() .filter(|&(idx, _)| idx < count) .map(|(idx, exactness)| SearchHit {@@ -1109,6 +1187,21 @@ impl<'a> MbPack<'a> { hits } + /// Weight of `token` against an already-tokenised text: 2 for an exact term,+ /// 1 for a word-prefix, `None` for no match.+ fn weight_in_terms(terms: &[String], token: &str) -> Option<u32> {+ let mut best = None;+ for term in terms {+ if term == token {+ return Some(2);+ }+ if term.starts_with(token) {+ best = Some(1);+ }+ }+ best+ }+ /// A free popularity proxy: how many children the entity has, which is a CSR /// range length already in the pack. Recordings have no child list, so they /// rank on match quality alone.