blib/lib/Backup/Hanoi.pm

filter: Statementstmt 100% 37 / 37 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 96.6% 57 / 59 CC 8 SCAR 10.5
CC
8
Cov
96%
get_device_for_cycle
4.0
_get_right_zeros_from_digital_representation
3.0
new
3.0
CRAP 2.8
↑ Summary bin/backup-hanoi »
1 package Backup::Hanoi;
2 # ABSTRACT: select backup according to algo
3 7 $Backup::Hanoi::VERSION = '0.005';
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use v5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
10 11 my $class = shift;
11 11 my $devices = shift;
12 11 ▶ $devices = [] unless ($devices); # // operator needs v5.10
Branch: 1 branch
BranchTrueFalseTotal
unless $devices 0 11 11
13  
14 # the number of devices predicts the size of the cycles
15 11 my $device_count = scalar @{$devices};
16  
17 11 ▶ die "three devices needed.\n" if ($device_count < 3);
Branch: 1 branch
BranchTrueFalseTotal
if $device_count < 3 1 10 11
18  
19 # half a hanoi cycle is just what we need for backup
20 10 my $hanoi_cycles_half = (2**$device_count) / 2;
21  
22 10 my $self = { devices => $devices,
23 hanoi_cycles_half => $hanoi_cycles_half,
24 };
25  
26 10 bless $self, $class;
27  
28 10 return $self;
29 }
30  
31 106 ▶sub get_device_for_cycle {
Subroutine get_device_for_cycle: called CC=4 SCAR=13.9
Pod documented
32  
33 106 my $self = shift;
34 106 my $cycle = shift;
35  
36 106 ▶ $cycle = 0 unless ($cycle); # // operator needs v5.10
Branch: 1 branch
BranchTrueFalseTotal
unless $cycle 7 99 106
37  
38 # treat negative numbers as normal FIFO
39 # e.g. index -1 gives the second last element
40 return $self->{devices}
41 106 ▶ ->[(scalar @{$self->{devices}}) + $cycle -1]
Branch: 1 branch
BranchTrueFalseTotal
if $cycle < 0 13 93 106
42 if ($cycle < 0);
43  
44 # allow cycle to cross hanoi limit, map it to first half
45 93 my $modulo_cycle = $cycle % $self->{hanoi_cycles_half};
46  
47 # change zero to maximum, so that zero gets highest device
48 $modulo_cycle = $self->{hanoi_cycles_half}
49 93 ▶ if ($modulo_cycle == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $modulo_cycle == 0 20 73 93
50  
51 # calculate which device is used for given cycle
52 93 my $hanoi_number =
53 _get_right_zeros_from_digital_representation(
54 $modulo_cycle);
55  
56 # select and return device
57 93 return $self->{devices}->[$hanoi_number];
58  
59 }
60  
61 93 ▶sub _get_right_zeros_from_digital_representation {
Subroutine _get_right_zeros_from_digital_representation: called CC=3 SCAR=11.0
62  
63 93 my $number = shift;
64  
65 # convert to binary format: e.g. number 5 -> 101
66 93 my $binary = sprintf "%b", $number;
67  
68 # represent number zero as empty string
69 93 ▶ $binary = '' if ($binary == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $binary == 0 0 93 93
70  
71  
72 # count the zeros, at the right from the binary number
73  
74 93 my $zeros_from_the_right = 0;
75  
76 93 ▶ if ( $binary =~ /(0+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($binary =~ /(0+)$/) 52 41 93
77 52 my $zero_capture = $1;
78  
79 52 $zeros_from_the_right = length $zero_capture;
80 }
81  
82 93 return $zeros_from_the_right;
83 }
84  
85 7 1;
86  
87 __END__
↑ Summary bin/backup-hanoi »