db::Writer share cur_buf_raw

This commit is contained in:
Dmitry Belyaev 2022-10-13 20:57:20 +03:00
parent 3f4a144431
commit 38cee92d5f

View File

@ -48,9 +48,9 @@ where
{ {
out: io::BufWriter<fs::File>, out: io::BufWriter<fs::File>,
data_buf: Cursor<Vec<u8>>, data_buf: Cursor<Vec<u8>>,
cur_buf_raw: Cursor<Vec<u8>>,
table: Vec<LSize>, table: Vec<LSize>,
compress_lvl: i32, compress_lvl: i32,
current_buf_size: usize,
_t: PhantomData<*const T>, _t: PhantomData<*const T>,
} }
@ -64,17 +64,19 @@ where
let data_buf: Vec<u8> = Vec::with_capacity(opts.data_buf_size); let data_buf: Vec<u8> = Vec::with_capacity(opts.data_buf_size);
let data_buf = Cursor::new(data_buf); let data_buf = Cursor::new(data_buf);
let cur_buf_raw: Vec<u8> = Vec::with_capacity(opts.current_buf_size);
let cur_buf_raw = Cursor::new(cur_buf_raw);
let compress_lvl = opts.compress_lvl; let compress_lvl = opts.compress_lvl;
let current_buf_size = opts.current_buf_size;
let table: Vec<LSize> = vec![]; let table: Vec<LSize> = vec![];
Ok(Self { Ok(Self {
out, out,
data_buf, data_buf,
cur_buf_raw,
table, table,
compress_lvl, compress_lvl,
current_buf_size,
_t: PhantomData, _t: PhantomData,
}) })
} }
@ -84,25 +86,24 @@ where
let item_data = bincode::encode_to_vec(item, BINCODE_CFG).str_err()?; let item_data = bincode::encode_to_vec(item, BINCODE_CFG).str_err()?;
let cur_buf_raw: Vec<u8> = 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()?; let mut zencoder = zstd::stream::raw::Encoder::new(self.compress_lvl).str_err()?;
zencoder zencoder
.set_pledged_src_size(item_data.len() as u64) .set_pledged_src_size(item_data.len() as u64)
.str_err()?; .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.write_all(&item_data).str_err()?;
cur_buf_z.finish().str_err()?; cur_buf_z.finish().str_err()?;
cur_buf_z.flush().str_err()?; cur_buf_z.flush().str_err()?;
self.table.push(pos); 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(); let size = cur_buf_raw.position();
cur_buf_raw.set_position(0); cur_buf_raw.set_position(0);
let mut chunk_reader = cur_buf_raw.take(size); let mut chunk = cur_buf_raw.take(size);
io::copy(&mut chunk_reader, &mut self.data_buf).str_err()?; io::copy(&mut chunk, &mut self.data_buf).str_err()?;
Ok(()) Ok(())
} }