1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use primal::Sieve;
use rand::{Rng, XorShiftRng};
use siphasher::sip::SipHasher;
use std::hash::{Hash, Hasher};
use std::iter;

/// A hashing ring implemented using maglev hashing.
///
/// Maglev hashing produces a lookup table that allows finding a node in constant time by
/// generating random permutations.
///
/// # Examples
/// ```
/// use hash_rings::maglev::Ring;
///
/// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
///
/// assert_eq!(ring.get_node(&"point-1"), &"node-3");
/// assert_eq!(ring.nodes(), 3);
/// assert_eq!(ring.capacity(), 307);
/// ```
pub struct Ring<'a, T>
where
    T: 'a + Hash,
{
    nodes: Vec<&'a T>,
    lookup: Vec<usize>,
    hasher: SipHasher,
}

impl<'a, T> Ring<'a, T>
where
    T: 'a + Hash,
{
    fn get_hashers() -> [SipHasher; 2] {
        let mut rng = XorShiftRng::new_unseeded();
        [
            SipHasher::new_with_keys(rng.next_u64(), rng.next_u64()),
            SipHasher::new_with_keys(rng.next_u64(), rng.next_u64()),
        ]
    }

    /// Constructs a new `Ring<T>` with a specified list of nodes.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
    /// ```
    pub fn new(nodes: Vec<&'a T>) -> Self {
        assert!(!nodes.is_empty());
        let capacity_hint = nodes.len() * 100;
        Ring::with_capacity_hint(nodes, capacity_hint)
    }

    /// Constructs a new `Ring<T>` with a specified list of nodes and a capacity hint. The actual
    /// capacity of the ring will always be the next prime greater than or equal to
    /// `capacity_hint`. If nodes are removed and the ring is regenerated, the ring should be
    /// rebuilt with the same capacity.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::with_capacity_hint(vec![&"node-1", &"node-2", &"node-3"], 100);
    /// assert_eq!(ring.capacity(), 101);
    /// ```
    pub fn with_capacity_hint(nodes: Vec<&'a T>, capacity_hint: usize) -> Self {
        let hashers = Self::get_hashers();
        let lookup = Self::populate(&hashers, &nodes, capacity_hint);
        Ring {
            nodes,
            lookup,
            hasher: hashers[0],
        }
    }

    fn get_hash<U>(hasher: SipHasher, key: &U) -> usize
    where
        U: Hash,
    {
        let mut sip = hasher;
        key.hash(&mut sip);
        sip.finish() as usize
    }

    fn populate(hashers: &[SipHasher; 2], nodes: &[&T], capacity_hint: usize) -> Vec<usize> {
        let m = Sieve::new(capacity_hint * 2)
            .primes_from(capacity_hint)
            .next()
            .unwrap();
        let n = nodes.len();

        let permutation: Vec<Vec<usize>> = nodes
            .iter()
            .map(|node| {
                let offset = Self::get_hash(hashers[0], node) % m;
                let skip = (Self::get_hash(hashers[1], node) % (m - 1)) + 1;
                (0..m).map(|i| (offset + i * skip) % m).collect()
            })
            .collect();

        let mut next: Vec<usize> = iter::repeat(0).take(n).collect();
        let mut entry: Vec<usize> = iter::repeat(<usize>::max_value()).take(m).collect();

        let mut i = 0;
        while i < m {
            for j in 0..n {
                let mut c = permutation[j][next[j]];
                while entry[c] != <usize>::max_value() {
                    next[j] += 1;
                    c = permutation[j][next[j]];
                }
                entry[c] = j;
                next[j] += 1;
                i += 1;

                if i == m {
                    break;
                }
            }
        }

        entry
    }

    /// Returns the number of nodes in the ring.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
    /// assert_eq!(ring.nodes(), 3);
    /// ```
    pub fn nodes(&self) -> usize {
        self.nodes.len()
    }

    /// Returns the capacity of the ring. If nodes are removed and the ring is regenerated, the
    /// ring should be rebuilt with the same capacity.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
    /// assert_eq!(ring.capacity(), 307);
    /// ```
    pub fn capacity(&self) -> usize {
        self.lookup.len()
    }

    /// Returns the node associated with a key.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
    /// assert_eq!(ring.get_node(&"point-1"), &"node-3");
    /// ```
    pub fn get_node<U>(&self, key: &U) -> &T
    where
        U: Hash,
    {
        let index = Self::get_hash(self.hasher, key) % self.capacity();
        self.nodes[self.lookup[index]]
    }

    /// Returns an iterator over the ring. The iterator will yield the nodes in the ring.
    ///
    /// # Examples
    /// ```
    /// use hash_rings::maglev::Ring;
    ///
    /// let ring = Ring::new(vec![&"node-1", &"node-2", &"node-3"]);
    ///
    /// let mut iterator = ring.iter();
    /// assert_eq!(iterator.next(), Some(&"node-1"));
    /// assert_eq!(iterator.next(), Some(&"node-2"));
    /// assert_eq!(iterator.next(), Some(&"node-3"));
    /// assert_eq!(iterator.next(), None);
    /// ```
    pub fn iter(&'a self) -> impl Iterator<Item = &'a T> {
        self.nodes.iter().map(|node| *node)
    }
}

impl<'a, T> IntoIterator for &'a Ring<'a, T>
where
    T: Hash + Eq,
{
    type Item = (&'a T);
    type IntoIter = Box<Iterator<Item = &'a T> + 'a>;

    fn into_iter(self) -> Self::IntoIter {
        Box::new(self.iter())
    }
}

#[cfg(test)]
mod tests {
    use super::Ring;

    #[test]
    #[should_panic]
    fn test_new_empty() {
        let _ring: Ring<u32> = Ring::new(vec![]);
    }

    #[test]
    fn test_get_node() {
        let ring = Ring::new(vec![&0, &1, &2]);
        assert_eq!(ring.get_node(&0), &0);
        assert_eq!(ring.get_node(&1), &1);

        let ring = Ring::with_capacity_hint(vec![&0, &1], ring.capacity());
        assert_eq!(ring.get_node(&0), &0);
        assert_eq!(ring.get_node(&1), &1);
    }

    #[test]
    fn test_nodes() {
        let ring = Ring::new(vec![&0, &1, &2]);
        assert_eq!(ring.nodes(), 3);
    }

    #[test]
    fn test_capacity() {
        let ring = Ring::new(vec![&0, &1, &2]);
        assert_eq!(ring.capacity(), 307);

        let ring = Ring::with_capacity_hint(vec![&0, &1], ring.capacity());
        assert_eq!(ring.capacity(), 307);
    }

    #[test]
    fn test_iter() {
        let ring = Ring::new(vec![&0, &1, &2]);

        let mut iterator = ring.iter();
        assert_eq!(iterator.next(), Some(&0));
        assert_eq!(iterator.next(), Some(&1));
        assert_eq!(iterator.next(), Some(&2));
        assert_eq!(iterator.next(), None);
    }
}