diff --git a/sdgb-cli/src/utils/helpers/mod.rs b/sdgb-cli/src/utils/helpers/mod.rs index ceda18c..f84b7c4 100644 --- a/sdgb-cli/src/utils/helpers/mod.rs +++ b/sdgb-cli/src/utils/helpers/mod.rs @@ -24,6 +24,27 @@ use bincode::{BorrowDecode, Encode, borrow_decode_from_slice}; use crate::{EARLY_QUIT, cache}; +pub fn read_cache( + definition: TableDefinition<'_, u32, Vec>, +) -> Result, Box> +where + D: for<'d> BorrowDecode<'d, ()>, +{ + let txn = cache::read_txn()?; + let table = cache::open_table_ro(&txn, definition)?; + + let config = + bincode::config::Configuration::::default().with_no_limit(); + + Ok(table + .iter()? + .flatten() + .map(|d| borrow_decode_from_slice(&d.1.value(), config)) + .flatten() + .map(|(value, _)| value) + .collect::>()) +} + pub fn dump_cache( output_path: impl AsRef, definition: TableDefinition<'_, u32, Vec>, @@ -40,23 +61,10 @@ where #[cfg(file_lock_ready)] file.try_lock()?; - let txn = cache::read_txn()?; - let table = cache::open_table_ro(&txn, definition)?; - - let config = - bincode::config::Configuration::::default().with_no_limit(); - - let user_ids = table - .iter()? - .flatten() - .map(|d| borrow_decode_from_slice(&d.1.value(), config)) - .flatten() - .map(|(value, _)| value) - .collect::>(); - + let data = read_cache::(definition)?; let writer = BufWriter::new(file); - serde_json::to_writer(writer, &user_ids)?; - info!("dumped {} user id", user_ids.len()); + serde_json::to_writer(writer, &data)?; + info!("dumped {} user id", data.len()); Ok(()) }