DiaColloDB/EnumFile/MMap.pm

filter: Statementstmt 15.4% 17 / 110 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 33.3% 5 / 15 Podpod 80.0% 8 / 10 totaltotal 12.2% 30 / 245 CC 40 SCAR 43.8
CC
40
Cov
9%
s2i
156.0
re2i
156.0
toArray
42.0
CRAP 80.0
« DiaColloDB/Corpus.pm ↑ Summary DiaColloDB/Compat/v0_08/MultiMapFile.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::EnumFile::MMap.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, symbol<->integer enum, using mmap
5  
6 package DiaColloDB::EnumFile::MMap;
7 1 ▶use DiaColloDB::EnumFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DiaColloDB::Utils qw(:fcntl :file :json :regex);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Map qw(map_handle);
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 ## $cldb = CLASS_OR_OBJECT->new(%args)
22 ## + %args, object structure:
23 ## (
24 ## ##-- EnumFile: basic options
25 ## base => $base, ##-- database basename; use files "${base}.es", "${base}.esx", "${base}.eix", "${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')
30 ## pack_l => $pack_l, ##-- string-length pack template (default='n')
31 ## pack_s => $pack_s, ##-- string pack template (default=undef) for text i/o
32 ## size => $size, ##-- number of mapped symbols, like scalar(@i2s)
33 ## utf8 => $bool, ##-- true iff strings are stored as utf8 (default, used by re2i())
34 ## ##
35 ## ##-- EnumFile: in-memory construction and caching
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, ##-- bytes::length(pack($pack_i,0))
44 ## len_o => $len_o, ##-- bytes::length(pack($pack_o,0))
45 ## len_l => $len_l, ##-- bytes::length(pack($pack_l,0))
46 ## len_sx => $len_sx, ##-- $len_o + $len_i
47 ## ##
48 ## ##-- EnumFile: filehandles (after open())
49 ## sfh => $sfh, ##-- $base.es : pack("(${pack_l}/A)*", @$i2s)
50 ## ixfh => $ixfh, ##-- $base.eix : [$i] => pack("${pack_o}", $offset_in_sfh_of_string_with_id_i)
51 ## sxfh => $sxfh, ##-- $base.esx : [$j] => pack("${pack_o}${pack_i}", $offset_in_sfh_of_string_with_sortindex_j_and_id_i, $i)
52 ## ##
53 ## ##-- EnumFile::MMap: buffers
54 ## sbufr => \$sbuf, ##-- mmap $base.es
55 ## ixbufr => \$ixbuf, ##-- mmap $base.eix
56 ## sxbufr => \$sxbuf, ##-- mmap $base.esx
57 ## )
58 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
59 0 my $that = shift;
60 0 return $that->SUPER::new(
61 #sbufr=>undef,
62 #ixbufr=>undef,
63 #sxbufr=>undef,
64 @_, ##-- user arguments
65 );
66 }
67  
68 ##==============================================================================
69 ## I/O
70  
71 ##--------------------------------------------------------------
72 ## I/O: open/close (file)
73  
74 ## $enum_or_undef = $enum->open($base,$flags)
75 ## $enum_or_undef = $enum->open($base)
76 ## $enum_or_undef = $enum->open()
77 ## + opens file(s), clears {loaded} flag
78 0 ▶sub open {
Subroutine open: not called CC=3 SCAR=24.8
Pod documented
79 0 my ($enum,$base,$flags) = @_;
80 0 ▶ $enum->SUPER::open($base,$flags) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$enum->SUPER::open($base, $flags) or return undef 0 0 0
81 0 ▶ return $enum if (!$enum->isa(__PACKAGE__)); ##-- superclass open() promoted us to another class
Branch: 1 branch
BranchTrueFalseTotal
unless $enum->isa(__PACKAGE__) 0 0 0
82 0 return $enum->remap();
83 }
84  
85 ## $enum_or_undef = $enum->remap()
86 ## + re-maps mmap buffers from enum handles
87 0 ▶sub remap {
Subroutine remap: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $enum = shift;
89  
90 ##-- mmap handles
91 0 my $mapmode = fcperl($enum->{flags});
92 0 map_handle(my $sbuf, $enum->{sfh}, $mapmode);
93 0 map_handle(my $ixbuf, $enum->{ixfh}, $mapmode);
94 0 map_handle(my $sxbuf, $enum->{sxfh}, $mapmode);
95  
96 ##-- buffers
97 0 $enum->{sbufr} = \$sbuf;
98 0 $enum->{ixbufr} = \$ixbuf;
99 0 $enum->{sxbufr} = \$sxbuf;
100  
101 ##-- flags
102 0 $enum->{loaded} = 0;
103  
104 0 return $enum;
105 }
106  
107 ## $enum_or_undef = $enum->close()
108 0 ▶sub close {
Subroutine close: not called CC=4 SCAR=30.0
Pod documented
109 0 my $enum = shift;
110 0 ▶ if ($enum->opened && fcwrite($enum->{flags})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($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
111 0 ▶ $enum->flush() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$enum->flush or return undef 0 0 0
112 }
113 0 delete @$enum{qw(sbufr ixbufr sxbufr)};
114 0 return $enum->SUPER::close();
115 }
116  
117 ## $bool = $enum->opened()
118 0 ▶sub opened {
Subroutine opened: not called CC=3 SCAR=24.8
Pod documented
119 0 my $enum = shift;
120 return
121 (
122 #defined($enum->{base}) &&
123 defined($enum->{sbufr})
124 && defined($enum->{ixbufr})
125 && defined($enum->{sxbufr})
126 0 ▶ );
Condition0%0/6
defined $enum->{'sbufr'} && defined $enum->{'ixbufr'} && defined $enum->{'sxbufr'}
!ll&&!rl&&r
000
defined $enum->{"sbufr"} and defined $enum->{"ixbufr"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $enum->{'sbufr'} && defined $enum->{'ixbufr'} && defined $enum->{'sxbufr'}
defined $enum->{"sbufr"} defined $enum->{"ixbufr"} defined $enum->{'sxbufr'}
Truth table0%0/4
A and B && C
A= defined $enum->{"sbufr"}B= defined $enum->{"ixbufr"}C= defined $enum->{'sxbufr'}
ABCresult
0XX0
10X0
1100
1111
127 }
128  
129 ## $bool = $enum->reopen()
130 ## + re-opens datafiles
131 ## + override also remaps buffers
132 0 ▶sub reopen {
Subroutine reopen: not called CC=2 SCAR=17.9
Pod undocumented
133 0 my $enum = shift;
134 0 ▶ return $enum->SUPER::reopen() && $enum->remap();
Condition0%0/3
$enum->SUPER::reopen && $enum->remap
!ll&&!rl&&r
000
MC/DC0%0/2
$enum->SUPER::reopen && $enum->remap
$enum->SUPER::reopen $enum->remap
Truth table0%0/3
A && B
A= $enum->SUPER::reopenB= $enum->remap
ABresult
0X0
100
111
135 }
136  
137  
138 ## $bool = $enum->dirty()
139 ## + returns true iff some in-memory structures haven't been flushed to disk
140 ## + INHERITED
141  
142 ## $bool = $enum->loaded()
143 ## + returns true iff in-memory structures have been populated from disk
144 ## + INHERITED
145  
146 ## $bool = $enum->flush()
147 ## $bool = $enum->flush($force)
148 ## + flush in-memory structures to disk
149 ## + no-op unless $force or $enum->dirty() is true
150 ## + clobbers any old disk-file contents with in-memory maps
151 ## + enum must be opened in write-mode
152 ## + INHERITED
153  
154  
155 ##--------------------------------------------------------------
156 ## I/O: memory <-> file
157  
158 ## \@i2s = $enum->toArray()
159 0 ▶sub toArray {
Subroutine toArray: not called CC=6 SCAR=37.4
Pod documented
160 0 my $enum = shift;
161 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/5
$enum->loaded or not $enum->opened
l!l&&r!l&&!r
000
$enum->{"i2s"} // []
l!l
00
MC/DC0%0/2
$enum->loaded or not $enum->opened
$enum->loaded not $enum->opened
MC/DC0%0/1
$enum->{"i2s"} // []
$enum->{"i2s"}
Truth table0%0/3
A or not(B)
A= $enum->loadedB= not $enum->opened
ABresult
1X1
011
000
Truth table0%0/2
A
A= $enum->{"i2s"}
Aresult
11
00
162 0 my @i2s = unpack("($enum->{pack_l}/A)*", ${$enum->{sbufr}});
163 0 ▶ push(@i2s, @{$enum->{i2s}}[scalar(@i2s)..$#{$enum->{i2s}}]) if ($enum->dirty && $enum->{i2s});
Branch: 1 branch
BranchTrueFalseTotal
if $enum->dirty and $enum->{"i2s"} 0 0 0
Condition0%0/3
$enum->dirty and $enum->{"i2s"}
!ll&&!rl&&r
000
MC/DC0%0/2
$enum->dirty and $enum->{"i2s"}
$enum->dirty $enum->{"i2s"}
Truth table0%0/3
A and B
A= $enum->dirtyB= $enum->{"i2s"}
ABresult
0X0
100
111
164 0 return \@i2s;
165 }
166  
167 ## $enum = $enum->fromArray(\@i2s)
168 ## + clobbers $enum contents, steals \@i2s
169 ## + INHERITED
170  
171 ## $enum = $enum->fromEnum($enum2)
172 ## + clobbers $enum contents, does NOT steal $enum2->{i2s}
173 ## + INHERITED
174  
175 ## $bool = $enum->load()
176 ## + loads files to memory; must be opened
177 ## + INHERITED
178  
179 ##--------------------------------------------------------------
180 ## I/O: header
181 ## + INHERITED
182  
183 ##--------------------------------------------------------------
184 ## I/O: text
185 ## + INHERITED
186  
187 ##==============================================================================
188 ## Methods: population (in-memory only)
189 ## + INHERITED
190  
191 ##==============================================================================
192 ## Methods: lookup
193  
194 ## $s_or_undef = $enum->i2s($i)
195 ## + in-memory cache overrides file contents
196 0 ▶sub i2s {
Subroutine i2s: not called CC=5 SCAR=34.0
Pod documented
197 0 my ($enum,$i) = @_;
198 0 ▶ return undef if ($i >= $enum->{size});
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $enum->{"size"} 0 0 0
199  
200 0 my $buf;
201 0 ▶ return $buf if (defined($buf=$enum->{i2s}[$i]));
Branch: 1 branch
BranchTrueFalseTotal
if defined($buf = $enum->{"i2s"}[$i]) 0 0 0
202  
203 0 ▶ my $soff = unpack($enum->{pack_o}, substr(${$enum->{ixbufr}}, $i*$enum->{len_o}, $enum->{len_o})) // return undef;
Condition0%0/2
unpack($enum->{"pack_o"}, substr(${$enum->{"ixbufr"};}, $i * $enum->{"len_o"}, $enum->{"len_o"})) // (return undef)
l!l
00
MC/DC0%0/1
unpack($enum->{"pack_o"}, substr(${$enum->{"ixbufr"};}, $i * $enum->{"len_o"}, $enum->{"len_o"})) // (return undef)
unpack($enum->{"pack_o"}, substr(${$enum->{"ixbufr"};}, $i * $enum->{"len_o"}, $enum->{"len_o"}))
Truth table0%0/2
A
A= unpack($enum->{"pack_o"}, substr(${$enum->{"ixbufr"};}, $i * $enum->{"len_o"}, $enum->{"len_o"}))
Aresult
11
00
204 0 my $slen = unpack($enum->{pack_l}, substr(${$enum->{sbufr}}, $soff, $enum->{len_l}));
205 0 $buf = substr(${$enum->{sbufr}}, $soff+$enum->{len_l}, $slen);
206 0 ▶ utf8::decode($buf) if ($enum->{utf8});
Branch: 1 branch
BranchTrueFalseTotal
if $enum->{"utf8"} 0 0 0
207 0 return $buf;
208 }
209  
210 ## $i_or_undef = $enum->s2i($s)
211 ## $i_or_undef = $enum->s2i($s, $ilo,$ihi)
212 ## + binary search; enum must be opened
213 0 ▶sub s2i {
Subroutine s2i: not called CC=12 SCAR=50.5
Pod documented
214 0 my ($enum,$key,$ilo,$ihi) = @_;
215  
216 0 my ($sxbufr,$sbufr,$len_sx,$pack_o,$len_o,$pack_l,$len_l) = @$enum{qw(sxbufr sbufr len_sx pack_o len_o pack_l len_l)};
217 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
218 0 ▶ $ihi //= $enum->{dirty} ? (length($$sxbufr)/$len_sx) : $enum->{size};
Branch: 1 branch
BranchTrueFalseTotal
$enum->{'dirty'} ? : 0 0 0
Condition0%0/3
$ihi //= $enum->{"dirty"} ? length($$sxbufr) / $len_sx : $enum->{"size"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ihi //= $enum->{"dirty"} ? length($$sxbufr) / $len_sx : $enum->{"size"}
$ihi $enum->{"dirty"} ? length($$sxbufr) / $len_sx : $enum->{"size"}
Truth table0%0/3
A //= B
A= $ihiB= $enum->{"dirty"} ? length($$sxbufr) / $len_sx : $enum->{"size"}
ABresult
1X1
011
000
219  
220 0 my ($imid,$buf,$soff,$slen,$si);
221 0 ▶ return $buf if (defined($buf=$enum->{s2i}{$key}));
Branch: 1 branch
BranchTrueFalseTotal
if defined($buf = $enum->{"s2i"}{$key}) 0 0 0
222  
223 0 ▶ utf8::encode($key) if ($enum->{utf8} && utf8::is_utf8($key));
Branch: 1 branch
BranchTrueFalseTotal
if $enum->{"utf8"} and utf8::is_utf8($key) 0 0 0
Condition0%0/3
$enum->{"utf8"} and utf8::is_utf8($key)
!ll&&!rl&&r
000
MC/DC0%0/2
$enum->{"utf8"} and utf8::is_utf8($key)
$enum->{"utf8"} utf8::is_utf8($key)
Truth table0%0/3
A and B
A= $enum->{"utf8"}B= utf8::is_utf8($key)
ABresult
0X0
100
111
224 0 ▶ while ($ilo < $ihi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ilo < $ihi) 0 0 0
225 0 $imid = ($ihi+$ilo) >> 1;
226  
227 ##-- get sx-record @ $imid
228 0 $soff = unpack($pack_o, substr($$sxbufr, $imid*$len_sx, $len_o));
229  
230 ##-- get string for sx-record
231 0 $slen = unpack($pack_l, substr($$sbufr, $soff, $len_l));
232 0 $buf = substr($$sbufr, $soff+$len_l, $slen);
233  
234 0 ▶ if ($buf lt $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf lt $key) { } 0 0 0
235 0 $ilo = $imid + 1;
236 } else {
237 0 $ihi = $imid;
238 }
239 }
240  
241 ##-- output
242 0 ▶ if ($ilo==$ihi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ilo == $ihi) 0 0 0
243 ##-- get sx-record @ $ilo
244 0 ($soff,$si) = unpack($enum->{pack_o}.$enum->{pack_i}, substr($$sxbufr, $ilo*$len_sx, $len_sx));
245 0 ▶ return undef if (!defined($soff));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $soff 0 0 0
246  
247 ##-- get string for sx-record
248 0 $slen = unpack($pack_l, substr($$sbufr, $soff, $len_l));
249 0 $buf = substr($$sbufr, $soff+$len_l, $slen);
250  
251 0 ▶ return $si if ($buf eq $key);
Branch: 1 branch
BranchTrueFalseTotal
if $buf eq $key 0 0 0
252 }
253  
254 0 return undef;
255 }
256  
257 ## \@is = $enum->re2i($regex)
258 ## + gets indices for all strings matching $regex
259 0 ▶sub re2i {
Subroutine re2i: not called CC=12 SCAR=50.5
Pod documented
260 0 my ($enum,$re) = @_;
261 0 my $utf8 = $enum->{utf8};
262  
263 0 ▶ if (!ref($re)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $re) 0 0 0
264 0 ▶ utf8::decode($re) if ($utf8 && !utf8::is_utf8($re));
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 and not utf8::is_utf8($re) 0 0 0
Condition0%0/3
$utf8 and not utf8::is_utf8($re)
!ll&&!rl&&r
000
MC/DC0%0/2
$utf8 and not utf8::is_utf8($re)
$utf8 not utf8::is_utf8($re)
Truth table0%0/3
A and not(B)
A= $utf8B= not utf8::is_utf8($re)
ABresult
0X0
100
111
265 0 $re = regex($re);
266 }
267  
268 0 my $i2s = $enum->{i2s};
269 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
270 ##-- easy answer: loaded
271 0 ▶ return [grep {utf8::decode($_) if ($utf8); $i2s->[$_] =~ $re} (0..$#$i2s)];
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 0 0 0
272 }
273  
274 ##-- iteration a la toArray()
275 0 my $pack_l = $enum->{pack_l};
276 0 my $len_l = $enum->{len_l};
277 0 my $sbufr = $enum->{sbufr};
278 0 my $offmax = length($$sbufr);
279 0 my @is = qw();
280 0 my ($off,$i,$len_s,$s);
281 0 ▶ for ($i=$off=0; $off < $offmax; ++$i, $off += ($len_l+$len_s)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off < $offmax) 0 0 0
282 0 $len_s = unpack($pack_l, substr($$sbufr, $off, $len_l));
283 0 $s = substr($$sbufr, $off+$len_l, $len_s);
284 0 ▶ utf8::decode($s) if ($utf8);
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 0 0 0
285 0 ▶ push(@is, $i) if ($s =~ $re);
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /$re/ 0 0 0
286 }
287  
288 0 ▶ push(@is, grep {utf8::decode($_) if ($utf8); $i2s->[$_] =~ $re} (((-s $enum->{ixfh})/$enum->{len_o})..$#{$enum->{i2s}})) if ($enum->dirty);
Branch: 2 branches
BranchTrueFalseTotal
if $enum->dirty 0 0 0
if $utf8 0 0 0
289 0 return \@is;
290 }
291  
292 ##==============================================================================
293 ## Footer
294 1 1;
295  
296 __END__
« DiaColloDB/Corpus.pm ↑ Summary DiaColloDB/Compat/v0_08/MultiMapFile.pm »