blib/lib/DBM/Deep/Storage/File.pm

filter: Statementstmt 97.2% 141 / 145 Branchbran 79.1% 38 / 48 Conditioncond 86.6% 13 / 15 MC/DCmcdc 80.0% 8 / 10 Subroutinesub 95.6% 22 / 23 Podpod 100% 14 / 14 totaltotal 92.5% 236 / 255 CC 31 SCAR 14.5
CC
31
Cov
92%
_lock
9.2
new
6.0
size
6.0
CRAP 4.3
« _build/lib/Module/Build/Custom.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Index.pm »
1 package DBM::Deep::Storage::File;
2  
3 47 ▶use 5.006_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 47 ▶use Fcntl qw( :DEFAULT :flock :seek );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 47 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 47 ▶use base 'DBM::Deep::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 DBM::Deep::Storage::File
17  
18 =head1 PURPOSE
19  
20 This is an internal-use-only object for L<DBM::Deep>. It mediates the low-level
21 interaction with the storage mechanism.
22  
23 Currently, the only storage mechanism supported is the file system.
24  
25 =head1 OVERVIEW
26  
27 This class provides an abstraction to the storage mechanism so that the Engine
28 (the only class that uses this class) doesn't have to worry about that.
29  
30 =head1 METHODS
31  
32 =head2 new( \%args )
33  
34 =cut
35  
36 sub new {
37 382 ▶ my $class = shift;
Subroutine new: called CC=6 SCAR=17.9
Pod documented
38 382 my ($args) = @_;
39  
40 382 my $self = bless {
41 autobless => 1,
42 autoflush => 1,
43 end => 0,
44 fh => undef,
45 file => undef,
46 file_offset => 0,
47 locking => 1,
48 locked => 0,
49 #XXX Migrate this to the engine, where it really belongs.
50 filter_store_key => undef,
51 filter_store_value => undef,
52 filter_fetch_key => undef,
53 filter_fetch_value => undef,
54 }, $class;
55  
56 # Grab the parameters we want to use
57 382 foreach my $param ( keys %$self ) {
58 4584 ▶ next unless exists $args->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$param} 4140 444 4584
59 444 $self->{$param} = $args->{$param};
60 }
61  
62 382 ▶ if ( $self->{fh} && !$self->{file_offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} and not $self->{"file_offset"}) 4 378 382
Condition100%3/3
$self->{"fh"} and not $self->{"file_offset"}
!ll&&!rl&&r
37714
MC/DC100%2/2
$self->{"fh"} and not $self->{"file_offset"}
$self->{"fh"} not $self->{"file_offset"}
Truth table100%3/3
A and not(B)
A= $self->{"fh"}B= not $self->{"file_offset"}
ABresult
0X0
100
111
63 4 $self->{file_offset} = tell( $self->{fh} );
64 }
65  
66 382 ▶ $self->open unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 377 5 382
67  
68 381 return $self;
69 }
70  
71 =head2 open()
72  
73 This method opens the filehandle for the filename in C< file >.
74  
75 There is no return value.
76  
77 =cut
78  
79 # TODO: What happens if we ->open when we already have a $fh?
80 sub open {
81 392 ▶ my $self = shift;
Subroutine open: called CC=5 SCAR=16.1
Pod documented
82  
83 # Adding O_BINARY should remove the need for the binmode below. However,
84 # I'm not going to remove it because I don't have the Win32 chops to be
85 # absolutely certain everything will be ok.
86 392 my $flags = O_CREAT | O_BINARY;
87  
88 392 ▶ if ( !-e $self->{file} || -w _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -e $self->{'file'} or -w _) { } 390 2 392
Condition100%3/3
not -e $self->{"file"} or -w _
l!l&&r!l&&!r
23882
MC/DC100%2/2
not -e $self->{"file"} or -w _
not -e $self->{"file"} -w _
Truth table100%3/3
not(A) or B
A= not -e $self->{"file"}B= -w _
ABresult
1X1
011
000
89 390 $flags |= O_RDWR;
90 }
91 else {
92 2 $flags |= O_RDONLY;
93 }
94  
95 392 my $fh;
96 392 ▶ sysopen( $fh, $self->{file}, $flags )
Branch: 1 branch
BranchTrueFalseTotal
sysopen $fh, $self->{"file"}, $flags or die qq[DBM::Deep: Cannot sysopen file '$self->{"file"}': $!\n] 1 391 392
97 or die "DBM::Deep: Cannot sysopen file '$self->{file}': $!\n";
98 391 $self->{fh} = $fh;
99  
100 # Even though we use O_BINARY, better be safe than sorry.
101 391 binmode $fh;
102  
103 391 ▶ if ($self->{autoflush}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoflush"}) 391 0 391
104 391 my $old = select $fh;
105 391 $|=1;
106 391 select $old;
107 }
108  
109 391 return 1;
110 }
111  
112 =head2 close()
113  
114 If the filehandle is opened, this will close it.
115  
116 There is no return value.
117  
118 =cut
119  
120 sub close {
121 408 ▶ my $self = shift;
Subroutine close: called CC=2 SCAR=6.9
Pod documented
122  
123 408 ▶ if ( $self->{fh} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"}) 387 21 408
124 387 close $self->{fh};
125 387 $self->{fh} = undef;
126 }
127  
128 408 return 1;
129 }
130  
131 =head2 size()
132  
133 This will return the size of the DB. If file_offset is set, this will take that into account.
134  
135 =cut
136  
137 sub size {
138 0 ▶ my $self = shift;
Subroutine size: not called CC=2 SCAR=17.9
Pod documented
139  
140 0 ▶ return 0 unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 0 0 0
141 0 return( (-s $self->{fh}) - $self->{file_offset} );
142 }
143  
144 =head2 set_inode()
145  
146 This will set the inode value of the underlying file object.
147  
148 This is only needed to handle some obscure Win32 bugs. It reqlly shouldn't be
149 needed outside this object.
150  
151 There is no return value.
152  
153 =cut
154  
155 sub set_inode {
156 2901 ▶ my $self = shift;
Subroutine set_inode: called CC=2 SCAR=6.9
Pod documented
157  
158 2901 ▶ unless ( defined $self->{inode} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"inode"}) 371 2530 2901
159 371 my @stats = stat($self->{fh});
160 371 $self->{inode} = $stats[1];
161 371 $self->{end} = $stats[7];
162 }
163  
164 2901 return 1;
165 }
166  
167 =head2 print_at( $offset, @data )
168  
169 This takes an optional offset and some data to print.
170  
171 C< $offset >, if defined, will be used to seek into the file. If file_offset is
172 set, it will be used as the zero location. If it is undefined, no seeking will
173 occur. Then, C< @data > will be printed to the current location.
174  
175 There is no return value.
176  
177 =cut
178  
179 sub print_at {
180 25587 ▶ my $self = shift;
Subroutine print_at: called CC=3 SCAR=11.1
Pod documented
181 25587 my $loc = shift;
182  
183 25587 local ($,,$\);
184  
185 25587 my $fh = $self->{fh};
186 25587 ▶ if ( defined $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc) 25587 0 25587
187 25587 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
188 }
189  
190 25587 if ( DEBUG ) {
191 my $caller = join ':', (caller)[0,2];
192 my $len = length( join '', @_ );
193 warn "($caller) print_at( " . (defined $loc ? $loc : '<undef>') . ", $len )\n";
194 }
195  
196 25587 ▶ print( $fh @_ ) or die "Internal Error (print_at($loc)): $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print $fh @_ or die "Internal Error (print_at($loc)): $!\n" 0 25587 25587
197  
198 25587 return 1;
199 }
200  
201 =head2 read_at( $offset, $length )
202  
203 This takes an optional offset and a length.
204  
205 C< $offset >, if defined, will be used to seek into the file. If file_offset is
206 set, it will be used as the zero location. If it is undefined, no seeking will
207 occur. Then, C< $length > bytes will be read from the current location.
208  
209 The data read will be returned.
210  
211 =cut
212  
213 sub read_at {
214 108429 ▶ my $self = shift;
Subroutine read_at: called CC=2 SCAR=6.9
Pod documented
215 108429 my ($loc, $size) = @_;
216  
217 108429 my $fh = $self->{fh};
218 108429 ▶ if ( defined $loc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc) 107894 535 108429
219 107894 seek( $fh, $loc + $self->{file_offset}, SEEK_SET );
220 }
221  
222 108429 if ( DEBUG ) {
223 my $caller = join ':', (caller)[0,2];
224 warn "($caller) read_at( " . (defined $loc ? $loc : '<undef>') . ", $size )\n";
225 }
226  
227 108429 my $buffer;
228 108429 read( $fh, $buffer, $size);
229  
230 108429 return $buffer;
231 }
232  
233 =head2 DESTROY
234  
235 When the ::Storage::File object goes out of scope, it will be closed.
236  
237 =cut
238  
239 sub DESTROY {
240 374 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=7.0
241 374 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 374 374
242  
243 374 $self->close;
244  
245 374 return;
246 }
247  
248 =head2 request_space( $size )
249  
250 This takes a size and adds that much space to the DBM.
251  
252 This returns the offset for the new location.
253  
254 =cut
255  
256 sub request_space {
257 5301 ▶ my $self = shift;
Subroutine request_space: called CC=1 SCAR=0.0
Pod documented
258 5301 my ($size) = @_;
259  
260 #XXX Do I need to reset $self->{end} here? I need a testcase
261 5301 my $loc = $self->{end};
262 5301 $self->{end} += $size;
263  
264 5301 return $loc;
265 }
266  
267 =head2 copy_stats( $target_filename )
268  
269 This will take the stats for the current filehandle and apply them to
270 C< $target_filename >. The stats copied are:
271  
272 =over 4
273  
274 =item * Onwer UID and GID
275  
276 =item * Permissions
277  
278 =back
279  
280 =cut
281  
282 sub copy_stats {
283 2 ▶ my $self = shift;
Subroutine copy_stats: called CC=1 SCAR=0.0
Pod documented
284 2 my ($temp_filename) = @_;
285  
286 2 my @stats = stat( $self->{fh} );
287 2 my $perms = $stats[2] & 07777;
288 2 my $uid = $stats[4];
289 2 my $gid = $stats[5];
290 2 chown( $uid, $gid, $temp_filename );
291 2 chmod( $perms, $temp_filename );
292 }
293  
294 sub flush {
295 5500 ▶ my $self = shift;
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
296  
297 # Flush the filehandle
298 5500 my $old_fh = select $self->{fh};
299 5500 my $old_af = $|; $| = 1; $| = $old_af;
300 5500 select $old_fh;
301  
302 5500 return 1;
303 }
304  
305 sub is_writable {
306 1978 ▶ my $self = shift;
Subroutine is_writable: called CC=3 SCAR=11.1
Pod documented
307  
308 1978 my $fh = $self->{fh};
309 1978 ▶ return unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 1978 1978
310 1978 ▶ return unless defined fileno $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined fileno $fh 0 1978 1978
311 1978 local $\ = ''; # just in case
312 47 ▶ no warnings; # temporarily disable warnings
Subroutine BEGIN: called CC=1 SCAR=0.0
313 1978 local $^W; # temporarily disable warnings
314 1978 return print $fh '';
315 }
316  
317 sub lock_exclusive {
318 5616 ▶ my $self = shift;
Subroutine lock_exclusive: called CC=1 SCAR=0.0
Pod documented
319 5616 my ($obj) = @_;
320 5616 return $self->_lock( $obj, LOCK_EX );
321 }
322  
323 sub lock_shared {
324 4433 ▶ my $self = shift;
Subroutine lock_shared: called CC=1 SCAR=0.0
Pod documented
325 4433 my ($obj) = @_;
326 4433 return $self->_lock( $obj, LOCK_SH );
327 }
328  
329 sub _lock {
330 10049 ▶ my $self = shift;
Subroutine _lock: called CC=9 SCAR=22.2
331 10049 my ($obj, $type) = @_;
332  
333 10049 ▶ $type = LOCK_EX unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 10049 10049
334  
335 #XXX This is a temporary fix for Win32 and autovivification. It
336 # needs to improve somehow. -RobK, 2008-03-09
337 10049 ▶ if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "cygwin") 0 10049 10049
Condition33%1/3
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
0010049
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "cygwin"
$^O eq "MSWin32" $^O eq "cygwin"
Truth table33%1/3
A or B
A= $^O eq "MSWin32"B= $^O eq "cygwin"
ABresult
1X1
011
000
338 0 $type = LOCK_EX;
339 }
340  
341 10049 ▶ if (!defined($self->{fh})) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 1 10048 10049
342  
343 #XXX This either needs to allow for upgrading a shared lock to an
344 # exclusive lock or something else with autovivification.
345 # -RobK, 2008-03-09
346 10048 ▶ if ($self->{locking}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locking"}) 10045 3 10048
347 10045 ▶ if (!$self->{locked}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"locked"}) 5418 4627 10045
348 5418 flock($self->{fh}, $type);
349  
350 # refresh end counter in case file has changed size
351 5418 my @stats = stat($self->{fh});
352 5418 $self->{end} = $stats[7];
353  
354 # double-check file inode, in case another process
355 # has optimize()d our file while we were waiting.
356 5418 ▶ if (defined($self->{inode}) && $stats[1] != $self->{inode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"inode"} and $stats[1] != $self->{"inode"}) 12 5406 5418
Condition100%3/3
defined $self->{"inode"} and $stats[1] != $self->{"inode"}
!ll&&!rl&&r
380502612
MC/DC100%2/2
defined $self->{"inode"} and $stats[1] != $self->{"inode"}
defined $self->{"inode"} $stats[1] != $self->{"inode"}
Truth table100%3/3
A and B
A= defined $self->{"inode"}B= $stats[1] != $self->{"inode"}
ABresult
0X0
100
111
357 12 $self->close;
358 12 $self->open;
359  
360 #XXX This needs work
361 12 $obj->{engine}->setup( $obj );
362  
363 12 flock($self->{fh}, $type); # re-lock
364  
365 # This may not be necessary after re-opening
366 12 $self->{end} = (stat($self->{fh}))[7]; # re-end
367 }
368 }
369 10045 $self->{locked}++;
370  
371 10045 return 1;
372 }
373  
374 3 return;
375 }
376  
377 sub unlock {
378 10050 ▶ my $self = shift;
Subroutine unlock: called CC=5 SCAR=16.1
Pod documented
379  
380 10050 ▶ if (!defined($self->{fh})) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"fh"}) 5 10045 10050
381  
382 10045 ▶ if ($self->{locking} && $self->{locked} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"locking"} and $self->{"locked"} > 0) 10039 6 10045
Condition100%3/3
$self->{"locking"} and $self->{"locked"} > 0
!ll&&!rl&&r
3310039
MC/DC100%2/2
$self->{"locking"} and $self->{"locked"} > 0
$self->{"locking"} $self->{"locked"} > 0
Truth table100%3/3
A and B
A= $self->{"locking"}B= $self->{"locked"} > 0
ABresult
0X0
100
111
383 10039 $self->{locked}--;
384  
385 10039 ▶ if (!$self->{locked}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"locked"}) 5412 4627 10039
386 5412 flock($self->{fh}, LOCK_UN);
387 5412 return 1;
388 }
389  
390 4627 return;
391 }
392  
393 6 return;
394 }
395  
396 47 1;
397 __END__
« _build/lib/Module/Build/Custom.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Index.pm »