blib/lib/Algorithm/VectorClocks.pm

filter: Statementstmt 86.6% 97 / 112 Branchbran 53.5% 15 / 28 Conditioncond 61.1% 11 / 18 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 91.6% 22 / 24 Podpod 100% 8 / 8 totaltotal 78.8% 157 / 199 CC 29 SCAR 24.7
CC
29
Cov
78%
_is_independent
56.0
are_independent
20.0
_compare
7.0
CRAP 11.8
↑ Summary
1 package Algorithm::VectorClocks;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use version; our $VERSION = qv('0.1.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Perl6::Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 '""' => \&serialize,
17 '++' => \&increment,
18 '+=' => \&merge,
19 '==' => \&equal,
20 'eq' => \&equal,
21 '!=' => \&not_equal,
22 'ne' => \&not_equal,
23 fallback => undef,
24 3 );
25  
26 3 ▶use base qw(Class::Accessor::Fast Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 __PACKAGE__->mk_accessors(qw(clocks));
29 3 __PACKAGE__->mk_classdata($_) for qw(id json);
30  
31 3 __PACKAGE__->id(hostname);
32 3 __PACKAGE__->json(JSON::Any->new);
33  
34 39 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
35 39 my $class = shift;
36 39 my($arg) = @_;
37 39 ▶ my $self = UNIVERSAL::isa($arg, $class) ? $arg
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($arg, $class) ? : 15 24 39
Condition100%2/2
$arg || '{}'
l!l
177
MC/DC100%1/1
$arg || '{}'
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
38 : { clocks => $class->json->jsonToObj($arg || '{}') };
39 39 bless $self, $class;
40 }
41  
42 4 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod documented
43 4 my $self = shift;
44 4 $self->json->objToJson($self->clocks);
45 }
46  
47 4 ▶sub increment {
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
48 4 my $self = shift;
49 4 $self->clocks->{ $self->id }++; # increment its own clock
50 4 $self;
51 }
52  
53 3 ▶sub merge {
Subroutine merge: called CC=4 SCAR=13.9
Pod documented
54 3 my $self = shift;
55 3 my($other) = @_;
56 3 $other = __PACKAGE__->new($other);
57 3 my @ids = _list_ids($self, $other);
58 3 for my $id (@ids) {
59 $self->clocks->{$id}
60 5 ▶ = max( ($self->clocks->{$id} || 0), ($other->clocks->{$id} || 0) );
Condition75%3/4
$self->clocks->{$id} || 0
l!l
14
$other->clocks->{$id} || 0
l!l
50
MC/DC100%1/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC0%0/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table100%2/2
A
A= $self->clocks->{$id}
Aresult
11
00
Truth table50%1/2
A
A= $other->clocks->{$id}
Aresult
11
00
61 }
62 3 $self;
63 }
64  
65 6 ▶sub equal {
Subroutine equal: called CC=6 SCAR=18.2
Pod documented
66 6 my @vcs = @_;
67 6 $_ = __PACKAGE__->new($_) for @vcs;
68 6 my @ids = _list_ids(@vcs);
69 6 for my $id (@ids) {
70 return 0
71 10 ▶ unless ($vcs[0]->clocks->{$id} || 0) == ($vcs[1]->clocks->{$id} || 0);
Branch: 1 branch
BranchTrueFalseTotal
unless ($vcs[0]->clocks->{$id} || 0) == ($vcs[1]->clocks->{$id} || 0) 2 8 10
Condition50%2/4
$vcs[0]->clocks->{$id} || 0
l!l
100
$vcs[1]->clocks->{$id} || 0
l!l
100
MC/DC0%0/1
$vcs[0]->clocks->{$id} || 0
$vcs[0]->clocks->{$id}
MC/DC0%0/1
$vcs[1]->clocks->{$id} || 0
$vcs[1]->clocks->{$id}
Truth table50%1/2
A
A= $vcs[0]->clocks->{$id}
Aresult
11
00
Truth table50%1/2
A
A= $vcs[1]->clocks->{$id}
Aresult
11
00
72 }
73 4 return 1;
74 }
75  
76 3 ▶sub not_equal { !equal(@_) }
Subroutine not_equal: called CC=1 SCAR=0.0
Pod documented
77  
78 4 ▶sub order_vector_clocks :Export(:DEFAULT) {
Subroutine order_vector_clocks: called CC=2 SCAR=6.9
Pod documented
79 4 my($vcs) = @_;
80 4 my @vcs;
81 4 ▶ while (my($id, $vc) = each %$vcs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$vcs) 8 4 12
82 8 $vc = __PACKAGE__->new($vc);
83 8 $vc->{_id} = $id;
84 8 push @vcs, $vc;
85 }
86 4 @vcs = sort { _compare($b, $a) } @vcs;
87 4 _pack_independent_vector_clocks(@vcs);
88 3 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 4 ▶sub _pack_independent_vector_clocks {
Subroutine _pack_independent_vector_clocks: called CC=3 SCAR=11.0
91 4 my @vcs = @_;
92 4 my @ret;
93 4 my $i = 0;
94 4 ▶ while ($i < @vcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @vcs) 6 4 10
95 my @equals = (
96 $vcs[$i],
97 6 (grep { _compare($vcs[$i], $_) == 0 } @vcs[($i+1)..$#vcs]),
98 );
99 6 ▶ push @ret, @equals == 1 ? $equals[0]->{_id} : [ map $_->{_id}, @equals ];
Branch: 1 branch
BranchTrueFalseTotal
@equals == 1 ? : 4 2 6
100 6 $i += @equals;
101 }
102 4 @ret;
103 }
104  
105 0 ▶sub are_independent {
Subroutine are_independent: not called CC=4 SCAR=30.0
Pod documented
106 0 my @vcs = @_;
107 0 ▶ for (my $j = 0; $j < @vcs; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @vcs) 0 0 0
108 0 ▶ for (my $k = $j+1; $k < @vcs; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @vcs) 0 0 0
109 0 ▶ return 1 if $vcs[$j]->_is_independent($vcs[$k]);
Branch: 1 branch
BranchTrueFalseTotal
if $vcs[$j]->_is_independent($vcs[$k]) 0 0 0
110 }
111 }
112 }
113  
114 0 ▶sub _is_independent {
Subroutine _is_independent: not called CC=7 SCAR=40.3
115 0 my $self = shift;
116 0 my($other) = @_;
117 0 $other = __PACKAGE__->new($other);
118 0 my @ids = _list_ids($self, $other);
119 0 my $res = 0;
120 0 for my $id (@ids) {
121 0 ▶ my $r = ($self->clocks->{$id} || 0) - ($other->clocks->{$id} || 0);
Condition0%0/4
$self->clocks->{$id} || 0
l!l
00
$other->clocks->{$id} || 0
l!l
00
MC/DC0%0/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC0%0/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table0%0/2
A
A= $self->clocks->{$id}
Aresult
11
00
Truth table0%0/2
A
A= $other->clocks->{$id}
Aresult
11
00
122 0 ▶ if ($res == 0 ) { $res = $r }
Branch: 3 branches
BranchTrueFalseTotal
if ($res == 0) { } 0 0 0
elsif ($r == 0) { } 0 0 0
elsif ($res != $r) { } 0 0 0
123 elsif ($r == 0 ) { }
124 0 elsif ($res != $r) { return 1 }
125 }
126 0 0;
127 }
128  
129 9 ▶sub _compare {
Subroutine _compare: called CC=7 SCAR=19.5
130 9 my $self = shift;
131 9 my($other) = @_;
132 9 $other = __PACKAGE__->new($other);
133 9 my @ids = _list_ids($self, $other);
134 9 my $res = 0;
135 9 for my $id (@ids) {
136 24 ▶ my $r = ($self->clocks->{$id} || 0) - ($other->clocks->{$id} || 0);
Condition100%4/4
$self->clocks->{$id} || 0
l!l
213
$other->clocks->{$id} || 0
l!l
213
MC/DC100%1/1
$self->clocks->{$id} || 0
$self->clocks->{$id}
MC/DC100%1/1
$other->clocks->{$id} || 0
$other->clocks->{$id}
Truth table100%2/2
A
A= $self->clocks->{$id}
Aresult
11
00
Truth table100%2/2
A
A= $other->clocks->{$id}
Aresult
11
00
137 24 ▶ if ($res == 0 ) { $res = $r }
Branch: 3 branches
BranchTrueFalseTotal
if ($res == 0) { } 13 11 24
elsif ($r == 0) { } 7 4 11
elsif ($res != $r) { } 4 0 4
138 elsif ($r == 0 ) { }
139 4 elsif ($res != $r) { return 0 } # independent
140 }
141 5 $res;
142 }
143  
144 18 ▶sub _list_ids { uniq map { keys %{ $_->clocks } } @_ }
Subroutine _list_ids: called CC=1 SCAR=0.0
145  
146 3 1; # Magic true value required at end of module
147 __END__
↑ Summary