| 1 | package ShardedKV::Continuum::Ketama; | ||||||||||||||||||||||||||||
| 2 | 3 | $ShardedKV::Continuum::Ketama::VERSION = '0.20'; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Continuum implementation based on ketama consistent hashing | ||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Algorithm::ConsistentHash::Ketama; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use JSON::XS qw(encode_json decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | with 'ShardedKV::Continuum'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | has '_orig_continuum_spec' => ( | |||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | has '_ketama' => ( | |||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | isa => 'Algorithm::ConsistentHash::Ketama', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22045 | ▶ | sub choose { | ||||||||||||||||||||||||||
|
Subroutine
choose: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 22045 | $_[0]->_ketama->hash($_[1]) | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # FIXME losing logger | ||||||||||||||||||||||||||||
| 24 | 0 | ▶ | sub serialize { | ||||||||||||||||||||||||||
|
Subroutine
serialize: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 26 | 0 | my $logger = $self->{logger}; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | $logger->debug("Serializing continuum, this will lose the logger!") if $logger; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 0 | encode_json( $self->_orig_continuum_spec ) | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub deserialize { | ||||||||||||||||||||||||||
|
Subroutine
deserialize: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 33 | 0 | return $class->new(from => decode_json( $_[1] )); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 38 | 2 | my $clone = ref($self)->new(from => $self->_orig_continuum_spec); | |||||||||||||||||||||||||||
| 39 | 2 | $clone->{logger} = $self->{logger}; | |||||||||||||||||||||||||||
| 40 | 2 | return $clone; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | ▶ | sub extend { | ||||||||||||||||||||||||||
|
Subroutine
extend: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 45 | 2 | my $spec = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | my $ketama = $self->_ketama; | |||||||||||||||||||||||||||
| 48 | 2 | ▶ | Carp::croak("Ketama spec must be an Array of Arrays, each inner record holding key and weight! This is not an array") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | if not ref($spec) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 50 | 2 | foreach my $elem (@$spec) { | |||||||||||||||||||||||||||
| 51 | 5 | ▶ | Carp::croak("Ketama spec must be an Array of Arrays, each inner record " | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $elem ne "ARRAY" or @$elem != 2
MC/DC0%0/2
ref $elem ne "ARRAY" or @$elem != 2
ref $elem ne "ARRAY"
@$elem != 2
Truth table33%1/3
A or B
A= ref $elem ne "ARRAY"B= @$elem != 2
| |||||||||||||||||||||||||||||
| 52 | . "holding key and weight! This particular record is not an array or does not hold two elements") | ||||||||||||||||||||||||||||
| 53 | if ref($elem) ne 'ARRAY' or @$elem != 2; | ||||||||||||||||||||||||||||
| 54 | 5 | $ketama->add_bucket(@$elem); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub get_bucket_names { | ||||||||||||||||||||||||||
|
Subroutine
get_bucket_names: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 0 | my $ketama = $self->_ketama; | |||||||||||||||||||||||||||
| 61 | 0 | my @buckets = $ketama->buckets; | |||||||||||||||||||||||||||
| 62 | 0 | return map $_->label, @buckets; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 6 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 6 | my $args = shift; | |||||||||||||||||||||||||||
| 68 | 6 | my $from = delete $args->{from}; | |||||||||||||||||||||||||||
| 69 | 6 | ▶ | if (ref($from) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 6 | $self->{_ketama} = $self->_make_ketama($from); | |||||||||||||||||||||||||||
| 71 | 6 | $self->{_orig_continuum_spec} = $from; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||
| 74 | 0 | die "Invalid 'from' specification for " . __PACKAGE__; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 6 | ▶ | sub _make_ketama { | ||||||||||||||||||||||||||
|
Subroutine
_make_ketama: called CC=6 SCAR=19.9
| |||||||||||||||||||||||||||||
| 79 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 80 | 6 | my $spec = shift; | |||||||||||||||||||||||||||
| 81 | 6 | my $ketama = Algorithm::ConsistentHash::Ketama->new; | |||||||||||||||||||||||||||
| 82 | 6 | ▶ | Carp::croak("Ketama spec must be an Array of Arrays, each inner record holding key and weight! This is not an array") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $spec eq "ARRAY" or @$spec == 0
MC/DC0%0/2
not ref $spec eq "ARRAY" or @$spec == 0
not ref $spec eq "ARRAY"
@$spec == 0
Truth table33%1/3
not(A) or B
A= not ref $spec eq "ARRAY"B= @$spec == 0
| |||||||||||||||||||||||||||||
| 83 | if not ref($spec) eq 'ARRAY' | ||||||||||||||||||||||||||||
| 84 | or @$spec == 0; | ||||||||||||||||||||||||||||
| 85 | 6 | foreach my $elem (@$spec) { | |||||||||||||||||||||||||||
| 86 | 16 | ▶ | Carp::croak("Ketama spec must be an Array of Arrays, each inner record " | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $elem ne "ARRAY" or @$elem != 2
MC/DC0%0/2
ref $elem ne "ARRAY" or @$elem != 2
ref $elem ne "ARRAY"
@$elem != 2
Truth table33%1/3
A or B
A= ref $elem ne "ARRAY"B= @$elem != 2
| |||||||||||||||||||||||||||||
| 87 | . "holding key and weight! This particular record is not an array or does not hold two elements") | ||||||||||||||||||||||||||||
| 88 | if ref($elem) ne 'ARRAY' or @$elem != 2; | ||||||||||||||||||||||||||||
| 89 | 16 | $ketama->add_bucket(@$elem); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 6 | return $ketama; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 3 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||