DiaColloDB/Client.pm

filter: Statementstmt 21.3% 22 / 103 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 26.6% 8 / 30 Podpod 95.2% 20 / 21 totaltotal 19.5% 50 / 256 CC 41 SCAR 38.2
CC
41
Cov
12%
open_rcfile
156.0
open
90.0
new
20.0
CRAP 45.8
« DiaColloDB/PDL/MM.pm ↑ Summary DiaColloDB/Upgrade/v0_09_multimap.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Client.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, client
5  
6 package DiaColloDB::Client;
7 2 ▶use DiaColloDB::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DiaColloDB::Client::file;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DiaColloDB::Client::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DiaColloDB::Client::list;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 ##==============================================================================
16 ## Globals & Constants
17  
18 1 our @ISA = qw(DiaColloDB::Persistent);
19  
20 ##==============================================================================
21 ## Constructors etc.
22  
23 ## $cli = CLASS_OR_OBJECT->new(%args)
24 ## $cli = CLASS_OR_OBJECT->new($url, %args)
25 ## + %args, object structure:
26 ## (
27 ## ##-- DiaColloDB::Client: options
28 ## url => $url, ##-- db url
29 ## )
30 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
31 0 my $that = shift;
32 0 ▶ my $url = (@_ % 2)==0 ? undef : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 0 0 0
33 0 ▶ my $cli = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
34 $that->defaults(),
35 url=>$url,
36 @_
37 }, ref($that)||$that);
38 0 ▶ return $cli->open($cli->{url},@_) if (defined($cli->{url}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $cli->{"url"} 0 0 0
39 0 return $cli;
40 }
41  
42 ## %defaults = $CLASS_OR_OBJ->defaults()
43 ## + called by new() and promote()
44 0 ▶sub defaults {
Subroutine defaults: not called CC=1 SCAR=6.9
Pod documented
45 0 return qw();
46 }
47  
48 ## $cli_or_undef = $cli->promote($class,%opts)
49 ## + promotes $cli to (a subclass of) $class
50 ## + ensures $class->defaults() keys are set for $cli
51 ## + client options are clobbered with %opts
52 0 ▶sub promote {
Subroutine promote: not called CC=3 SCAR=24.8
Pod documented
53 0 my ($cli,$class,%opts) = @_;
54 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
55 0 ▶ bless($cli,$class) if (!UNIVERSAL::isa($cli,$class));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($cli, $class) 0 0 0
56 0 @$cli{keys %opts} = values(%opts);
57 0 %$cli = ($class->defaults,%$cli);
58 0 return $cli;
59 }
60  
61 ## undef = $obj->DESTROY
62 ## + destructor calls close() if necessary
63 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=4 SCAR=30.0
64 #$_[0]->trace("DESTROY (tid=", (UNIVERSAL::can('threads','tid') ? threads->tid : '-undef-'), ')'); ##-- DEBUG:thrads
65 0 ▶ return if (UNIVERSAL::can('threads','tid') && threads->tid!=0); ##-- don't implicitly close if we're not in main thread
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::can("threads", "tid") and "threads"->tid != 0 0 0 0
Condition0%0/3
UNIVERSAL::can("threads", "tid") and "threads"->tid != 0
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::can("threads", "tid") and "threads"->tid != 0
UNIVERSAL::can("threads", "tid") "threads"->tid != 0
Truth table0%0/3
A and B
A= UNIVERSAL::can("threads", "tid")B= "threads"->tid != 0
ABresult
0X0
100
111
66 0 ▶ $_[0]->close() if ($_[0]->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->opened 0 0 0
67 }
68  
69 ##==============================================================================
70 ## I/O: open/close
71  
72 ## $cli_or_undef = $cli->open($url,%opts)
73 ## $cli_or_undef = $cli->open()
74 ## + calls open_rcfile(), open_file(), open_http(), or open_list() as appropriate
75 0 ▶sub open {
Subroutine open: not called CC=9 SCAR=45.0
Pod documented
76 0 my ($cli,$url) = (shift,shift);
77 0 ▶ $url //= $cli->{url};
Condition0%0/3
$url //= $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$url //= $cli->{"url"}
$url $cli->{"url"}
Truth table0%0/3
A //= B
A= $urlB= $cli->{"url"}
ABresult
1X1
011
000
78 0 ▶ my $scheme = UNIVERSAL::isa($url,'ARRAY') ? 'list' : (URI->new($url)->scheme // 'file');
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($url, 'ARRAY') ? : 0 0 0
Condition0%0/2
"URI"->new($url)->scheme // "file"
l!l
00
MC/DC0%0/1
"URI"->new($url)->scheme // "file"
"URI"->new($url)->scheme
Truth table0%0/2
A
A= "URI"->new($url)->scheme
Aresult
11
00
79 0 ▶ if ($scheme =~ /^rcfile|rc$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($scheme =~ /^rcfile|rc$/) { } 0 0 0
elsif ($scheme eq 'file') { } 0 0 0
elsif ($scheme =~ /^https?$/) { } 0 0 0
elsif ($scheme eq 'list') { } 0 0 0
80 0 return $cli->open_rcfile($url,@_);
81 }
82 elsif ($scheme eq 'file') {
83 0 return $cli->open_file($url,@_);
84 }
85 elsif ($scheme =~ /^https?$/) {
86 0 return $cli->open_http($url,@_);
87 }
88 elsif ($scheme eq 'list') {
89 0 return $cli->open_list($url,@_);
90 }
91 0 ▶ $cli->logconfess("open(): unsupported URL scheme ".($scheme ? "'$scheme'" : '(undef)')." for $url");
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 0 0 0
92 0 return undef;
93 }
94  
95 ## $cli_or_undef = $cli->open_rcfile($rcfile_url,%opts)
96 ## $cli_or_undef = $cli->open_rcfile()
97 ## + opens a local file url
98 ## + may re-bless() $cli into an appropriate package
99 ## + loads a JSON config file containing one or more of the following keys:
100 ## (
101 ## class => $CLASS, ##-- bless() client into class $CLASS
102 ## url => $url, ##-- open client url $url
103 ## $key => $val, ##-- ... other keys passed to $cli->open($url,%opts)
104 ## )
105 0 ▶sub open_rcfile {
Subroutine open_rcfile: not called CC=12 SCAR=50.5
Pod documented
106 0 my ($cli,$rcurl,%opts) = @_;
107 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
108 0 ▶ $cli->close() if ($cli->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->opened 0 0 0
109 0 ▶ $cli->{rcurl} = ($rcurl //= $cli->{url});
Condition0%0/3
$rcurl //= $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rcurl //= $cli->{"url"}
$rcurl $cli->{"url"}
Truth table0%0/3
A //= B
A= $rcurlB= $cli->{"url"}
ABresult
1X1
011
000
110 0 my $uri = URI->new($rcurl);
111 0 ▶ my $rcpath = ($uri->authority//'') . ($uri->path//'');
Condition0%0/4
$uri->authority // ""
l!l
00
$uri->path // ""
l!l
00
MC/DC0%0/1
$uri->authority // ""
$uri->authority
MC/DC0%0/1
$uri->path // ""
$uri->path
Truth table0%0/2
A
A= $uri->authority
Aresult
11
00
Truth table0%0/2
A
A= $uri->path
Aresult
11
00
112 0 my %rcopts = $uri->query_form;
113  
114 ##-- load data from file
115 0 ▶ my $hdr = $cli->readHeaderFile($rcpath)
Branch: 1 branch
BranchTrueFalseTotal
my $hdr = $cli->readHeaderFile($rcpath) or $cli->logconfess("open_rcfile() failed to open config file '${rcpath}' for URL '${rcurl}': $!") 0 0 0
116 or $cli->logconfess("open_rcfile() failed to open config file '$rcpath' for URL '$rcurl': $!");
117 0 ▶ $cli->promote($hdr->{class}) if ($hdr->{class});
Branch: 1 branch
BranchTrueFalseTotal
if $hdr->{"class"} 0 0 0
118 0 delete $hdr->{class};
119 0 @$cli{keys %$hdr} = values %$hdr;
120 0 @$cli{keys %rcopts} = values %rcopts;
121 0 @$cli{keys %opts} = values %opts;
122  
123 ##-- dispatch to lower-level open:// call
124 0 ▶ delete $cli->{url} if (($cli->{url}//'') eq $rcurl);
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->{"url"} // "") eq $rcurl 0 0 0
Condition0%0/2
$cli->{"url"} // ""
l!l
00
MC/DC0%0/1
$cli->{"url"} // ""
$cli->{"url"}
Truth table0%0/2
A
A= $cli->{"url"}
Aresult
11
00
125 0 ▶ return $cli->opened || !$cli->{url} ? $cli : $cli->open($cli->{url},%opts);
Branch: 1 branch
BranchTrueFalseTotal
$cli->opened || !$cli->{'url'} ? : 0 0 0
Condition0%0/3
$cli->opened or not $cli->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cli->opened or not $cli->{"url"}
$cli->opened not $cli->{"url"}
Truth table0%0/3
A or not(B)
A= $cli->openedB= not $cli->{"url"}
ABresult
1X1
011
000
126 }
127  
128 ## $cli_or_undef = $cli->open_file($file_url,%opts)
129 ## $cli_or_undef = $cli->open_file()
130 ## + opens a local file url
131 ## + may re-bless() $cli into an appropriate package
132 ## + OVERRIDE in subclasses supporting file urls
133 0 ▶sub open_file {
Subroutine open_file: not called CC=4 SCAR=30.0
Pod documented
134 0 my $cli = shift;
135 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
136 0 ▶ $cli->close() if ($cli->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->opened 0 0 0
137 0 $cli->promote('DiaColloDB::Client::file');
138 0 ▶ $cli->logconfess("open_file(): not implemented") if ($cli->can('open_file') eq \&open_file);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->can("open_file") eq \&DiaColloDB::Client::open_file 0 0 0
139 0 return $cli->open_file(@_)
140 }
141  
142 ## $cli_or_undef = $cli->open_http($http_url,%opts)
143 ## $cli_or_undef = $cli->open_http()
144 ## + opens a http url
145 ## + may re-bless() $cli into an appropriate package
146 ## + OVERRIDE in subclasses supporting http urls
147 0 ▶sub open_http {
Subroutine open_http: not called CC=4 SCAR=30.0
Pod documented
148 0 my $cli = shift;
149 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
150 0 ▶ $cli->close() if ($cli->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->opened 0 0 0
151 0 $cli->promote('DiaColloDB::Client::http');
152 0 ▶ $cli->logconfess("open_http(): not implemented") if ($cli->can('open_http') eq \&open_http);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->can("open_http") eq \&DiaColloDB::Client::open_http 0 0 0
153 0 return $cli->open_http(@_)
154 }
155  
156 ## $cli_or_undef = $cli->open_list($list_url,%opts)
157 ## $cli_or_undef = $cli->open_list(\@urls, %opts)
158 ## $cli_or_undef = $cli->open_list()
159 ## + opens a list url
160 ## + may re-bless() $cli into an appropriate package
161 ## + OVERRIDE in subclasses supporting list urls
162 0 ▶sub open_list {
Subroutine open_list: not called CC=4 SCAR=30.0
Pod documented
163 0 my $cli = shift;
164 0 ▶ $cli = $cli->new() if (!ref($cli));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $cli 0 0 0
165 0 ▶ $cli->close() if ($cli->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->opened 0 0 0
166 0 $cli->promote('DiaColloDB::Client::list');
167 0 ▶ $cli->logconfess("open_list(): not implemented") if ($cli->can('open_list') eq \&open_list);
Branch: 1 branch
BranchTrueFalseTotal
if $cli->can("open_list") eq \&DiaColloDB::Client::open_list 0 0 0
168 0 return $cli->open_list(@_)
169 }
170  
171 ## $cli_or_undef = $cli->close()
172 ## + default just returns $cli
173 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
174 0 return $_[0];
175 }
176  
177 ## $bool = $cli->opened()
178 ## + default just checks for $cli->{url}
179 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod documented
180 0 ▶ return ref($_[0]) && defined($_[0]{url});
Condition0%0/3
ref $_[0] && defined $_[0]{'url'}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] && defined $_[0]{'url'}
ref $_[0] defined $_[0]{'url'}
Truth table0%0/3
A && B
A= ref $_[0]B= defined $_[0]{'url'}
ABresult
0X0
100
111
181 }
182  
183 ## %opts = $cli->dbOptions()
184 ## + options to be passed down to bottom-level DB
185 ## + default is all log-related options
186 0 ▶sub dbOptions {
Subroutine dbOptions: not called CC=2 SCAR=17.9
Pod undocumented
187 0 my $cli = shift;
188 0 ▶ return ref($cli) ? (map {($_=>$cli->{$_})} grep {/^log/} keys %$cli) : qw();
Branch: 1 branch
BranchTrueFalseTotal
ref $cli ? : 0 0 0
189 }
190  
191 ##==============================================================================
192 ## db-info
193  
194 ## \%info = $cli->dbinfo()
195 0 ▶sub dbinfo {
Subroutine dbinfo: not called CC=1 SCAR=6.9
Pod documented
196 0 my $cli = shift;
197 0 $cli->logconfess("dbinfo(): not implemented");
198 }
199  
200 ##==============================================================================
201 ## Profiling
202  
203 ##--------------------------------------------------------------
204 ## Profiling: Wrappers
205  
206 ## $mprf = $cli->query($rel,%opts)
207 ## + get a generic DiaColloDB::Profile::Multi object for $rel
208 ## + calls $coldb->profile() or $coldb->compare() as appropriate
209 0 ▶sub query {
Subroutine query: not called CC=3 SCAR=24.8
Pod documented
210 0 my ($cli,$rel) = (shift,shift);
211 0 ▶ if ($rel =~ s{^(?:d(?!dc)(?:iff)?|co?mp(?:are)?)[\-\/\.\:]?}{}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rel =~ s[^(?:d(?!dc)(?:iff)?|co?mp(?:are)?)[\-\/\.\:]?][]) { } 0 0 0
elsif ($rel =~ s[^ext(?:end)?[\-\/\.\:]?][]) { } 0 0 0
212 0 return $cli->compare($rel,@_);
213 }
214 elsif ($rel =~ s{^ext(?:end)?[\-\/\.\:]?}{}) {
215 0 return $cli->extend($rel,@_);
216 }
217 0 return $cli->profile($rel,@_);
218 }
219  
220 ## $mprf = $cli->profile1(%opts)
221 ## + get unigram frequency profile for selected items as a DiaColloDB::Profile::Multi object
222 ## + really just wraps $cli->profile('xf', %opts)
223 ## + %opts: see profile() method
224 0 ▶sub profile1 {
Subroutine profile1: not called CC=1 SCAR=6.9
Pod documented
225 0 return $_[0]->profile('xf',@_[1..$#_]);
226 }
227  
228  
229 ## $mprf = $cli->profile2(%opts)
230 ## + get co-frequency profile for selected items as a DiaColloDB::Profile::Multi object
231 ## + really just wraps $cli->profile('cof', %opts)
232 ## + %opts: see profile() method
233 0 ▶sub profile2 {
Subroutine profile2: not called CC=1 SCAR=6.9
Pod documented
234 0 return $_[0]->profile('cof',@_[1..$#_]);
235 }
236  
237 ## $mprf = $cli->compare1(%opts)
238 ## + get unigram comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object
239 ## + really just wraps $cli->compare('xf', %opts)
240 ## + %opts: see compare() method
241 1 ▶BEGIN { *diff1 = \&compare1; }
Subroutine BEGIN: called CC=1 SCAR=0.0
242 0 ▶sub compare1 {
Subroutine compare1: not called CC=1 SCAR=6.9
Pod documented
243 0 return $_[0]->compare('xf',@_[1..$#_]);
244 }
245  
246 ## $mprf = $cli->compare2(%opts)
247 ## + get co-frequency comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object
248 ## + really just wraps $cli->profile('cof', %opts)
249 ## + %opts: see compare() method
250 1 ▶BEGIN { *diff2 = \&compare2; }
Subroutine BEGIN: called CC=1 SCAR=0.0
251 0 ▶sub compare2 {
Subroutine compare2: not called CC=1 SCAR=6.9
Pod documented
252 0 return $_[0]->compare('cof',@_[1..$#_]);
253 }
254  
255 ##--------------------------------------------------------------
256 ## Profiling: Generic
257  
258 ## $mprf = $cli->profile($relation, %opts)
259 ## + get a relation profile for selected items as a DiaColloDB::Profile::Multi object
260 ## + %opts: as for DiaColloDB::profile()
261 ## + sets $cli->{error} on error
262 0 ▶sub profile {
Subroutine profile: not called CC=1 SCAR=6.9
Pod documented
263 0 my ($cli,$rel,%opts) = @_;
264 0 $cli->logconfess("profile(): not implemented");
265 }
266  
267 ##--------------------------------------------------------------
268 ## Profiling: extend (pass-2 for multi-clients)
269  
270 ## $mprf = $cli->extend($relation, %opts)
271 ## + get an extension-profile for selected items as a DiaColloDB::Profile::Multi object
272 ## + %opts: as for DiaColloDB::extend()
273 ## + sets $cli->{error} on error
274 0 ▶sub extend {
Subroutine extend: not called CC=1 SCAR=6.9
Pod documented
275 0 my ($cli,$rel,%opts) = @_;
276 0 $cli->logconfess("extend(): not implemented");
277 }
278  
279 ##--------------------------------------------------------------
280 ## Profiling: Comparison (diff)
281  
282 ## $mprf = $cli->compare($relation, %opts)
283 ## + get a relation comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object
284 ## + %opts: as for DiaColloDB::compare()
285 ## + sets $cli->{error} on error
286 0 ▶sub diff { $_[0]->compare(@_[1..$#_]); }
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
287 0 ▶sub compare {
Subroutine compare: not called CC=1 SCAR=6.9
Pod documented
288 0 my ($cli,$rel,%opts) = @_;
289 0 $cli->logconfess("compare(): not implemented");
290 }
291  
292 ##==============================================================================
293 ## Footer
294 1 1;
295  
296 __END__
« DiaColloDB/PDL/MM.pm ↑ Summary DiaColloDB/Upgrade/v0_09_multimap.pm »