| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::multiplex; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 1131 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 50 |  | 
| 4 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 48 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 9 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 116 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 334 | use App::RecordStream::Clumper::Options; | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 46 |  | 
| 9 | 2 |  |  | 2 |  | 692 | use App::RecordStream::Operation::multiplex::BaseClumperCallback; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 896 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub init { | 
| 12 | 4 |  |  | 4 | 0 | 12 | my $this = shift; | 
| 13 | 4 |  |  |  |  | 10 | my $args = shift; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 4 |  |  |  |  | 38 | my $clumper_options = App::RecordStream::Clumper::Options->new(); | 
| 16 | 4 |  |  |  |  | 10 | my $line_key = undef; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 4 |  |  |  |  | 22 | my $spec = { | 
| 19 |  |  |  |  |  |  | $clumper_options->main_options(), | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # other options | 
| 22 |  |  |  |  |  |  | "line-key|L=s"          => \$line_key, | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | # help | 
| 25 |  |  |  |  |  |  | $clumper_options->help_options(), | 
| 26 |  |  |  |  |  |  | }; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 4 |  |  |  |  | 39 | $this->parse_options($args, $spec); | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 4 |  |  |  |  | 15 | my ($script, @args) = @$args; | 
| 31 | 4 |  |  |  |  | 12 | @$args = (); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # check help first | 
| 34 | 4 |  |  | 8 |  | 59 | $clumper_options->check_options(App::RecordStream::Operation::multiplex::BaseClumperCallback->new($script, \@args, $line_key, sub { return $this->push_record($_[0]); }, sub { return $this->push_line($_[0]); })); | 
|  | 14 |  |  |  |  | 47 |  | 
|  | 8 |  |  |  |  | 29 |  | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 4 |  |  |  |  | 70 | $this->{'CLUMPER_OPTIONS'} = $clumper_options; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub accept_record { | 
| 40 | 32 |  |  | 32 | 0 | 63 | my $this   = shift; | 
| 41 | 32 |  |  |  |  | 49 | my $record = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 32 |  |  |  |  | 109 | $this->{'CLUMPER_OPTIONS'}->accept_record($record); | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub stream_done { | 
| 47 | 4 |  |  | 4 | 0 | 8 | my $this = shift; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 4 |  |  |  |  | 18 | $this->{'CLUMPER_OPTIONS'}->stream_done(); | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub print_usage { | 
| 53 | 0 |  |  | 0 | 0 | 0 | my $this    = shift; | 
| 54 | 0 |  |  |  |  | 0 | my $message = shift; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 | 0 | 0 |  |  | 0 | if ( $message && UNIVERSAL::isa($message, 'CODE') ) { | 
| 57 | 0 |  |  |  |  | 0 | $message->(); | 
| 58 | 0 |  |  |  |  | 0 | exit 1; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  | 0 | $this->SUPER::print_usage($message); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub add_help_types { | 
| 65 | 4 |  |  | 4 | 0 | 9 | my $this = shift; | 
| 66 | 4 |  |  |  |  | 24 | $this->use_help_type('keyspecs'); | 
| 67 | 4 |  |  |  |  | 17 | $this->use_help_type('keygroups'); | 
| 68 | 4 |  |  |  |  | 15 | $this->use_help_type('keys'); | 
| 69 | 4 |  |  |  |  | 15 | $this->use_help_type('domainlanguage'); | 
| 70 | 4 |  |  |  |  | 15 | $this->use_help_type('clumping'); | 
| 71 |  |  |  |  |  |  | $this->add_help_type( | 
| 72 |  |  |  |  |  |  | 'more', | 
| 73 | 0 |  |  | 0 |  |  | sub { $this->more_help() }, | 
| 74 | 4 |  |  |  |  | 35 | 'Larger help documentation' | 
| 75 |  |  |  |  |  |  | ); | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub usage { | 
| 79 | 0 |  |  | 0 | 0 |  | my $this = shift; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | my $options = [ | 
| 82 |  |  |  |  |  |  | App::RecordStream::Clumper::Options->main_usage(), | 
| 83 |  |  |  |  |  |  | [ 'line-key|-L ', 'Use the value of this key as line input for the nested operation (rather than the entire record).  Use with recs-from* scripts generally.'], | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | App::RecordStream::Clumper::Options->help_usage(), | 
| 86 |  |  |  |  |  |  | ]; | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  |  | my $args_string = $this->options_string($options); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | return < | 
| 91 |  |  |  |  |  |  | Usage: recs-multiplex  -- | 
| 92 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 93 |  |  |  |  |  |  | Take records, grouped together by --keys, and run an operation for each | 
| 94 |  |  |  |  |  |  | group. | 
| 95 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | Arguments: | 
| 98 |  |  |  |  |  |  | $args_string | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | Examples: | 
| 101 |  |  |  |  |  |  | Separate out a stream of text by PID into separate invocations of recs-frommultire. | 
| 102 |  |  |  |  |  |  | recs-fromre '^(.*PID=([0-9]*).*)\$' -f line,pid | recs-multiplex -L line -k pid -- recs-frommultire ... | 
| 103 |  |  |  |  |  |  | Tag lines with counts by thread | 
| 104 |  |  |  |  |  |  | recs-multiplex -k thread -- recs-eval '{{nbr}} = ++\$nbr' | 
| 105 |  |  |  |  |  |  | USAGE | 
| 106 | 0 |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | sub more_help { | 
| 109 | 0 |  |  | 0 | 0 |  | my $this = shift; | 
| 110 | 0 |  |  |  |  |  | my $usage = $this->usage() . < | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | Cubing: | 
| 113 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 114 |  |  |  |  |  |  | Instead of added one entry for each input record, we add 2 ** (number of key | 
| 115 |  |  |  |  |  |  | fields), with every possible combination of fields replaced with the default | 
| 116 |  |  |  |  |  |  | of "ALL".  This is not meant to be used with --adjacent or --size.  If our | 
| 117 |  |  |  |  |  |  | key fields were x and y then we'd get output for {x = 1, y = 2}, {x = 1, y = | 
| 118 |  |  |  |  |  |  | ALL}, {x = ALL, y = 2} and {x = ALL, y = ALL}. | 
| 119 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | Domain Lanuage Integration: | 
| 122 |  |  |  |  |  |  | USAGE | 
| 123 | 0 |  |  |  |  |  | $usage .= App::RecordStream::DomainLanguage::short_usage() . < | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 126 |  |  |  |  |  |  | Keys may be specified using the recs domain language.  --dlkey requires an | 
| 127 |  |  |  |  |  |  | option of the format '='.  --dlkey requires the | 
| 128 |  |  |  |  |  |  | code evaluate as a valuation. | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | See --help-domainlanguage for a more complete description of its workings | 
| 131 |  |  |  |  |  |  | and a list of available functions. | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | See the examples in the recs-collate help for a more gentle introduction. | 
| 134 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 135 |  |  |  |  |  |  | USAGE | 
| 136 | 0 |  |  |  |  |  | print $this->format_usage($usage); | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | 1; |