musicbrainz: one-pass table extract for the full export
- Commit
c6fcc7afb35e44f7b161d6fd680c5b5d0ccafd5f- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-20 21:04 UTC (3 months ago)
- Parents
6405673
Diff
scripts/musicbrainz/mb_ingest/README.md
diff --git a/scripts/musicbrainz/mb_ingest/README.md b/scripts/musicbrainz/mb_ingest/README.mdindex 8b4a7392c2..67b1c78396 100644--- a/scripts/musicbrainz/mb_ingest/README.md+++ b/scripts/musicbrainz/mb_ingest/README.md@@ -17,8 +17,13 @@ convention as `scripts/maps/gtfs_ingest`. ```sh cargo build --release -# Build from the real export. Either a .tar.bz2 (streamed through `tar`, nothing-# is extracted to disk) or a directory of already-extracted tables.+# Recommended for the real 7 GB export: extract only the 20 tables this crate+# reads (one bz2 pass, ~13 GB of disk), then build from the directory.+./target/release/mb_ingest extract mbdump.tar.bz2 /scratch/mbdump+./target/release/mb_ingest build /scratch/mbdump musicbrainz.pack++# Also works, and streams with nothing extracted -- but costs one bz2+# decompression pass PER TABLE, so it is for small archives and fixtures. ./target/release/mb_ingest build mbdump.tar.bz2 musicbrainz.pack # Prove the whole pipeline on a synthetic dump in ~a second.@@ -26,7 +31,7 @@ cargo build --release ./target/release/mb_ingest build /tmp/mb musicbrainz.pack ./target/release/mb_ingest inspect musicbrainz.pack -cargo test # 31 tests, including a full round trip through the reader+cargo test # 36 tests, including a full round trip through the reader ``` Flags: `--tier-a` adds track MBIDs (+918 MB at full scale), `--official-only`@@ -137,10 +142,14 @@ Projected peak for a full run, again a projection and not a measurement: An external merge sort for the track pass, and spilling the output sections to temporary files, are the two changes that would bring that down if a build has to-run somewhere smaller. Wall time is dominated by bz2 decompression: `tar` is-invoked once per table, so the archive is decompressed ~20 times. Extracting the-20 tables once and building from the directory is much faster if the disk is-available (~13 GB for the tables this crate reads, versus ~40 GB for everything).+run somewhere smaller. Wall time is dominated by bz2 decompression, which is why+`extract` exists: one pass over the archive rather than one per table. The build+itself is a sort plus linear scans. Output is written to `<out>.pack.tmp` and+renamed, so a crashed build never leaves a half-written pack where the server+would mmap it.++The same pack is produced byte for byte whether the input is the archive or an+extracted directory; the round-trip test asserts reproducibility. ## What tier B gives up
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 e7cc289d7e..1424c35534 100644--- a/scripts/musicbrainz/mb_ingest/src/build.rs+++ b/scripts/musicbrainz/mb_ingest/src/build.rs@@ -38,6 +38,31 @@ use crate::reader::encode_recording_credit; /// an absurd primary key. const MAX_ID: u32 = 400_000_000; +/// Every table this crate reads, in the order the passes want them. All of these+/// live in the core `mbdump.tar.bz2`; none needs `mbdump-derived.tar.bz2`.+pub const TABLES: &[&str] = &[+ "area",+ "iso_3166_1",+ "artist_type",+ "release_group_primary_type",+ "release_group_secondary_type",+ "release_status",+ "medium_format",+ "artist_credit",+ "artist_credit_name",+ "artist",+ "release_group",+ "release_group_meta",+ "release_group_secondary_type_join",+ "release",+ "release_country",+ "release_unknown_country",+ "medium",+ "recording",+ "track",+ "isrc",+];+ #[derive(Clone, Copy)] pub struct BuildOptions { pub include_track_mbids: bool,
scripts/musicbrainz/mb_ingest/src/copy.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/copy.rs b/scripts/musicbrainz/mb_ingest/src/copy.rsindex 7c44924849..1d7e37b11b 100644--- a/scripts/musicbrainz/mb_ingest/src/copy.rs+++ b/scripts/musicbrainz/mb_ingest/src/copy.rs@@ -177,6 +177,42 @@ impl Input { std::str::from_utf8(&d).ok().and_then(|s| s.parse().ok()).unwrap_or(0) } + /// Extract just `tables` (plus `TIMESTAMP`) into `dir` in ONE pass.+ ///+ /// Reading tables straight out of the archive costs one full bz2 decompression+ /// per table, because `tar` has to stream to the member it wants. That is fine+ /// for a fixture and badly wrong for the real 7 GB dump: ~20 tables means ~20+ /// passes over ~40 GB of decompressed output. Extracting the tables this crate+ /// actually reads costs one pass and ~13 GB of disk, versus ~40 GB for the+ /// whole archive.+ pub fn extract_to(&self, dir: &Path, tables: &[&str]) -> io::Result<()> {+ let Input::Archive(path) = self else {+ return Err(other_err(format!(+ "{} is already a directory; nothing to extract",+ self.describe()+ )));+ };+ std::fs::create_dir_all(dir)?;+ let mut cmd = Command::new("tar");+ cmd.arg("-xf").arg(path).arg("-C").arg(dir).arg("TIMESTAMP");+ for t in tables {+ cmd.arg(format!("mbdump/{t}"));+ }+ let status = cmd.status().map_err(|e| {+ io::Error::new(+ e.kind(),+ format!("could not run `tar` to extract {}: {e}", path.display()),+ )+ })?;+ if !status.success() {+ return Err(other_err(format!(+ "tar failed extracting {}; is it the core mbdump.tar.bz2?",+ path.display()+ )));+ }+ Ok(())+ }+ fn open_reader(&self, table: &str) -> io::Result<Option<TableStream>> { match self { Input::Dir(dir) => {
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 75405a5c47..52b3405934 100644--- a/scripts/musicbrainz/mb_ingest/src/main.rs+++ b/scripts/musicbrainz/mb_ingest/src/main.rs@@ -5,6 +5,7 @@ //! mb_ingest build <mbdump.tar.bz2 | dir> <out.pack> [--tier-a] [--official-only] //! [--no-isrcs] [--no-recording-search] //! [--no-recording-mbids] [-q]+//! mb_ingest extract <mbdump.tar.bz2> <dir> one-pass extract of just the tables needed //! mb_ingest fixture <dir> write the synthetic test dump //! mb_ingest inspect <out.pack> header, section sizes and a smoke query //! ```@@ -13,7 +14,7 @@ use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process::ExitCode; -use mb_ingest::build::{build, BuildOptions};+use mb_ingest::build::{build, BuildOptions, TABLES}; use mb_ingest::copy::Input; use mb_ingest::fixture::write_fixture; use mb_ingest::pack::format_mbid;@@ -35,6 +36,7 @@ fn usage() -> io::Error { "usage:\n \ mb_ingest build <mbdump.tar.bz2|dir> <out.pack> [--tier-a] [--official-only] \ [--no-isrcs] [--no-recording-search] [--no-recording-mbids] [-q]\n \+ mb_ingest extract <mbdump.tar.bz2> <dir>\n \ mb_ingest fixture <dir>\n \ mb_ingest inspect <pack>", )@@ -44,6 +46,18 @@ fn run() -> io::Result<()> { let args: Vec<String> = std::env::args().skip(1).collect(); match args.first().map(String::as_str) { Some("build") => cmd_build(&args[1..]),+ Some("extract") => {+ let (archive, dir) = (args.get(1).ok_or_else(usage)?, args.get(2).ok_or_else(usage)?);+ let input = Input::detect(Path::new(archive));+ let started = std::time::Instant::now();+ input.extract_to(Path::new(dir), TABLES)?;+ println!(+ "extracted {} tables to {dir} in {:.1}s",+ TABLES.len(),+ started.elapsed().as_secs_f64()+ );+ Ok(())+ } Some("fixture") => { let dir = args.get(1).ok_or_else(usage)?; write_fixture(Path::new(dir))?;@@ -79,6 +93,15 @@ fn cmd_build(args: &[String]) -> io::Result<()> { return Err(usage()); } let input = Input::detect(Path::new(positional[0]));+ if matches!(input, Input::Archive(_)) {+ eprintln!(+ "note: reading tables straight out of an archive costs one bz2 decompression \+ pass per table ({} of them). For the 7 GB full export, run `mb_ingest extract \+ {} <dir>` once (~13 GB of disk) and build from <dir> instead.",+ TABLES.len(),+ positional[0]+ );+ } // Write to a .tmp and rename, so a crashed build never leaves a half-written // pack where the server will mmap it (the sb_build.sh:90-92 convention). let final_path = PathBuf::from(positional[1]);