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

filter: Statementstmt 100% 55 / 55 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 6 totaltotal 90.1% 73 / 81 CC 7 SCAR 8.1
CC
7
Cov
97%
process_record
4.0
accept_line
3.0
stream_done
2.0
CRAP 2.3
« blib/lib/App/RecordStream/Aggregator/ValuesToKeys.pm ↑ Summary blib/lib/App/RecordStream/Operation/decollate.pm »
1 package App::RecordStream::Operation::fromkv;
2  
3 2 our $VERSION = "4.0.23";
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 4 ▶ my $this = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
14 4 my $args = shift;
15  
16 4 my $kv_delim = " ";
17 4 my $entry_delim = "\n";
18 4 my $record_delim = "END\n";
19  
20 4 my $spec = {
21 "kv-delim|f=s" => \$kv_delim,
22 "entry-delim|e=s" => \$entry_delim,
23 "record-delim|r=s" => \$record_delim,
24 };
25  
26 4 $this->parse_options($args, $spec);
27  
28 4 $this->{'KV_DELIM'} = $kv_delim;
29 4 $this->{'ENTRY_DELIM'} = $entry_delim;
30 4 $this->{'RECORD_DELIM'} = $record_delim;
31 4 $this->{'ACC'} = undef;
32 }
33  
34  
35 sub wants_input {
36 6 ▶ return 1;
Subroutine wants_input: called CC=1 SCAR=0.0
Pod undocumented
37 }
38  
39 sub stream_done {
40 3 ▶ my $this = shift;
Subroutine stream_done: called CC=2 SCAR=7.0
Pod undocumented
41  
42 3 my $acc = $this->{'ACC'};
43 3 ▶ if(defined($acc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $acc) 3 0 3
44 3 $this->process_record($acc);
45 }
46 }
47  
48 sub accept_line {
49 12 ▶ my $this = shift;
Subroutine accept_line: called CC=3 SCAR=11.0
Pod undocumented
50 12 my $line = shift;
51  
52 12 ▶ if(!defined($this->{'ACC'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"ACC"}) 3 9 12
53 3 $this->{'ACC'} = '';
54 }
55 12 $this->{'ACC'} .= "$line\n";
56  
57 12 my $record_delim = $this->{'RECORD_DELIM'};
58  
59 12 ▶ if($this->{'ACC'} =~ s/^(.*?)\Q$record_delim\E//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"ACC"} =~ s/^(.*?)\Q$record_delim\E//s) 5 7 12
60 5 $this->process_record($1);
61 }
62  
63 12 return 1;
64 }
65  
66 sub process_record {
67 8 ▶ my $this = shift;
Subroutine process_record: called CC=4 SCAR=13.9
Pod undocumented
68 8 my $line = shift;
69  
70 8 my $kv_delim = $this->{'KV_DELIM'};
71 8 my $entry_delim = $this->{'ENTRY_DELIM'};
72  
73 # trim trailing and leading whitespace from record
74 8 $line =~ s/^\s+|\s+$//g;
75  
76 8 my @entries = split(/\Q$entry_delim\E/, $line);
77  
78 8 ▶ if (scalar(@entries) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entries > 0) 6 2 8
79 6 my $current_record = {};
80  
81 6 for my $entry (@entries) {
82 18 my @pair = split($kv_delim, $entry);
83  
84 18 ▶ $current_record->{$pair[0]} = $pair[1] if scalar(@pair) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @pair == 2 18 0 18
85 }
86  
87 6 $this->push_record(App::RecordStream::Record->new($current_record));
88 }
89 }
90  
91 sub usage
92 {
93 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
94  
95 1 my $options = [
96 [ 'record-delim|r <delim>', 'Delimiter to for separating records (defaults to "END\\n").'],
97 [ 'entry-delim|e <delim>', 'Delimiter to for separating entries within records (defaults to "\\n").'],
98 [ 'kv-delim|f <delim>', 'Delimiter to for separating key/value pairs within an entry (defaults to " ").'],
99 ];
100  
101 1 my $args_string = $this->options_string($options);
102  
103 1 return <<USAGE;
104 Usage : recs-fromkv <args> [<files>]
105 __FORMAT_TEXT__
106 Records are generated from character input with the form "<record><record-delim><record>...".
107 Records have the form "<entry><entry-delim><entry>...". Entries are pairs of the form
108 "<key><kv-delim><value>".
109 __FORMAT_TEXT__
110  
111 Arguments:
112 $args_string
113  
114 Examples:
115 Parse memcached stat metrics into records
116 echo -ne 'stats\\r\\n' | nc -i1 localhost 11211 | tr -d "\\r" | awk '{if (! /END/) {print \$2" "\$3} else {print \$0}}' | recs-fromkv
117  
118 Parse records separated by "E\\n" with entries separated by '\|' and pairs separated by '='
119 recs-fromkv --kv-delim '=' --entry-delim '\|' --record-delim \$(echo -ne "E\\n")
120  
121 Parse records separated by "%\\n" with entries separated by "\\n" and pairs separated by '='
122 recs-fromkv --kv-delim '=' --record-delim \$(echo -ne "%\\n")
123  
124 Parse records separated by '%' with entries separated by '\|' and pairs separated by '='
125 recs-fromkv --kv-delim '=' --entry-delim '\|' --record-delim '%'
126 USAGE
127 }
128  
129 2 1;
« blib/lib/App/RecordStream/Aggregator/ValuesToKeys.pm ↑ Summary blib/lib/App/RecordStream/Operation/decollate.pm »