blib/lib/ShardedKV/Continuum/Jump.pm

filter: Statementstmt 87.8% 72 / 82 Branchbran 87.5% 7 / 8 Conditioncond 41.6% 5 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 75.0% 12 / 16 Podpod 0.0% 0 / 7 totaltotal 73.8% 96 / 130 CC 12 SCAR 12.6
CC
12
Cov
82%
_assert_spec_ok
8.7
extend
3.0
BUILD
3.0
CRAP 3.5
↑ Summary
1 package ShardedKV::Continuum::Jump;
2  
3 # This implementation is based heavily on the ShardedKV::Continuum::CHash module by Steffen Mueller
4  
5 1 ▶use 5.014002;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.04';
10  
11 1 ▶use Algorithm::ConsistentHash::JumpHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 with 'ShardedKV::Continuum';
17  
18 1 has '_orig_continuum_spec' => (
19 is => 'ro',
20 );
21  
22 1 has '_expanded_weights' => (
23 is => 'ro'
24 );
25  
26 50000 ▶sub choose {
Subroutine choose: called CC=1 SCAR=0.0
Pod undocumented
27 50000 my ($self, $key) = @_;
28 50000 my $idx = Algorithm::ConsistentHash::JumpHash::jumphash_siphash($key, scalar @{$self->_expanded_weights});
29 50000 $idx = $self->{_expanded_weights}->[$idx];
30 50000 return $self->_orig_continuum_spec->{ids}->[$idx];
31 }
32  
33 0 ▶sub serialize {
Subroutine serialize: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $self = shift;
35 0 encode_json( $self->_orig_continuum_spec )
36 }
37  
38 0 ▶sub deserialize {
Subroutine deserialize: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my $class = shift;
40 0 return $class->new(from => decode_json( $_[1] ));
41 }
42  
43 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $self = shift;
45 0 my $clone = ref($self)->new(from => $self->_orig_continuum_spec);
46 0 return $clone;
47 }
48  
49 3 ▶sub extend {
Subroutine extend: called CC=3 SCAR=11.0
Pod undocumented
50 3 my $self = shift;
51 3 my $spec = shift;
52  
53 3 $self->_assert_spec_ok($spec);
54  
55 # Build clone of the original spec (to avoid action at a
56 # distance) and add the new nodes.
57 3 my $orig_spec = $self->_orig_continuum_spec;
58 my $clone_spec = {
59 %$orig_spec, # replicas + in case there's other gunk in it, at least make an effort
60 3 ids => [ @{$orig_spec->{ids}} ], # deep clone
61 3 weights => [ @{$orig_spec->{weights}} ], # deep clone
62 };
63 3 push @{ $clone_spec->{ids} }, @{ $spec->{ids} };
64  
65 3 my $weights = $spec->{weights};
66 3 ▶ if (!defined($weights)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $weights) 1 2 3
67 1 push @$weights, 1 for 1..@{$spec->{ids}};
68 }
69 3 push @{ $clone_spec->{weights} }, @{ $weights };
70  
71 3 $self->{_expanded_weights} = _expand_weights($clone_spec->{weights});
72  
73 3 $self->{_orig_continuum_spec} = $clone_spec;
74 3 return 1;
75 }
76  
77 0 ▶sub get_bucket_names {
Subroutine get_bucket_names: not called CC=1 SCAR=6.9
Pod undocumented
78 0 my $self = shift;
79  
80 0 return @{ $self->_orig_continuum_spec()->{ids} };
81 }
82  
83 2 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
84 2 my ($self, $args) = @_;
85  
86 2 my $from = delete $args->{from};
87 2 $self->_assert_spec_ok($from);
88  
89 2 $self->{_orig_continuum_spec} = $from;
90  
91 2 my $weights = $from->{weights};
92 2 ▶ if (!defined($weights)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $weights) 1 1 2
93 1 $weights = [];
94 1 push @$weights, 1 for 1..@{$from->{ids}};
95 }
96  
97 2 $from->{weights} = $weights;
98 2 $self->{_expanded_weights} = _expand_weights($weights);
99 }
100  
101 5 ▶sub _expand_weights {
Subroutine _expand_weights: called CC=3 SCAR=11.0
102 5 my ($weights) = @_;
103  
104 5 my @expanded;
105  
106 5 ▶ for(my $i=0;$i<@$weights;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$weights) 28 5 33
107 28 my $w = $weights->[$i];
108 28 push @expanded, $i for 1..$w;
109 }
110  
111 5 return \@expanded;
112 }
113  
114 5 ▶sub _assert_spec_ok {
Subroutine _assert_spec_ok: called CC=6 SCAR=21.6
115 5 my ($self, $spec) = @_;
116  
117 Carp::croak("Continuum spec must be a hash of the form {ids => [qw(node1 node2 node3)]}")
118 if not ref($spec) eq 'HASH'
119 or not ref($spec->{ids}) eq 'ARRAY'
120 5 ▶ or (defined($spec->{weights}) && (ref($spec->{weights}) ne 'ARRAY' || @{$spec->{ids}} != @{$spec->{weights}}));
Branch: 1 branch
BranchTrueFalseTotal
if not ref $spec eq "HASH" && ref $spec->{"ids"} eq "ARRAY" or defined $spec->{"weights"} and ref $spec->{"weights"} ne "ARRAY" || @{$spec->{"ids"};} != @{$spec->{"weights"};} 0 5 5
Condition41%5/12
not ref $spec eq "HASH" && ref $spec->{"ids"} eq "ARRAY" or defined $spec->{"weights"} and ref $spec->{"weights"} ne "ARRAY" || @{$spec->{"ids"};} != @{$spec->{"weights"};}
l!l&&r!l&&!r
005
ref $spec eq 'HASH' && ref $spec->{'ids'} eq 'ARRAY'
!ll&&!rl&&r
005
defined $spec->{"weights"} and ref $spec->{"weights"} ne "ARRAY" || @{$spec->{"ids"};} != @{$spec->{"weights"};}
!ll&&!rl&&r
230
ref $spec->{"weights"} ne "ARRAY" or @{$spec->{"ids"};} != @{$spec->{"weights"};}
l!l&&r!l&&!r
003
MC/DC0%0/5
not ref $spec eq "HASH" && ref $spec->{"ids"} eq "ARRAY" or defined $spec->{"weights"} and ref $spec->{"weights"} ne "ARRAY" || @{$spec->{"ids"};} != @{$spec->{"weights"};}
ref $spec eq 'HASH' ref $spec->{'ids'} eq 'ARRAY' defined $spec->{"weights"} ref $spec->{"weights"} ne "ARRAY" @{$spec->{"ids"};} != @{$spec->{"weights"};}
Truth table33%2/6
not(A && B) or C and D or E
A= ref $spec eq 'HASH'B= ref $spec->{'ids'} eq 'ARRAY'C= defined $spec->{"weights"}D= ref $spec->{"weights"} ne "ARRAY"E= @{$spec->{"ids"};} != @{$spec->{"weights"};}
ABCDEresult
0XXXX1
10XXX1
1111X1
111011
110XX0
111000
121 5 return 1;
122 }
123  
124 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 __PACKAGE__->meta->make_immutable;
126  
127 __END__
↑ Summary