| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RecordStream::Operation::stream2table; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 852 | use strict; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 41 |  | 
| 4 | 2 |  |  | 2 |  | 8 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 40 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 2 |  |  | 2 |  | 7 | use base qw(App::RecordStream::Operation); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 837 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | sub init { | 
| 9 | 1 |  |  | 1 | 0 | 2 | my $this = shift; | 
| 10 | 1 |  |  |  |  | 1 | my $args = shift; | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  |  |  | 2 | my $field; | 
| 13 | 1 |  |  |  |  | 2 | my $spec = { | 
| 14 |  |  |  |  |  |  | "field|f=s" => \$field, | 
| 15 |  |  |  |  |  |  | }; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 1 |  |  |  |  | 6 | $this->parse_options($args, $spec); | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 1 | 50 |  |  |  | 3 | die "You must specify a --field option\n" unless defined($field); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  |  |  |  | 3 | $this->{'FIELD'} = $field; | 
| 22 | 1 |  |  |  |  | 7 | $this->{'REMOVE_FIELD'} = (not ($field =~ m![/@]!)); | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub accept_record { | 
| 26 | 4 |  |  | 4 | 0 | 5 | my $this   = shift; | 
| 27 | 4 |  |  |  |  | 5 | my $record = shift; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 4 |  |  |  |  | 5 | my $key = ${$record->guess_key_from_spec($this->{'FIELD'})}; | 
|  | 4 |  |  |  |  | 10 |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 4 | 50 |  |  |  | 8 | if ( $this->{'REMOVE_FIELD'} ) { | 
| 32 | 4 |  |  |  |  | 10 | $record->remove($this->{'FIELD'}); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 4 |  | 100 |  |  | 13 | $this->{'HASH'}->{$key} ||= []; | 
| 36 | 4 |  |  |  |  | 4 | push @{$this->{'HASH'}->{$key}}, $record; | 
|  | 4 |  |  |  |  | 8 |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 4 |  |  |  |  | 15 | return 1; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub stream_done { | 
| 42 | 1 |  |  | 1 | 0 | 8 | my $this = shift; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 5 | while(scalar keys %{$this->{'HASH'}} > 0) { | 
|  | 4 |  |  |  |  | 10 |  | 
| 45 | 3 |  |  |  |  | 8 | my $record = App::RecordStream::Record->new(); | 
| 46 | 3 |  |  |  |  | 4 | my $found = 0; | 
| 47 | 3 |  |  |  |  | 3 | foreach my $key (keys %{$this->{'HASH'}}) { | 
|  | 3 |  |  |  |  | 6 |  | 
| 48 | 6 |  |  |  |  | 15 | my $old_record = shift @{$this->{'HASH'}->{$key}}; | 
|  | 6 |  |  |  |  | 10 |  | 
| 49 | 6 | 100 |  |  |  | 10 | if ( $old_record ) { | 
| 50 | 4 |  |  |  |  | 9 | $record->set($key, $old_record); | 
| 51 | 4 |  |  |  |  | 5 | $found = 1; | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  | else { | 
| 54 | 2 |  |  |  |  | 7 | delete $this->{'HASH'}->{$key}; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } | 
| 57 | 3 | 100 |  |  |  | 12 | $this->push_record($record) if ( $found ); | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | sub add_help_types { | 
| 62 | 1 |  |  | 1 | 0 | 2 | my $this = shift; | 
| 63 | 1 |  |  |  |  | 6 | $this->use_help_type('keyspecs'); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | sub usage { | 
| 67 | 0 |  |  | 0 | 0 |  | my $this = shift; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 |  |  |  |  |  | my $options = [ | 
| 70 |  |  |  |  |  |  | ['field ', 'Field to use as the column key, may be a keyspec'], | 
| 71 |  |  |  |  |  |  | ]; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  |  |  |  |  | my $args_string = $this->options_string($options); | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 0 |  |  |  |  |  | my $usage =  < | 
| 76 |  |  |  |  |  |  | Usage: recs-stream2table  [] | 
| 77 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 78 |  |  |  |  |  |  | Transforms a list of records, combinging records based on a column, FIELD. | 
| 79 |  |  |  |  |  |  | In order, the values of the column will be added to the output records. | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Note: This script spools the stream into memory | 
| 82 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | This stream: | 
| 85 |  |  |  |  |  |  | { "column": "foo", "data": "foo1" } | 
| 86 |  |  |  |  |  |  | { "column": "foo", "data": "foo2" } | 
| 87 |  |  |  |  |  |  | { "column": "boo", "data": "boo1" } | 
| 88 |  |  |  |  |  |  | { "column": "boo", "data": "boo2" } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | with recs-stream2table --field column becomes: | 
| 91 |  |  |  |  |  |  | {"boo":{"data":"boo1"},"foo":{"data":"foo1"}} | 
| 92 |  |  |  |  |  |  | {"boo":{"data":"boo2"},"foo":{"data":"foo2"}} | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 95 |  |  |  |  |  |  | Hint: use recs-flatten if you want those values to be in the top level of | 
| 96 |  |  |  |  |  |  | the record | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | The full input record will be associated with the value of the FIELD.  The | 
| 99 |  |  |  |  |  |  | field itself will be removed from the nested record if the passed field is | 
| 100 |  |  |  |  |  |  | not a key spec. | 
| 101 |  |  |  |  |  |  | __FORMAT_TEXT__ | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | Arguments: | 
| 104 |  |  |  |  |  |  | $args_string | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | Examples: | 
| 107 |  |  |  |  |  |  | # Transform a record stream with each stat on one line to a stream with one | 
| 108 |  |  |  |  |  |  | # value for each stat present on one line | 
| 109 |  |  |  |  |  |  | ... | recs-stream2table --field stat | 
| 110 |  |  |  |  |  |  | USAGE | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | 1; |