| 1 | package #hide | ||||||||||||
| 2 | AnyEvent::Memcached::Buckets; | ||||||||||||
| 3 | |||||||||||||
| 4 | 6 | ▶ | use common::sense 2;m{ | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | use strict; | ||||||||||||
| 6 | use warnings; | ||||||||||||
| 7 | }x; | ||||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | sub new { | ||||||||||||
| 11 | 2 | ▶ | my $self = bless {}, shift; | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 12 | 2 | my %args = @_; | |||||||||||
| 13 | 2 | $self->set_servers(delete $args{servers}); | |||||||||||
| 14 | 2 | $self; | |||||||||||
| 15 | } | ||||||||||||
| 16 | |||||||||||||
| 17 | sub set_servers { | ||||||||||||
| 18 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
set_servers: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||
| 19 | 2 | ▶ | my $list = shift or return; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 2 | ▶ | $list = [$list] unless ref $list eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 2 | ▶ | $self->{servers} = $list || []; | ||||||||||
|
Condition50%1/2
$list || []
MC/DC0%0/1
$list || []
$list
Truth table50%1/2
A
A= $list
| |||||||||||||
| 22 | 2 | $self->_init_buckets; | |||||||||||
| 23 | 2 | return $self; | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | sub peers { | ||||||||||||
| 27 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
peers: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 28 | 0 | ▶ | @{$self->{servers}} or croak "servers not set during peers"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 0 | $self->{peers}; | |||||||||||
| 30 | } | ||||||||||||
| 31 | |||||||||||||
| 32 | sub _init_buckets { | ||||||||||||
| 33 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_init_buckets: called CC=8 SCAR=20.9
| |||||||||||||
| 34 | 2 | ▶ | @{$self->{servers}} or croak "servers not set during _init_buckets"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | 2 | ▶ | if ($self->{buckets}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | @{ $self->{buckets} } = (); | |||||||||||
| 37 | } else { | ||||||||||||
| 38 | 2 | $self->{buckets} = []; | |||||||||||
| 39 | } | ||||||||||||
| 40 | 2 | my $bu = $self->{buckets}; | |||||||||||
| 41 | 2 | my $i = 0; | |||||||||||
| 42 | 2 | foreach my $v (@{$self->{servers}}) { | |||||||||||
| 43 | 10 | my $peer; | |||||||||||
| 44 | 10 | my $buck = [ 0+@$bu ]; | |||||||||||
| 45 | 10 | ▶ | if (ref $v eq "ARRAY") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 2 | $peer = $v->[0]; | |||||||||||
| 47 | 2 | for (1..$v->[1]) { | |||||||||||
| 48 | 6 | push @$bu, $v->[0]; | |||||||||||
| 49 | } | ||||||||||||
| 50 | 2 | push @$buck, $buck->[0]+1 .. $#$bu; | |||||||||||
| 51 | } else { | ||||||||||||
| 52 | 8 | push @$bu, $peer = $v; | |||||||||||
| 53 | } | ||||||||||||
| 54 | 10 | my ($host,$port) = $peer =~ /^(.+?)(?:|:(\d+))$/; | |||||||||||
| 55 | 10 | ▶ | if ( exists $self->{peers}{$peer} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 2 | push @{ $self->{peers}{$peer}{bucks} }, @$buck; | |||||||||||
| 57 | } else { | ||||||||||||
| 58 | 8 | ▶ | push @{ $self->{srv} ||= [] }, $peer; | ||||||||||
|
Condition100%2/2
$self->{"srv"} ||= []
MC/DC100%1/1
$self->{"srv"} ||= []
$self->{"srv"}
Truth table100%2/2
A
A= $self->{"srv"}
| |||||||||||||
| 59 | $self->{peers}{$peer} = { | ||||||||||||
| 60 | 8 | index => $#{ $self->{srv} }, | |||||||||||
| 61 | bucks => $buck, | ||||||||||||
| 62 | host => $host, | ||||||||||||
| 63 | port => $port, | ||||||||||||
| 64 | }; | ||||||||||||
| 65 | } | ||||||||||||
| 66 | } | ||||||||||||
| 67 | 2 | return; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | |||||||||||||
| 71 | sub peer { | ||||||||||||
| 72 | 82 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
peer: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 73 | 82 | my $hash = shift; | |||||||||||
| 74 | 82 | ▶ | @{$self->{servers}} or croak "servers not set during peer"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 82 | return $self->{buckets}[ $hash % @{ $self->{buckets} } ]; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | sub next { | ||||||||||||
| 79 | 41 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
next: called CC=8 SCAR=21.4
Pod
undocumented
| |||||||||||||
| 80 | 41 | my $srv = shift; | |||||||||||
| 81 | 41 | ▶ | @{$self->{servers}} or croak "servers not set during next"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 41 | ▶ | my $peer = $self->{peers}{$srv} or croak "No such server in buckets: $srv"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 41 | my %args = @_; | |||||||||||
| 84 | 41 | ▶ | my $by = $args{by} || 1; | ||||||||||
|
Condition50%1/2
$args{'by'} || 1
MC/DC0%0/1
$args{'by'} || 1
$args{'by'}
Truth table50%1/2
A
A= $args{'by'}
| |||||||||||||
| 85 | 41 | my $next = ( $peer->{index} + $by ) % @{$self->{srv}}; | |||||||||||
| 86 | 41 | ▶ | my $nsrv = $self->{srv}[$next] or die "Cant find next server by index $next"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 41 | ▶ | $nsrv = $nsrv->[0] if ref $nsrv; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | #warn R::Dump($nsrv); | ||||||||||||
| 89 | 41 | ▶ | if ( ( my @bucks = @{ $self->{peers}{$nsrv}{bucks} } ) > 1 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 5 | ▶ | my $which = $bucks[ ( $args{hash} || 0 ) % @bucks ]; | ||||||||||
|
Condition50%1/2
$args{'hash'} || 0
MC/DC0%0/1
$args{'hash'} || 0
$args{'hash'}
Truth table50%1/2
A
A= $args{'hash'}
| |||||||||||||
| 91 | #warn "many buckets (@bucks) for $nsrv. using $which ($self->{buckets}[ $which ])"; | ||||||||||||
| 92 | 5 | return $self->{buckets}[ $which ]; | |||||||||||
| 93 | } else { | ||||||||||||
| 94 | 36 | return $nsrv; | |||||||||||
| 95 | } | ||||||||||||
| 96 | } | ||||||||||||
| 97 | sub prev { | ||||||||||||
| 98 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
prev: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 99 | 0 | my $srv = shift; | |||||||||||
| 100 | 0 | my %args = @_; | |||||||||||
| 101 | 0 | ▶ | my $by = $args{by} || 1; | ||||||||||
|
Condition0%0/2
$args{'by'} || 1
MC/DC0%0/1
$args{'by'} || 1
$args{'by'}
Truth table0%0/2
A
A= $args{'by'}
| |||||||||||||
| 102 | 0 | $self->next( $srv, %args, by => @{$self->{srv}}-$by ); | |||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | 6 | 1; | |||||||||||