Skip to main content

rocketmq_common/common/consistenthash/
hash_function.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Hash function trait for consistent hashing
16//!
17//! Provides an abstraction for hashing string keys to 64-bit integer values.
18
19/// Trait for hashing string keys to long integer values
20///
21/// Implementations of this trait provide different hash algorithms
22/// that can be used in consistent hashing rings to distribute keys
23/// across nodes.
24///
25/// # Example
26/// ```
27/// use rocketmq_common::common::consistenthash::HashFunction;
28///
29/// struct SimpleHashFunction;
30///
31/// impl HashFunction for SimpleHashFunction {
32///     fn hash(&self, key: &str) -> i64 {
33///         // Simple example: sum of character codes
34///         key.chars().map(|c| c as i64).sum()
35///     }
36/// }
37///
38/// let hasher = SimpleHashFunction;
39/// let hash_value = hasher.hash("test-key");
40/// assert!(hash_value > 0);
41/// ```
42pub trait HashFunction {
43    /// Hash a string key to a 64-bit signed integer value
44    ///
45    /// # Arguments
46    /// * `key` - The string key to hash
47    ///
48    /// # Returns
49    /// A 64-bit signed integer hash value
50    fn hash(&self, key: &str) -> i64;
51}