blib/lib/File/Set/Writer.pm

filter: Statementstmt 100% 72 / 72 Branchbran 76.9% 20 / 26 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 17 / 17 Podpod 100% 1 / 1 totaltotal 87.7% 115 / 131 CC 19 SCAR 11.7
CC
19
Cov
91%
_write
7.0
_write_files
4.3
_write_pending_files
3.1
CRAP 3.2
↑ Summary
1 package File::Set::Writer;
2  
3 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use MooX::Types::MooseLike::Base qw( Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use MooX::Types::MooseLike::Numeric qw( PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 our $VERSION = '0.000001'; # 0.0.1
8 7 $VERSION = eval $VERSION;
9  
10 1 ▶has max_lines => ( is => 'rw', default => sub { 500 }, isa => PositiveInt );
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 4 ▶has max_files => ( is => 'rw', default => sub { 100 }, isa => PositiveInt );
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 7 has max_handles => ( is => 'rw', required => 1, isa => PositiveInt );
15  
16 8 ▶has line_join => ( is => 'rw', default => sub { "\n" }, isa => Str );
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 7 has expire_files_batch_size => ( is => 'rw', isa => PositiveInt );
19  
20 7 has expire_handles_batch_size => ( is => 'rw', isa => PositiveInt );
21  
22 # If the user doesn't set a batch_size for files or handles
23 # we will use 20% of max_(files|handles). This will be updated
24 # if max_files or max_handles is updated _unless_ the user explictly
25 # sets the batch_size, at which point it becomes their responsiblity
26 # to manage the values.
27  
28 around expire_files_batch_size => sub {
29 1006 ▶ my ( $orig, $self ) = ( shift, shift );
Subroutine __ANON__: called CC=2 SCAR=7.1
30  
31 1006 ▶ return $self->$orig( @_ ) || int( $self->max_files / 5 );
Condition66%2/3
$self->$orig(@_) || int $self->max_files / 5
l!l&&r!l&&!r
100330
MC/DC0%0/2
$self->$orig(@_) || int $self->max_files / 5
$self->$orig(@_) int $self->max_files / 5
Truth table66%2/3
A || B
A= $self->$orig(@_)B= int $self->max_files / 5
ABresult
1X1
011
000
32 7 };
33  
34 around expire_handles_batch_size => sub {
35 1392 ▶ my ( $orig, $self ) = ( shift, shift );
Subroutine __ANON__: called CC=2 SCAR=7.1
36  
37 1392 ▶ return $self->$orig( @_ ) || int( $self->max_handles / 5 );
Condition66%2/3
$self->$orig(@_) || int $self->max_handles / 5
l!l&&r!l&&!r
10212900
MC/DC0%0/2
$self->$orig(@_) || int $self->max_handles / 5
$self->$orig(@_) int $self->max_handles / 5
Truth table66%2/3
A || B
A= $self->$orig(@_)B= int $self->max_handles / 5
ABresult
1X1
011
000
38 7 };
39  
40 sub print {
41 31000 ▶ my ( $self, $file, @lines ) = @_;
Subroutine print: called CC=3 SCAR=11.0
Pod documented
42
43 31000 push @{$self->{queue}->{$file}}, @lines;
44  
45 $self->_write_files( $file )
46 31000 ▶ if @{$self->{queue}->{$file}} >= $self->max_lines;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"queue"}{$file};} >= $self->max_lines 3000 28000 31000
47  
48 31000 ▶ $self->_write_pending_files
Branch: 1 branch
BranchTrueFalseTotal
if $self->_files >= $self->max_files 1000 30000 31000
49 if $self->_files >= $self->max_files;
50
51 31000 return $self;
52 }
53  
54 # Write $self->expire_files_batch_size amount of files to disk,
55 # in the order of files with the most lines of content. This
56 # is used when ->_files >= ->max_files in ->print.
57  
58 sub _write_pending_files {
59 1000 ▶ my ( $self ) = @_;
Subroutine _write_pending_files: called CC=3 SCAR=11.2
60
61 my @files = sort {
62 19000 ▶ scalar @{$self->{queue}->{$b} || []} <=> scalar @{$self->{queue}->{$a} || []}
Branch: 2 branches
BranchTrueFalseTotal
$self->{"queue"}{$b} or [] 0 19000 19000
$self->{"queue"}{$a} or [] 0 19000 19000
63 1000 } keys %{$self->{queue}};
64  
65 1000 $self->_write_files( splice @files, 0, $self->expire_files_batch_size );
66 }
67  
68 # Given names of files with queued lines, write the lines to the
69 # file handle with $self->_write(), joining the lines together with
70 # $self->line_join.
71  
72 sub _write_files {
73 4012 ▶ my ( $self, @files ) = @_;
Subroutine _write_files: called CC=4 SCAR=14.6
74  
75 4012 foreach my $file ( @files ) {
76 die "Error _write_files called with invalid argument \"$file\""
77 13000 ▶ unless defined $file and exists $self->{queue}->{$file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and exists $self->{"queue"}{$file} 0 13000 13000
Condition33%1/3
defined $file and exists $self->{"queue"}{$file}
!ll&&!rl&&r
0013000
MC/DC0%0/2
defined $file and exists $self->{"queue"}{$file}
defined $file exists $self->{"queue"}{$file}
Truth table33%1/3
A and B
A= defined $fileB= exists $self->{"queue"}{$file}
ABresult
0X0
100
111
78  
79 $self->_write(
80 $file,
81 13000 join( $self->line_join, @{$self->{queue}->{$file}}, '' )
82 );
83 13000 delete $self->{queue}->{$file};
84 }
85 }
86  
87  
88 # Given a filename and a message, write the message to the file.
89 #
90 # This function implements a Least Recently Used (LRU) algorithm to cache file
91 # handles for repeated use.
92 # $self->max_handles is the limit of open file descriptors at any given time,
93 # while $self->expires_handles_batch_size handles will be closed when max_handles
94 # has been reached.
95  
96 sub _write {
97 13000 ▶ my ( $self, $file, @contents ) = @_;
Subroutine _write: called CC=7 SCAR=19.5
98
99 13000 ▶ if ( $self->_handles >= $self->max_handles ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_handles >= $self->max_handles) 1386 11614 13000
100 my @files = sort {
101 $self->{fcache}->{$a}->{stamp} <=> $self->{fcache}->{$b}->{stamp}
102 1386 } keys %{$self->{fcache}};
103
104 1386 foreach my $i ( 0 .. $self->expire_handles_batch_size ) {
105 4950 ▶ last unless $files[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $files[$i] 99 4851 4950
106 4851 delete $self->{fcache}->{$files[$i]};
107 }
108 }
109  
110 13000 ▶ if ( ! exists $self->{fcache}->{$file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"fcache"}{$file}) 4873 8127 13000
111 4873 ▶ open my $new_fh, ">>", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $new_fh, ">>", $file or die "Failed to open $file for writing: $!" 0 4873 4873
112 or die "Failed to open $file for writing: $!";
113 4873 $self->{fcache}->{$file} = {
114 fh => $new_fh,
115 name => $file,
116 stamp => time(),
117 };
118 }
119  
120 13000 my $wfh = $self->{fcache}->{$file}->{fh};
121 13000 my $content = join ("", @contents);
122 13000 ▶ print $wfh $content
Branch: 1 branch
BranchTrueFalseTotal
print $wfh $content or die "Failed to write ${file}: $!" 0 13000 13000
123 or die "Failed to write $file: $!";
124 13000 $self->{fcache}->{$file}->{stamp} = time;
125 }
126  
127 # Write all staged data to disk and closes all currently-open
128 # file handles. This happens automatically at the objects
129 # destruction.
130  
131 sub _sync {
132 12 ▶ my ( $self ) = @_;
Subroutine _sync: called CC=1 SCAR=0.0
133  
134 12 $self->_write_files( keys %{$self->{queue}} );
135 }
136  
137 # Return the count of open file handles currently in the cache.
138  
139 sub _handles {
140 14000 ▶ return scalar keys %{ shift->{fcache} || {} };
Subroutine _handles: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"fcache"} or {} 4 13996 14000
141 }
142  
143 # Return the count of files currently staged for being written.
144  
145 sub _files {
146 41000 ▶ return scalar keys %{ shift->{queue} || {} };
Subroutine _files: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"queue"} or {} 0 41000 41000
147 }
148  
149 # $self->_lines( "filename" );
150 #
151 # Return the count of lines staged for the given filename.
152  
153 sub _lines {
154 10000 ▶ return scalar @{ shift->{queue}->{ shift() } || [] };
Subroutine _lines: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"queue"}{shift()} or [] 1000 9000 10000
155 }
156  
157 # Push our buffered arrays into the file handles before
158 # we close the file handles.
159 12 ▶sub DESTROY { shift->_sync; }
Subroutine DESTROY: called CC=1 SCAR=0.0
160  
161 7 1;
162  
163  
164 __END__
↑ Summary