blib/lib/App/RecordStream/Operation/chain.pm

filter: Statementstmt 95.7% 178 / 186 Branchbran 80.6% 50 / 62 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 25 / 25 Podpod 0.0% 0 / 12 totaltotal 87.4% 258 / 295 CC 36 SCAR 14.9
CC
36
Cov
91%
stream_done
9.9
setup_operations
6.0
init
5.0
CRAP 4.5
« blib/lib/App/RecordStream/Operation.pm ↑ Summary blib/lib/App/RecordStream/Operation/flatten.pm »
1 package App::RecordStream::Operation::chain;
2  
3 6 our $VERSION = "4.0.23";
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use App::RecordStream::Operation;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use App::RecordStream::Stream::Printer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub init {
14 6 ▶ my $this = shift;
Subroutine init: called CC=5 SCAR=16.2
Pod undocumented
15 6 my $args = shift;
16  
17 6 my ($show_chain, $dry_run);
18 my $spec = {
19 'show-chain' => \$show_chain,
20 1 ▶ 'n' => sub { $show_chain = 1; $dry_run = 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 6 };
22  
23 6 $this->parse_options($args, $spec, ['require_order']);
24  
25 5 ▶ return unless (@$args);
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 0 5 5
26  
27 5 ▶ unless ( App::RecordStream::Operation::is_recs_operation($args->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (App::RecordStream::Operation::is_recs_operation($args->[0])) 0 5 5
28 0 die "First chained command must be standard recs command not shell command!\n";
29 }
30  
31 5 ▶ $this->{'SAVED_ARGS'} = [@$args] if ( $show_chain );
Branch: 1 branch
BranchTrueFalseTotal
if $show_chain 1 4 5
32  
33 5 ▶ if ( $dry_run ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) 1 4 5
34 1 $this->{'DRY_RUN'} = $dry_run;
35 1 return;
36 }
37  
38 4 my $operations = $this->create_operations($args);
39  
40 4 my ($first_operation, $last_operation, $continuation_pid) = $this->setup_operations($operations);
41  
42 3 $this->{'CHAIN_HEAD'} = $first_operation;
43 3 $this->{'CHAIN_TAIL'} = $last_operation;
44 3 $this->{'CONTINUATION_PID'} = $continuation_pid;
45 }
46  
47 sub print_chain {
48 1 ▶ my $this = shift;
Subroutine print_chain: called CC=3 SCAR=11.0
Pod undocumented
49 1 my $args = shift;
50  
51 1 my @current_command;
52 1 my $was_shell = 0;
53  
54 1 $this->push_line("Chain Starts with:");
55  
56 1 my $indent = 1;
57 1 my $last;
58 1 foreach my $arg ( @$args ) {
59 13 ▶ if ( $arg eq '|' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "|") 4 9 13
60 4 $was_shell = $this->print_command(\@current_command, $last, \$indent);
61  
62 4 $last = [@current_command];
63 4 @current_command = ();
64 4 next;
65 }
66 9 push @current_command, $arg;
67 }
68  
69 1 $this->print_command(\@current_command, $last, \$indent);
70 }
71  
72 sub print_command {
73 5 ▶ my $this = shift;
Subroutine print_command: called CC=5 SCAR=16.1
Pod undocumented
74 5 my $current_command = shift;
75 5 my $last = shift;
76 5 my $indent = shift;
77  
78 5 my $message = '';
79 5 ▶ if ( defined $last ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last) 4 1 5
80 4 ▶ if ( App::RecordStream::Operation::is_recs_operation($last->[0]) && App::RecordStream::Operation::is_recs_operation($current_command->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (App::RecordStream::Operation::is_recs_operation($last->[0]) and App::RecordStream::Operation::is_recs_operation($current_command->[0])) { } 3 1 4
Condition66%2/3
App::RecordStream::Operation::is_recs_operation($last->[0]) and App::RecordStream::Operation::is_recs_operation($current_command->[0])
!ll&&!rl&&r
013
MC/DC50%1/2
App::RecordStream::Operation::is_recs_operation($last->[0]) and App::RecordStream::Operation::is_recs_operation($current_command->[0])
App::RecordStream::Operation::is_recs_operation($last->[0]) App::RecordStream::Operation::is_recs_operation($current_command->[0])
Truth table66%2/3
A and B
A= App::RecordStream::Operation::is_recs_operation($last->[0])B= App::RecordStream::Operation::is_recs_operation($current_command->[0])
ABresult
0X0
100
111
81 3 $message .= "Passed in memory to ";
82 }
83 else {
84 1 $message .= "Passed through a pipe to ";
85 1 $$indent++;
86 }
87 }
88  
89 5 my $prefix = ' ' x $$indent . $message;
90  
91 5 ▶ if ( App::RecordStream::Operation::is_recs_operation($current_command->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (App::RecordStream::Operation::is_recs_operation($current_command->[0])) { } 4 1 5
92 4 $this->push_line($prefix . "Recs command: " . join(' ', @$current_command));
93 4 return 0;
94 }
95 else {
96 1 $this->push_line($prefix . "Shell command: " . join(' ', @$current_command));
97 1 return 1;
98 }
99 }
100  
101 sub setup_operations {
102 4 ▶ my $this = shift;
Subroutine setup_operations: called CC=6 SCAR=18.0
Pod undocumented
103 4 my $operations = shift;
104  
105 # others need this
106 4 $operations = [@$operations];
107  
108 4 my ($first_operation, $last_operation, $continuation_pid);
109 4 ▶ while ( my $operation = shift @$operations ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $operation = shift @$operations) 13 2 15
110 13 ▶ if ( $operation->[0] eq 'SHELL' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($operation->[0] eq 'SHELL') { } 3 10 13
elsif ($operation->[0] eq 'RECS') { } 10 0 10
111 3 my $in_continuation;
112 3 ($in_continuation, $continuation_pid) = $this->setup_fork($operation->[1]);
113  
114 2 ▶ if ( $in_continuation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_continuation) { } 1 1 2
115 1 $first_operation = undef;
116 1 $last_operation = undef;
117 1 $continuation_pid = undef;
118 1 next;
119 }
120 else {
121 1 last;
122 }
123 }
124 elsif ( $operation->[0] eq 'RECS' ) {
125 # fall through
126 }
127 else {
128 0 die;
129 }
130  
131 10 ▶ $first_operation ||= $operation;
Condition66%2/3
$first_operation ||= $operation
l!l&&r!l&&!r
550
MC/DC0%0/2
$first_operation ||= $operation
$first_operation $operation
Truth table66%2/3
A ||= B
A= $first_operationB= $operation
ABresult
1X1
011
000
132 10 $last_operation = $operation;
133 }
134  
135 # we return $continuation_pid so we can wait on it, we can wait on our shell
136 # child via close(STDOUT)
137 3 return ($first_operation, $last_operation, $continuation_pid);
138 }
139  
140 sub create_operations {
141 4 ▶ my $this = shift;
Subroutine create_operations: called CC=3 SCAR=11.0
Pod undocumented
142 4 my $args = shift;
143  
144 4 my @single_command;
145 my @operations;
146 4 foreach my $arg ( @$args ) {
147 45 ▶ if ( $arg eq '|' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq "|") 11 34 45
148 11 $this->add_operation(\@single_command, \@operations);
149 11 @single_command = ();
150 11 next;
151 }
152  
153 34 push @single_command, $arg;
154 }
155  
156 4 $this->add_operation(\@single_command, \@operations);
157  
158 4 return \@operations;
159 }
160  
161 sub add_operation {
162 15 ▶ my $this = shift;
Subroutine add_operation: called CC=2 SCAR=6.9
Pod undocumented
163 15 my $single_command = shift;
164 15 my $operations = shift;
165  
166 15 my $idx = @$operations;
167 15 my $push_shim = App::RecordStream::Operation::chain::PushShim->new($operations, $idx);
168  
169 15 ▶ if ( App::RecordStream::Operation::is_recs_operation($single_command->[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (App::RecordStream::Operation::is_recs_operation($single_command->[0])) { } 12 3 15
170 12 my ($sc1, @args) = @$single_command;
171 12 my $operation = App::RecordStream::Operation::create_operation($sc1, \@args, $push_shim);
172 12 push @$operations, ['RECS', $operation, \@args];
173 }
174 else {
175 3 push @$operations, ['SHELL', [@$single_command]];
176 }
177 }
178  
179 sub setup_fork {
180 3 ▶ my $this = shift;
Subroutine setup_fork: called CC=5 SCAR=16.2
Pod undocumented
181 3 my $command_arguments = shift;
182  
183 3 my $continuation_pid = open(STDOUT, "|-");
184 3 ▶ die "cannot fork: $!" unless defined $continuation_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $continuation_pid 0 3 3
185  
186 3 ▶ if ( ! $continuation_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($continuation_pid) 1 2 3
187 # in continuation
188 1 return 1;
189 }
190  
191 # in parent, now split off the shell command as well
192 2 my $shell_pid = open(STDOUT, "|-");
193 2 ▶ die "cannot fork: $!" unless defined $shell_pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $shell_pid 0 2 2
194  
195 2 ▶ if ( ! $shell_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shell_pid) 1 1 2
196 # the child runs the shell command
197 1 exec (@$command_arguments);
198 }
199  
200 # still in parent, we're responsible for the children so we keep the PID
201 # around (shell pid can be waited for via close(STDOUT)).
202 1 return (0, $continuation_pid);
203 }
204  
205 sub wants_input {
206 3 ▶ return 0;
Subroutine wants_input: called CC=1 SCAR=0.0
Pod undocumented
207 }
208  
209 sub stream_done {
210 4 ▶ my $this = shift;
Subroutine stream_done: called CC=9 SCAR=22.9
Pod undocumented
211  
212 4 ▶ if ( my $args = $this->{'SAVED_ARGS'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args = $this->{"SAVED_ARGS"}) 1 3 4
213 1 $this->print_chain($args);
214 }
215  
216 4 ▶ if ( $this->{'DRY_RUN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"DRY_RUN"}) 1 3 4
217 1 return;
218 }
219  
220 3 my $head = $this->{'CHAIN_HEAD'};
221  
222 3 ▶ if ( $head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head) { } 3 0 3
223 3 my $head_operation = $head->[1];
224 3 my $head_args = $head->[2];
225 3 ▶ if ( $head_operation->wants_input() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($head_operation->wants_input) 3 0 3
226 3 local @ARGV = @$head_args;
227 3 ▶ while(my $line = <>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline ARGV)) 23 3 26
228 23 chomp $line;
229 23 App::RecordStream::Operation::set_current_filename($ARGV);
230 23 ▶ if ( ! $head_operation->accept_line($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($head_operation->accept_line($line)) 0 23 23
231 0 last;
232 }
233 }
234 }
235 3 $head_operation->finish();
236 }
237 else {
238 0 ▶ while(<>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ARGV)) 0 0 0
239 0 chomp;
240 0 $this->push_line($_);
241 }
242 }
243  
244 # wait for shell child (if we even have one)
245 3 close(STDOUT);
246  
247 # wait for possible other child (next sequence of recs operations)
248 3 my $continuation_pid = $this->{'CONTINUATION_PID'};
249 3 ▶ if ( $continuation_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continuation_pid) { } 1 2 3
250 # We have recs operation processes wait for the next recs operation
251 # process to the "right" in the chain. so that the left-most is last to
252 # exit. The shell is waiting on this left-most process to exit so this
253 # exit order ensures everyone finishes up before the shell notice the
254 # left-most PID is done and resumes control.
255 1 waitpid $continuation_pid, 0;
256 }
257 else {
258 # no next sequence, we must be the last sequence of recs operations
259 }
260  
261 }
262  
263 sub get_exit_value {
264 3 ▶ my $this = shift;
Subroutine get_exit_value: called CC=2 SCAR=7.6
Pod undocumented
265  
266 3 ▶ if ( my $tail = $this->{'CHAIN_TAIL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tail = $this->{"CHAIN_TAIL"}) 3 0 3
267 3 return $tail->[1]->get_exit_value();
268 }
269  
270 0 return 0;
271 }
272  
273 sub add_help_types {
274 6 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
275 6 $this->use_help_type('keyspecs');
276 }
277  
278 sub usage {
279 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
280  
281 1 my $options = [
282 ['show-chain', 'Before running the commands, print out what will happen in the chain'],
283 ['n', 'Do not run commands, implies --show-chain'],
284 ];
285  
286 1 my $args_string = $this->options_string($options);
287  
288 1 return <<USAGE;
289 Usage: recs-chain <command> | <command> | ...
290 __FORMAT_TEXT__
291 Creates an in-memory chain of recs operations. This avoid serialization and
292 deserialization of records at each step in a complex recs pipeline. For
293 ease of use the chain of recs commands main contain non-recs command,
294 anything that does not start with a recs- is interpreted as a shell command.
295 That command is forked off to the shell. In this case, serialization and
296 deserialization costs apply, but only to and from the shell command,
297 everything else is done in memory. If you have many shell commands in a
298 row, there is extra over head, you should instead consider splitting those
299 into separate pipes. See the examples for more information on this.
300  
301 Arugments are specified in on the command line separated by pipes. For most
302 shells, you will need to escape the pipe character to avoid having the shell
303 interpret the pipe as a shell pipe.
304 __FORMAT_TEXT__
305  
306 $args_string
307  
308 Examples:
309 Parse some fields, sort and collate, all in memory
310 recs-chain recs-frommultire 'data,time=(\\S+) (\\S+)' \\| recs-sort --key time=n \\| recs-collate --a perc,90,data
311 Use shell commands in your recs stream
312 recs-chain recs-frommultire 'data,time=(\\S+) (\\S+)' \\| recs-sort --key time=n \\| grep foo \\| recs-collate --a perc,90,data
313 Many shell commands should be split into real pipes
314 recs-chain recs-frommultire 'data,time=(\\S+) (\\S+)' \\| recs-xform '\$r->{now} = time();' \
315 | grep foo | sort | uniq | recs-chain recs-collate --a perc,90,data \\| recs-totable
316 USAGE
317 }
318  
319 package App::RecordStream::Operation::chain::PushShim;
320  
321 6 ▶use App::RecordStream::Stream::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
322  
323 6 ▶use base 'App::RecordStream::Stream::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
324  
325 sub new {
326 15 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
327 15 my $operations = shift;
328 15 my $idx = shift;
329  
330 15 my $this = {
331 OPERATIONS => $operations,
332 IDX => $idx,
333 };
334  
335 15 bless $this, $class;
336  
337 15 return $this;
338 }
339  
340 sub accept_record {
341 44 ▶ my $this = shift;
Subroutine accept_record: called CC=1 SCAR=0.0
342 44 my $record = shift;
343  
344 44 return $this->_get_delegate()->accept_record($record);
345 }
346  
347 sub accept_line {
348 16 ▶ my $this = shift;
Subroutine accept_line: called CC=1 SCAR=0.0
349 16 my $line = shift;
350  
351 16 return $this->_get_delegate()->accept_line($line);
352 }
353  
354 sub finish {
355 6 ▶ my $this = shift;
Subroutine finish: called CC=1 SCAR=0.0
356  
357 6 $this->_get_delegate()->finish();
358 }
359  
360 sub _get_delegate {
361 66 ▶ my $this = shift;
Subroutine _get_delegate: called CC=5 SCAR=16.1
362  
363 66 my $delegate = $this->{'DELEGATE'};
364  
365 66 ▶ if ( ! $delegate ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($delegate) 6 60 66
366 6 my $operations = $this->{'OPERATIONS'};
367 6 my $idx = $this->{'IDX'};
368 6 ▶ if ( $idx + 1 < @$operations ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx + 1 < @$operations) { } 4 2 6
369 4 my $next_operation = $operations->[$idx + 1];
370 4 ▶ if ( $next_operation->[0] eq 'RECS' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_operation->[0] eq 'RECS') { } 3 1 4
elsif ($next_operation->[0] eq 'SHELL') { } 1 0 1
371 # next operation is actually a recs operation, it must be
372 # in process with us, give it our output
373 3 $delegate = $next_operation->[1];
374 }
375 elsif ($next_operation->[0] eq 'SHELL' ) {
376 # next is shell, we're set up to print to it
377 1 $delegate = App::RecordStream::Stream::Printer->new();
378 }
379 else {
380 0 die;
381 }
382 }
383 else {
384 # at the end, we print out
385 2 $delegate = App::RecordStream::Stream::Printer->new();
386 }
387  
388 6 $this->{'DELEGATE'} = $delegate;
389 }
390  
391 66 return $delegate;
392 }
393  
394 6 1;
« blib/lib/App/RecordStream/Operation.pm ↑ Summary blib/lib/App/RecordStream/Operation/flatten.pm »