skip tab len

This commit is contained in:
Dmitry Belyaev 2022-10-07 23:00:09 +03:00
parent 9316bbbf5c
commit 73e8b34af4

View File

@ -242,15 +242,17 @@ fn read_from_db2(id: u32) -> Option<Question> {
let input = fs::File::open("test.bin").expect("open input"); let input = fs::File::open("test.bin").expect("open input");
let mut input = std::io::BufReader::with_capacity(4 * 1024, input); let mut input = std::io::BufReader::with_capacity(4 * 1024, input);
let mut len_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; let mut first_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
input.read_exact(&mut len_data).expect("read len"); input.read_exact(&mut first_data).expect("read first");
let tab_len = LSize::from_le_bytes(len_data) as usize; let first_pos = LSize::from_le_bytes(first_data);
let tab_len = (first_pos as usize) / LEN_SIZE;
let records_count = tab_len - 1; let records_count = tab_len - 1;
// println!( // println!(
// "read tab_len done: {}, pos {}", // "read tab_len done: {}, pos {}, first_pos {}",
// tab_len, // tab_len,
// input.stream_position().unwrap() // input.stream_position().unwrap(),
// first_pos
// ); // );
let index = match id { let index = match id {
@ -264,29 +266,36 @@ fn read_from_db2(id: u32) -> Option<Question> {
// println!("index {}", index); // println!("index {}", index);
assert!(index < records_count); assert!(index < records_count);
let tab_tail_len = (tab_len - (index + 2)) * LEN_SIZE; let data_pos = if 0 == index {
first_pos
} else {
let mut pos_curr_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; let mut pos_curr_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
let mut pos_next_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
input input
.seek_relative((index * LEN_SIZE).try_into().expect("index to i64")) .seek_relative(((index - 1) * LEN_SIZE).try_into().expect("index to i64"))
.expect("seek to tab pos"); .expect("seek to tab pos");
input input
.read_exact(&mut pos_curr_data) .read_exact(&mut pos_curr_data)
.expect("read current pos"); .expect("read current pos");
LSize::from_le_bytes(pos_curr_data)
};
let mut pos_next_data: [u8; LEN_SIZE] = [0; LEN_SIZE];
input.read_exact(&mut pos_next_data).expect("read next pos"); input.read_exact(&mut pos_next_data).expect("read next pos");
let data_pos = LSize::from_le_bytes(pos_curr_data);
let data_pos_next = LSize::from_le_bytes(pos_next_data); let data_pos_next = LSize::from_le_bytes(pos_next_data);
let data_len = data_pos_next - data_pos; let data_len = data_pos_next - data_pos;
// println!( let tab_pos = input.stream_position().expect("stream pos") as u32;
// "pos {} | next {} | len {} | tab_tail_len {}",
// data_pos, data_pos_next, data_len, tab_tail_len
// );
let data_pos = data_pos + tab_tail_len as LSize;
input.seek_relative(data_pos as i64).expect("q seek"); // println!(
// "pos {} | next {} | len {} | tab_pos {}",
// data_pos, data_pos_next, data_len, tab_pos
// );
let advance_len = data_pos - tab_pos as LSize;
// println!("advance_len {}", advance_len);
input.seek_relative(advance_len as i64).expect("q seek");
let reader = input.take(data_len as u64); let reader = input.take(data_len as u64);
let data = zstd::decode_all(reader).expect("zstd decode data"); let data = zstd::decode_all(reader).expect("zstd decode data");
@ -344,12 +353,11 @@ fn db_writer2_task(rx: mpsc::Receiver<Question>) {
buf.set_position(0); buf.set_position(0);
let tab_data = vec![0u8; (table.len() + 1) * LEN_SIZE]; let tab_data = vec![0u8; table.len() * LEN_SIZE];
let mut tab_cursor = Cursor::new(tab_data); let mut tab_cursor = Cursor::new(tab_data);
let len_data = (table.len() as LSize).to_le_bytes(); let tab_size = (table.len() * LEN_SIZE) as LSize;
tab_cursor.write_all(&len_data).expect("write len");
for pos in table { for pos in table {
let pos_data = pos.to_le_bytes(); let pos_data = (pos + tab_size).to_le_bytes();
tab_cursor.write_all(&pos_data).expect("write pos"); tab_cursor.write_all(&pos_data).expect("write pos");
} }