This is a creation in Article, where the information may have evolved or changed.
3.go Open Source Groupcache Project--consistenthash Code
The Consistenthash code is the implementation that provides the ring hash.
Consists of two files Consistenthash.go and consistenthash_test.go.
Package name is called Consistenthash
Defines a hash function, a map structure.
1 map structure
Defines a hash variable that returns an unsigned integer for the function
A variable of int
an int array
and a map type variable HASHMAP.
2 new function
The entry parameter is an shaping and a function.
The role of plastic replicas is to add in the amount of time needed to copy, in fact, how many keys to map a hash value in the meaning of the inside.
Create a map structure body. Where HashMap in the struct is created using make.
If the function is nil, it is assigned a value of CRC32. Checksumieee
3 IsEmpty function
Determine whether the number of keys in the map structure is 0.
4 Add function
The incoming parameter is a mutable string.
Adds the key to the hash.
Use range to get the entry, and then
Hashes the incoming parameter based on the hash function defined to get the hash value.
Adds a value, the hash value, to the keys shaping array in the map structure body.
The parametric is added to the HASHMAP variable in the map struct (map struct), the entry parameter is the key, and the hash value is the key value.
5 Get function
The entry parameter is a key value (string) that gets the close string based on the supplied key.
Determine if it is empty first.
The hash value is then computed based on the string.
Then, based on the search function in the sort package, the search () method uses the "binary lookup" algorithm to search for a specified slice [0:n] and returns the smallest I (0<=i<n) value that can make F (i) =true, and assumes that if f (i) =true, then F (i+1) =true, which means that the F () function returns True for the elements of the slice [0:n],i before the F () functions return false,i and I. However, the Search () method returns N (instead of 1) when I cannot be found in the slice (at which point the slice element does not return the F () function to True) when F (i) =true i.
If not found, 0 is returned.
Finally, the corresponding hash value in the HASHMAP variable in the map structure body is returned.