common len type and size

This commit is contained in:
Dmitry Belyaev 2022-10-07 16:26:08 +03:00
parent 7ee0b62ed7
commit 9316bbbf5c
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3

View File

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