File Coverage

blib/lib/Memcached/Client/Selector/Traditional.pm
Criterion Covered Total %
statement 44 47 93.6
branch 13 18 72.2
condition 1 2 50.0
subroutine 8 8 100.0
pod 2 2 100.0
total 68 77 88.3


line stmt bran cond sub pod time code
1             package Memcached::Client::Selector::Traditional;
2             BEGIN {
3 1     1   11910 $Memcached::Client::Selector::Traditional::VERSION = '2.01';
4             }
5             #ABSTRACT: Implements Traditional Memcached Hashing
6              
7 1     1   10 use strict;
  1         2  
  1         36  
8 1     1   6 use warnings;
  1         3  
  1         36  
9 1     1   584 use Memcached::Client::Log qw{DEBUG};
  1         3  
  1         64  
10 1     1   6 use String::CRC32 qw{crc32};
  1         2  
  1         43  
11 1     1   6 use base qw{Memcached::Client::Selector};
  1         3  
  1         1221  
12              
13              
14             sub set_servers {
15 6     6 1 17 my ($self, $list) = @_;
16 6         8 $self->log ("list: %s", $list) if DEBUG;
17 6 50       19 if ($list) {
18 6         8 my $count = scalar @{$list};
  6         17  
19 6 100       21 if (1 < $count) {
    50          
20 5         15 $self->{buckets} = [];
21 5         8 for my $server (@{$list}) {
  5         13  
22 11 100       25 if (ref $server eq "ARRAY") {
23 3         11 for (1..$server->[1]) {
24 5         8 push @{$self->{buckets}}, $server->[0];
  5         19  
25             }
26             } else {
27 8         12 push @{$self->{buckets}}, $server;
  8         27  
28             }
29             }
30 5         9 $self->{bucketcount} = scalar @{$self->{buckets}};
  5         14  
31 5         11 $self->log ("bucket count: %d\nbucket list: %s", $self->{bucketcount}, $self->{buckets}) if DEBUG;
32             } elsif (1 == $count) {
33 1 50       14 $self->{_single_sock} = ref $list->[0] ? $list->[0]->[0] : $list->[0];
34             }
35             } else {
36 0         0 delete $self->{buckets};
37 0         0 delete $self->{bucket_count};
38 0         0 delete $self->{_single_sock};
39             }
40              
41 6         57 1;
42             }
43              
44             sub get_server {
45 15     15 1 104 my ($self, $key, $namespace) = @_;
46 15 100       73 return unless $key;
47 13 100       41 return $self->{_single_sock} if $self->{_single_sock};
48 12 50       35 return unless $self->{buckets};
49 12   50     53 $namespace ||= "";
50 12 50       64 my $hash = ref $key ? int ($key->[0]) : crc32 ($namespace . $key) >> 16 & 0x7fff;
51 12         18 $self->log ("Hash is %d, bucket # %d, bucket %s", $hash, $hash % $self->{bucketcount}, $self->{buckets}->[$hash % $self->{bucketcount}]) if DEBUG;
52 12         73 return $self->{buckets}->[$hash % $self->{bucketcount}];
53             }
54              
55             1;
56              
57             __END__