blib/lib/AnyEvent/Memcached/Hash.pm untested

filter: Statementstmt 0.0% 0 / 37 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 65 SCAR 30.0
CC
4
Cov
0%
hashes
12.0
set_buckets
6.0
new
2.0
CRAP 20.0
1 package #hide
2 AnyEvent::Memcached::Hash;
3  
4 use common::sense 2;m{
5 use strict;
6 use warnings;
7 }x;
8 use Carp;
9 use String::CRC32 'crc32';
10  
11 sub new {
12 my $self = bless {}, shift;
13 my %args = @_;
14 $self->{buckets} = $args{buckets};
15 $self;
16 }
17  
18 sub set_buckets { shift->{buckets} = @_ == 1 ? $_[0] : \@_ }
19  
20 sub hash { (crc32($_[1]) >> 16) & 0x7fff; }
21  
22 sub peers {
23 my $self = shift;
24 my ($hash,$real,$peers) = @_;
25 $peers ||= {};
26 my $peer = $self->{buckets}->peer( $hash );
27 push @{ $peers->{$peer} ||= [] }, $real;
28 return $peers;
29 }
30  
31 sub hashes {
32 my $self = shift;
33 $self->{buckets} or croak "No buckets set during hashes";
34 my $keys = shift;
35 my $array;
36 if (ref $keys and ref $keys eq 'ARRAY') {
37 $array = 1;
38 } else {
39 $keys = [$keys];
40 }
41 my %peers;
42 for my $keyx (@$keys) {
43 my ($hash,$real) = ref $keyx ?
44 (int($keyx->[0]), $keyx->[1]) :
45 ($self->hash($keyx), $keyx);
46 $self->peers($hash,$real,\%peers);
47 }
48 return \%peers;
49 }
50 *servers = \&hashes;
51  
52 1;