line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::RecordStream::Operation::fromkv; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = "4.0.25"; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
861
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
43
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
7
|
use base qw(App::RecordStream::Operation); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
151
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
8
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
33
|
|
10
|
2
|
|
|
2
|
|
7
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
855
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub init { |
13
|
3
|
|
|
3
|
0
|
15
|
my $this = shift; |
14
|
3
|
|
|
|
|
5
|
my $args = shift; |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
|
|
5
|
my $kv_delim = " "; |
17
|
3
|
|
|
|
|
3
|
my $entry_delim = "\n"; |
18
|
3
|
|
|
|
|
3
|
my $record_delim = "END\n"; |
19
|
|
|
|
|
|
|
|
20
|
3
|
|
|
|
|
8
|
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
|
3
|
|
|
|
|
11
|
$this->parse_options($args, $spec); |
27
|
|
|
|
|
|
|
|
28
|
3
|
|
|
|
|
4
|
$this->{'KV_DELIM'} = $kv_delim; |
29
|
3
|
|
|
|
|
6
|
$this->{'ENTRY_DELIM'} = $entry_delim; |
30
|
3
|
|
|
|
|
4
|
$this->{'RECORD_DELIM'} = $record_delim; |
31
|
3
|
|
|
|
|
13
|
$this->{'ACC'} = undef; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub wants_input { |
36
|
6
|
|
|
6
|
0
|
22
|
return 1; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub stream_done { |
40
|
3
|
|
|
3
|
0
|
4
|
my $this = shift; |
41
|
|
|
|
|
|
|
|
42
|
3
|
|
|
|
|
5
|
my $acc = $this->{'ACC'}; |
43
|
3
|
50
|
|
|
|
4
|
if(defined($acc)) { |
44
|
3
|
|
|
|
|
6
|
$this->process_record($acc); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub accept_line { |
49
|
12
|
|
|
12
|
0
|
45
|
my $this = shift; |
50
|
12
|
|
|
|
|
14
|
my $line = shift; |
51
|
|
|
|
|
|
|
|
52
|
12
|
100
|
|
|
|
19
|
if(!defined($this->{'ACC'})) { |
53
|
3
|
|
|
|
|
22
|
$this->{'ACC'} = ''; |
54
|
|
|
|
|
|
|
} |
55
|
12
|
|
|
|
|
17
|
$this->{'ACC'} .= "$line\n"; |
56
|
|
|
|
|
|
|
|
57
|
12
|
|
|
|
|
12
|
my $record_delim = $this->{'RECORD_DELIM'}; |
58
|
|
|
|
|
|
|
|
59
|
12
|
100
|
|
|
|
68
|
if($this->{'ACC'} =~ s/^(.*?)\Q$record_delim\E//s) { |
60
|
5
|
|
|
|
|
10
|
$this->process_record($1); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
12
|
|
|
|
|
29
|
return 1; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub process_record { |
67
|
8
|
|
|
8
|
0
|
9
|
my $this = shift; |
68
|
8
|
|
|
|
|
11
|
my $line = shift; |
69
|
|
|
|
|
|
|
|
70
|
8
|
|
|
|
|
8
|
my $kv_delim = $this->{'KV_DELIM'}; |
71
|
8
|
|
|
|
|
9
|
my $entry_delim = $this->{'ENTRY_DELIM'}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# trim trailing and leading whitespace from record |
74
|
8
|
|
|
|
|
22
|
$line =~ s/^\s+|\s+$//g; |
75
|
|
|
|
|
|
|
|
76
|
8
|
|
|
|
|
28
|
my @entries = split(/\Q$entry_delim\E/, $line); |
77
|
|
|
|
|
|
|
|
78
|
8
|
100
|
|
|
|
18
|
if (scalar(@entries) > 0) { |
79
|
6
|
|
|
|
|
8
|
my $current_record = {}; |
80
|
|
|
|
|
|
|
|
81
|
6
|
|
|
|
|
7
|
for my $entry (@entries) { |
82
|
18
|
|
|
|
|
78
|
my @pair = split($kv_delim, $entry); |
83
|
|
|
|
|
|
|
|
84
|
18
|
50
|
|
|
|
48
|
$current_record->{$pair[0]} = $pair[1] if scalar(@pair) == 2; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
6
|
|
|
|
|
19
|
$this->push_record(App::RecordStream::Record->new($current_record)); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub usage |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
0
|
0
|
|
my $this = shift; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $options = [ |
96
|
|
|
|
|
|
|
[ 'record-delim|r ', 'Delimiter to for separating records (defaults to "END\\n").'], |
97
|
|
|
|
|
|
|
[ 'entry-delim|e ', 'Delimiter to for separating entries within records (defaults to "\\n").'], |
98
|
|
|
|
|
|
|
[ 'kv-delim|f ', 'Delimiter to for separating key/value pairs within an entry (defaults to " ").'], |
99
|
|
|
|
|
|
|
]; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $args_string = $this->options_string($options); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return <
|
104
|
|
|
|
|
|
|
Usage : recs-fromkv [] |
105
|
|
|
|
|
|
|
__FORMAT_TEXT__ |
106
|
|
|
|
|
|
|
Records are generated from character input with the form "...". |
107
|
|
|
|
|
|
|
Records have the form "...". Entries are pairs of the form |
108
|
|
|
|
|
|
|
"". |
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
|
|
|
|
|
|
|
1; |