mirror of
https://github.com/NotAShelf/stash.git
synced 2026-04-17 16:19:51 +00:00
stash: deduplicate Fnv1aHasher; add derive for u64 wrapper
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: Ic2886815721f6eefc66a8ddacd44fb286a6a6964
This commit is contained in:
parent
a2a609f07d
commit
d643376cd7
4 changed files with 108 additions and 58 deletions
|
|
@ -11,6 +11,10 @@ use std::{
|
|||
|
||||
pub mod nonblocking;
|
||||
|
||||
use std::hash::Hasher;
|
||||
|
||||
use crate::hash::Fnv1aHasher;
|
||||
|
||||
/// Cache for process scanning results to avoid expensive `/proc` reads on every
|
||||
/// store operation. TTL of 5 seconds balances freshness with performance.
|
||||
struct ProcessCache {
|
||||
|
|
@ -66,35 +70,6 @@ impl ProcessCache {
|
|||
}
|
||||
}
|
||||
|
||||
/// FNV-1a hasher for deterministic hashing across process runs.
|
||||
/// Unlike `DefaultHasher` (`SipHash` with random seed), this produces stable
|
||||
/// hashes.
|
||||
pub struct Fnv1aHasher {
|
||||
state: u64,
|
||||
}
|
||||
|
||||
impl Fnv1aHasher {
|
||||
const FNV_OFFSET: u64 = 0xCBF29CE484222325;
|
||||
const FNV_PRIME: u64 = 0x100000001B3;
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
state: Self::FNV_OFFSET,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write(&mut self, bytes: &[u8]) {
|
||||
for byte in bytes {
|
||||
self.state ^= u64::from(*byte);
|
||||
self.state = self.state.wrapping_mul(Self::FNV_PRIME);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish(&self) -> u64 {
|
||||
self.state
|
||||
}
|
||||
}
|
||||
|
||||
use base64::prelude::*;
|
||||
use log::{debug, error, info, warn};
|
||||
use mime_sniffer::MimeTypeSniffer;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue