Move hash generation into stateless function

This commit is contained in:
Greg Fitzgerald
2018-02-19 12:09:56 -07:00
parent d88d1b2a09
commit 4a7156de43
2 changed files with 12 additions and 16 deletions

View File

@ -7,7 +7,7 @@
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
use log::{Entry, Event}; use log::{hash, Entry, Event};
pub struct Historian { pub struct Historian {
pub sender: Sender<Event>, pub sender: Sender<Event>,
@ -64,24 +64,16 @@ pub fn create_logger(
receiver: Receiver<Event>, receiver: Receiver<Event>,
sender: Sender<Entry>, sender: Sender<Entry>,
) -> JoinHandle<(Entry, ExitReason)> { ) -> JoinHandle<(Entry, ExitReason)> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::thread; use std::thread;
thread::spawn(move || { thread::spawn(move || {
let mut end_hash = start_hash; let mut end_hash = start_hash;
let mut hasher = DefaultHasher::new();
let mut num_hashes = 0; let mut num_hashes = 0;
loop { loop {
match log_events(&receiver, &sender, num_hashes, end_hash) { match log_events(&receiver, &sender, num_hashes, end_hash) {
Ok(0) => { Ok(n) => num_hashes = n,
num_hashes = 0;
hasher = DefaultHasher::new();
}
Ok(_) => {}
Err(err) => return err, Err(err) => return err,
} }
end_hash.hash(&mut hasher); end_hash = hash(end_hash);
end_hash = hasher.finish();
num_hashes += 1; num_hashes += 1;
} }
}) })

View File

@ -48,15 +48,19 @@ impl Entry {
} }
} }
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'. pub fn hash(val: u64) -> u64 {
pub fn next_tick(start_hash: u64, num_hashes: u64) -> Entry {
use std::collections::hash_map::DefaultHasher; use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
let mut end_hash = start_hash;
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
val.hash(&mut hasher);
hasher.finish()
}
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
pub fn next_tick(start_hash: u64, num_hashes: u64) -> Entry {
let mut end_hash = start_hash;
for _ in 0..num_hashes { for _ in 0..num_hashes {
end_hash.hash(&mut hasher); end_hash = hash(end_hash);
end_hash = hasher.finish();
} }
Entry::new_tick(num_hashes, end_hash) Entry::new_tick(num_hashes, end_hash)
} }