From 38cee92d5f73c7813437567d3a78d8911165ae26 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Thu, 13 Oct 2022 20:57:20 +0300 Subject: [PATCH] db::Writer share cur_buf_raw --- src/db.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/db.rs b/src/db.rs index d8afdbe..9d93cee 100644 --- a/src/db.rs +++ b/src/db.rs @@ -48,9 +48,9 @@ where { out: io::BufWriter, data_buf: Cursor>, + cur_buf_raw: Cursor>, table: Vec, compress_lvl: i32, - current_buf_size: usize, _t: PhantomData<*const T>, } @@ -64,17 +64,19 @@ where let data_buf: Vec = Vec::with_capacity(opts.data_buf_size); let data_buf = Cursor::new(data_buf); + let cur_buf_raw: Vec = Vec::with_capacity(opts.current_buf_size); + let cur_buf_raw = Cursor::new(cur_buf_raw); + let compress_lvl = opts.compress_lvl; - let current_buf_size = opts.current_buf_size; let table: Vec = vec![]; Ok(Self { out, data_buf, + cur_buf_raw, table, compress_lvl, - current_buf_size, _t: PhantomData, }) } @@ -84,25 +86,24 @@ where let item_data = bincode::encode_to_vec(item, BINCODE_CFG).str_err()?; - let cur_buf_raw: Vec = Vec::with_capacity(self.current_buf_size); - let cur_buf_raw = Cursor::new(cur_buf_raw); let mut zencoder = zstd::stream::raw::Encoder::new(self.compress_lvl).str_err()?; zencoder .set_pledged_src_size(item_data.len() as u64) .str_err()?; - let mut cur_buf_z = zstd::stream::zio::Writer::new(cur_buf_raw, zencoder); + self.cur_buf_raw.set_position(0); + let mut cur_buf_z = zstd::stream::zio::Writer::new(&mut self.cur_buf_raw, zencoder); cur_buf_z.write_all(&item_data).str_err()?; cur_buf_z.finish().str_err()?; cur_buf_z.flush().str_err()?; self.table.push(pos); - let (mut cur_buf_raw, _) = cur_buf_z.into_inner(); + let (cur_buf_raw, _) = cur_buf_z.into_inner(); let size = cur_buf_raw.position(); cur_buf_raw.set_position(0); - let mut chunk_reader = cur_buf_raw.take(size); - io::copy(&mut chunk_reader, &mut self.data_buf).str_err()?; + let mut chunk = cur_buf_raw.take(size); + io::copy(&mut chunk, &mut self.data_buf).str_err()?; Ok(()) }