feat: one transaction, build in memory and vacuum out

This commit is contained in:
mehbark 2025-03-27 02:36:22 -04:00
parent 9337e57e0b
commit 049aa6f2b1

View file

@ -45,11 +45,13 @@ fn run(
); );
log::info!("opening connection"); log::info!("opening connection");
let mut conn = Connection::open(output_file)?; let mut conn = Connection::open_in_memory()?;
create_tables(&mut conn)?; create_tables(&mut conn)?;
insert_stats(&mut conn, &stats)?; insert_stats(&mut conn, &stats)?;
conn.execute("VACUUM INTO ?1", (output_file,))?;
Ok(()) Ok(())
} }
@ -58,8 +60,9 @@ fn run(
struct Config { struct Config {
#[arg()] #[arg()]
input_file: PathBuf, input_file: PathBuf,
// TODO: non-utf-8 (the problem is saving the database)
#[arg(short, long = "output")] #[arg(short, long = "output")]
output_file: PathBuf, output_file: String,
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -125,12 +128,12 @@ fn insert_stats(conn: &mut Connection, stats: &Stats) -> rusqlite::Result<()> {
let mon_count = stats.data.len(); let mon_count = stats.data.len();
let mon_count_digits = mon_count.to_string().len(); let mon_count_digits = mon_count.to_string().len();
let tx = conn.transaction()?;
for (i, (mon, data)) in stats.data.iter().enumerate() { for (i, (mon, data)) in stats.data.iter().enumerate() {
log::debug!( log::debug!(
"Processing mon {:mon_count_digits$}/{mon_count} ({mon})", "Processing mon {:mon_count_digits$}/{mon_count} ({mon})",
i + 1 i + 1
); );
let tx = conn.transaction()?;
// normalizing with mon_count gives us data that is much easier to work // normalizing with mon_count gives us data that is much easier to work
// with. for example, if pikachu has 10k count (weighted) and thunderbolt // with. for example, if pikachu has 10k count (weighted) and thunderbolt
@ -196,9 +199,8 @@ fn insert_stats(conn: &mut Connection, stats: &Stats) -> rusqlite::Result<()> {
(mon, opp, percentage, stddev), (mon, opp, percentage, stddev),
)?; )?;
} }
tx.commit()?;
} }
tx.commit()?;
Ok(()) Ok(())
} }