line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=pod |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
ETL::Pipeline::Output::Memory - Save records in memory |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# Save the records into a giant list. |
10
|
|
|
|
|
|
|
use ETL::Pipeline; |
11
|
|
|
|
|
|
|
ETL::Pipeline->new( { |
12
|
|
|
|
|
|
|
input => ['UnitTest'], |
13
|
|
|
|
|
|
|
mapping => {First => 'Header1', Second => 'Header2'}, |
14
|
|
|
|
|
|
|
output => ['Memory'] |
15
|
|
|
|
|
|
|
} )->process; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Save the records into a hash, keyed by an identifier. |
18
|
|
|
|
|
|
|
use ETL::Pipeline; |
19
|
|
|
|
|
|
|
ETL::Pipeline->new( { |
20
|
|
|
|
|
|
|
input => ['UnitTest'], |
21
|
|
|
|
|
|
|
mapping => {First => 'Header1', Second => 'Header2'}, |
22
|
|
|
|
|
|
|
output => ['Memory', key => 'First'] |
23
|
|
|
|
|
|
|
} )->process; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 DESCRIPTION |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
B<ETL::Pipeline::Output::Memory> writes the record into a Perl data structure, |
28
|
|
|
|
|
|
|
in memory. The records can be accessed later in the same script. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
This output destination comes in useful when processing multiple input files. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head2 Internal data structure |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
B<ETL::Pipeline::Output::Memory> offers two ways of storing the records - in |
35
|
|
|
|
|
|
|
a hash or in a list. B<ETL::Pipeline::Output::Memory> automatically chooses |
36
|
|
|
|
|
|
|
the correct one depending on the L</key> attribute. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
When L</key> is set, B<ETL::Pipeline::Output::Memory> saves the records in a |
39
|
|
|
|
|
|
|
hash, keyed by the given field. This allows for faster look-up. Use L</key> |
40
|
|
|
|
|
|
|
when the record has an identifier. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
When L</key> is not set, B<ETL::Pipeline::Output::Memory> saves the record in |
43
|
|
|
|
|
|
|
a list. The list saves records unordered - first in first out. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
package ETL::Pipeline::Output::Memory; |
48
|
1
|
|
|
1
|
|
4
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
1
|
|
4623
|
use 5.14.0; |
|
1
|
|
|
|
|
4
|
|
51
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
52
|
|
|
|
|
|
|
|
53
|
1
|
|
|
1
|
|
3
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
64
|
|
54
|
1
|
|
|
1
|
|
4
|
use String::Util qw/hascontent/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
261
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
our $VERSION = '1.00'; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head1 METHODS & ATTRIBUTES |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head2 Arguments for L<ETL::Pipeline/output> |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head3 key |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The L</current> record is stored as a Perl hash. B<key> is the output |
67
|
|
|
|
|
|
|
destination field name that ties this record with whatever other data you have. |
68
|
|
|
|
|
|
|
In short, B<key> is the identifier field. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
B<key> can be blank. In that case, records are stored in L</list>, unsorted. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
has 'key' => ( |
75
|
|
|
|
|
|
|
default => '', |
76
|
|
|
|
|
|
|
is => 'ro', |
77
|
|
|
|
|
|
|
isa => 'Str', |
78
|
|
|
|
|
|
|
); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 Called from L<ETL::Pipeline/process> |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head3 write_record |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
B<write_record> copies the contents of L</current> into L</hash> or L</list>, |
86
|
|
|
|
|
|
|
saving the record into memory. You can retrieve the records later using the |
87
|
|
|
|
|
|
|
L</with_id> or L</records> methods. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub write_record { |
92
|
|
|
|
|
|
|
my $self = shift; |
93
|
|
|
|
|
|
|
my $key = $self->key; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Key field = hash |
96
|
|
|
|
|
|
|
# No key field = list |
97
|
|
|
|
|
|
|
my $list; |
98
|
|
|
|
|
|
|
if (hascontent( $key )) { |
99
|
|
|
|
|
|
|
# NULL is an invalid key. Empty strings are okay, though. If the data |
100
|
|
|
|
|
|
|
# has NULLs, then your script should translate them. |
101
|
|
|
|
|
|
|
my $id = $self->get_value( $key ); |
102
|
|
|
|
|
|
|
return $self->error( "The field '$key' was not set" ) unless defined $id; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
$list = $self->with_id( $id ); |
105
|
|
|
|
|
|
|
unless (defined $list) { |
106
|
|
|
|
|
|
|
$list = []; |
107
|
|
|
|
|
|
|
$self->_add_to_id( $id, $list ); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} else { $list = $self->list; } |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# "new_record" creates a brand new reference. So it's safe to store this |
112
|
|
|
|
|
|
|
# reference without making a copy. |
113
|
|
|
|
|
|
|
push @$list, $self->current; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
return 1; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head3 configure |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
B<configure> doesn't actually do anything. But it is required by |
122
|
|
|
|
|
|
|
L<ETL::Pipeline/process>. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub configure {} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head3 finish |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
B<finish> doesn't actually do anything. But it is required by |
132
|
|
|
|
|
|
|
L<ETL::Pipeline/process>. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=cut |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
0
|
1
|
|
sub finish {} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head2 Other methods and attributes |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head3 with_id |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
B<with_id> returns a list of records for the given key. Pass in a value for the |
144
|
|
|
|
|
|
|
key and B<with_id> returns an array reference of records. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
B<with_id> only works if the L</key> attribute was set. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head3 hash |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
B<hash> is a hash reference used when L</key> is set. The key is the value |
151
|
|
|
|
|
|
|
of the field idnetified by L</key>. The value is an array reference. The array |
152
|
|
|
|
|
|
|
contains all of the records with that same key. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=cut |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
has 'hash' => ( |
157
|
|
|
|
|
|
|
default => sub { {} }, |
158
|
|
|
|
|
|
|
handles => {_add_to_id => 'set', number_of_ids => 'count', with_id => 'get'}, |
159
|
|
|
|
|
|
|
is => 'ro', |
160
|
|
|
|
|
|
|
isa => 'HashRef[ArrayRef[HashRef[Any]]]', |
161
|
|
|
|
|
|
|
traits => [qw/Hash/], |
162
|
|
|
|
|
|
|
); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head3 records |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
B<records> returns a list of hash references. Each hash reference is one data |
168
|
|
|
|
|
|
|
record. B<records> only works when the L</key> attribute is blank. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head3 list |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
B<list> is an array reference that stores records. The records are saved in |
173
|
|
|
|
|
|
|
same order as they are read from the input source. Each list element is a |
174
|
|
|
|
|
|
|
hash reference (the record). |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
has 'list' => ( |
179
|
|
|
|
|
|
|
default => sub { [] }, |
180
|
|
|
|
|
|
|
handles => {number_of_records => 'count', records => 'elements'}, |
181
|
|
|
|
|
|
|
is => 'ro', |
182
|
|
|
|
|
|
|
isa => 'ArrayRef[HashRef[Any]]', |
183
|
|
|
|
|
|
|
traits => [qw/Array/], |
184
|
|
|
|
|
|
|
); |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head3 default_fields |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Initialize L</current> for the next record. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=cut |
192
|
|
|
|
|
|
|
|
193
|
5
|
|
|
5
|
1
|
12
|
sub default_fields { () } |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=head1 SEE ALSO |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
L<ETL::Pipeline>, L<ETL::Pipeline::Output>, |
199
|
|
|
|
|
|
|
L<ETL::Pipeline::Output::Storage::Hash> |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
with 'ETL::Pipeline::Output::Storage::Hash'; |
204
|
|
|
|
|
|
|
with 'ETL::Pipeline::Output'; |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 AUTHOR |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Robert Wohlfarth <robert.j.wohlfarth@vanderbilt.edu> |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 LICENSE |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Copyright 2016 (c) Vanderbilt University |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under |
216
|
|
|
|
|
|
|
the same terms as Perl itself. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=cut |
219
|
|
|
|
|
|
|
|
220
|
1
|
|
|
1
|
|
4
|
no Moose; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
221
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |