blib/lib/App/dupfind/Guts.pm

filter: Statementstmt 33.7% 25 / 74 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 6 / 12 Podpod n/a totaltotal 31.0% 31 / 100 CC 12 SCAR 39.1
CC
12
Cov
28%
_pull_weeds
110.0
_plan_weed_passes
12.0
_build_ftl
2.0
CRAP 50.0
↑ Summary lib/App/dupfind/Guts/Algorithms.pm »
1 # ABSTRACT: Private methods supporting the App::dupfind::Common public interface
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package App::dupfind::Guts;
7 {
8 1 $App::dupfind::Guts::VERSION = '0.140230'; # TRIAL
9 }
10  
11 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use File::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 requires 'opts';
20  
21 1 with 'App::dupfind::Guts::Algorithms';
22  
23 1 has weed_pass_map => ( is => 'ro', builder => '_build_wpmap' );
24  
25 1 has ftl => ( is => 'ro', builder => '_build_ftl', lazy => 1 );
26  
27 0 ▶has stats => ( is => 'rw', builder => sub { {} } );
Subroutine _build_stats: not called CC=1 SCAR=1.2
28  
29  
30 sub _build_ftl
31 0 ▶{
Subroutine _build_ftl: not called CC=1 SCAR=6.9
32 0 my $self = shift;
33  
34 return File::Util->new
35 (
36 {
37 use_flock => 0,
38 diag => 1,
39 read_limit => $self->opts->{bytes},
40 abort_depth => $self->opts->{maxdepth},
41 0 onfail => 'undefined',
42 }
43 );
44 }
45  
46 sub _build_wpmap
47 0 ▶{
Subroutine _build_wpmap: not called CC=1 SCAR=6.9
48 {
49 0 last => '_get_last_bytes',
50 first => '_get_first_bytes',
51 middle => '_get_middle_byte',
52 middle_last => '_get_middle_last_bytes',
53 almost_middle => '_get_bytes_n_offset_n',
54 first_middle_last => '_get_first_middle_last_bytes',
55 }
56 }
57  
58 sub _plan_weed_passes
59 0 ▶{
Subroutine _plan_weed_passes: not called CC=3 SCAR=24.8
60 0 my $self = shift;
61 0 my @plan = ();
62  
63 0 for my $pass_type ( @{ $self->opts->{wpass} } )
64 {
65 die "Unrecognized weed pass type $pass_type"
66 0 ▶ if ! exists $self->weed_pass_map->{ $pass_type };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->weed_pass_map->{$pass_type} 0 0 0
67  
68 0 push @plan, $self->weed_pass_map->{ $pass_type };
69 }
70  
71 0 return @plan;
72 }
73  
74 sub _do_weed_pass
75 0 ▶{
Subroutine _do_weed_pass: not called CC=1 SCAR=6.9
76 0 my ( $self, $size_dups, $pass_type, $pass_count ) = @_;
77  
78 0 my $dup_count = $self->count_dups( $size_dups );
79  
80 0 my ( $new_count, $difference );
81  
82 0 $self->say_stderr( " $dup_count POTENTIAL DUPLICATES" );
83  
84 0 $size_dups = $self->_pull_weeds( $size_dups => $pass_type => $pass_count );
85  
86 0 $new_count = $self->count_dups( $size_dups );
87  
88 0 $difference = $dup_count - $new_count;
89  
90 0 $dup_count = $new_count;
91  
92 0 $self->say_stderr
93 (
94 sprintf ' ...ELIMINATED %d NON-DUPS IN PASS #%d. %d REMAIN',
95 $difference,
96 $pass_count,
97 $new_count
98 );
99  
100 0 return $size_dups;
101 }
102  
103 sub _pull_weeds
104 0 ▶{
Subroutine _pull_weeds: not called CC=10 SCAR=47.0
105 # weed out files that are obviously different, based on the last
106 # few bytes in the file. This saves us from unnecessary hashing
107  
108 0 my ( $self, $size_dups, $weeder, $pass_count ) = @_;
109  
110 0 my $len = $self->opts->{wpsize};
111  
112 0 my ( $progress, $i );
113  
114 0 ▶ if ( $self->opts->{progress} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{"progress"}) 0 0 0
115 {
116 0 my $dup_count = $self->count_dups( $size_dups );
117  
118 0 $progress = Term::ProgressBar->new
119 (
120 {
121 name => ' ...WEED-OUT PASS ' . $pass_count,
122 count => $dup_count,
123 remove => 1,
124 }
125 );
126 }
127  
128 0 for my $same_size ( keys %$size_dups )
129 {
130 0 my $same_bytes = {};
131 0 my $weed_failed = [];
132  
133 0 for my $file ( @{ $size_dups->{ $same_size } } )
134 {
135 0 my $bytes_read = $self->$weeder( $file, $len, $same_size );
136  
137 0 ▶ push @{ $same_bytes->{ $bytes_read } }, $file
Branch: 1 branch
BranchTrueFalseTotal
if defined $bytes_read 0 0 0
138 if defined $bytes_read;
139  
140 0 ▶ push @$weed_failed, $file unless defined $bytes_read;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes_read 0 0 0
141  
142 0 ▶ $progress->update( $i++ ) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
143 }
144  
145 # delete obvious non-dupe files from the group of same-size files
146 # by virtue of the fact that they will be a single length arrayref
147  
148 delete $same_bytes->{ $_ }
149 0 for grep { @{ $same_bytes->{ $_ } } == 1 }
150 keys %$same_bytes;
151  
152 # recompose the arrayref of filenames for the same-size file grouping
153 # but leave out the files we just weeded out from the group
154  
155 0 $size_dups->{ $same_size } = []; # start fresh
156  
157 0 @{ $size_dups->{ $same_size } } =
158 0 map { @{ $same_bytes->{ $_ } } }
159 keys %$same_bytes;
160  
161 0 ▶ push @{ $size_dups->{ $same_size } }, @$weed_failed if @$weed_failed;
Branch: 1 branch
BranchTrueFalseTotal
if @$weed_failed 0 0 0
162 }
163  
164 0 ▶ $progress->update( $i ) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 0 0
165  
166 0 return $size_dups;
167 }
168  
169 1 1;
170  
171 __END__
↑ Summary lib/App/dupfind/Guts/Algorithms.pm »