File Coverage

lib/AnyEvent/Memcached/Hash.pm
Criterion Covered Total %
statement 33 34 97.0
branch 5 8 62.5
condition 4 7 57.1
subroutine 7 8 87.5
pod 0 5 0.0
total 49 62 79.0


line stmt bran cond sub pod time code
1             package #hide
2             AnyEvent::Memcached::Hash;
3              
4 6     6   45991 use common::sense 2;m{
  6         111  
  6         44  
5             use strict;
6             use warnings;
7             }x;
8 6     6   369 use Carp;
  6         10  
  6         356  
9 6     6   3133 use String::CRC32 'crc32';
  6         2132  
  6         2559  
10              
11             sub new {
12 2     2 0 23 my $self = bless {}, shift;
13 2         5 my %args = @_;
14 2         9 $self->{buckets} = $args{buckets};
15 2         14 $self;
16             }
17              
18 0 0   0 0 0 sub set_buckets { shift->{buckets} = @_ == 1 ? $_[0] : \@_ }
19              
20 60     60 0 205 sub hash { (crc32($_[1]) >> 16) & 0x7fff; }
21              
22             sub peers {
23 41     41 0 41 my $self = shift;
24 41         39 my ($hash,$real,$peers) = @_;
25 41   50     68 $peers ||= {};
26 41         109 my $peer = $self->{buckets}->peer( $hash );
27 41   50     42 push @{ $peers->{$peer} ||= [] }, $real;
  41         190  
28 41         83 return $peers;
29             }
30              
31             sub hashes {
32 70     70 0 98 my $self = shift;
33 70 50       170 $self->{buckets} or croak "No buckets set during hashes";
34 70         69 my $keys = shift;
35 70         63 my $array;
36 70 100 66     215 if (ref $keys and ref $keys eq 'ARRAY') {
37 18         21 $array = 1;
38             } else {
39 52         91 $keys = [$keys];
40             }
41 70         72 my %peers;
42 70         95 for my $keyx (@$keys) {
43 82 100       171 my ($hash,$real) = ref $keyx ?
44             (int($keyx->[0]), $keyx->[1]) :
45             ($self->hash($keyx), $keyx);
46 82         224 $self->peers($hash,$real,\%peers);
47             }
48 70         413 return \%peers;
49             }
50             *servers = \&hashes;
51              
52             1;