Hash::ConsistentHash - Constant hash algorithm
Version 0.05
use Hash::ConsistentHash; use String::CRC32; my $chash = Hash::ConsistentHash->new( buckets => [qw(10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4)], hash_func => \&crc32 ); # get just one bucket my $server = $chash->get_bucket('foo'); # or get a serie of non-repeating buckets through iterator my $next = $chash->lookup('bar'); $server = $next->(); # get bucket # do stuff with $server $server = $next->(); # get another bucket ...
Hash::ConsistentHash algorithm distributes keys over fixed number of buckets. Constant hash distribution means that if we add a bucket to a hash with N buckets filled with M keys we have to reassign only M/(N+1) keys to new buckets.
What puts apart this module from all similar modules available is that you could ask for non-repeatable series of buckets. Using this property you could implement not only consistent distribution but also redundancy - one key to be directed to more than one bucket.
Creates ConsistentHash object. It accept following params:
Hash function to be used on keys and buckets
Arrayref or Hashref. If buckets are given as arrayref they will have same weight. If given as hashref, every bucket could have differend weight.
Examples:
# All buckets have same weight so they will hold equal amount of keys my $chash = Hash::ConsistentHash->new( buckets => [qw(A B C)], hash_func=>\&crc32 ); # Bucket "B" will hold twice the amount of keys of bucket A or C my $chash = Cash::ConsistentHash->new( buckets => {A=>1, B=>2, C=>1}, hash_func=>\&crc32 );
Lookup a key in the hash. Accept one param - the key. Returns an iterator over the hash buckets.
Example:
my $chash = Hash::ConsistentHash->new( buckets => [qw(A B C)], hash_func=>\&crc32 ); my $next = $chash->lookup('foo'); my $bucket = $next->(); # B $bucket = $next->(); # A $bucket = $next->(); # C, hash is exhausted $bucket = $next->(); # A ...
Returned buckets will not repeat until all buckets are exhausted.
Lookup a key in the hash. Accept one param - the key. Returns a bucket.
my $chash = Hash::ConsistentHash->new( buckets => [qw(A B C)], hash_func=>\&crc32 ); my $bucket = $chash->get_bucket('foo');
Set::ConsistentHash, Algorithm::ConsistentHash::Ketama
Luben Karavelov, <karavelov at spnet.net>
<karavelov at spnet.net>
Please report any bugs or feature requests to bug-hash-consistenthash at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hash-ConsistentHash. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-hash-consistenthash at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Hash::ConsistentHash
You can also look for information at:
GIT repository with the latest stuff
https://github.com/luben/Hash-ConsistentHash
git://github.com/luben/Hash-ConsistentHash.git
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-ConsistentHash
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Hash-ConsistentHash
CPAN Ratings
http://cpanratings.perl.org/d/Hash-ConsistentHash
Search CPAN
http://search.cpan.org/dist/Hash-ConsistentHash/
Copyright 2011 Luben Karavelov.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Hash::ConsistentHash, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Hash::ConsistentHash
CPAN shell
perl -MCPAN -e shell install Hash::ConsistentHash
For more information on module installation, please visit the detailed CPAN module installation guide.