DiaColloDB/MultiMapFile.pm

filter: Statementstmt 13.6% 23 / 168 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 62 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 26.9% 7 / 26 Podpod 94.4% 17 / 18 totaltotal 11.9% 47 / 393 CC 66 SCAR 42.5
CC
66
Cov
7%
open
210.0
flush
132.0
close
90.0
CRAP 70.2
« DiaColloDB/Compat/v0_08/MultiMapFile.pm ↑ Summary DiaColloDB/Utils.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::MultiMapFile.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, integer->integer* multimap file, e.g. for expansion indices
5  
6 package DiaColloDB::MultiMapFile;
7 1 ▶use DiaColloDB::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use DiaColloDB::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use DiaColloDB::Utils qw(:fcntl :file :json :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 File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ##==============================================================================
16 ## Globals & Constants
17  
18 1 our @ISA = qw(DiaColloDB::Persistent);
19  
20 ##==============================================================================
21 ## Constructors etc.
22  
23 ## $mmf = CLASS_OR_OBJECT->new(%args)
24 ## + %args, object structure:
25 ## (
26 ## ##-- basic options
27 ## base => $base, ##-- database basename; use files "${base}.ma", "${base}.mb", "${base}.hdr"
28 ## perms => $perms, ##-- default: 0666 & ~umask
29 ## flags => $flags, ##-- default: 'r'
30 ## pack_i => $pack_i, ##-- integer pack template (default='N')
31 ## size => $size, ##-- number of mapped , like scalar(@data)
32 ## logCompat => $level, ##-- log-level for compatibility warnings (default='warn')
33 ## ##
34 ## ##-- in-memory construction
35 ## a2b => \@a2b, ##-- maps source integers to (packed) target integer-sets: [$a] => pack("${pack_i}*", @bs)
36 ## ##
37 ## ##-- computed pack templates and lengths (after open())
38 ## pack_a => $pack_a, ##-- "($pack_i)[2]"
39 ## pack_b => $pack_a, ##-- "($pack_i)*"
40 ## len_i => $len_i, ##-- bytes::length(pack($pack_i,0))
41 ## len_a => $len_a, ##-- bytes::length(pack($pack_a,0))
42 ## ##
43 ## ##-- filehandles (after open())
44 ## afh => $afh, ##-- $base.ma : [$a] => pack(${pack_a}, $bidx_a, $blen_a) : $byte_offset_in_bfh = $len_i*$bidx_a
45 ## bfh => $bfh, ##-- $base.mb : [$bidx_a] => pack(${pack_b}, @targets_for_a) : $byte_length_in_bfh = $len_i*$blen_a
46 ## )
47 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
48 0 my $that = shift;
49 0 ▶ my $mmf = 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
50 base => undef,
51 perms => (0666 & ~umask),
52 flags => 'r',
53 size => 0,
54 pack_i => 'N',
55 version => $DiaColloDB::VERSION,
56 logCompat => 'warn',
57  
58 a2b=>[],
59  
60 #len_i => undef,
61  
62 #afh =>undef,
63 #bfh =>undef,
64  
65 @_, ##-- user arguments
66 },
67 ref($that)||$that);
68 0 $mmf->{class} = ref($mmf);
69 0 ▶ $mmf->{a2b} //= [];
Condition0%0/2
$mmf->{"a2b"} //= []
l!l
00
MC/DC0%0/1
$mmf->{"a2b"} //= []
$mmf->{"a2b"}
Truth table0%0/2
A
A= $mmf->{"a2b"}
Aresult
11
00
70 0 ▶ return defined($mmf->{base}) ? $mmf->open($mmf->{base}) : $mmf;
Branch: 1 branch
BranchTrueFalseTotal
defined $mmf->{'base'} ? : 0 0 0
71 }
72  
73 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
74 0 ▶ $_[0]->close() if ($_[0]->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->opened 0 0 0
75 }
76  
77 ##==============================================================================
78 ## I/O
79  
80 ##--------------------------------------------------------------
81 ## I/O: open/close (file)
82  
83 ## $mmf_or_undef = $mmf->open($base,$flags)
84 ## $mmf_or_undef = $mmf->open($base)
85 ## $mmf_or_undef = $mmf->open()
86 0 ▶sub open {
Subroutine open: not called CC=14 SCAR=53.5
Pod documented
87 0 my ($mmf,$base,$flags) = @_;
88 0 ▶ $base //= $mmf->{base};
Condition0%0/3
$base //= $mmf->{"base"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$base //= $mmf->{"base"}
$base $mmf->{"base"}
Truth table0%0/3
A //= B
A= $baseB= $mmf->{"base"}
ABresult
1X1
011
000
89 0 ▶ $flags //= $mmf->{flags};
Condition0%0/3
$flags //= $mmf->{"flags"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags //= $mmf->{"flags"}
$flags $mmf->{"flags"}
Truth table0%0/3
A //= B
A= $flagsB= $mmf->{"flags"}
ABresult
1X1
011
000
90 0 ▶ $mmf->close() if ($mmf->opened);
Branch: 1 branch
BranchTrueFalseTotal
if $mmf->opened 0 0 0
91 0 $mmf->{base} = $base;
92 0 $mmf->{flags} = $flags = fcflags($flags);
93 0 my ($hdr); ##-- save header, for version-checking
94 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
95 0 ▶ $hdr = $mmf->readHeader()
Branch: 1 branch
BranchTrueFalseTotal
$hdr = $mmf->readHeader or $mmf->logconess(qq[failed to read header data from '$mmf->{"base"}.hdr': $!]) 0 0 0
96 or $mmf->logconess("failed to read header data from '$mmf->{base}.hdr': $!");
97 0 ▶ $mmf->loadHeaderData($hdr)
Branch: 1 branch
BranchTrueFalseTotal
$mmf->loadHeaderData($hdr) or $mmf->logconess(qq[failed to instantiate header from '$mmf->{"base"}.hdr': $!]) 0 0 0
98 or $mmf->logconess("failed to instantiate header from '$mmf->{base}.hdr': $!");
99 }
100  
101 ##-- check compatibility
102 0 my $min_version = qv(0.09.000);
103 0 ▶ if ($hdr && (!defined($hdr->{version}) || version->parse($hdr->{version}) < $min_version)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr and !defined($hdr->{"version"}) || "version"->parse($hdr->{"version"}) < $min_version) 0 0 0
Condition0%0/6
$hdr and !defined($hdr->{"version"}) || "version"->parse($hdr->{"version"}) < $min_version
!ll&&!rl&&r
000
not defined $hdr->{"version"} or "version"->parse($hdr->{"version"}) < $min_version
l!l&&r!l&&!r
000
MC/DC0%0/3
$hdr and !defined($hdr->{"version"}) || "version"->parse($hdr->{"version"}) < $min_version
$hdr not defined $hdr->{"version"} "version"->parse($hdr->{"version"}) < $min_version
Truth table0%0/4
A and not(B) or C
A= $hdrB= not defined $hdr->{"version"}C= "version"->parse($hdr->{"version"}) < $min_version
ABCresult
0XX0
1000
11X1
1011
104 0 $mmf->vlog($mmf->{logCompat}, "using compatibility mode for $mmf->{base}.*; consider running \`dcdb-upgrade.perl ", dirname($mmf->{base}), "\'");
105 0 DiaColloDB::Compat->usecompat('v0_08');
106 0 bless($mmf, 'DiaColloDB::Compat::v0_08::MultiMapFile');
107 0 $mmf->{version} = $hdr->{version};
108 0 return $mmf->open($base,$flags);
109 }
110  
111 ##-- open underlying files
112 $mmf->{afh} = fcopen("$base.ma", $flags, $mmf->{perms})
113 0 ▶ or $mmf->logconfess("open failed for $base.ma: $!");
Branch: 1 branch
BranchTrueFalseTotal
$mmf->{"afh"} = fcopen("$base.ma", $flags, $mmf->{"perms"}) or $mmf->logconfess("open failed for $base.ma: $!") 0 0 0
114 $mmf->{bfh} = fcopen("$base.mb", $flags, $mmf->{perms})
115 0 ▶ or $mmf->logconfess("open failed for $base.mb: $!");
Branch: 1 branch
BranchTrueFalseTotal
$mmf->{"bfh"} = fcopen("$base.mb", $flags, $mmf->{"perms"}) or $mmf->logconfess("open failed for $base.mb: $!") 0 0 0
116 0 binmode($_,':raw') foreach (@$mmf{qw(afh bfh)});
117  
118 ##-- computed pack-templates & lengths
119 0 $mmf->{pack_a} = $mmf->{pack_i}."[2]";
120 0 $mmf->{pack_b} = $mmf->{pack_i}."*";
121 0 $mmf->{len_i} = packsize($mmf->{pack_i});
122 0 $mmf->{len_a} = packsize($mmf->{pack_a});
123  
124 0 return $mmf;
125 }
126  
127 ## $mmf_or_undef = $mmf->close()
128 0 ▶sub close {
Subroutine close: not called CC=9 SCAR=45.0
Pod documented
129 0 my $mmf = shift;
130 0 ▶ if ($mmf->opened && fcwrite($mmf->{flags})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mmf->opened and fcwrite($mmf->{"flags"})) 0 0 0
Condition0%0/3
$mmf->opened and fcwrite($mmf->{"flags"})
!ll&&!rl&&r
000
MC/DC0%0/2
$mmf->opened and fcwrite($mmf->{"flags"})
$mmf->opened fcwrite($mmf->{"flags"})
Truth table0%0/3
A and B
A= $mmf->openedB= fcwrite($mmf->{"flags"})
ABresult
0X0
100
111
131 0 ▶ $mmf->flush() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$mmf->flush or return undef 0 0 0
132 }
133 0 ▶ !defined($mmf->{afh}) or $mmf->{afh}->close() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
not defined $mmf->{"afh"} or $mmf->{"afh"}->close or return undef 0 0 0
Condition0%0/3
not defined $mmf->{"afh"} or $mmf->{"afh"}->close
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $mmf->{"afh"} or $mmf->{"afh"}->close
not defined $mmf->{"afh"} $mmf->{"afh"}->close
Truth table0%0/3
not(A) or B
A= not defined $mmf->{"afh"}B= $mmf->{"afh"}->close
ABresult
1X1
011
000
134 0 ▶ !defined($mmf->{bfh}) or $mmf->{bfh}->close() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
not defined $mmf->{"bfh"} or $mmf->{"bfh"}->close or return undef 0 0 0
Condition0%0/3
not defined $mmf->{"bfh"} or $mmf->{"bfh"}->close
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $mmf->{"bfh"} or $mmf->{"bfh"}->close
not defined $mmf->{"bfh"} $mmf->{"bfh"}->close
Truth table0%0/3
not(A) or B
A= not defined $mmf->{"bfh"}B= $mmf->{"bfh"}->close
ABresult
1X1
011
000
135 $mmf->{a2b} //= [],
136 0 ▶ undef $mmf->{base};
Condition0%0/2
$mmf->{"a2b"} //= []
l!l
00
MC/DC0%0/1
$mmf->{"a2b"} //= []
$mmf->{"a2b"}
Truth table0%0/2
A
A= $mmf->{"a2b"}
Aresult
11
00
137 0 return $mmf;
138 }
139  
140 ## $bool = $mmf->opened()
141 0 ▶sub opened {
Subroutine opened: not called CC=2 SCAR=17.9
Pod documented
142 0 my $mmf = shift;
143 return
144 (
145 #defined($mmf->{base}) &&
146 defined($mmf->{afh})
147 && defined($mmf->{bfh})
148 0 ▶ );
Condition0%0/3
defined $mmf->{'afh'} && defined $mmf->{'bfh'}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $mmf->{'afh'} && defined $mmf->{'bfh'}
defined $mmf->{'afh'} defined $mmf->{'bfh'}
Truth table0%0/3
A && B
A= defined $mmf->{'afh'}B= defined $mmf->{'bfh'}
ABresult
0X0
100
111
149 }
150  
151 ## $bool = $enum->reopen()
152 ## + re-opens datafiles
153 0 ▶sub reopen {
Subroutine reopen: not called CC=4 SCAR=30.0
Pod undocumented
154 0 my $mmf = shift;
155 0 ▶ my $base = $mmf->{base} || "$mmf";
Condition0%0/3
$mmf->{'base'} || "$mmf"
l!l&&r!l&&!r
000
MC/DC0%0/2
$mmf->{'base'} || "$mmf"
$mmf->{'base'} "$mmf"
Truth table0%0/3
A || B
A= $mmf->{'base'}B= "$mmf"
ABresult
1X1
011
000
156 return (
157 $mmf->opened
158 && fh_reopen($mmf->{afh}, "$base.ma")
159 0 ▶ && fh_reopen($mmf->{bfh}, "$base.mb")
Condition0%0/6
$mmf->opened && fh_reopen($mmf->{'afh'}, "$base.ma") && fh_reopen($mmf->{'bfh'}, "$base.mb")
!ll&&!rl&&r
000
$mmf->opened and fh_reopen($mmf->{"afh"}, "$base.ma")
!ll&&!rl&&r
000
MC/DC0%0/3
$mmf->opened && fh_reopen($mmf->{'afh'}, "$base.ma") && fh_reopen($mmf->{'bfh'}, "$base.mb")
$mmf->opened fh_reopen($mmf->{"afh"}, "$base.ma") fh_reopen($mmf->{'bfh'}, "$base.mb")
Truth table0%0/4
A and B && C
A= $mmf->openedB= fh_reopen($mmf->{"afh"}, "$base.ma")C= fh_reopen($mmf->{'bfh'}, "$base.mb")
ABCresult
0XX0
10X0
1100
1111
160 );
161 }
162  
163  
164 ## $bool = $mmf->dirty()
165 ## + returns true iff some in-memory structures haven't been flushed to disk
166 0 ▶sub dirty {
Subroutine dirty: not called CC=1 SCAR=6.9
Pod documented
167 0 return @{$_[0]{a2b}};
168 }
169  
170 ## $bool = $mmf->flush()
171 ## + flush in-memory structures to disk
172 ## + clobbers any old disk-file contents with in-memory maps
173 ## + file must be opened in write-mode
174 ## + invalidates any old references to {a2b} (but doesn't empty them if you need to keep a reference)
175 0 ▶sub flush {
Subroutine flush: not called CC=11 SCAR=48.8
Pod documented
176 0 my $mmf = shift;
177 0 ▶ return undef if (!$mmf->opened || !fcwrite($mmf->{flags}));
Branch: 1 branch
BranchTrueFalseTotal
unless $mmf->opened and fcwrite($mmf->{"flags"}) 0 0 0
Condition0%0/3
$mmf->opened and fcwrite($mmf->{"flags"})
!ll&&!rl&&r
000
MC/DC0%0/2
$mmf->opened and fcwrite($mmf->{"flags"})
$mmf->opened fcwrite($mmf->{"flags"})
Truth table0%0/3
A and B
A= $mmf->openedB= fcwrite($mmf->{"flags"})
ABresult
0X0
100
111
178 0 ▶ return $mmf if (!$mmf->dirty);
Branch: 1 branch
BranchTrueFalseTotal
unless $mmf->dirty 0 0 0
179  
180 ##-- save header
181 0 ▶ $mmf->saveHeader()
Branch: 1 branch
BranchTrueFalseTotal
$mmf->saveHeader or $mmf->logconfess(qq[flush(): failed to store header $mmf->{"base"}.hdr: $!]) 0 0 0
182 or $mmf->logconfess("flush(): failed to store header $mmf->{base}.hdr: $!");
183  
184 #use bytes; ##-- deprecated in perl v5.18.2
185 0 my ($afh,$bfh) = @$mmf{qw(afh bfh)};
186 0 $afh->seek(0,SEEK_SET);
187 0 $bfh->seek(0,SEEK_SET);
188  
189 ##-- dump datafiles $base.ma, $base.mb
190 0 my ($a2b,$pack_a,$len_i) = @$mmf{qw(a2b pack_a len_i)};
191 0 my $bidx = 0;
192 0 my $ai = 0;
193 0 my ($blen);
194 0 foreach (@$a2b) {
195 0 ▶ $_ //= '';
Condition0%0/2
$_ //= ""
l!l
00
MC/DC0%0/1
$_ //= ""
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
196 0 $blen = length($_)/$len_i;
197 0 ▶ $afh->print(pack($pack_a, $bidx, $blen))
Branch: 1 branch
BranchTrueFalseTotal
$afh->print(pack($pack_a, $bidx, $blen)) or $mmf->logconfess(qq[flush(): failed to write source record for a=$ai to $mmf->{"base"}.ma]) 0 0 0
198 or $mmf->logconfess("flush(): failed to write source record for a=$ai to $mmf->{base}.ma");
199 0 ▶ $bfh->print($_)
Branch: 1 branch
BranchTrueFalseTotal
$bfh->print($_) or $mmf->logconfess(qq[flush(): failed to write targets for a=$ai to $mmf->{"base"}.mb]) 0 0 0
200 or $mmf->logconfess("flush(): failed to write targets for a=$ai to $mmf->{base}.mb");
201 0 ++$ai;
202 0 $bidx += $blen;
203 }
204  
205 ##-- truncate datafiles at current position
206 0 CORE::truncate($afh, $afh->tell());
207 0 CORE::truncate($bfh, $bfh->tell());
208  
209 ##-- clear in-memory structures (but don't clobber existing references)
210 0 $mmf->{a2b} = [];
211  
212 0 ▶ $mmf->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$/ || ($mmf->reopen || (return undef))
l!l&&r!l&&!r
000
$mmf->reopen or return undef
l!l
00
MC/DC0%0/2
(caller 1)[3] =~ /::close$/ || ($mmf->reopen || (return undef))
(caller 1)[3] =~ /::close$/ $mmf->reopen
Truth table0%0/3
A || B
A= (caller 1)[3] =~ /::close$/B= $mmf->reopen
ABresult
1X1
011
000
213 0 return $mmf;
214 }
215  
216  
217 ##--------------------------------------------------------------
218 ## I/O: memory <-> file
219  
220 ## \@a2b = $mmf->toArray()
221 0 ▶sub toArray {
Subroutine toArray: not called CC=6 SCAR=37.4
Pod documented
222 0 my $mmf = shift;
223 0 ▶ return $mmf->{a2b} if (!$mmf->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless $mmf->opened 0 0 0
224  
225 #use bytes; ##-- deprecated in perl v5.18.2
226 0 my ($afh,$bfh,$pack_a,$len_a,$pack_i,$len_i) = @$mmf{qw(afh bfh pack_a len_a pack_i len_i)};
227 0 my @a2b = qw();
228  
229 ##-- ye olde loope
230 0 my ($bidx,$blen,$buf);
231 0 ▶ for (CORE::seek($afh,0,SEEK_SET); !eof($afh); ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $afh) 0 0 0
232 ##-- get position, length
233 0 ▶ CORE::read($afh,$buf,$len_a)==$len_a
Branch: 1 branch
BranchTrueFalseTotal
read($afh, $buf, $len_a) == $len_a or $mmf->logconfess(qq[toArray(): read() failed for $mmf->{"base"}.ma item ], scalar @a2b) 0 0 0
234 or $mmf->logconfess("toArray(): read() failed for $mmf->{base}.ma item ", scalar(@a2b));
235 0 ($bidx,$blen) = unpack($pack_a,$buf);
236  
237 ##-- read targets
238 0 $blen *= $len_i;
239 0 CORE::seek($bfh,$bidx*$len_i,SEEK_SET);
240 0 ▶ CORE::read($bfh,$buf,$blen)==$blen
Branch: 1 branch
BranchTrueFalseTotal
read($bfh, $buf, $blen) == $blen or $mmf->logconfess(qq[toArray(): read() failed for $blen byte(s) on $mmf->{"base"}.mb at logical record $bidx, item ], scalar @a2b) 0 0 0
241 or $mmf->logconfess("toArray(): read() failed for $blen byte(s) on $mmf->{base}.mb at logical record $bidx, item ", scalar(@a2b));
242 0 push(@a2b,$buf);
243 }
244 0 ▶ push(@a2b, @{$mmf->{a2b}}[scalar(@a2b)..$#{$mmf->{a2b}}]) if ($mmf->dirty);
Branch: 1 branch
BranchTrueFalseTotal
if $mmf->dirty 0 0 0
245 0 return \@a2b;
246 }
247  
248 ## $mmf = $mmf->fromArray(\@a2b)
249 ## + clobbers $mmf contents, steals \@a2b
250 0 ▶sub fromArray {
Subroutine fromArray: not called CC=1 SCAR=6.9
Pod documented
251 0 my ($mmf,$a2b) = @_;
252 0 $mmf->{a2b} = $a2b;
253 0 $mmf->{size} = scalar(@{$mmf->{a2b}});
254 0 return $mmf;
255 }
256  
257 ## $bool = $mmf->load()
258 ## + loads files to memory; must be opened
259 0 ▶sub load {
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
260 0 my $mmf = shift;
261 0 return $mmf->fromArray($mmf->toArray);
262 }
263  
264 ## $mmf = $mmf->save()
265 ## $mmf = $mmf->save($base)
266 ## + saves multimap to $base; really just a wrapper for open() and flush()
267 0 ▶sub save {
Subroutine save: not called CC=4 SCAR=30.0
Pod documented
268 0 my ($mmf,$base) = @_;
269 0 ▶ $mmf->open($base,'rw') if (defined($base));
Branch: 1 branch
BranchTrueFalseTotal
if defined $base 0 0 0
270 0 ▶ $mmf->logconfess("save(): cannot save un-opened multimap") if (!$mmf->opened);
Branch: 1 branch
BranchTrueFalseTotal
unless $mmf->opened 0 0 0
271 0 ▶ $mmf->flush() or $mmf->logconfess("save(): failed to flush to $mmf->{base}: $!");
Branch: 1 branch
BranchTrueFalseTotal
$mmf->flush or $mmf->logconfess(qq[save(): failed to flush to $mmf->{"base"}: $!]) 0 0 0
272 0 return $mmf;
273 }
274  
275  
276 ##--------------------------------------------------------------
277 ## I/O: header
278 ## + see also DiaColloDB::Persistent
279  
280 ## @keys = $coldb->headerKeys()
281 ## + keys to save as header
282 0 ▶sub headerKeys {
Subroutine headerKeys: not called CC=2 SCAR=17.9
Pod documented
283 0 ▶ return (qw(version), grep {!ref($_[0]{$_}) && $_ !~ m{^(?:flags|perms|base|version)$}} keys %{$_[0]});
Condition0%0/3
ref $_[0]{$_} || $_ =~ /^(?:flags|perms|base|version)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0]{$_} || $_ =~ /^(?:flags|perms|base|version)$/
ref $_[0]{$_} $_ =~ /^(?:flags|perms|base|version)$/
Truth table0%0/3
A || B
A= ref $_[0]{$_}B= $_ =~ /^(?:flags|perms|base|version)$/
ABresult
1X1
011
000
284 }
285  
286 ## $bool = $CLASS_OR_OBJECT->loadHeader()
287 ## + wraps $CLASS_OR_OBJECT->loadHeaderFile($CLASS_OR_OBJ->headerFile())
288 ## + INHERITED from DiaColloDB::Persistent
289  
290 ## $bool = $mmf->loadHeaderData($hdr)
291 0 ▶sub loadHeaderData {
Subroutine loadHeaderData: not called CC=4 SCAR=30.0
Pod documented
292 0 my ($mmf,$hdr) = @_;
293 0 ▶ if (!defined($hdr) && (fcflags($mmf->{flags})&O_CREAT) != O_CREAT) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $hdr and (fcflags($mmf->{'flags'}) & 64) != 64) { } 0 0 0
elsif (defined $hdr) { } 0 0 0
Condition0%0/3
not defined $hdr and (fcflags($mmf->{"flags"}) & 64) != 64
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $hdr and (fcflags($mmf->{"flags"}) & 64) != 64
not defined $hdr (fcflags($mmf->{"flags"}) & 64) != 64
Truth table0%0/3
not(A) and B
A= not defined $hdrB= (fcflags($mmf->{"flags"}) & 64) != 64
ABresult
0X0
100
111
294 0 $mmf->logconfess("loadHeader() failed to load '$mmf->{base}.hdr': $!");
295 }
296 elsif (defined($hdr)) {
297 0 return $mmf->SUPER::loadHeaderData($hdr);
298 }
299 0 return $mmf;
300 }
301  
302 ## $bool = $enum->saveHeader()
303 ## + inherited from DiaColloDB::Persistent
304  
305 ##--------------------------------------------------------------
306 ## I/O: text
307  
308 ## $bool = $obj->loadTextFile($filename_or_handle, %opts)
309 ## + wraps loadTextFh()
310 ## + INHERITED from DiaColloDB::Persistent
311  
312 ## $mmf = $CLASS_OR_OBJECT->loadTextFh($fh)
313 ## + loads from text file with lines of the form "A B1 B2..."
314 ## + clobbers multimap contents
315 0 ▶sub loadTextFh {
Subroutine loadTextFh: not called CC=5 SCAR=34.0
Pod documented
316 0 my ($mmf,$fh,%opts) = @_;
317 0 ▶ $mmf = $mmf->new(%opts) if (!ref($mmf));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mmf 0 0 0
318  
319 0 my $pack_b = $mmf->{pack_b};
320 0 my @a2b = qw();
321 0 my ($a,@b);
322 0 ▶ while (defined($_=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
323 0 chomp;
324 0 ▶ next if (/^%%/ || /^$/);
Branch: 1 branch
BranchTrueFalseTotal
if /^%%/ or /^$/ 0 0 0
Condition0%0/3
/^%%/ or /^$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%/ or /^$/
/^%%/ /^$/
Truth table0%0/3
A or B
A= /^%%/B= /^$/
ABresult
1X1
011
000
325 0 ($a,@b) = split(' ',$_);
326 0 $a2b[$a] = pack($pack_b, @b);
327 }
328  
329 ##-- clobber multimap
330 0 return $mmf->fromArray(\@a2b);
331 }
332  
333 ## $bool = $obj->saveTextFile($filename_or_handle, %opts)
334 ## + wraps saveTextFh()
335 ## + INHERITED from DiaColloDB::Persistent
336  
337 ## $bool = $mmf->saveTextFh($filename_or_fh,%opts)
338 ## + save from text file with lines of the form "A B1 B2..."
339 ## + %opts:
340 ## a2s=>\&a2s ##-- stringification code for A items, called as $s=$a2s->($bi)
341 ## b2s=>\&b2s ##-- stringification code for B items, called as $s=$b2s->($bi)
342 0 ▶sub saveTextFh {
Subroutine saveTextFh: not called CC=5 SCAR=34.0
Pod documented
343 0 my ($mmf,$fh,%opts) = @_;
344  
345 0 my $a2s = $opts{a2s};
346 0 my $b2s = $opts{b2s};
347 0 my $pack_b = $mmf->{pack_b};
348 0 my $a2b = $mmf->toArray;
349 0 my $a = 0;
350 0 foreach (@$a2b) {
351 0 ▶ if (defined($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_) 0 0 0
352 $fh->print(($a2s ? $a2s->($a) : $a),
353 "\t",
354 join(' ',
355 ($b2s
356 0 ▶ ? (map {$b2s->($_)} unpack($pack_b,$_))
Branch: 2 branches
BranchTrueFalseTotal
$a2s ? : 0 0 0
$b2s ? : 0 0 0
357 : unpack($pack_b, $_))),
358 "\n");
359 }
360 0 ++$a;
361 }
362  
363 0 return $mmf;
364 }
365  
366  
367 ##==============================================================================
368 ## Methods: population (in-memory only)
369  
370 ## $newsize = $mmf->addPairs($a,@bs)
371 ## $newsize = $mmf->addPairs($a,\@bs)
372 ## + adds mappings $a=>$b foreach $b in @bs
373 ## + multimap must be loaded to memory
374 0 ▶sub addPairs {
Subroutine addPairs: not called CC=2 SCAR=17.9
Pod documented
375 0 my $mmf = shift;
376 0 my $a = shift;
377 0 ▶ my $bs = UNIVERSAL::isa($_[0],'ARRAY') ? $_[0] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'ARRAY') ? : 0 0 0
378 0 $mmf->{a2b}[$a] .= pack($mmf->{pack_b}, @$bs);
379 0 return $mmf->{size} = scalar(@{$mmf->{a2b}});
380 }
381  
382 ##==============================================================================
383 ## Methods: lookup
384  
385 ## $bs_packed = $mmf->fetchraw($a)
386 ## + returns packed array \@bs of targets for $a, or undef if not found
387 0 ▶sub fetchraw {
Subroutine fetchraw: not called CC=6 SCAR=37.4
Pod documented
388 0 my ($mmf,$a) = @_;
389 0 ▶ return '' if (!defined($a));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 0 0 0
390  
391 0 my ($boff,$blen,$buf);
392 0 ▶ CORE::seek($mmf->{afh}, $a*$mmf->{len_a}, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $mmf->{"afh"}, $a * $mmf->{"len_a"}, 0 or $mmf->logconfess(qq[fetch(): seek() failed on $mmf->{"base"}.ma for a=$a]) 0 0 0
393 or $mmf->logconfess("fetch(): seek() failed on $mmf->{base}.ma for a=$a");
394 CORE::read($mmf->{afh},$buf,$mmf->{len_a})==$mmf->{len_a}
395 0 ▶ or $mmf->logconfess("fetch(): read() failed on $mmf->{base}.ma for a=$a");
Branch: 1 branch
BranchTrueFalseTotal
read($mmf->{"afh"}, $buf, $mmf->{"len_a"}) == $mmf->{"len_a"} or $mmf->logconfess(qq[fetch(): read() failed on $mmf->{"base"}.ma for a=$a]) 0 0 0
396 0 ($boff,$blen) = unpack($mmf->{pack_a}, $buf);
397  
398 0 $boff *= $mmf->{len_i};
399 0 $blen *= $mmf->{len_i};
400 0 ▶ CORE::seek($mmf->{bfh}, $boff, SEEK_SET)
Branch: 1 branch
BranchTrueFalseTotal
seek $mmf->{"bfh"}, $boff, 0 or $mmf->logconfess(qq[fetch(): seek() failed on $mmf->{"base"}.mb to offset $boff for a=$a]) 0 0 0
401 or $mmf->logconfess("fetch(): seek() failed on $mmf->{base}.mb to offset $boff for a=$a");
402 0 ▶ CORE::read($mmf->{bfh}, $buf, $blen)==$blen
Branch: 1 branch
BranchTrueFalseTotal
read($mmf->{"bfh"}, $buf, $blen) == $blen or $mmf->logconfess(qq[fetch(): read() failed on $mmf->{"base"}.mb for target-set of $blen byte(s) at offset $boff for a=$a]) 0 0 0
403 or $mmf->logconfess("fetch(): read() failed on $mmf->{base}.mb for target-set of $blen byte(s) at offset $boff for a=$a");
404  
405 0 return $buf;
406 }
407  
408 ## \@bs_or_undef = $mmf->fetch($a)
409 ## + returns array \@bs of targets for $a, or undef if not found
410 ## + multimap must be opened
411 0 ▶sub fetch {
Subroutine fetch: not called CC=1 SCAR=6.9
Pod documented
412 0 return [unpack($_[0]{pack_b}, $_[0]->fetchraw(@_[1..$#_]))];
413 }
414  
415 ##==============================================================================
416 ## Footer
417 1 1;
418  
419 __END__
« DiaColloDB/Compat/v0_08/MultiMapFile.pm ↑ Summary DiaColloDB/Utils.pm »