Improved hasher performance

This commit is contained in:
Jeremy Karst 2024-06-10 18:16:28 -04:00
parent 8ee4a67bbe
commit addf13ea39
2 changed files with 13 additions and 18 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target /target
*.db

View file

@ -10,39 +10,28 @@ use tqdm;
use rusqlite::{params, Connection}; use rusqlite::{params, Connection};
use dpc_pariter::IteratorExt as _; use dpc_pariter::IteratorExt as _;
const HASHLEN:usize = 1024*1; const HASHLEN:usize = 1024*8;
fn get_file_hash(size: u64, path: OsString) -> Result<Vec<u8>, std::io::Error> { fn get_file_hash(size: u64, path: OsString) -> Result<Vec<u8>, std::io::Error> {
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
let mut f: File = File::open(path)?; let mut f: File = File::open(path)?;
let mut data = [0u8; 8+2*HASHLEN]; hasher.update(&size.to_le_bytes());
data[..8].copy_from_slice(&size.to_le_bytes());
if size > HASHLEN as u64 { if size > HASHLEN as u64 {
let mut file_start_block = [0u8; HASHLEN]; let mut file_start_block = [0u8; HASHLEN];
f.read_exact(&mut file_start_block)?; f.read_exact(&mut file_start_block)?;
for i in 0..HASHLEN { hasher.update(file_start_block);
data[8+i] = file_start_block[i];
}
f.seek(SeekFrom::End(0 - HASHLEN as i64))?; f.seek(SeekFrom::End(0 - HASHLEN as i64))?;
let mut file_end_block = [0u8; HASHLEN]; let mut file_end_block = [0u8; HASHLEN];
f.read_exact(&mut file_end_block)?; f.read_exact(&mut file_end_block)?;
for i in 0..HASHLEN { hasher.update(file_end_block);
let i: usize = i;
data[8+i+HASHLEN] = file_end_block[i];
}
} else if size > 0 { } else if size > 0 {
let mut file_block = Vec::new(); let mut file_block = Vec::new();
f.read_to_end(&mut file_block)?; f.read_to_end(&mut file_block)?;
for i in 0..(size as usize) { hasher.update(file_block);
let i: usize = i;
data[8+i] = file_block[i];
}
} }
drop(f); drop(f);
hasher.update(data);
Ok(hasher.finalize().to_vec()) Ok(hasher.finalize().to_vec())
} }
fn process_entry(conn_mutex: &Arc<Mutex<Connection>>, entry: DirEntry) { fn process_entry(conn_mutex: &Arc<Mutex<Connection>>, entry: DirEntry) {
@ -90,7 +79,7 @@ fn process_entry(conn_mutex: &Arc<Mutex<Connection>>, entry: DirEntry) {
} }
fn main() { fn main() {
let path: &str = "G:\\"; let path: &str = "Y:\\";
let db_path: &str = "YDrive.db"; let db_path: &str = "YDrive.db";
let conn = Connection::open(db_path).unwrap(); let conn = Connection::open(db_path).unwrap();
@ -123,4 +112,9 @@ fn main() {
}; };
}).for_each(drop); }).for_each(drop);
/*
select name, hash, count(hash) as cnt from files group by hash
ORDER BY cnt DESC;
*/
} }