NAME

Hash::ConsistentHash - Constant hash algorithm

VERSION

Version 0.01

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
);
my $next  = $chash->lookup('foo');
my $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.

METHODS

new

Creates ConsistentHash object. It accept following params:

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 );
factor

Randomization factor for the sequence. Default 10.

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.

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:

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.