TT/CDBFile.pm

filter: Statementstmt 20.2% 17 / 84 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 21.7% 5 / 23 Podpod 7.1% 1 / 14 totaltotal 11.8% 23 / 194 CC 29 SCAR 34.8
CC
29
Cov
12%
open
110.0
truncate
30.0
__ANON__
30.0
CRAP 32.5
« TT/Packed/zt.pm ↑ Summary TT/CDBFile/JSON.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::CDBFile.pm
3 ## Author: Bryan Jurish <TT/IO.pm>
4 ## Descript: TT I/O: CDB: tied read-only access via CDB_File
5  
6 package Lingua::TT::CDBFile;
7 1 ▶use Lingua::TT::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use CDB_File;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ##==============================================================================
14 ## Globals & Constants
15  
16 1 our @ISA = qw(Lingua::TT::Dict);
17  
18 ##==============================================================================
19 ## Constructors etc.
20  
21 ## $dbf = CLASS_OR_OBJECT->new(%opts)
22 ## + %opts, %$doc:
23 ## ##-- user options
24 ## file => $filename, ##-- default: undef (none)
25 ## tmpfile => $tmpfilename, ##-- defualt: "$filename.$$" (not used correctly due to CDB_File bug)
26 ## mode => $mode, ##-- open mode 'r', 'w', 'rw', '<', '>', '>>': default='r'
27 ## utf8 => $bool, ##-- if true, keys/values are stored as UTF8 (default=1)
28 ## ##
29 ## ##-- low-level data
30 ## data => \%data, ##-- tied data (hash)
31 ## tied => $ref, ##-- read-only: reference returned by tie()
32 ## writer => $ref, ##-- read/write: reference returned by CDB_File::new()
33 ## fetch => \&fetch, ##-- fetch subroutine: $val = $fetch->($key)
34 ## store => \&store, ##-- store subroutine: $val = $store->($key,$val)#
35 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
36 0 my $that = shift;
37 0 ▶ my $dbf = 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
38 file => undef,
39 tmpfile => undef,
40 mode => 'r',
41 utf8 => 1,
42 ##
43 data => undef,
44 tied => undef,
45 @_
46 }, ref($that)||$that);
47 0 ▶ return $dbf->open($dbf->{file}) if (defined($dbf->{file}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $dbf->{"file"} 0 0 0
48 0 return $dbf;
49 }
50  
51 ## undef = $dbf->clear()
52 ## + clears data (if any)
53 0 ▶sub clear {
Subroutine clear: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my $dbf = shift;
55 0 ▶ return if (!$dbf->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbf->opened 0 0 0
56 0 %{$dbf->{data}} = qw();
57 0 return $dbf;
58 }
59  
60  
61 ##==============================================================================
62 ## Methods: low-level utilities
63  
64 ##==============================================================================
65 ## Methods: I/O
66  
67 ## $bool = $dbf->opened()
68 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod undocumented
69 0 ▶ return (defined($_[0]{tied}) || defined($_[0]{writer}));
Condition0%0/3
defined $_[0]{'tied'} || defined $_[0]{'writer'}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $_[0]{'tied'} || defined $_[0]{'writer'}
defined $_[0]{'tied'} defined $_[0]{'writer'}
Truth table0%0/3
A || B
A= defined $_[0]{'tied'}B= defined $_[0]{'writer'}
ABresult
1X1
011
000
70 }
71  
72 ## $dbf = $dbf->close()
73 0 ▶sub close {
Subroutine close: not called CC=4 SCAR=30.0
Pod undocumented
74 0 my $dbf = shift;
75 0 ▶ return $dbf if (!$dbf->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbf->opened 0 0 0
76 0 ▶ $dbf->{writer}->finish() if ($dbf->{writer});
Branch: 1 branch
BranchTrueFalseTotal
if $dbf->{"writer"} 0 0 0
77 0 delete(@$dbf{qw(fetch store)});
78 0 ▶ if (defined($dbf->{tied})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbf->{"tied"}) 0 0 0
79 0 $dbf->{tied} = undef;
80 0 untie(%{$dbf->{data}});
81 }
82 0 return $dbf;
83 }
84  
85 ## $dbf = $dbf->open($file,%opts)
86 ## + %opts are as for new()
87 ## + $file defaults to $dbf->{file}
88 0 ▶sub open {
Subroutine open: not called CC=10 SCAR=47.0
Pod undocumented
89 0 my ($dbf,$file,%opts) = @_;
90 0 ▶ $dbf->close() if ($dbf->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $dbf->opened 0 0 0
91 0 @$dbf{keys %opts} = values(%opts);
92 0 ▶ $file = $dbf->{file} if (!defined($file));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
93 0 $dbf->{file} = $file;
94 0 ▶ $dbf->{tmpfile} = "$file.$$" if (!defined($dbf->{tmpfile}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dbf->{"tmpfile"} 0 0 0
95  
96 ##-- truncate file here if user requested it
97 0 ▶ if ($dbf->{mode} =~ /^[\+r]*[>w]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbf->{"mode"} =~ /^[\+r]*[>w]$/) 0 0 0
98 0 ▶ $dbf->truncate()
Branch: 1 branch
BranchTrueFalseTotal
$dbf->truncate or confess(ref($dbf) . qq[::open(): could not truncate file '$dbf->{"file"}': $!]) 0 0 0
99 or confess(ref($dbf)."::open(): could not truncate file '$dbf->{file}': $!");
100 }
101  
102 ##-- tie data hash
103 0 delete(@$dbf{qw(writer data tied fetch store)});
104 0 ▶ if ($dbf->{mode} =~ /[\+w>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbf->{"mode"} =~ /[\+w>]/) 0 0 0
105 $dbf->{writer} = CDB_File->new($dbf->{file}, $dbf->{tmpfile})
106 0 ▶ or confess(ref($dbf)."::open(): CDB_File->new() failed for '$dbf->{file}': $!");
Branch: 1 branch
BranchTrueFalseTotal
$dbf->{"writer"} = "CDB_File"->new($dbf->{"file"}, $dbf->{"tmpfile"}) or confess(ref($dbf) . qq[::open(): CDB_File->new() failed for '$dbf->{"file"}': $!]) 0 0 0
107 }
108 0 ▶ if ($dbf->{mode} =~ /[\+r<]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbf->{"mode"} =~ /[\+r<]/) 0 0 0
109 0 $dbf->{data} = {};
110 0 $dbf->{tied} = tie(%{$dbf->{data}}, 'CDB_File', $dbf->{file}) #$dbf->{tmpfile}
111 0 ▶ or confess(ref($dbf).":open(): could not tie CDB_File for file '$dbf->{file}': $!");
Branch: 1 branch
BranchTrueFalseTotal
$dbf->{"tied"} = tie(%{$$dbf{"data"};}, "CDB_File", $dbf->{"file"}) or confess(ref($dbf) . qq[:open(): could not tie CDB_File for file '$dbf->{"file"}': $!]) 0 0 0
112 }
113  
114 ##-- set fetch/store closures
115 0 $dbf->{fetch} = $dbf->fetchSub();
116 0 $dbf->{store} = $dbf->storeSub();
117  
118 0 return $dbf;
119 }
120  
121 ## $bool = $dbf->truncate()
122 ## $bool = $CLASS_OR_OBJ->truncate($file)
123 ## + actually calls unlink($file)
124 ## + no-op if $file and $dbf->{file} are both undef
125 0 ▶sub truncate {
Subroutine truncate: not called CC=5 SCAR=34.0
Pod undocumented
126 0 my ($dbf,$file) = @_;
127 0 ▶ $file = $dbf->{file} if (!defined($file));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
128 0 ▶ return if (!defined($file));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 0 0
129 0 ▶ !-e $file || unlink($file) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
not -e $file or unlink $file or return undef 0 0 0
Condition0%0/3
not -e $file or unlink $file
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e $file or unlink $file
not -e $file unlink $file
Truth table0%0/3
not(A) or B
A= not -e $fileB= unlink $file
ABresult
1X1
011
000
130 }
131  
132 ## $bool = $dbf->sync()
133 ## $bool = $dbf->sync($flags)
134 0 ▶sub sync {
Subroutine sync: not called CC=4 SCAR=30.0
Pod undocumented
135 0 my $dbf = shift;
136 0 ▶ return 1 if (!$dbf->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless $dbf->opened 0 0 0
137 0 ▶ return $dbf->close() && $dbf->open() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbf->close && $dbf->open ? : 0 0 0
Condition0%0/3
$dbf->close and $dbf->open
!ll&&!rl&&r
000
MC/DC0%0/2
$dbf->close and $dbf->open
$dbf->close $dbf->open
Truth table0%0/3
A and B
A= $dbf->closeB= $dbf->open
ABresult
0X0
100
111
138 }
139  
140 ## $bool = $dbf->copy($file2)
141 ## $bool = PACKAGE::copy($file1,$file2)
142 ## + copies database data to $file2
143 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod undocumented
144 0 confess(ref($_[0])."::copy() not implemented");
145 }
146  
147 ##==============================================================================
148 ## Methods: Lookup: Closures
149  
150 ## \&sub = $dbf->fetchSub($key)
151 ## + subroutine to return (decoded) value
152 0 ▶sub fetchSub {
Subroutine fetchSub: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my $tied = $_[0]{tied};
154 0 ▶ if ($_[0]{utf8}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"utf8"}) 0 0 0
155 ##-- TT mode, utf8
156 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
157 0 local $_ = $tied->FETCH($_[0]);
158 0 utf8::decode($_);
159 0 return $_;
160 0 };
161 }
162 ##-- TT mode, raw
163 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
164 0 return $tied->FETCH($_[0]);
165 0 };
166 }
167  
168 ## $storeSub = $dbf->store($key,$val)
169 0 ▶sub storeSub {
Subroutine storeSub: not called CC=1 SCAR=6.9
Pod undocumented
170 0 my $tied = $_[0]{tied};
171 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 return $tied->STORE($_[0],$_[1]);
173 0 };
174 }
175  
176 ##==============================================================================
177 ## Methods: Lookup
178  
179 ## $val = $dbf->fetch($key)
180 ## + returns (decoded) value
181 0 ▶sub fetch {
Subroutine fetch: not called CC=1 SCAR=6.9
Pod undocumented
182 0 return $_[0]{fetch}->($_[1]);
183 }
184  
185 ## $val = $dbf->store($key)
186 ## + just stores value
187 0 ▶sub store {
Subroutine store: not called CC=1 SCAR=6.9
Pod undocumented
188 0 return $_[0]{store}->($_[1],$_[2]);
189 }
190  
191 ##==============================================================================
192 ## Methods: Apply
193  
194 ## \&apply = $dict->applySub(%opts)
195 ## + returns a CODE-ref for applying dictionary analysis to a single item
196 ## + returned sub is called without arguments
197 ## - data line to be analyzed (chomped) is in $_
198 ## - output for current data line should be stored in $_
199 0 ▶sub applySub {
Subroutine applySub: not called CC=1 SCAR=6.9
Pod undocumented
200 0 my ($dict,%opts) = @_;
201 0 my $fetch = $dict->{fetch};
202 0 my $include_empty = $opts{allow_empty};
203 0 my ($text,$a_in,$a_dict);
204 0 ▶ return sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
205 0 ($text,$a_in) = split(/\t/,$_,2);
206 0 $a_dict = $fetch->($text);
207 0 ▶ $_ = join("\t", $text, (defined($a_in) ? $a_in : qw()), (defined($a_dict) && ($include_empty || $a_dict ne '') ? $a_dict : qw()))."\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $a_in ? : 0 0 0
defined $a_dict && ($include_empty || $a_dict ne '') ? : 0 0 0
Condition0%0/6
defined $a_dict and $include_empty || $a_dict ne ""
!ll&&!rl&&r
000
$include_empty or $a_dict ne ""
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $a_dict and $include_empty || $a_dict ne ""
defined $a_dict $include_empty $a_dict ne ""
Truth table0%0/4
A and B or C
A= defined $a_dictB= $include_emptyC= $a_dict ne ""
ABCresult
0XX0
1000
11X1
1011
208 0 };
209 }
210  
211  
212  
213 ##==============================================================================
214 ## Methods: TT::Persistent
215  
216 ## @keys = $dbf->noSaveKeys()
217 0 ▶sub noSaveKeys {
Subroutine noSaveKeys: not called CC=1 SCAR=6.9
Pod documented
218 0 return qw(data tied writer fetch store);
219 }
220  
221 ##==============================================================================
222 ## Footer
223 1 1;
224  
225 __END__
« TT/Packed/zt.pm ↑ Summary TT/CDBFile/JSON.pm »