pub struct NamespaceHasher { /* private fields */ }Expand description
A utility struct for namespacing keys using XXH3 hashing.
This ensures that keys are uniquely identified within a given namespace, even if they share the same suffix. By hashing both the namespace and key separately before combining them, it prevents unintended collisions.
§Example:
use simd_r_drive::utils::NamespaceHasher;
let hasher = NamespaceHasher::new(b"opt");
let namespaced_key = hasher.namespace(b"my_key");
assert_eq!(namespaced_key.len(), 16, "Namespaced key should be exactly 16 bytes");Implementations§
Source§impl NamespaceHasher
impl NamespaceHasher
Sourcepub fn new(prefix: &[u8]) -> Self
pub fn new(prefix: &[u8]) -> Self
Creates a new NamespaceHasher with a given prefix.
The prefix itself is hashed using XXH3 to ensure a unique namespace identifier. This avoids collisions between different namespaces while keeping the hashing fast.
§Arguments
prefix: A byte slice representing the namespace prefix.
§Returns
- A
NamespaceHasherinstance with a precomputed prefix hash.
Sourcepub fn namespace(&self, key: &[u8]) -> Vec<u8> ⓘ
pub fn namespace(&self, key: &[u8]) -> Vec<u8> ⓘ
Computes a namespaced key, returning it as a 16-byte vector.
The final namespaced key is derived by:
- Hashing the key separately to ensure uniqueness.
- Combining it with the precomputed namespace hash.
- Returning the concatenation of both hashes as a 16-byte key.
This ensures that:
- Different namespaces do not generate overlapping keys.
- Keys within a namespace remain uniquely identifiable.
§Arguments
key: A byte slice representing the key to be namespaced.
§Returns
- A
Vec<u8>containing the 16-byte namespaced key (8-byte prefix hash + 8-byte key hash).
Auto Trait Implementations§
impl Freeze for NamespaceHasher
impl RefUnwindSafe for NamespaceHasher
impl Send for NamespaceHasher
impl Sync for NamespaceHasher
impl Unpin for NamespaceHasher
impl UnwindSafe for NamespaceHasher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more