NAME
Hash::ConsistentHash - Constant hash algorithm
VERSION
Version 0.05
SYNOPSIS
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
...
DESCRIPTION
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.
METHODS
new
Creates ConsistentHash object. It accept following params:
- hash_func
-
Hash function to be used on keys and buckets
- 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
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.
get_bucket
Lookup a key in the hash. Accept one param - the key. Returns a bucket.
Example:
my $chash = Hash::ConsistentHash->new(
buckets => [qw(A B C)],
hash_func=>\&crc32 );
my $bucket = $chash->get_bucket('foo');
SEE ALSO
Set::ConsistentHash, Algorithm::ConsistentHash::Ketama
AUTHOR
Luben Karavelov, <karavelov at spnet.net>
BUGS
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.
SUPPORT
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
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Hash-ConsistentHash
AnnoCPAN: Annotated CPAN documentation
CPAN Ratings
Search CPAN
LICENSE AND COPYRIGHT
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.