blib/lib/ShardedKV/Continuum/CHash.pm

filter: Statementstmt 82.4% 47 / 57 Branchbran 33.3% 2 / 6 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 76.9% 10 / 13 Podpod 0.0% 0 / 7 totaltotal 66.3% 61 / 92 CC 6 SCAR 10.0
CC
6
Cov
74%
serialize
6.0
_assert_spec_ok
5.2
BUILD
2.1
CRAP 2.7
↑ Summary
1 package ShardedKV::Continuum::CHash;
2 {
3 3 $ShardedKV::Continuum::CHash::VERSION = '0.01';
4 }
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: Continuum implementation based on Algorithm::ConsistentHash::CHash
7 3 ▶use Algorithm::ConsistentHash::CHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 with 'ShardedKV::Continuum';
11  
12 3 has '_orig_continuum_spec' => (
13 is => 'ro',
14 );
15  
16 3 has '_chash' => (
17 is => 'ro',
18 isa => 'Algorithm::ConsistentHash::CHash',
19 );
20  
21 21877 ▶sub choose {
Subroutine choose: called CC=1 SCAR=0.0
Pod undocumented
22 21877 $_[0]->_chash->lookup($_[1])
23 }
24  
25 # FIXME losing logger
26 0 ▶sub serialize {
Subroutine serialize: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my $self = shift;
28 0 my $logger = $self->{logger};
29 0 ▶ $logger->debug("Serializing continuum, this will lose the logger!") if $logger;
Branch: 1 branch
BranchTrueFalseTotal
if $logger 0 0 0
30 0 encode_json( $self->_orig_continuum_spec )
31 }
32  
33 0 ▶sub deserialize {
Subroutine deserialize: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $class = shift;
35 0 return $class->new(from => decode_json( $_[1] ));
36 }
37  
38 2 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
39 2 my $self = shift;
40 2 my $clone = ref($self)->new(from => $self->_orig_continuum_spec);
41 2 $clone->{logger} = $self->{logger};
42 2 return $clone;
43 }
44  
45 2 ▶sub extend {
Subroutine extend: called CC=1 SCAR=0.0
Pod undocumented
46 2 my $self = shift;
47 2 my $spec = shift;
48
49 2 $self->_assert_spec_ok($spec);
50  
51 # Build clone of the original spec (to avoid action at a
52 # distance) and add the new nodes.
53 2 my $orig_spec = $self->_orig_continuum_spec;
54 my $clone_spec = {
55 %$orig_spec, # replicas + in case there's other gunk in it, at least make an effort
56 2 ids => [ @{$orig_spec->{ids}} ], # deep clone
57 };
58 2 push @{ $clone_spec->{ids} }, @{ $spec->{ids} };
59  
60 2 $self->{_chash} = $self->_make_chash($clone_spec);
61 2 $self->{_orig_continuum_spec} = $clone_spec;
62 2 return 1;
63 }
64  
65 0 ▶sub get_bucket_names {
Subroutine get_bucket_names: not called CC=1 SCAR=6.9
Pod undocumented
66 0 my $self = shift;
67  
68 0 return @{ $self->_orig_continuum_spec()->{ids} };
69 }
70  
71 6 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
72 6 my ($self, $args) = @_;
73  
74 6 my $from = delete $args->{from};
75 6 ▶ if (ref($from) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $from eq 'HASH') { } 6 0 6
76 6 $self->{_chash} = $self->_make_chash($from);
77 6 $self->{_orig_continuum_spec} = $from;
78 }
79 else {
80 0 die "Invalid 'from' specification for " . __PACKAGE__;
81 }
82 }
83  
84 10 ▶sub _assert_spec_ok {
Subroutine _assert_spec_ok: called CC=4 SCAR=16.4
85 10 my ($self, $spec) = @_;
86 Carp::croak("Continuum spec must be a hash of the form {ids => [qw(node1 node2 node3)], replicas => 123}")
87 if not ref($spec) eq 'HASH'
88 or not ref($spec->{ids}) eq 'ARRAY'
89 10 ▶ or not @{$spec->{ids}};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY" and @{$spec->{"ids"};} 0 10 10
Condition33%2/6
ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY" and @{$spec->{"ids"};}
!ll&&!rl&&r
0010
ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/3
ref $spec eq "HASH" and ref $spec->{"ids"} eq "ARRAY" and @{$spec->{"ids"};}
ref $spec eq "HASH" ref $spec->{"ids"} eq "ARRAY" @{$spec->{"ids"};}
Truth table25%1/4
A and B and C
A= ref $spec eq "HASH"B= ref $spec->{"ids"} eq "ARRAY"C= @{$spec->{"ids"};}
ABCresult
0XX0
10X0
1100
1111
90 10 return 1;
91 }
92  
93 8 ▶sub _make_chash {
Subroutine _make_chash: called CC=1 SCAR=0.0
94 8 my ($self, $spec) = @_;
95  
96 8 $self->_assert_spec_ok($spec);
97  
98 8 return Algorithm::ConsistentHash::CHash->new(%$spec);
99 }
100  
101 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 3 __PACKAGE__->meta->make_immutable;
103  
104 __END__
↑ Summary