DiaColloDB/EnumFile/FixedLen.pm

filter: Statementstmt 12.4% 17 / 137 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 52 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 33.3% 5 / 15 Podpod 90.0% 9 / 10 totaltotal 9.5% 31 / 324 CC 59 SCAR 47.6
CC
59
Cov
6%
flush
182.0
s2i
182.0
re2i
156.0
CRAP 117.2
« DiaColloDB/Relation/Unigrams.pm ↑ Summary DiaColloDB/Upgrade/Base.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::EnumFile::FixedLen.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, symbol<->integer enum, fixed-length symbols
5  
6 package DiaColloDB::EnumFile::FixedLen;
7 1 ▶use DiaColloDB::EnumFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DiaColloDB::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DiaColloDB::Utils qw(:fcntl :file :json :regex :pack);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Fcntl qw(:DEFAULT :seek);
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(DiaColloDB::EnumFile);
17  
18 ##==============================================================================
19 ## Constructors etc.
20  
21 ## $enum = CLASS_OR_OBJECT->new(%args)
22 ## + %args, object structure:
23 ## (
24 ## ##-- EnumFile: basic options
25 ## base => $base, ##-- database basename; use files "${base}.fsx", "${base}.fix", "${base}.hdr"
26 ## perms => $perms, ##-- default: 0666 & ~umask
27 ## flags => $flags, ##-- default: 'r'
28 ## pack_i => $pack_i, ##-- integer pack template (default='N')
29 ## #pack_o => $pack_o, ##-- file offset pack template (default='N') ; OVERRIDE:unused
30 ## #pack_l => $pack_l, ##-- string-length pack template (default='n'); OVERRIDE:unused
31 ## pack_s => $pack_s, ##-- string pack template for text i/o; OVERRIDE:REQUIRED (default='Nn')
32 ## size => $size, ##-- number of mapped symbols, like scalar(@i2s)
33 ## utf8 => $bool, ##-- true iff strings are stored as utf8 (used by re2i()) OVERRIDE: unused
34 ## ##
35 ## ##-- EnumFile: in-memory construction
36 ## s2i => \%s2i, ##-- maps symbols to integers
37 ## i2s => \@i2s, ##-- maps integers to symbols
38 ## dirty => $bool, ##-- true if in-memory structures are not in-sync with file data
39 ## loaded => $bool, ##-- true if file data has been loaded to memory
40 ## shared => $bool, ##-- true to avoid closing filehandles on close() or DESTROY() (default=false)
41 ## ##
42 ## ##-- EnumFile: pack lengths (after open())
43 ## len_i => $len_i, ##-- packsize($pack_i)
44 ## #len_o => $len_o, ##-- packsize($pack_o) ; OVERRIDE: unused
45 ## #len_l => $len_l, ##-- packsize($pack_l) ; OVERRIDE: unused
46 ## len_s => $len_s, ##-- packsize($pack_s); OVERRIDE: new
47 ## len_sx => $len_sx, ##-- $len_s + $len_i ; OVERRIDE: new value
48 ## ##
49 ## ##-- EnumFile: filehandles (after open())
50 ## #sfh => $sfh, ##-- $base.es : OVERRIDE: unused
51 ## ixfh => $ixfh, ##-- $base.fix : [$i] => pack("${pack_s}", $s_with_id_i) : OVERRIDE: new extension, new format
52 ## sxfh => $sxfh, ##-- $base.fsx : [$j] => pack("${pack_s}${pack_i}", $s_with_sortorder_j_and_id_i, $i) : OVERRIDE: new extension, new format
53 ## )
54 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
55 0 my $that = shift;
56 0 return $that->SUPER::new(
57 utf8 => 0,
58 pack_s => 'Nn',
59 @_, ##-- user arguments
60 );
61 }
62  
63 ##==============================================================================
64 ## I/O
65  
66 ##--------------------------------------------------------------
67 ## I/O: open/close (file)
68  
69 ## $enum_or_undef = $enum->open($base,$flags)
70 ## $enum_or_undef = $enum->open($base)
71 ## $enum_or_undef = $enum->open()
72 ## + opens file(s), clears {loaded} flag
73 0 ▶sub open {
Subroutine open: not called CC=11 SCAR=48.8
Pod documented
74 0 my ($enum,$base,$flags) = @_;
75 0 ▶ $base //= $enum->{base};
Condition0%0/3
$base //= $enum->{"base"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$base //= $enum->{"base"}
$base $enum->{"base"}
Truth table0%0/3
A //= B
A= $baseB= $enum->{"base"}
ABresult
1X1
011
000
76 0 ▶ $flags //= $enum->{flags};
Condition0%0/3
$flags //= $enum->{"flags"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags //= $enum->{"flags"}
$flags $enum->{"flags"}
Truth table0%0/3
A //= B
A= $flagsB= $enum->{"flags"}
ABresult
1X1
011
000
77 0 ▶ $enum->close() if ($enum->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $enum->opened 0 0 0
78 0 $enum->{base} = $base;
79 0 $enum->{flags} = $flags = fcflags($flags);
80 0 ▶ if (fcread($flags) && !fctrunc($flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcread($flags) and not fctrunc($flags)) 0 0 0
Condition0%0/3
fcread($flags) and not fctrunc($flags)
!ll&&!rl&&r
000
MC/DC0%0/2
fcread($flags) and not fctrunc($flags)
fcread($flags) not fctrunc($flags)
Truth table0%0/3
A and not(B)
A= fcread($flags)B= not fctrunc($flags)
ABresult
0X0
100
111
81 0 ▶ $enum->loadHeader()
Branch: 1 branch
BranchTrueFalseTotal
$enum->loadHeader or $enum->logconess(qq[failed to load header from '$enum->{"base"}.hdr': $!]) 0 0 0
82 or $enum->logconess("failed to load header from '$enum->{base}.hdr': $!");
83 return $enum->promote($enum->{hclass})->open($base,$flags)
84 0 ▶ if ($enum->{hclass} && !$enum->isa($enum->{hclass})); ##-- auto-promote based on header data
Branch: 1 branch
BranchTrueFalseTotal
if $enum->{"hclass"} and not $enum->isa($enum->{"hclass"}) 0 0 0
Condition0%0/3
$enum->{"hclass"} and not $enum->isa($enum->{"hclass"})
!ll&&!rl&&r
000
MC/DC0%0/2
$enum->{"hclass"} and not $enum->isa($enum->{"hclass"})
$enum->{"hclass"} not $enum->isa($enum->{"hclass"})
Truth table0%0/3
A and not(B)
A= $enum->{"hclass"}B= not $enum->isa($enum->{"hclass"})
ABresult
0X0
100
111
85 }
86  
87 $enum->{sxfh} = fcopen("$base.fsx", $flags, $enum->{perms})
88 0 ▶ or $enum->logconfess("open failed for $base.fsx: $!");
Branch: 1 branch
BranchTrueFalseTotal
$enum->{"sxfh"} = fcopen("$base.fsx", $flags, $enum->{"perms"}) or $enum->logconfess("open failed for $base.fsx: $!") 0 0 0
89 $enum->{ixfh} = fcopen("$base.fix", $flags, $enum->{perms})
90 0 ▶ or $enum->logconfess("open failed for $base.fix: $!");
Branch: 1 branch
BranchTrueFalseTotal
$enum->{"ixfh"} = fcopen("$base.fix", $flags, $enum->{"perms"}) or $enum->logconfess("open failed for $base.fix: $!") 0 0 0
91  
92 ##-- pack lengths
93 0 $enum->{len_i} = packsize($enum->{pack_i});
94 0 $enum->{len_s} = packsize($enum->{pack_s});
95 0 $enum->{len_sx} = $enum->{len_s} + $enum->{len_i};
96  
97 ##-- flags
98 0 $enum->{loaded} = 0;
99  
100 ##-- cleanup
101 0 delete(@$enum{qw(pack_o len_o pack_l len_l sfh)});
102  
103 0 return $enum;
104 }
105  
106 ## $enum_or_undef = $enum->close()
107 ## + INHERITED
108  
109 ## $bool = $enum->opened()
110 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod documented
111 0 my $enum = shift;
112 return
113 (
114 #defined($enum->{base}) &&
115 #defined($enum->{sfh}) &&
116 defined($enum->{ixfh})
117 && defined($enum->{sxfh})
118 0 ▶ );
Condition0%0/3
defined $enum->{'ixfh'} && defined $enum->{'sxfh'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $enum->{'ixfh'} && defined $enum->{'sxfh'}
defined $enum->{'ixfh'} defined $enum->{'sxfh'}
Truth table0%0/3
A && B
A= defined $enum->{'ixfh'}B= defined $enum->{'sxfh'}
ABresult
0X0
100
111
119 }
120  
121 ## $bool = $enum->reopen()
122 ## + re-opens datafiles
123 0 ▶sub reopen {
Subroutine reopen: not called CC=4 SCAR=30.0
Pod undocumented
124 0 my $enum = shift;
125 0 ▶ my $base = $enum->{base} || "$enum";
Condition0%0/3
$enum->{'base'} || "$enum"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enum->{'base'} || "$enum"
$enum->{'base'} "$enum"
Truth table0%0/3
A || B
A= $enum->{'base'}B= "$enum"
ABresult
1X1
011
000
126 return (
127 $enum->opened
128 #&& fh_reopen($enum->{sfh}, "$base.fs")
129 && fh_reopen($enum->{ixfh}, "$base.fix")
130 0 ▶ && fh_reopen($enum->{sxfh}, "$base.fsx")
Condition0%0/6
$enum->opened && fh_reopen($enum->{'ixfh'}, "$base.fix") && fh_reopen($enum->{'sxfh'}, "$base.fsx")
!ll&&!rl&&r
000
$enum->opened and fh_reopen($enum->{"ixfh"}, "$base.fix")
!ll&&!rl&&r
000
MC/DC0%0/3
$enum->opened && fh_reopen($enum->{'ixfh'}, "$base.fix") && fh_reopen($enum->{'sxfh'}, "$base.fsx")
$enum->opened fh_reopen($enum->{"ixfh"}, "$base.fix") fh_reopen($enum->{'sxfh'}, "$base.fsx")
Truth table0%0/4
A and B && C
A= $enum->openedB= fh_reopen($enum->{"ixfh"}, "$base.fix")C= fh_reopen($enum->{'sxfh'}, "$base.fsx")
ABCresult
0XX0
10X0
1100
1111
131 );
132 }
133  
134 ## $bool = $enum->dirty()
135 ## + returns true iff some in-memory structures haven't been flushed to disk
136 ## + INHERITED
137  
138 ## $bool = $enum->loaded()
139 ## + returns true iff in-memory structures have been populated from disk
140 ## + INHERITED
141  
142 ## $bool = $enum->flush()
143 ## $bool = $enum->flush($force)
144 ## + flush in-memory structures to disk
145 ## + no-op unless $force or $enum->dirty() is true
146 ## + clobbers any old disk-file contents with in-memory maps
147 ## + enum must be opened in write-mode
148 ## + invalidates any old references to {s2i}, {i2s} (but doesn't empty them if you need to keep a reference)
149 ## + clears {dirty} flag
150 0 ▶sub flush {
Subroutine flush: not called CC=13 SCAR=52.0
Pod documented
151 0 my ($enum,$force) = @_;
152 0 ▶ return undef if (!$enum->opened || !fcwrite($enum->{flags}));
Branch: 1 branch
BranchTrueFalseTotal
unless $enum->opened and fcwrite($enum->{"flags"}) 0 0 0
Condition0%0/3
$enum->opened and fcwrite($enum->{"flags"})
!ll&&!rl&&r
000
MC/DC0%0/2
$enum->opened and fcwrite($enum->{"flags"})
$enum->opened fcwrite($enum->{"flags"})
Truth table0%0/3
A and B
A= $enum->openedB= fcwrite($enum->{"flags"})
ABresult
0X0
100
111
153 0 ▶ return $enum if (!$force && !$enum->dirty);
Branch: 1 branch
BranchTrueFalseTotal
unless $force or $enum->dirty 0 0 0
Condition0%0/3
$force or $enum->dirty
l!l&&r!l&&!r
000
MC/DC0%0/2
$force or $enum->dirty
$force $enum->dirty
Truth table0%0/3
A or B
A= $forceB= $enum->dirty
ABresult
1X1
011
000
154  
155 ##-- save header
156 0 ▶ $enum->saveHeader()
Branch: 1 branch
BranchTrueFalseTotal
$enum->saveHeader or $enum->logconfess(qq[flush(): failed to store header $enum->{"base"}.hdr: $!]) 0 0 0
157 or $enum->logconfess("flush(): failed to store header $enum->{base}.hdr: $!");
158  
159 0 my ($ixfh,$sxfh) = @$enum{qw(ixfh sxfh)};
160 0 $ixfh->seek(0,SEEK_SET);
161 0 $sxfh->seek(0,SEEK_SET);
162  
163 ##-- dump $base.fix
164 0 my $i2s = $enum->{i2s};
165 0 my ($len_s,$pack_i,$pack_s) = @$enum{qw(len_s pack_i pack_s)};
166 0 my $i = 0;
167 0 my $null = "\0" x $len_s;
168 0 foreach (@$i2s) {
169 0 ▶ $_ //= $null;
Condition0%0/3
$_ //= $null
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ //= $null
$_ $null
Truth table0%0/3
A //= B
A= $_B= $null
ABresult
1X1
011
000
170 0 ▶ $ixfh->print($_)
Branch: 1 branch
BranchTrueFalseTotal
$ixfh->print($_) or $enum->logconfess(qq[flush(): failed to write ix-record for id=$i to $enum->{"base"}.fix]) 0 0 0
171 or $enum->logconfess("flush(): failed to write ix-record for id=$i to $enum->{base}.fix");
172 0 ++$i;
173 }
174 0 CORE::truncate($ixfh, $ixfh->tell());
175  
176 ##-- dump $base.fsx
177 0 foreach $i (sort {$i2s->[$a] cmp $i2s->[$b]} (0..$#$i2s)) {
178 0 ▶ $sxfh->print($i2s->[$i], pack($pack_i, $i))
Branch: 1 branch
BranchTrueFalseTotal
$sxfh->print($i2s->[$i], pack($pack_i, $i)) or $enum->logconfess(qq[flush(): failed to dump sx-record for id $i to $enum->{"base"}.fsx]) 0 0 0
179 or $enum->logconfess("flush(): failed to dump sx-record for id $i to $enum->{base}.fsx");
180 }
181 0 CORE::truncate($sxfh, $sxfh->tell());
182  
183 ##-- clear in-memory structures (but don't clobber existing references; used for xenum by DiaColloDB::create())
184 0 $enum->{i2s} = [];
185 0 $enum->{s2i} = {};
186 0 $enum->{dirty} = 0;
187  
188 0 ▶ $enum->reopen() or return undef if ((caller(1))[3] !~ /::close$/);
Branch: 1 branch
BranchTrueFalseTotal
unless (caller 1)[3] =~ /::close$/ 0 0 0
Condition0%0/5
(caller 1)[3] =~ /::close$/ || ($enum->reopen || (return undef))
l!l&&r!l&&!r
000
$enum->reopen or return undef
l!l
00
MC/DC0%0/2
(caller 1)[3] =~ /::close$/ || ($enum->reopen || (return undef))
(caller 1)[3] =~ /::close$/ $enum->reopen
Truth table0%0/3
A || B
A= (caller 1)[3] =~ /::close$/B= $enum->reopen
ABresult
1X1
011
000
189 0 return $enum;
190 }
191  
192  
193 ##--------------------------------------------------------------
194 ## I/O: memory <-> file
195  
196 ## \@i2s = $enum->toArray()
197 0 ▶sub toArray {
Subroutine toArray: not called CC=6 SCAR=37.4
Pod documented
198 0 my $enum = shift;
199 0 ▶ return $enum->{i2s} if ($enum->loaded || !$enum->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $enum->loaded or not $enum->opened 0 0 0
Condition0%0/3
$enum->loaded or not $enum->opened
l!l&&r!l&&!r
000
MC/DC0%0/2
$enum->loaded or not $enum->opened
$enum->loaded not $enum->opened
Truth table0%0/3
A or not(B)
A= $enum->loadedB= not $enum->opened
ABresult
1X1
011
000
200  
201 #use bytes; ##-- deprecated in perl v5.18.2
202 0 my $ixfh = $enum->{ixfh};
203 0 my $ixlen = (-s $ixfh);
204 0 my ($ixbuf,@i2s);
205 0 ▶ CORE::seek($ixfh,0,SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $ixfh, 0, 0 or $enum->logconfess(qq[toArray(): seek(0) failed on $enum->{"base"}.fix: $!]) 0 0 0
206 or $enum->logconfess("toArray(): seek(0) failed on $enum->{base}.fix: $!");
207 0 ▶ CORE::read($ixfh, $ixbuf, $ixlen)==$ixlen
Branch: 1 branch
BranchTrueFalseTotal
read($ixfh, $ixbuf, $ixlen) == $ixlen or $enum->logconfess(qq[toArray(): read() failed for $ixlen bytes from $enum->{"base"}.fix: $!]) 0 0 0
208 or $enum->logconfess("toArray(): read() failed for $ixlen bytes from $enum->{base}.fix: $!");
209 0 @i2s = unpack("(A[$enum->{len_s}])*", $ixbuf);
210 0 undef $ixbuf;
211 0 ▶ push(@i2s, @{$enum->{i2s}}[scalar(@i2s)..$#{$enum->{i2s}}]) if ($enum->dirty);
Branch: 1 branch
BranchTrueFalseTotal
if $enum->dirty 0 0 0
212 0 return \@i2s;
213 }
214  
215 ## $enum = $enum->fromArray(\@i2s)
216 ## + clobbers $enum contents, steals \@i2s
217 ## + INHERITED
218  
219 ## $enum = $enum->fromHash(\%s2i)
220 ## + clobbers $enum contents, steals \%s2i
221 ## + INERHITED
222  
223 ## $enum = $enum->fromEnum($enum2)
224 ## + clobbers $enum contents, does NOT steal $enum2->{i2s}
225 ## + INHERITED
226  
227 ## $bool = $enum->load()
228 ## + loads files to memory; must be opened
229 ## + INHERITED
230  
231 ## $enum = $enum->save()
232 ## $enum = $enum->save($base)
233 ## + saves enum to $base; really just a wrapper for open() and flush()
234 ## + INHERITED
235  
236 ##--------------------------------------------------------------
237 ## I/O: header
238  
239 ## @keys = $coldb->headerKeys()
240 ## + keys to save as header
241 0 ▶sub headerKeys {
Subroutine headerKeys: not called CC=1 SCAR=6.9
Pod documented
242 0 my $enum = shift;
243 0 return grep {!m{(?:(?:pack|len)_[lo])$}} $enum->SUPER::headerKeys();
244 }
245  
246 ## $bool = $enum->loadHeader()
247 ## + INHERITED
248  
249 ## $bool = $enum->saveHeader()
250 ## + INHERITED
251  
252 ##--------------------------------------------------------------
253 ## I/O: text
254 ## + INHERITED
255  
256 ##==============================================================================
257 ## Methods: population (in-memory only)
258 ## + INHERITED
259  
260 ##==============================================================================
261 ## Methods: lookup
262  
263 ## $s_or_undef = $enum->i2s($i)
264 ## + in-memory cache overrides file contents
265 0 ▶sub i2s {
Subroutine i2s: not called CC=5 SCAR=34.0
Pod documented
266 0 my ($enum,$i) = @_;
267 0 ▶ return undef if ($i >= $enum->{size});
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $enum->{"size"} 0 0 0
268  
269 0 my ($buf,$soff,$slen);
270 0 ▶ return $buf if (defined($buf=$enum->{i2s}[$i]));
Branch: 1 branch
BranchTrueFalseTotal
if defined($buf = $enum->{"i2s"}[$i]) 0 0 0
271  
272 0 ▶ CORE::seek($enum->{ixfh}, $i*$enum->{len_s}, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $enum->{"ixfh"}, $i * $enum->{"len_s"}, 0 or $enum->logconfess(qq[i2s(): seek() failed on $enum->{"base"}.fix for i=$i]) 0 0 0
273 or $enum->logconfess("i2s(): seek() failed on $enum->{base}.fix for i=$i");
274 CORE::read($enum->{ixfh},$buf,$enum->{len_s})==$enum->{len_s}
275 0 ▶ or $enum->logconfess("i2s(): read() failed on $enum->{base}.fix for i=$i");
Branch: 1 branch
BranchTrueFalseTotal
read($enum->{"ixfh"}, $buf, $enum->{"len_s"}) == $enum->{"len_s"} or $enum->logconfess(qq[i2s(): read() failed on $enum->{"base"}.fix for i=$i]) 0 0 0
276  
277 0 return $buf;
278 }
279  
280 ## $i_or_undef = $enum->s2i($s)
281 ## $i_or_undef = $enum->s2i($s, $ilo,$ihi)
282 ## + binary search; in-memory cache overrides file contents
283 0 ▶sub s2i {
Subroutine s2i: not called CC=13 SCAR=52.0
Pod documented
284 0 my ($enum,$key,$ilo,$ihi) = @_;
285  
286 0 my ($sxfh,$len_s,$len_sx) = @$enum{qw(sxfh len_s len_sx)};
287 0 ▶ $ilo //= 0;
Condition0%0/2
$ilo //= 0
l!l
00
MC/DC0%0/1
$ilo //= 0
$ilo
Truth table0%0/2
A
A= $ilo
Aresult
11
00
288 0 ▶ $ihi //= $enum->{dirty} ? ((-s $sxfh)/$len_sx) : $enum->{size};
Branch: 1 branch
BranchTrueFalseTotal
$enum->{'dirty'} ? : 0 0 0
Condition0%0/3
$ihi //= $enum->{"dirty"} ? (-s $sxfh) / $len_sx : $enum->{"size"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ihi //= $enum->{"dirty"} ? (-s $sxfh) / $len_sx : $enum->{"size"}
$ihi $enum->{"dirty"} ? (-s $sxfh) / $len_sx : $enum->{"size"}
Truth table0%0/3
A //= B
A= $ihiB= $enum->{"dirty"} ? (-s $sxfh) / $len_sx : $enum->{"size"}
ABresult
1X1
011
000
289  
290 0 my ($imid,$buf,$s,$si);
291 0 ▶ return $buf if (defined($buf=$enum->{s2i}{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if defined($buf = $enum->{"s2i"}{$key}) 0 0 0
292  
293 0 ▶ while ($ilo < $ihi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ilo < $ihi) 0 0 0
294 0 $imid = ($ihi+$ilo) >> 1;
295  
296 ##-- get sx-record @ $imid
297 0 ▶ CORE::seek($sxfh, $imid*$len_sx, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $sxfh, $imid * $len_sx, 0 or $enum->logconfess(qq[s2i(): seek() failed on $enum->{"base"}.fsx for item $imid]) 0 0 0
298 or $enum->logconfess("s2i(): seek() failed on $enum->{base}.fsx for item $imid");
299 0 ▶ CORE::read($sxfh, $buf, $len_s)==$len_s
Branch: 1 branch
BranchTrueFalseTotal
read($sxfh, $buf, $len_s) == $len_s or $enum->logconfess(qq[s2i(): read() failed on $enum->{"base"}.fsx for item $imid]) 0 0 0
300 or $enum->logconfess("s2i(): read() failed on $enum->{base}.fsx for item $imid");
301  
302 0 ▶ if ($buf lt $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf lt $key) { } 0 0 0
303 0 $ilo = $imid + 1;
304 } else {
305 0 $ihi = $imid;
306 }
307 }
308  
309 ##-- output
310 0 ▶ if ($ilo==$ihi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ilo == $ihi) 0 0 0
311 ##-- get sx-record @ $ilo
312 0 ▶ CORE::seek($sxfh, $ilo*$len_sx, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $sxfh, $ilo * $len_sx, 0 or $enum->logconfess(qq[s2i(): seek() failed on $enum->{"base"}.fsx for item $imid]) 0 0 0
313 or $enum->logconfess("s2i(): seek() failed on $enum->{base}.fsx for item $imid");
314 0 ▶ CORE::read($sxfh, $buf, $len_sx)==$len_sx
Branch: 1 branch
BranchTrueFalseTotal
read($sxfh, $buf, $len_sx) == $len_sx or $enum->logconfess(qq[s2i(): read() failed on $enum->{"base"}.fsx for item $imid]) 0 0 0
315 or $enum->logconfess("s2i(): read() failed on $enum->{base}.fsx for item $imid");
316 0 ($s,$si) = unpack($enum->{pack_s}.$enum->{pack_i}, $buf);
317  
318 0 ▶ return $si if ($buf eq $key);
Branch: 1 branch
BranchTrueFalseTotal
if $buf eq $key 0 0 0
319 }
320  
321 0 return undef;
322 }
323  
324  
325 ## \@is = $enum->re2i($regex, $pack_s)
326 ## + gets indices for all (packed) strings matching $regex
327 ## + if $pack_s is specified, is will be used to unpack strings (default=$enum->{pack_s}), only the first unpacked element will be tested
328 0 ▶sub re2i {
Subroutine re2i: not called CC=12 SCAR=50.5
Pod documented
329 #use bytes; ##-- deprecated in perl v5.18.2
330 0 my ($enum,$re,$pack_s) = @_;
331 0 ▶ $re = regex($re) if (!ref($re));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $re 0 0 0
332  
333 0 ▶ $pack_s //= $enum->{pack_s};
Condition0%0/3
$pack_s //= $enum->{"pack_s"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$pack_s //= $enum->{"pack_s"}
$pack_s $enum->{"pack_s"}
Truth table0%0/3
A //= B
A= $pack_sB= $enum->{"pack_s"}
ABresult
1X1
011
000
334 0 my $i2s = $enum->{i2s};
335  
336 0 ▶ if ($enum->loaded || !$enum->opened) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enum->loaded or not $enum->opened) 0 0 0
Condition0%0/3
$enum->loaded or not $enum->opened
l!l&&r!l&&!r
000
MC/DC0%0/2
$enum->loaded or not $enum->opened
$enum->loaded not $enum->opened
Truth table0%0/3
A or not(B)
A= $enum->loadedB= not $enum->opened
ABresult
1X1
011
000
337 ##-- easy answer: loaded
338 0 ▶ if ($pack_s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack_s) { } 0 0 0
339 0 my ($s);
340 0 return [grep {($s)=unpack($pack_s,$i2s->[$_]); $s =~ $re} [0..$#$i2s]];
341 } else {
342 0 return [grep {$i2s->[$_] =~ $re} [0..$#$i2s]];
343 }
344 }
345  
346 ##-- iteration a la toArray
347 0 my $ixfh = $enum->{ixfh};
348 0 my $len_s = $enum->{len_s};
349 0 my @is = qw();
350 0 my ($i,$buf);
351 0 ▶ for ($i=0,CORE::seek($ixfh,0,SEEK_SET); !eof($ixfh); ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $ixfh) 0 0 0
352 0 ▶ CORE::read($ixfh, $buf, $len_s)==$len_s
Branch: 1 branch
BranchTrueFalseTotal
read($ixfh, $buf, $len_s) == $len_s or $enum->logconfess(qq[re2i(): read() failed for $len_s bytes from $enum->{"base"}.fix: $!]) 0 0 0
353 or $enum->logconfess("re2i(): read() failed for $len_s bytes from $enum->{base}.fix: $!");
354 0 ▶ ($buf) = unpack($pack_s, $buf) if ($pack_s);
Branch: 1 branch
BranchTrueFalseTotal
if $pack_s 0 0 0
355 0 ▶ push(@is, $i) if ($buf =~ $re);
Branch: 1 branch
BranchTrueFalseTotal
if $buf =~ /$re/ 0 0 0
356 }
357  
358 ##-- append expansions from in-memory cache
359 0 ▶ if ($enum->dirty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enum->dirty) 0 0 0
360 0 ▶ if ($pack_s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pack_s) { } 0 0 0
361 0 my ($s);
362 0 push(@is, grep {($s)=unpack($pack_s,$i2s->[$_]); $s =~ $re} (((-s $ixfh)/$len_s)..$#$i2s));
363 } else {
364 0 push(@is, grep {$i2s->[$_] =~ $re} (((-s $ixfh)/$len_s)..$#$i2s));
365 }
366 }
367  
368 0 return \@is;
369 }
370  
371  
372  
373 ##==============================================================================
374 ## Footer
375 1 1;
376  
377 __END__
« DiaColloDB/Relation/Unigrams.pm ↑ Summary DiaColloDB/Upgrade/Base.pm »