Skip to content

Commit

Permalink
Sarthak | Adds support for calculating the murmur hash of the key
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jul 14, 2024
1 parent af230d9 commit 2c04829
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
136 changes: 136 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
bytes = "1.6.1"
crossbeam-utils = "0.8.20"
crossbeam-utils = "0.8.20"
fasthash = "0.4.0"
14 changes: 14 additions & 0 deletions src/memory/key_value.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::hash::{Hash, Hasher};
use std::io::{Error, Read};

use bytes::{Buf, BufMut, BytesMut};
use fasthash::{FastHasher, MurmurHasher};

pub(crate) struct KeyValue {
key: Vec<u8>,
Expand Down Expand Up @@ -40,6 +42,12 @@ impl KeyValue {
return Ok(KeyValue::new(key, value));
}

pub(crate) fn hash_of(&self) -> u64 {
let mut hasher: MurmurHasher = MurmurHasher::new();
self.key.hash(&mut hasher);
hasher.finish()
}

pub(crate) fn key(&self) -> Vec<u8> {
return self.key.clone()
}
Expand All @@ -62,4 +70,10 @@ mod tests {
assert_eq!(b"raft", &decoded.key[..]);
assert_eq!(b"consensus", &decoded.value[..]);
}

#[test]
fn get_the_hash_of_the_key() {
let key_value = KeyValue::new(Vec::from(b"raft"), Vec::from(b"consensus"));
assert!(key_value.hash_of() > 0);
}
}

0 comments on commit 2c04829

Please sign in to comment.