Safe Haskell | None |
---|
Data.Hash.Consistent
A consistent hash is a technique to manage the fair distribution of cacheable entities among hosts. Each host identifier has its crc32 hash calculated and stored in a Vector along with its canonical host name. The host identifier may be differentiated from its canonical host name by a multiplying factor, in our case a simple integer appeneded to the hostname to provide it with a number of entries in the consistent hash, all evenly distributed.
This technique is explained in these links:
http:en.wikipedia.orgwikiConsistent_hashing
http:www.tomkleinpeter.com20080317programmers-toolbox-part-3-consistent-hashing/
Here is a small program illustrating its use:
module Main where import qualified Data.Hash.Consistent as CH main = do let hosts = [hi.example.net,bar.example.net,foo.example.net] :: [CH.Host] let n = 2 :: Int let ch = CH.new print $ show $ ch ch <- return $ CH.add hosts n ch print $ show $ ch let fh = [head hosts] :: [CH.Host] let hh_fh = CH.hashHosts fh n print hh_fh ch <- return $ CH.del fh n ch print $ show $ ch let i = 770931073 let tgt = CH.targetHost i ch print tgt return ()
License info:
The license is a simple BSD3-style license available here: https:www.b7j0c.orgstufflicense.txt
Documentation
search :: Hash -> ConsistentHash -> IntSource
add :: [Host] -> Int -> ConsistentHash -> ConsistentHashSource
del :: [Host] -> Int -> ConsistentHash -> ConsistentHashSource
targetHost :: Hash -> ConsistentHash -> HashHostSource
hashHosts :: [Host] -> Int -> ConsistentHashSource
type ConsistentHash = Vector HashHostSource