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

filter: Statementstmt 94.3% 101 / 107 Branchbran 68.7% 22 / 32 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 16 / 16 Podpod 0.0% 0 / 9 totaltotal 83.3% 145 / 174 CC 23 SCAR 17.7
CC
23
Cov
88%
accept_record
12.3
init
7.1
stream_done
5.0
CRAP 5.8
« blib/lib/App/RecordStream/DomainLanguage/Snippet.pm ↑ Summary blib/lib/App/RecordStream/Operation/grep.pm »
1 package App::RecordStream::Operation::join;
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 App::RecordStream::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use App::RecordStream::InputStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use App::RecordStream::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub init {
14 6 ▶ my $this = shift;
Subroutine init: called CC=7 SCAR=19.6
Pod undocumented
15 6 my $args = shift;
16  
17 6 my $left = 0;
18 6 my $right = 0;
19 6 my $inner = 0;
20 6 my $outer = 0;
21 6 my $operation = "";
22 6 my $accumulate_right = 0;
23  
24 6 my $spec = {
25 "left" => \$left,
26 "right" => \$right,
27 "inner" => \$inner,
28 "outer" => \$outer,
29 "operation=s" => \$operation,
30 "accumulate-right" => \$accumulate_right,
31 };
32  
33 6 $this->parse_options($args, $spec);
34  
35 5 ▶ my $inputkey = shift @$args
Branch: 1 branch
BranchTrueFalseTotal
my $inputkey = shift @$args or die "You must provide inputkey\n" 0 5 5
36 or die "You must provide inputkey\n";
37  
38 5 ▶ my $dbkey = shift @$args
Branch: 1 branch
BranchTrueFalseTotal
my $dbkey = shift @$args or die "You must provide dbkey\n" 0 5 5
39 or die "You must provide dbkey\n";
40  
41 5 ▶ my $dbfile = shift @$args
Branch: 1 branch
BranchTrueFalseTotal
my $dbfile = shift @$args or die "You must provide dbfile\n" 0 5 5
42 or die "You must provide dbfile\n";
43  
44 5 $this->{'ACCUMULATE_RIGHT'} = $accumulate_right;
45 5 $this->{'DB_KEY'} = $dbkey;
46 5 $this->{'INPUT_KEY'} = $inputkey;
47 5 ▶ $this->{'KEEP_LEFT'} = $left || $outer;
Condition66%2/3
$left || $outer
l!l&&r!l&&!r
104
MC/DC50%1/2
$left || $outer
$left $outer
Truth table66%2/3
A || B
A= $leftB= $outer
ABresult
1X1
011
000
48 5 ▶ $this->{'KEEP_RIGHT'} = $right || $outer;
Condition66%2/3
$right || $outer
l!l&&r!l&&!r
104
MC/DC50%1/2
$right || $outer
$right $outer
Truth table66%2/3
A || B
A= $rightB= $outer
ABresult
1X1
011
000
49  
50  
51 5 ▶ if ( $operation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation) 1 4 5
52 1 $this->{'OPERATION'} = App::RecordStream::Executor->transform_code($operation);
53 }
54  
55 5 $this->create_db($dbfile, $dbkey);
56  
57 5 $this->{'KEYS_PRINTED'} = {};
58 }
59  
60 sub create_db {
61 5 ▶ my $this = shift;
Subroutine create_db: called CC=3 SCAR=11.0
Pod undocumented
62 5 my $file = shift;
63 5 my $key = shift;
64  
65 5 my $db_stream = App::RecordStream::InputStream->new('FILE' => $file);
66 5 my %db;
67 my $record;
68  
69 5 ▶ while($record = $db_stream->get_record()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record = $db_stream->get_record) 21 5 26
70 21 my $value = $this->value_for_key($record, $key);
71  
72 21 ▶ $db{$value} = [] unless ( $db{$value} );
Branch: 1 branch
BranchTrueFalseTotal
unless $db{$value} 21 0 21
73 21 push @{$db{$value}}, $record;
74 }
75  
76 5 $this->{'DB'} = \%db;
77 }
78  
79 sub value_for_key {
80 50 ▶ my $this = shift;
Subroutine value_for_key: called CC=1 SCAR=0.0
Pod undocumented
81 50 my $record = shift;
82 50 my $key = shift;
83  
84 return join "\x1E", # ASCII record separator (RS)
85 50 map { ${$record->guess_key_from_spec($_, 0)} }
86 split /,/, $key;
87 }
88  
89 sub accept_record {
90 25 ▶ my $this = shift;
Subroutine accept_record: called CC=10 SCAR=25.1
Pod undocumented
91 25 my $record = shift;
92  
93 25 my $value = $this->value_for_key($record, $this->{'INPUT_KEY'});
94  
95 25 my $db = $this->{'DB'};
96  
97 25 ▶ if(my $db_records = $db->{$value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $db_records = $db->{$value}) { } 14 11 25
elsif ($this->{'KEEP_RIGHT'}) { } 2 9 11
98 14 foreach my $db_record (@$db_records) {
99 14 ▶ if ($this->{'ACCUMULATE_RIGHT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'ACCUMULATE_RIGHT'}) { } 0 14 14
100 0 ▶ if ($this->{'OPERATION'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'OPERATION'}) { } 0 0 0
101 0 $this->run_expression($db_record, $record);
102 }
103 else {
104 0 foreach my $this_key (keys %$record) {
105 0 ▶ if (!exists($db_record->{$this_key})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $db_record->{$this_key}) 0 0 0
106 0 $db_record->{$this_key} = $record->{$this_key};
107 }
108 }
109 }
110 }
111 else {
112 14 ▶ if ($this->{'OPERATION'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'OPERATION'}) { } 3 11 14
113 3 my $output_record = App::RecordStream::Record->new(%$db_record);
114 3 $this->run_expression($output_record, $record);
115 3 $this->push_record($output_record);
116 }
117 else {
118 11 $this->push_record(App::RecordStream::Record->new(%$record, %$db_record));
119 }
120  
121 14 ▶ if ($this->{'KEEP_LEFT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"KEEP_LEFT"}) 3 11 14
122 3 $this->{'KEYS_PRINTED'}->{$value} = 1;
123 }
124 }
125 }
126 }
127 elsif ($this->{'KEEP_RIGHT'}) {
128 2 $this->push_record($record);
129 }
130  
131 25 return 1;
132 }
133  
134 # TODO: shove down into executor
135 sub run_expression {
136 3 ▶ my $__MY__this = shift;
Subroutine run_expression: called CC=2 SCAR=7.0
Pod undocumented
137 3 my $d = shift;
138 3 my $i = shift;
139  
140 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
141 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 3 eval $__MY__this->{'OPERATION'};
143  
144 3 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
145 0 warn "Code died with $@\n";
146 }
147 }
148  
149 sub stream_done {
150 5 ▶ my $this = shift;
Subroutine stream_done: called CC=5 SCAR=16.1
Pod undocumented
151 5 ▶ if ($this->{'KEEP_LEFT'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"KEEP_LEFT"}) 1 4 5
152 1 foreach my $db_records (values %{$this->{'DB'}}) {
153 4 foreach my $db_record (@$db_records) {
154 4 my $value = $this->value_for_key($db_record, $this->{'DB_KEY'});
155 4 ▶ if (!exists($this->{'KEYS_PRINTED'}->{$value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->{"KEYS_PRINTED"}{$value}) 1 3 4
156 1 $this->push_record($db_record);
157 }
158 }
159 }
160 }
161 }
162  
163 sub add_help_types {
164 6 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
165 6 $this->use_help_type('keyspecs');
166 6 $this->use_help_type('snippet');
167 6 $this->add_help_type(
168 'full',
169 \&full_help,
170 'Help on join types and accumulate-right'
171 );
172 }
173  
174 sub full_help {
175 1 ▶ print <<HELP_FULL;
Subroutine full_help: called CC=1 SCAR=0.0
Pod undocumented
176 Join Types
177 For instance, if you did:
178 recs-join type typeName dbfile fromfile
179  
180 with a db file like:
181 { 'typeName': 'foo', 'hasSetting': 1 }
182 { 'typeName': 'bar', 'hasSetting': 0 }
183  
184 and joined that with
185 { 'name': 'something', 'type': 'foo'}
186 { 'name': 'blarg', 'type': 'hip'}
187  
188 for an inner (default) join, you would get
189 { 'name': 'something', 'type': 'foo', 'typeName': 'foo', 'hasSetting': 1}
190  
191 for an outer join, you would get
192 { 'name': 'something', 'type': 'foo', 'typeName': 'foo', 'hasSetting': 1}
193 { 'name': 'blarg', 'type': 'hip'}
194 { 'typeName': 'bar', 'hasSetting': 0 }
195  
196 for a left join, you would get
197 { 'name': 'something', 'type': 'foo', 'typeName': 'foo', 'hasSetting': 1}
198 { 'typeName': 'bar', 'hasSetting': 0 }
199  
200 for a right join, you would get
201 { 'name': 'something', 'type': 'foo', 'typeName': 'foo', 'hasSetting': 1}
202 { 'name': 'blarg', 'type': 'hip'}
203  
204 Accumulate Right:
205 Accumulate all input records with the same key onto each db record matching
206 that key. This means that a db record can have multiple input records merged
207 into it. If no operation is provided, any fields in second or later records
208 will be lost due to them being discarded. This option is most useful with a
209 user defined operation to handle collisions. For example, one could provide
210 an operation to add fields together:
211  
212 recs-join --left --operation '
213 foreach \$k (keys \%\$i) {
214 if (exists(\$d->{\$k})) {
215 if (\$k =~ /^value/) {\$d->{\$k} = \$d->{\$k} + \$i->{\$k};}
216 } else {
217 \$d->{\$k} = \$i->{\$k};
218 }
219 }' --accumulate-right name name dbfile inputfile
220 HELP_FULL
221 }
222  
223 sub usage {
224 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
225 1 my $message = shift;
226  
227 1 my $options = [
228 ['left', 'Do a left join'],
229 ['right', 'Do a right join'],
230 ['inner', 'Do an inner join (This is the default)'],
231 ['outer', 'Do an outer join'],
232 ['operation', 'An perl expression to evaluate for merging two records together, in place of the default behavior of db fields overwriting input fields. See "Operation" below.'],
233 ['accumulate-right', 'Accumulate all input records with the same key onto each db record matching that key. See "Accumulate Right" below.'],
234 ];
235  
236 1 my $args_string = $this->options_string($options);
237  
238 1 return <<USAGE;
239 $message
240 Usage: recs-join <args> <inputkey> <dbkey> <dbfile> [<files>]
241 __FORMAT_TEXT__
242 Records of input (or records from <files>) are joined against records in
243 <dbfile>, using field <inputkey> from input and field <dbkey> from <dbfile>.
244 Each record from input may match 0, 1, or more records from <dbfile>. Each
245 pair of matches will be combined to form a larger record, with fields from
246 the dbfile overwriting fields from the input stream. If the join is a left
247 join or inner join, any inputs that do not match a dbfile record are
248 discarded. If the join is a right join or inner join, any db records that do
249 not match an input record are discarded.
250  
251 dbkey and inputkey may be key specs, see '--help-keyspecs' for more
252 information. Separate multiple keyspecs with commas to use a composite key.
253 __FORMAT_TEXT__
254  
255 Arguments:
256 $args_string
257  
258 Operation:
259 __FORMAT_TEXT__
260 The expression provided is evaluated for every pair of db record and input
261 record that have matching keys, in place of the default operation to
262 overwrite input fields with db fields. The variable \$d is set to a
263 App::RecordStream::Record object for the db record, and \$i is set to a
264 App::RecordStream::Record object for the input record. The \$d record is
265 used for the result. Thus, if you provide an empty operation, the result
266 will contain only fields from the db record. Note that an empty operation is
267 different from no --operation at all.
268 __FORMAT_TEXT__
269  
270 Examples:
271 Join type from STDIN and typeName from dbfile
272 cat recs | recs-join type typeName dbfile
273  
274 Join host name from a mapping file to machines to get IPs
275 recs-join host host hostIpMapping machines
276 USAGE
277 }
278  
279 2 1;
« blib/lib/App/RecordStream/DomainLanguage/Snippet.pm ↑ Summary blib/lib/App/RecordStream/Operation/grep.pm »