SimpleBackuper/DB/BaseTable.pm

filter: Statementstmt 83.1% 79 / 95 Branchbran 81.6% 49 / 60 Conditioncond 60.0% 3 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 75.0% 9 / 12 Podpod 0.0% 0 / 6 totaltotal 78.4% 142 / 181 CC 34 SCAR 28.9
CC
34
Cov
82%
_find
27.2
find_row
12.0
new
6.0
CRAP 18.0
« SimpleBackuper/RegularFile.pm ↑ Summary SimpleBackuper/_print_table.pm »
1 package App::SimpleBackuper::DB::BaseTable;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings FATAL => qw( all );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
9 0 my($class, $number_of_records) = @_;
10
11 0 ▶ $number_of_records //= 0;
Condition0%0/2
$number_of_records //= 0
l!l
00
MC/DC0%0/1
$number_of_records //= 0
$number_of_records
Truth table0%0/2
A
A= $number_of_records
Aresult
11
00
12 0 my @arr = (undef) x $number_of_records;
13
14 0 return bless \@arr => $class;
15 }
16  
17 0 ▶sub find_row {
Subroutine find_row: not called CC=3 SCAR=24.8
Pod undocumented
18 0 my $self = shift;
19 0 my($from, $to) = $self->_find(@_);
20 0 ▶ die "Found more then one" if $to > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $to > $from 0 0 0
21 0 ▶ return undef if $from > $to;
Branch: 1 branch
BranchTrueFalseTotal
if $from > $to 0 0 0
22 0 return $self->unpack( $self->[ $from ] );
23 }
24  
25 0 ▶sub find_all {
Subroutine find_all: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my($self) = shift;
27 0 my($from, $to) = $self->_find(@_);
28 #warn "$self->_find(@_): from=$from, to=$to";
29 #warn "self=[@$self]";
30 0 my @result = map { $self->unpack( $self->[ $_ ] ) } $from .. $to;
31 0 return \@result;
32 }
33  
34 # Binary search
35 # Parameters:
36 # 0 - search query us hashref with fields & values.
37 # Packed record must begins on this fields.
38 # All fields must be a fixed width when packed.
39 # Result:
40 # [0] - index of begin of range of matches;
41 # [1] - index of end of range of matches;
42 # For empty array result always is 0, -1.
43 # If nothing found, you can insert new value with queried fields values to index of begin of range of matches and it not broke sorting
44 2510 ▶sub _find {
Subroutine _find: called CC=27 SCAR=33.0
45 2510 my($self, $data) = @_;
46
47 2510 ▶ return 0, -1 if ! @$self;
Branch: 1 branch
BranchTrueFalseTotal
unless @$self 2 2508 2510
48
49 2508 ▶ $data = $self->pack($data) if ref($data);
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 2508 2508
50
51 2508 my($from, $to, $min_from, $min_to, $max_from, $max_to) = (undef, undef, 0, 0, $#$self, $#$self);
52 2508 ▶ while(! defined $from or ! defined $to) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from and defined $to) 12422 2182 14604
Condition100%3/3
defined $from and defined $to
!ll&&!rl&&r
1065217702182
MC/DC100%2/2
defined $from and defined $to
defined $from defined $to
Truth table100%3/3
A and B
A= defined $fromB= defined $to
ABresult
0X0
100
111
53
54 12422 my $mid_from = int($min_from + ($max_from - $min_from) / 2);
55 12422 my $mid_to = int($min_to + ($max_to - $min_to) / 2);
56
57 12422 my $cmp;
58 12422 ▶ if(! defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $from) 10652 1770 12422
59 10652 $cmp = $data cmp substr( $self->[ $mid_from ], 0, length($data) );
60 10652 ▶ if($cmp == -1) { # data < mid_from
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == -1) { } 2221 8431 10652
elsif ($cmp == 1) { } 4168 4263 8431
61 2221 ▶ if($mid_from == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid_from == 0) { } 54 2167 2221
62 54 return 0, -1;
63 } else {
64 2167 $cmp = $data cmp substr( $self->[ $mid_from - 1 ], 0, length($data) );
65 2167 ▶ if($cmp == -1) { # data < mid_from-1
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == -1) { } 1781 386 2167
elsif ($cmp == 1) { } 121 265 386
66 1781 $max_from = $mid_from - 1;
67 1781 ▶ return 0, -1 if $max_from < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_from < 0 0 1781 1781
68 }
69 elsif($cmp == 1) { # data > mid_from-1
70 121 return $mid_from, $mid_from - 1;
71 }
72 else { # data = mid_from-1
73 265 $max_from = $to = $mid_from - 1;
74 }
75 }
76 }
77 elsif($cmp == 1) { # data > mid_from
78 4168 $min_from = $mid_from + 1;
79 4168 ▶ return $#$self + 1, $#$self if $min_from > $#$self;
Branch: 1 branch
BranchTrueFalseTotal
if $min_from > $self->$#* 15 4153 4168
80 }
81 else { # data = mid_from
82 4263 ▶ if($mid_from == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid_from == 0) { } 372 3891 4263
83 372 $from = 0;
84 } else {
85 3891 $cmp = $data cmp substr( $self->[ $mid_from - 1 ], 0, length($data) );
86 3891 ▶ if($cmp == -1) { # data < mid_from-1
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == -1) { } 0 3891 3891
elsif ($cmp == 1) { } 1546 2345 3891
87 0 die "Array is not sorted: item #".($mid_from - 1)." > item #$mid_from ($self->[$mid_from-1] > $self->[$mid_from])";
88 }
89 elsif($cmp == 1) { # data > mid_from-1
90 1546 $from = $mid_from;
91 }
92 else { # data = mid_from-1
93 2345 $max_from = $mid_from - 1;
94 2345 ▶ return 0, -1 if $max_from < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_from < 0 0 2345 2345
95 }
96 }
97 }
98 }
99
100 12232 ▶ if(! defined $to) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to) 10256 1976 12232
101 10256 $cmp = $data cmp substr( $self->[ $mid_to ], 0, length($data) );
102 10256 ▶ if($cmp == 1) { # data > mid_to
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == 1) { } 2190 8066 10256
elsif ($cmp == -1) { } 3624 4442 8066
103 2190 ▶ if($mid_to == $#$self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid_to == $self->$#*) { } 16 2174 2190
104 16 return $#$self + 1, $#$self;
105 } else {
106 2174 $cmp = $data cmp substr( $self->[ $mid_to + 1 ], 0, length($data) );
107 2174 ▶ if($cmp == 1) { # data > mid_to+1
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == 1) { } 1801 373 2174
elsif ($cmp == -1) { } 92 281 373
108 1801 $min_to = $mid_to + 2;
109 1801 ▶ return $#$self + 1, $#$self if $min_to > $#$self;
Branch: 1 branch
BranchTrueFalseTotal
if $min_to > $self->$#* 28 1773 1801
110 }
111 elsif($cmp == -1) { # data < mid_to+1
112 92 return $mid_to + 1, $mid_to;
113 }
114 else { # data = mid_to+1
115 281 $min_to = $from = $mid_to + 1;
116 }
117 }
118 }
119 elsif($cmp == -1) { # data < mid_to
120 3624 $max_to = $mid_to;
121 }
122 else { # data = mid_to
123 4442 ▶ if($mid_to == $#$self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mid_to == $self->$#*) { } 391 4051 4442
124 391 $to = $#$self;
125 } else {
126 4051 $cmp = $data cmp substr( $self->[ $mid_to + 1 ], 0, length($data) );
127 4051 ▶ if($cmp == -1) { # data < mid_to+1
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp == -1) { } 1552 2499 4051
elsif ($cmp == 1) { } 0 2499 2499
128 1552 $to = $mid_to;
129 }
130 elsif($cmp == 1) { # data > mid_to+1
131 0 die "Array is not sorted: item #$mid_to > item #".($mid_to + 1)." ($self->[$mid_to] > $self->[$mid_to+1])";
132 }
133 else { # data = mid_to+1
134 2499 $min_to = $mid_to + 1;
135 }
136 }
137 }
138 }
139 }
140
141 2182 return $from, $to;
142 }
143  
144 4 ▶sub upsert {
Subroutine upsert: called CC=4 SCAR=13.9
Pod undocumented
145 4 my($self, $search_row, $data) = @_;
146
147 4 $_ = $self->pack( $_ ) foreach grep { ref $_ } $search_row, $data;
148
149 4 my($from, $to) = $self->_find( $search_row );
150
151 4 ▶ if($to < $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to < $from) { } 3 1 4
152 3 splice( @$self, $from, 0, $data );
153 } else {
154 1 ▶ die "Found more then 1 row, can't update" if $to > $from;
Branch: 1 branch
BranchTrueFalseTotal
if $to > $from 0 1 1
155 1 $self->[ $from ] = $data;
156 }
157
158 4 return $self;
159 }
160  
161 3 ▶sub delete {
Subroutine delete: called CC=2 SCAR=7.0
Pod undocumented
162 3 my($self, $row) = @_;
163
164 3 my($from, $to) = $self->_find($row);
165 3 ▶ confess "Value ".Data::Dumper->new($row)->Indent(0)->Terse(1)->Pair('=>')->Quotekeys(0)->Sortkeys(1)->Dump()." wasn't found in $self" if $to < $from;
Branch: 1 branch
BranchTrueFalseTotal
if $to < $from 0 3 3
166 3 splice(@$self, $from, $to - $from + 1);
167
168 3 return $self;
169 }
170  
171 4 ▶use App::SimpleBackuper::DB::Packer;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 12 ▶sub packer { shift; App::SimpleBackuper::DB::Packer->new( @_ ) }
Subroutine packer: called CC=1 SCAR=0.0
Pod undocumented
173  
174 4 1;
« SimpleBackuper/RegularFile.pm ↑ Summary SimpleBackuper/_print_table.pm »