This commit is contained in:
Dmitry Belyaev 2022-10-10 15:42:43 +03:00
parent 7c27a20ebd
commit 17446a6318
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

@ -66,7 +66,7 @@ where
let compress_lvl = opts.compress_lvl; let compress_lvl = opts.compress_lvl;
let current_buf_size = opts.current_buf_size; let current_buf_size = opts.current_buf_size;
let table: Vec<LSize> = vec![]; let table: Vec<LSize> = vec![];
Ok(Self { Ok(Self {
@ -83,7 +83,7 @@ where
let pos: LSize = self.data_buf.position() as LSize; let pos: LSize = self.data_buf.position() as LSize;
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: Vec<u8> = Vec::with_capacity(self.current_buf_size);
let cur_buf_raw = Cursor::new(cur_buf_raw); 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()?;
@ -99,7 +99,7 @@ where
self.table.push(pos); self.table.push(pos);
let (mut cur_buf_raw, _) = cur_buf_z.into_inner(); let (mut 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_reader = cur_buf_raw.take(size);
io::copy(&mut chunk_reader, &mut self.data_buf).str_err()?; io::copy(&mut chunk_reader, &mut self.data_buf).str_err()?;
@ -190,8 +190,10 @@ where
let tab_pos: u64 = (index * LEN_SIZE).try_into().str_err()?; let tab_pos: u64 = (index * LEN_SIZE).try_into().str_err()?;
let mut pos_curr_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; let mut pos_curr_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
let cur_pos = self.input.stream_position().str_err()? as i64; let cur_pos = self.input.stream_position().str_err()? as i64;
self.input.seek_relative((tab_pos as i64) - cur_pos).str_err()?; self.input
.seek_relative((tab_pos as i64) - cur_pos)
.str_err()?;
self.input.read_exact(&mut pos_curr_data).str_err()?; self.input.read_exact(&mut pos_curr_data).str_err()?;
LSize::from_le_bytes(pos_curr_data) LSize::from_le_bytes(pos_curr_data)
}; };
@ -220,7 +222,6 @@ where
pub fn iter(&mut self) -> ReaderIter<'_, T> { pub fn iter(&mut self) -> ReaderIter<'_, T> {
ReaderIter::new(self) ReaderIter::new(self)
} }
} }
pub struct ReaderIter<'a, T> pub struct ReaderIter<'a, T>
@ -316,14 +317,19 @@ mod test {
} }
fn gen_data(count: usize) -> impl Iterator<Item = TestData> { fn gen_data(count: usize) -> impl Iterator<Item = TestData> {
(0..count).into_iter().map(|i| TestData{ num: i as u64}) (0..count).into_iter().map(|i| TestData { num: i as u64 })
} }
#[test] #[test]
fn test_write_read() { fn test_write_read() {
let dir = tempdir().expect("tempdir"); let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp"); let tmpfile = dir.path().join("test.tmp");
let opts = WriterOpts { compress_lvl: 1, data_buf_size: 10 * 1024 * 1024, out_buf_size: 10 * 1024 * 1024, current_buf_size: 4096 }; let opts = WriterOpts {
compress_lvl: 1,
data_buf_size: 10 * 1024 * 1024,
out_buf_size: 10 * 1024 * 1024,
current_buf_size: 4096,
};
let mut writer: Writer<TestData> = Writer::new(&tmpfile, opts).expect("new writer"); let mut writer: Writer<TestData> = Writer::new(&tmpfile, opts).expect("new writer");
let items_iter = gen_data(5); let items_iter = gen_data(5);
@ -335,7 +341,7 @@ mod test {
let mut reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader"); let mut reader: Reader<TestData> = Reader::new(&tmpfile, 2048).expect("new reader");
assert_eq!(items.len(), reader.len()); assert_eq!(items.len(), reader.len());
for (idx,item) in items.iter().enumerate() { for (idx, item) in items.iter().enumerate() {
let ritem = reader.get(idx).expect("get"); let ritem = reader.get(idx).expect("get");
assert_eq!(*item, ritem); assert_eq!(*item, ritem);
} }
@ -345,7 +351,12 @@ mod test {
fn test_write_read_iter() { fn test_write_read_iter() {
let dir = tempdir().expect("tempdir"); let dir = tempdir().expect("tempdir");
let tmpfile = dir.path().join("test.tmp"); let tmpfile = dir.path().join("test.tmp");
let opts = WriterOpts { compress_lvl: 1, data_buf_size: 10 * 1024 * 1024, out_buf_size: 10 * 1024 * 1024, current_buf_size: 4096 }; let opts = WriterOpts {
compress_lvl: 1,
data_buf_size: 10 * 1024 * 1024,
out_buf_size: 10 * 1024 * 1024,
current_buf_size: 4096,
};
let mut writer: Writer<TestData> = Writer::new(&tmpfile, opts).expect("new writer"); let mut writer: Writer<TestData> = Writer::new(&tmpfile, opts).expect("new writer");
let items_iter = gen_data(10); let items_iter = gen_data(10);