blib/lib/App/RecordStream/Clumper/Options.pm

filter: Statementstmt 89.8% 106 / 118 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.8% 26 / 31 Podpod 0.0% 0 / 8 totaltotal 82.6% 148 / 179 CC 13 SCAR 8.7
CC
13
Cov
87%
accept_record
6.0
_wrap_key_cb
4.1
check_options
3.7
CRAP 2.4
« blib/lib/App/RecordStream/Operation/generate.pm ↑ Summary blib/lib/App/RecordStream/Operation/tocsv.pm »
1 package App::RecordStream::Clumper::Options;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use App::RecordStream::Clumper::CubeKeyPerfect;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use App::RecordStream::Clumper::KeyLRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use App::RecordStream::Clumper::KeyPerfect;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use App::RecordStream::Clumper::WrappedClumperCallback;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use App::RecordStream::Clumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use App::RecordStream::DomainLanguage::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use App::RecordStream::DomainLanguage::Snippet;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use App::RecordStream::DomainLanguage::Valuation::KeySpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use App::RecordStream::DomainLanguage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use App::RecordStream::KeyGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 sub new {
18 21 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
19  
20 21 App::RecordStream::Clumper->load_implementations();
21  
22 21 my $this = {
23 # options for old-style clumping
24 'KEY_SIZE' => undef,
25 'KEY_CUBE' => 0,
26  
27 'TBD' => [],
28  
29 # help
30 'HELP_LIST' => 0,
31 'HELP_SHOW' => 0,
32 };
33  
34 21 bless $this, $class;
35  
36 21 return $this;
37 }
38  
39 sub main_options {
40 21 ▶ my $this = shift;
Subroutine main_options: called CC=1 SCAR=0.2
Pod undocumented
41  
42 21 my $clumpers = $this->{'TBD'};
43  
44 return (
45 # old style clumping
46 10 ▶ "key|k=s" => sub { push @$clumpers, ['KEYGROUP', $_[1]]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 ▶ "dlkey|K=s" => sub { push @$clumpers, ['VALUATION', _build_dlkey($_[1])]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 "size|sz|n=i" => \($this->{'KEY_SIZE'}),
49 0 ▶ "adjacent|1" => sub { $this->{'KEY_SIZE'} = 1; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 "cube" => \($this->{'KEY_CUBE'}),
51  
52 # new style clumping
53 9 ▶ "clumper|c=s" => sub { push @$clumpers, ['CLUMPER', App::RecordStream::Clumper->make_clumper($_[1])]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 0 ▶ "dlclumper|C=s" => sub { push @$clumpers, ['CLUMPER', _build_dlclumper($_[1])]; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 21 );
56 }
57  
58 sub help_options {
59 21 ▶ my $this = shift;
Subroutine help_options: called CC=1 SCAR=0.0
Pod undocumented
60  
61 return (
62 "list-clumpers" => \($this->{'HELP_LIST'}),
63 21 "show-clumper=s" => \($this->{'HELP_SHOW'}),
64 );
65 }
66  
67 sub check_options {
68 19 ▶ my $this = shift;
Subroutine check_options: called CC=3 SCAR=13.1
Pod undocumented
69 19 my $clumper_callback = shift;
70  
71 19 ▶ if($this->{'HELP_LIST'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"HELP_LIST"}) 0 19 19
72 0 ▶ die sub { print App::RecordStream::Clumper->list_implementations(); };
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 }
74  
75 19 ▶ if($this->{'HELP_SHOW'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"HELP_SHOW"}) 0 19 19
76 0 ▶ die sub { App::RecordStream::Clumper->show_implementation($this->{'HELP_SHOW'}) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
77 }
78  
79 19 $this->{'CALLBACK'} = $clumper_callback;
80 19 $this->{'CALLBACK_COOKIE'} = undef;
81 }
82  
83 sub _build_dlkey {
84 1 ▶ my $string = shift;
Subroutine _build_dlkey: called CC=2 SCAR=7.2
85  
86 1 my $name;
87 1 ▶ if($string =~ s/^([^=]*)=//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/^([^=]*)=//) { } 1 0 1
88 1 $name = $1;
89 }
90 else {
91 0 die "Bad domain language key option (missing '=' to separate name and code): " . $string;
92 }
93  
94 1 return ($name, App::RecordStream::DomainLanguage::Snippet->new($string)->evaluate_as('VALUATION'));
95 }
96  
97 sub _build_dlclumper {
98 0 ▶ my $string = shift;
Subroutine _build_dlclumper: not called CC=1 SCAR=6.9
99  
100 0 return App::RecordStream::DomainLanguage::Snippet->new($string)->evaluate_as('CLUMPER');
101 }
102  
103 sub _get_cb_and_cookie {
104 146 ▶ my $this = shift;
Subroutine _get_cb_and_cookie: called CC=2 SCAR=6.9
105  
106 146 my $cb = $this->{'CALLBACK'};
107 146 my $cookie = $this->{'CALLBACK_COOKIE'};
108 146 ▶ if(!defined($cookie)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cookie) 19 127 146
109 19 $cookie = $this->{'CALLBACK_COOKIE'} = $cb->clumper_callback_begin({});
110 }
111  
112 146 return ($cb, $cookie);
113 }
114  
115 sub accept_record {
116 128 ▶ my $this = shift;
Subroutine accept_record: called CC=6 SCAR=17.9
Pod undocumented
117 128 my $record = shift;
118  
119 128 my $clumpers = $this->{'TBD'};
120 128 ▶ while(@$clumpers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$clumpers) 20 128 148
121 20 my $clumper_tuple = pop @$clumpers;
122 20 my ($type, @rest) = @$clumper_tuple;
123  
124 20 my $cb = $this->{'CALLBACK'};
125  
126 20 ▶ if(0) {
Branch: 3 branches
BranchTrueFalseTotal
if (do { $type eq 'KEYGROUP' }) { } 10 10 20
elsif ($type eq 'VALUATION') { } 1 9 10
elsif ($type eq 'CLUMPER') { } 9 0 9
127 }
128 elsif($type eq 'KEYGROUP') {
129 10 my ($group_spec) = @rest;
130  
131 10 my $key_groups = App::RecordStream::KeyGroups->new();
132 10 $key_groups->add_groups($group_spec);
133 10 my $keys = $key_groups->get_keyspecs($record);
134  
135 10 for my $spec (@$keys) {
136 12 $cb = $this->_wrap_key_cb($spec, App::RecordStream::DomainLanguage::Valuation::KeySpec->new($spec), $cb);
137 }
138 }
139 elsif($type eq 'VALUATION') {
140 1 my ($name, $val) = @rest;
141  
142 1 $cb = $this->_wrap_key_cb($name, $val, $cb);
143 }
144 elsif($type eq 'CLUMPER') {
145 9 my ($clumper) = @rest;
146  
147 9 $cb = App::RecordStream::Clumper::WrappedClumperCallback->new($clumper, $cb);
148 }
149 else {
150 0 die "Internal error";
151 }
152  
153 20 $this->{'CALLBACK'} = $cb;
154 }
155  
156 128 my ($cb, $cookie) = $this->_get_cb_and_cookie();
157  
158 128 $cb->clumper_callback_push_record($cookie, $record);
159  
160 128 return 1;
161 }
162  
163 sub _wrap_key_cb {
164 13 ▶ my $this = shift;
Subroutine _wrap_key_cb: called CC=4 SCAR=14.2
165 13 my $name = shift;
166 13 my $val = shift;
167 13 my $cb = shift;
168  
169 13 my $size = $this->{'KEY_SIZE'};
170 13 my $cube = $this->{'KEY_CUBE'};
171  
172 13 my $clumper;
173 13 ▶ if($cube) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cube) { } 4 9 13
elsif (defined $size) { } 0 9 9
174 4 ▶ if(defined($size)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 4 4
175 0 die "--cube with --size (or --adjacent) is no longer a thing (and it never made sense)";
176 }
177 4 $clumper = App::RecordStream::Clumper::CubeKeyPerfect->new_from_valuation($name, $val);
178 }
179 elsif(defined($size)) {
180 0 $clumper = App::RecordStream::Clumper::KeyLRU->new_from_valuation($name, $val, $size);
181 }
182 else {
183 9 $clumper = App::RecordStream::Clumper::KeyPerfect->new_from_valuation($name, $val);
184 }
185  
186 13 return App::RecordStream::Clumper::WrappedClumperCallback->new($clumper, $cb);
187 }
188  
189 sub stream_done {
190 18 ▶ my $this = shift;
Subroutine stream_done: called CC=1 SCAR=0.0
Pod undocumented
191  
192 18 my ($cb, $cookie) = $this->_get_cb_and_cookie();
193  
194 18 $cb->clumper_callback_end($cookie);
195 }
196  
197 sub main_usage {
198 return (
199 4 ▶ [ 'key|-k <keys>', 'Comma separated list of key fields. May be a key spec or key group'],
Subroutine main_usage: called CC=1 SCAR=0.0
Pod undocumented
200 [ 'dlkey|-K ...', 'Specify a domain language key. See "Domain Language Integration" section in --help-more.'],
201 [ 'size|--sz|-n <number>', 'Number of running clumps to keep.'],
202 [ 'adjacent|-1', 'Only group together adjacent records. Avoids spooling records into memeory'],
203 [ 'cube', 'See "Cubing" section in --help-more.'],
204 [ 'clumper ...', 'Use this clumper to group records. May be specified multiple times. See --help-clumping.'],
205 [ 'dlclumper ...', 'Use this domain language clumper to group records. May be specified multiple times. See --help-clumping.'],
206 );
207 }
208  
209 sub help_usage {
210 return (
211 4 ▶ [ 'list-clumpers', 'Bail and output a list of clumpers' ],
Subroutine help_usage: called CC=1 SCAR=0.0
Pod undocumented
212 [ 'show-clumper <clumper>', 'Bail and output this clumper\'s detailed usage.'],
213 );
214 }
215  
216 5 1;
« blib/lib/App/RecordStream/Operation/generate.pm ↑ Summary blib/lib/App/RecordStream/Operation/tocsv.pm »