DiaColloDB/Persistent.pm

filter: Statementstmt 17.8% 17 / 95 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 22.5% 9 / 40 Podpod 93.5% 29 / 31 totaltotal 23.7% 55 / 232 CC 28 SCAR 23.2
CC
28
Cov
11%
headerFile
30.0
mmclass
20.0
copyto
20.0
CRAP 10.2
« DiaColloDB/EnumFile/Tied.pm ↑ Summary DiaColloDB/Document/JSON.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: DiaColloDB::Persistent.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: collocation db, persistent objects
5  
6  
7 package DiaColloDB::Persistent;
8 2 ▶use DiaColloDB::Utils qw(:list);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ##==============================================================================
13 ## Globals & Constants
14  
15 2 our @ISA = qw(DiaColloDB::Logger);
16  
17 ##==============================================================================
18 ## mmap usage
19  
20 ## $CLASS = $obj->mmclass($CLASSNAME) : respects $obj->{mmap} option
21 0 ▶sub mmclass {
Subroutine mmclass: not called CC=4 SCAR=30.0
Pod undocumented
22 0 my ($that,$class) = @_;
23 0 ▶ $class =~ s/::MMap$//i if (ref($that) && !($that->{mmap}//1));
Branch: 1 branch
BranchTrueFalseTotal
if ref $that and not $that->{"mmap"} // 1 0 0 0
Condition0%0/5
ref $that and not $that->{"mmap"} // 1
!ll&&!rl&&r
000
$that->{"mmap"} // 1
l!l
00
MC/DC0%0/2
ref $that and not $that->{"mmap"} // 1
ref $that $that->{"mmap"}
Truth table0%0/3
A and not(B)
A= ref $thatB= $that->{"mmap"}
ABresult
0X0
110
101
24 0 return $class;
25 }
26  
27  
28 ##==============================================================================
29 ## disk usage, timestamp
30  
31 ## @files = $obj->diskFiles()
32 ## + returns disk storage files, used by du() and timestamp()
33 ## + default implementation returns $obj->{file} or glob("$obj->{base}*")
34 0 ▶sub diskFiles {
Subroutine diskFiles: not called CC=3 SCAR=24.8
Pod documented
35 0 my $obj = shift;
36 0 ▶ return ($obj->{file}) if ($obj->{file});
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"file"} 0 0 0
37 0 ▶ return glob("$obj->{base}*") if ($obj->{base});
Branch: 1 branch
BranchTrueFalseTotal
if $obj->{"base"} 0 0 0
38 0 return qw();
39 }
40  
41 ## $nbytes = $obj->du()
42 ## + default implementation wraps DiaColloDB::Utils::du_file($obj->diskFiles)
43 0 ▶sub du {
Subroutine du: not called CC=1 SCAR=6.9
Pod documented
44 0 return DiaColloDB::Utils::du_file($_[0]->diskFiles);
45 }
46  
47 ## $mtime = $obj->mtime()
48 ## + default returns newest mtime for $obj->diskFiles()
49 0 ▶sub mtime {
Subroutine mtime: not called CC=3 SCAR=24.8
Pod documented
50 0 my $obj = shift;
51 0 my $mtime = 0;
52 0 foreach (map {DiaColloDB::Utils::file_mtime($_)} $obj->diskFiles) {
53 0 ▶ $mtime = $_ if ($_ > $mtime);
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $mtime 0 0 0
54 }
55 0 return $mtime;
56 }
57  
58 ## $timestamp = $obj->timestamp()
59 ## + default returns timestamp for $obj->mtime()
60 0 ▶sub timestamp {
Subroutine timestamp: not called CC=1 SCAR=6.9
Pod documented
61 0 return DiaColloDB::Utils::timestamp($_[0]->mtime);
62 }
63  
64 ## $bool = $obj->unlink()
65 ## + unlinks disk files
66 ## + implcitly calls $obj->close() if available
67 0 ▶sub unlink {
Subroutine unlink: not called CC=2 SCAR=17.9
Pod documented
68 0 my $obj = shift;
69 0 my @files = $obj->diskFiles();
70 0 ▶ $obj->close() if ($obj->can('close'));
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can("close") 0 0 0
71 0 CORE::unlink(grep {-e $_} @files);
72 }
73  
74 ## $bool = $obj->copyto($todir, %opts)
75 ## + copies object file(s) to $todir, creating $todir if it doesn't already exist;
76 ## options %opts:
77 ## (
78 ## method => \&method, ##-- use CODE-ref \&method(\@srcfiles,$todir,%opts) to copy file(s) (default=\&DiaColloDB::Utils::copyto)
79 ## close => $bool, ##-- implicitly close() object before operation? (default=0)
80 ## ... ##-- other options are passed to \&method
81 ## )
82 0 ▶sub copyto {
Subroutine copyto: not called CC=4 SCAR=30.0
Pod documented
83 0 my ($obj,$todir,%opts) = @_;
84 0 ▶ my $method = $opts{method} || \&DiaColloDB::Utils::copyto;
Condition0%0/2
$opts{'method'} || \&DiaColloDB::Utils::copyto
l!l
00
MC/DC0%0/1
$opts{'method'} || \&DiaColloDB::Utils::copyto
$opts{'method'}
Truth table0%0/2
A
A= $opts{'method'}
Aresult
11
00
85 0 my @files = $obj->diskFiles();
86 0 ▶ $obj->close() if ($opts{close} && $obj->can('close'));
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"close"} and $obj->can("close") 0 0 0
Condition0%0/3
$opts{"close"} and $obj->can("close")
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"close"} and $obj->can("close")
$opts{"close"} $obj->can("close")
Truth table0%0/3
A and B
A= $opts{"close"}B= $obj->can("close")
ABresult
0X0
100
111
87 0 return $method->(\@files, $todir, %opts);
88 }
89  
90 ## $bool = $obj->copyto_a($todir, %opts)
91 ## + wrapper for copyto() which propagates timestamps, ownership, and permissions
92 0 ▶sub copyto_a {
Subroutine copyto_a: not called CC=1 SCAR=6.9
Pod documented
93 0 return $_[0]->copyto(@_[1..$#_], method=>\&DiaColloDB::Utils::copyto_a);
94 }
95  
96 ## $bool = $obj->moveto($todir, %opts)
97 ## + wrapper for $obj->copyto($todir, %opts, method=>\&DiaColloDB::Utils::moveto, close=>1);
98 0 ▶sub moveto {
Subroutine moveto: not called CC=1 SCAR=6.9
Pod documented
99 0 return $_[0]->copyto(@_[1..$#_], method=>\&DiaColloDB::Utils::moveto, close=>1);
100 }
101  
102 ##==============================================================================
103 ## IO
104  
105 ##--------------------------------------------------------------
106 ## I/O: Header
107  
108 ## @keys = $obj->headerKeys()
109 ## + keys to save as header; default implementation returns all keys of all non-references
110 0 ▶sub headerKeys {
Subroutine headerKeys: not called CC=1 SCAR=6.9
Pod documented
111 0 return grep {!ref($_[0]{$_})} keys %{$_[0]};
112 }
113  
114 ## $hdr = $obj->headerData()
115 ## + returns reference to object header data; default returns anonymous HASH-ref for $obj->headerKeys()
116 0 ▶sub headerData {
Subroutine headerData: not called CC=2 SCAR=17.9
Pod documented
117 0 ▶ return {(map {($_=>$_[0]->{$_})} $_[0]->headerKeys), %{$_[0]->headerDataExtra//{}}};
Condition0%0/2
$_[0]->headerDataExtra // {}
l!l
00
MC/DC0%0/1
$_[0]->headerDataExtra // {}
$_[0]->headerDataExtra
Truth table0%0/2
A
A= $_[0]->headerDataExtra
Aresult
11
00
118 }
119  
120 ## $extra = $obj->headerDataExtra()
121 ## + returns extra data for inclusion in default headerData() HASH-ref
122 0 ▶sub headerDataExtra {
Subroutine headerDataExtra: not called CC=1 SCAR=6.9
Pod undocumented
123 0 return {class=>ref($_[0])};
124 }
125  
126 ## $filename = $obj->headerFile()
127 ## + returns header filename; default returns "$obj->{base}.hdr" or "$obj->{dbdir}/header.json"
128 0 ▶sub headerFile {
Subroutine headerFile: not called CC=5 SCAR=34.0
Pod documented
129 0 ▶ return undef if (!ref($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 0 0 0
130 0 ▶ return "$_[0]{dbdir}/header.json" if (defined($_[0]{dbdir}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"dbdir"} 0 0 0
131 0 ▶ return "$_[0]{base}.hdr" if (defined($_[0]{base}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"base"} 0 0 0
132 0 ▶ return "$_[0]{file}.hdr" if (defined($_[0]{file}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"file"} 0 0 0
133 0 return undef;
134 }
135  
136 ## $str = $obj->saveHeaderString(%opts)
137 ## + returns JSON string for object header data
138 0 ▶sub saveHeaderString {
Subroutine saveHeaderString: not called CC=1 SCAR=6.9
Pod documented
139 0 return DiaColloDB::Utils::saveJsonString($_[0]->headerData, @_[1..$#_]);
140 }
141  
142 ## $bool = $obj->saveHeaderFh($fh, %opts)
143 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
144 2 *saveHeaderFh = \&saveHeaderFile;
145 }
146  
147 ## $bool = $obj->saveHeaderFile($filename_or_handle, %opts)
148 0 ▶sub saveHeaderFile {
Subroutine saveHeaderFile: not called CC=1 SCAR=6.9
Pod documented
149 0 return DiaColloDB::Utils::saveJsonFile($_[0]->headerData, @_[1..$#_]);
150 }
151  
152 ## $bool = $obj->saveHeader()
153 ## $bool = $obj->saveHeader($headerFile,%opts)
154 ## + wraps $obj->saveHeaderFile($headerFile//$obj->headerFile(), %opts)
155 0 ▶sub saveHeader {
Subroutine saveHeader: not called CC=2 SCAR=17.9
Pod documented
156 0 ▶ $_[0]->saveHeaderFile(($_[1]//$_[0]->headerFile()), @_[2..$#_]);
Condition0%0/3
$_[1] // $_[0]->headerFile
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] // $_[0]->headerFile
$_[1] $_[0]->headerFile
Truth table0%0/3
A // B
A= $_[1]B= $_[0]->headerFile
ABresult
1X1
011
000
157 }
158  
159 ##--
160  
161 ## $obj = $CLASS_OR_OBJECT->loadHeaderData($data_or_undef)
162 ## + instantiates header data from $data
163 ## + default just sets @$obj{keys %$data} = values %$data and clobbers $obj->{class}=ref($obj)
164 0 ▶sub loadHeaderData {
Subroutine loadHeaderData: not called CC=3 SCAR=24.8
Pod documented
165 0 my ($that,$hdr) = @_;
166 0 ▶ $that->logconfess("loadHeaderData(): header data undefined") if (!defined($hdr));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hdr 0 0 0
167 0 ▶ $that = $that->new() if (!ref($that));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $that 0 0 0
168 0 @$that{keys %$hdr} = values %$hdr;
169 0 $that->{class} = ref($that);
170 0 return $that;
171 }
172  
173 ## $obj = $CLASS_OR_OBJECT->loadHeaderString( $string,%opts)
174 ## $obj = $CLASS_OR_OBJECT->loadHeaderString(\$string,%opts)
175 ## + loads header data from JSON string $string
176 ## + wraps $CLASS_OR_OBJECT->loadHeaderData()
177 0 ▶sub loadHeaderString {
Subroutine loadHeaderString: not called CC=1 SCAR=6.9
Pod documented
178 0 return $_[0]->loadHeaderData(DiaColloDB::Utils::loadJsonString(@_[1..$#_]));
179 }
180  
181 ## $hdr = $CLASS_OR_OBJECT->readHeaderFh($fh, %opts)
182 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
183 2 *readHeaderFh = *readHeaderFile;
184 }
185  
186 ## $hdr = $CLASS_OR_OBJECT->readHeaderFile($filename_or_handle, %opts)
187 ## + wraps DiaColloDB::Utils::loadJsonFile()
188 0 ▶sub readHeaderFile {
Subroutine readHeaderFile: not called CC=1 SCAR=6.9
Pod documented
189 0 return DiaColloDB::Utils::loadJsonFile(@_[1..$#_]);
190 }
191  
192 ## $hdr = $CLASS_OR_OBJECT->readHeader()
193 ## $hdr = $CLASS_OR_OBJECT->readHeader($headerFile,%opts)
194 ## + wraps $CLASS_OR_OBJECT->readHeaderFile($headerFile//$CLASS_OR_OBJ->headerFile())
195 0 ▶sub readHeader {
Subroutine readHeader: not called CC=2 SCAR=17.9
Pod documented
196 0 ▶ return $_[0]->readHeaderFile(($_[1]//$_[0]->headerFile), @_[2..$#_]);
Condition0%0/3
$_[1] // $_[0]->headerFile
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] // $_[0]->headerFile
$_[1] $_[0]->headerFile
Truth table0%0/3
A // B
A= $_[1]B= $_[0]->headerFile
ABresult
1X1
011
000
197 }
198  
199 ## $obj = $CLASS_OR_OBJECT->loadHeaderFh($fh, %opts)
200 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
201 2 *loadHeaderFh = \&loadHeaderFile;
202 }
203  
204 ## $obj = $CLASS_OR_OBJECT->loadHeaderFile()
205 ## $obj = $CLASS_OR_OBJECT->loadHeaderFile($filename_or_handle, %opts)
206 ## + wraps $CLASS_OR_OBJECT->loadHeaderData($CLASS_OR_OBJECT->readHeader($filename_or_handle, %opts))
207 0 ▶sub loadHeaderFile {
Subroutine loadHeaderFile: not called CC=1 SCAR=6.9
Pod documented
208 0 return $_[0]->loadHeaderData($_[0]->readHeader(@_[1..$#_]));
209 }
210  
211 ## $bool = $CLASS_OR_OBJECT->loadHeader()
212 ## $bool = $CLASS_OR_OBJECT->loadHeader($headerFile,%opts)
213 ## + alias for loadHeaderFile()
214 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
215 2 *loadHeader = \&loadHeaderFile;
216 }
217  
218 ##--------------------------------------------------------------
219 ## I/O: JSON
220  
221 ## $thingy = $obj->TO_JSON()
222 ## + JSON module wrapper; default just returns anonymous HASH-ref
223 0 ▶sub TO_JSON {
Subroutine TO_JSON: not called CC=1 SCAR=6.9
Pod documented
224 0 return { %{$_[0]} };
225 }
226  
227 ## $str = $obj->saveJsonString(%opts)
228 0 ▶sub saveJsonString {
Subroutine saveJsonString: not called CC=1 SCAR=6.9
Pod documented
229 0 return DiaColloDB::Utils::saveJsonString(@_);
230 }
231  
232 ## $bool = $obj->saveJsonFh($fh, %opts)
233 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
234 2 *saveJsonFh = \&saveJsonFile;
235 }
236  
237 ## $bool = $obj->saveJsonFile($filename_or_handle, %opts)
238 0 ▶sub saveJsonFile {
Subroutine saveJsonFile: not called CC=1 SCAR=6.9
Pod documented
239 0 return DiaColloDB::Utils::saveJsonFile(@_);
240 }
241  
242 ## $obj = $CLASS_OR_OBJECT->loadJsonData( $data,%opts)
243 ## + guts for loadJsonString(), loadJsonFile()
244 0 ▶sub loadJsonData {
Subroutine loadJsonData: not called CC=2 SCAR=17.9
Pod documented
245 0 my ($that,$data) = @_;
246 0 ▶ return bless($data,$that) if (!ref($that));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $that 0 0 0
247 0 %$that = %$data;
248 0 return $that;
249 }
250  
251 ## $obj = $CLASS_OR_OBJECT->loadJsonString( $string,%opts)
252 ## $obj = $CLASS_OR_OBJECT->loadJsonString(\$string,%opts)
253 0 ▶sub loadJsonString {
Subroutine loadJsonString: not called CC=1 SCAR=6.9
Pod documented
254 0 my $that = shift;
255 0 return $that->loadJsonData(DiaColloDB::Utils::loadJsonString(@_));
256 }
257  
258 ## $obj = $CLASS_OR_OBJECT->loadJsonFh($fh,%opts)
259 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
260 2 *loadJsonFh = \&loadJsonFile;
261 }
262  
263 ## $obj = $CLASS_OR_OBJECT->loadJsonFile($filename_or_handle,%opts)
264 0 ▶sub loadJsonFile {
Subroutine loadJsonFile: not called CC=1 SCAR=6.9
Pod documented
265 0 my $that = shift;
266 0 return $that->loadJsonData(DiaColloDB::Utils::loadJsonFile(@_));
267 }
268  
269 ##--------------------------------------------------------------
270 ## I/O: Text
271  
272 ## $bool = $obj->saveTextFh($fh, %opts)
273 ## + save text representation to a filehandle (dummy)
274 0 ▶sub saveTextFh {
Subroutine saveTextFh: not called CC=1 SCAR=6.9
Pod documented
275 0 $_[0]->logconfess("saveTextFh() not implemented");
276 }
277  
278 ## $bool = $obj->saveTextFile($filename_or_handle, %opts)
279 ## + wraps saveTextFh()
280 0 ▶sub saveTextFile {
Subroutine saveTextFile: not called CC=4 SCAR=30.0
Pod documented
281 0 my ($obj,$file,@args) = @_;
282 0 ▶ my $fh = ref($file) ? $file : IO::File->new(">$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
283 0 ▶ $obj->logconfess("saveTextFile(): failed to open '$file': $!") if (!ref($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh 0 0 0
284 0 my $rc = $obj->saveTextFh($fh,@args);
285 0 ▶ $fh->close() if (!ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
286 0 return $rc;
287 }
288  
289 ## $obj = $CLASS_OR_OBJECT->loadTextFh($fh, %opts)
290 ## + load object from a text filehandle (dummy)
291 0 ▶sub loadTextFh {
Subroutine loadTextFh: not called CC=1 SCAR=6.9
Pod documented
292 0 $_[0]->logconfess("loadTextFh() not implemented");
293 }
294  
295 ## $bool = $CLASS_OR_OBJECT->loadTextFile($filename_or_handle, %opts)
296 ## + wraps loadTextFh()
297 0 ▶sub loadTextFile {
Subroutine loadTextFile: not called CC=4 SCAR=30.0
Pod documented
298 0 my ($that,$file,@args) = @_;
299 0 ▶ my $fh = ref($file) ? $file : IO::File->new("<$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
300 0 ▶ $that->logconfess("loadTextFile(): failed to open '$file': $!") if (!ref($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fh 0 0 0
301 0 my $obj = $that->loadTextFh($fh,@args);
302 0 ▶ $fh->close() if (!ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
303 0 return $obj;
304 }
305  
306  
307 ##==============================================================================
308 ## Footer
309 2 1;
310  
311 __END__
« DiaColloDB/EnumFile/Tied.pm ↑ Summary DiaColloDB/Document/JSON.pm »