diff --git a/src/main.rs b/src/main.rs index d352dcd..120c1a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -242,15 +242,17 @@ fn read_from_db2(id: u32) -> Option { let input = fs::File::open("test.bin").expect("open input"); let mut input = std::io::BufReader::with_capacity(4 * 1024, input); - let mut len_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; - input.read_exact(&mut len_data).expect("read len"); - let tab_len = LSize::from_le_bytes(len_data) as usize; + let mut first_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; + input.read_exact(&mut first_data).expect("read first"); + let first_pos = LSize::from_le_bytes(first_data); + let tab_len = (first_pos as usize) / LEN_SIZE; let records_count = tab_len - 1; // println!( - // "read tab_len done: {}, pos {}", + // "read tab_len done: {}, pos {}, first_pos {}", // tab_len, - // input.stream_position().unwrap() + // input.stream_position().unwrap(), + // first_pos // ); let index = match id { @@ -264,29 +266,36 @@ fn read_from_db2(id: u32) -> Option { // println!("index {}", index); 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]; + input + .seek_relative(((index - 1) * LEN_SIZE).try_into().expect("index to i64")) + .expect("seek to tab pos"); + input + .read_exact(&mut pos_curr_data) + .expect("read current pos"); + LSize::from_le_bytes(pos_curr_data) + }; - let mut pos_curr_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; let mut pos_next_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; - input - .seek_relative((index * LEN_SIZE).try_into().expect("index to i64")) - .expect("seek to tab pos"); - input - .read_exact(&mut pos_curr_data) - .expect("read current 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_len = data_pos_next - data_pos; - // println!( - // "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; + let tab_pos = input.stream_position().expect("stream pos") as u32; - 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 data = zstd::decode_all(reader).expect("zstd decode data"); @@ -344,12 +353,11 @@ fn db_writer2_task(rx: mpsc::Receiver) { 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 len_data = (table.len() as LSize).to_le_bytes(); - tab_cursor.write_all(&len_data).expect("write len"); + let tab_size = (table.len() * LEN_SIZE) as LSize; 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"); }