diff --git a/src/main.rs b/src/main.rs index 24bd221..d352dcd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -233,8 +233,10 @@ fn main() { action(); } +type LSize = u32; +const LEN_SIZE: usize = std::mem::size_of::(); + fn read_from_db2(id: u32) -> Option { - const LEN_SIZE: usize = std::mem::size_of::(); let cfg = bincode::config::standard(); let input = fs::File::open("test.bin").expect("open input"); @@ -242,7 +244,7 @@ fn read_from_db2(id: u32) -> Option { let mut len_data: [u8; LEN_SIZE] = [0; LEN_SIZE]; input.read_exact(&mut len_data).expect("read len"); - let tab_len = u32::from_le_bytes(len_data) as usize; + let tab_len = LSize::from_le_bytes(len_data) as usize; let records_count = tab_len - 1; // println!( @@ -274,15 +276,15 @@ fn read_from_db2(id: u32) -> Option { .expect("read current pos"); input.read_exact(&mut pos_next_data).expect("read next pos"); - let data_pos = u32::from_le_bytes(pos_curr_data); - let data_pos_next = u32::from_le_bytes(pos_next_data); + 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 u32; + let data_pos = data_pos + tab_tail_len as LSize; input.seek_relative(data_pos as i64).expect("q seek"); let reader = input.take(data_len as u64); @@ -309,16 +311,15 @@ fn write_db2() { println!("all done"); } fn db_writer2_task(rx: mpsc::Receiver) { - const LEN_SIZE: usize = std::mem::size_of::(); const COMP_DATA_LEVEL: i32 = 2; let cfg = bincode::config::standard(); - let mut table: Vec = vec![]; + let mut table: Vec = vec![]; let buf_data: Vec = Vec::with_capacity(500 * 1024 * 1024); use std::io::Cursor; let mut buf = Cursor::new(buf_data); - let mut pos: u32 = 0; + let mut pos: LSize = 0; let mut num = 1; rx.into_iter().for_each(|mut q| { q.num = num; @@ -329,7 +330,7 @@ fn db_writer2_task(rx: mpsc::Receiver) { //println!("write [{}]: {}", num, pos); - pos += len as u32; + pos += len as LSize; num += 1; }); table.push(pos); @@ -345,7 +346,7 @@ fn db_writer2_task(rx: mpsc::Receiver) { let tab_data = vec![0u8; (table.len() + 1) * LEN_SIZE]; let mut tab_cursor = Cursor::new(tab_data); - let len_data = (table.len() as u32).to_le_bytes(); + let len_data = (table.len() as LSize).to_le_bytes(); tab_cursor.write_all(&len_data).expect("write len"); for pos in table { let pos_data = pos.to_le_bytes();