blib/lib/DBM/Deep.pm

filter: Statementstmt 100% 693 / 693 Branchbran 85.4% 94 / 110 Conditioncond 83.3% 25 / 30 MC/DCmcdc 66.6% 12 / 18 Subroutinesub 100% 231 / 231 Podpod 100% 25 / 25 totaltotal 97.5% 1080 / 1107 CC 68 SCAR 7.9
CC
68
Cov
97%
__ANON__
13.0
_copy_value
8.1
_init
8.0
CRAP 2.2
« blib/lib/DBM/Deep/Engine.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Scalar.pm »
1 package DBM::Deep;
2  
3 85 ▶use 5.006_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 85 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 72 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 54 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 49 our $VERSION = q(1.0019_003);
10  
11 54 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 use overload
14 100440 ▶ '""' => sub { overload::StrVal( $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
15 52 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 51 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 51 ▶use DBM::Deep::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3659 ▶sub TYPE_HASH () { DBM::Deep::Engine->SIG_HASH }
Subroutine TYPE_HASH: called CC=1 SCAR=0.0
Pod documented
22 4490 ▶sub TYPE_ARRAY () { DBM::Deep::Engine->SIG_ARRAY }
Subroutine TYPE_ARRAY: called CC=1 SCAR=0.0
Pod documented
23  
24 # This is used in all the children of this class in their TIE<type> methods.
25 sub _get_args {
26 5597 ▶ my $proto = shift;
Subroutine _get_args: called CC=6 SCAR=17.9
27  
28 5597 my $args;
29 5597 ▶ if (scalar(@_) > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ > 1) { } 3045 2549 5594
elsif (ref $_[0]) { } 2526 23 2549
30 3048 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 2 3043 3045
31 5 $proto->_throw_error( "Odd number of parameters to " . (caller(1))[2] );
32 }
33 3046 $args = {@_};
34 }
35 elsif ( ref $_[0] ) {
36 2529 ▶ unless ( eval { local $SIG{'__DIE__'}; %{$_[0]} || 1 } ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (eval { do { local $SIG{"__DIE__"}; %{$_[0];} or 1 } }) 2 2524 2526
%{$_[0];} or 1 0 2524 2524
37 5 $proto->_throw_error( "Not a hashref in args to " . (caller(1))[2] );
38 }
39 2527 $args = $_[0];
40 }
41 else {
42 26 $args = { file => shift };
43 }
44  
45 5593 return $args;
46 }
47  
48 # Class constructor method for Perl OO interface.
49 # Calls tie() and returns blessed reference to tied hash or array,
50 # providing a hybrid OO/tie interface.
51 sub new {
52 2695 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
53 2695 my $args = $class->_get_args( @_ );
54 2695 my $self;
55  
56 2695 ▶ if (defined($args->{type}) && $args->{type} eq TYPE_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'type'} and $args->{'type'} eq &TYPE_ARRAY()) { } 2099 593 2692
Condition100%3/3
defined $args->{"type"} and $args->{"type"} eq &TYPE_ARRAY()
!ll&&!rl&&r
3572362099
MC/DC100%2/2
defined $args->{"type"} and $args->{"type"} eq &TYPE_ARRAY()
defined $args->{"type"} $args->{"type"} eq &TYPE_ARRAY()
Truth table100%3/3
A and B
A= defined $args->{"type"}B= $args->{"type"} eq &TYPE_ARRAY()
ABresult
0X0
100
111
57 2102 $class = 'DBM::Deep::Array';
58 2102 require DBM::Deep::Array;
59 2102 tie @$self, $class, %$args;
60 }
61 else {
62 596 $class = 'DBM::Deep::Hash';
63 596 require DBM::Deep::Hash;
64 596 tie %$self, $class, %$args;
65 }
66  
67 2688 return bless $self, $class;
68 }
69  
70 # This initializer is called from the various TIE* methods. new() calls tie(),
71 # which allows for a single point of entry.
72 sub _init {
73 2901 ▶ my $class = shift;
Subroutine _init: called CC=8 SCAR=20.8
74 2901 my ($args) = @_;
75  
76 # locking implicitly enables autoflush
77 2901 ▶ if ($args->{locking}) { $args->{autoflush} = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"locking"}) 20 2878 2898
78  
79 # These are the defaults to be optionally overridden below
80 2899 my $self = bless {
81 type => TYPE_HASH,
82 base_offset => undef,
83 staleness => undef,
84 engine => undef,
85 }, $class;
86  
87 2899 ▶ unless ( exists $args->{engine} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"engine"}) 382 2516 2898
88 my $class = exists $args->{dbi}
89 383 ▶ ? 'DBM::Deep::Engine::DBI'
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{'dbi'} ? : 0 382 382
90 : 'DBM::Deep::Engine::File';
91  
92 50 ▶ eval "use $class"; die $@ if $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 382 382
93 $args->{engine} = $class->new({
94 383 %{$args},
95 obj => $self,
96 });
97 }
98  
99 # Grab the parameters we want to use
100 2898 foreach my $param ( keys %$self ) {
101 11589 ▶ next unless exists $args->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$param} 762 10826 11588
102 10827 $self->{$param} = $args->{$param};
103 }
104  
105 2898 eval {
106 2898 local $SIG{'__DIE__'};
107  
108 2898 $self->lock_exclusive;
109 2898 $self->_engine->setup( $self );
110 2888 $self->unlock;
111 2898 ▶ }; if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 10 2887 2897
112 11 my $e = $@;
113 11 eval { local $SIG{'__DIE__'}; $self->unlock; };
114 11 die $e;
115 }
116  
117 2888 return $self;
118 }
119  
120 sub TIEHASH {
121 165 ▶ shift;
Subroutine TIEHASH: called CC=1 SCAR=0.0
122 165 require DBM::Deep::Hash;
123 165 return DBM::Deep::Hash->TIEHASH( @_ );
124 }
125  
126 sub TIEARRAY {
127 47 ▶ shift;
Subroutine TIEARRAY: called CC=1 SCAR=0.0
128 47 require DBM::Deep::Array;
129 47 return DBM::Deep::Array->TIEARRAY( @_ );
130 }
131  
132 sub lock_exclusive {
133 5617 ▶ my $self = shift->_get_self;
Subroutine lock_exclusive: called CC=1 SCAR=0.0
Pod documented
134 5617 return $self->_engine->lock_exclusive( $self, @_ );
135 }
136 49 *lock = \&lock_exclusive;
137 sub lock_shared {
138 4434 ▶ my $self = shift->_get_self;
Subroutine lock_shared: called CC=2 SCAR=6.9
Pod documented
139 51 ▶use Carp qw( cluck ); use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
140 4434 ▶cluck Dumper($self) unless $self->_engine;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_engine 0 4433 4433
141 4434 return $self->_engine->lock_shared( $self, @_ );
142 }
143  
144 sub unlock {
145 10051 ▶ my $self = shift->_get_self;
Subroutine unlock: called CC=1 SCAR=0.0
Pod documented
146 10051 return $self->_engine->unlock( $self, @_ );
147 }
148  
149 sub _copy_value {
150 71 ▶ my $self = shift->_get_self;
Subroutine _copy_value: called CC=8 SCAR=20.9
151 71 my ($spot, $value) = @_;
152  
153 71 ▶ if ( !ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value) { } 38 32 70
154 39 ${$spot} = $value;
155 }
156 else {
157 33 my $r = Scalar::Util::reftype( $value );
158 33 my $tied;
159 33 ▶ if ( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 19 13 32
elsif ($r eq 'HASH') { } 13 0 13
160 20 $tied = tied(@$value);
161 }
162 elsif ( $r eq 'HASH' ) {
163 14 $tied = tied(%$value);
164 }
165 else {
166 1 __PACKAGE__->_throw_error( "Unknown type for '$value'" );
167 }
168  
169 33 ▶ if ( eval { local $SIG{'__DIE__'}; $tied->isa( __PACKAGE__ ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { local $SIG{'__DIE__'}; $tied->isa(__PACKAGE__) } }) { } 32 0 32
170 33 ${$spot} = $tied->_repr;
171 33 $tied->_copy_node( ${$spot} );
172 }
173 else {
174 1 ▶ if ( $r eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 0 0 0
175 1 ${$spot} = [ @{$value} ];
176 }
177 else {
178 1 ${$spot} = { %{$value} };
179 }
180 }
181  
182 33 my $c = Scalar::Util::blessed( $value );
183 33 ▶ if ( defined $c && !$c->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and not $c->isa(__PACKAGE__)) 16 16 32
Condition66%2/3
defined $c and not $c->isa(__PACKAGE__)
!ll&&!rl&&r
01616
MC/DC50%1/2
defined $c and not $c->isa(__PACKAGE__)
defined $c not $c->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $cB= not $c->isa(__PACKAGE__)
ABresult
0X0
100
111
184 17 ${$spot} = bless ${$spot}, $c
185 }
186 }
187  
188 71 return 1;
189 }
190  
191 #sub _copy_node {
192 # die "Must be implemented in a child class\n";
193 #}
194 #
195 #sub _repr {
196 # die "Must be implemented in a child class\n";
197 #}
198  
199 sub export {
200 11 ▶ my $self = shift->_get_self;
Subroutine export: called CC=2 SCAR=6.9
Pod documented
201  
202 11 my $temp = $self->_repr;
203  
204 11 $self->lock_exclusive;
205 11 $self->_copy_node( $temp );
206 11 $self->unlock;
207  
208 11 my $classname = $self->_engine->get_classname( $self );
209 11 ▶ if ( defined $classname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $classname) 4 6 10
210 5 bless $temp, $classname;
211 }
212  
213 11 return $temp;
214 }
215  
216 sub _check_legality {
217 111 ▶ my $self = shift;
Subroutine _check_legality: called CC=5 SCAR=16.1
218 111 my ($val) = @_;
219  
220 111 my $r = Scalar::Util::reftype( $val );
221  
222 111 ▶ return $r if !defined $r || '' eq $r;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $r or "" eq $r 39 71 110
Condition66%2/3
not defined $r or "" eq $r
l!l&&r!l&&!r
39071
MC/DC50%1/2
not defined $r or "" eq $r
not defined $r "" eq $r
Truth table66%2/3
not(A) or B
A= not defined $rB= "" eq $r
ABresult
1X1
011
000
223 72 ▶ return $r if 'HASH' eq $r;
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq $r 35 36 71
224 37 ▶ return $r if 'ARRAY' eq $r;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq $r 33 3 36
225  
226 4 __PACKAGE__->_throw_error(
227 "Storage of references of type '$r' is not supported."
228 );
229 }
230  
231 sub import {
232 60 ▶ return if !ref $_[0]; # Perl calls import() on use -- ignore
Subroutine import: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] 47 12 59
233  
234 13 my $self = shift->_get_self;
235 13 my ($struct) = @_;
236  
237 13 my $type = $self->_check_legality( $struct );
238 13 ▶ if ( !$type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 2 10 12
239 3 __PACKAGE__->_throw_error( "Cannot import a scalar" );
240 }
241  
242 11 ▶ if ( substr( $type, 0, 1 ) ne $self->_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($type, 0, 1) ne $self->_type) 2 8 10
243 3 ▶ __PACKAGE__->_throw_error(
Branch: 2 branches
BranchTrueFalseTotal
'HASH' eq $type ? : 1 1 2
'HASH' eq $type ? : 1 1 2
244 "Cannot import " . ('HASH' eq $type ? 'a hash' : 'an array')
245 . " into " . ('HASH' eq $type ? 'an array' : 'a hash')
246 );
247 }
248  
249 9 my %seen;
250 my $recurse;
251 $recurse = sub {
252 34 ▶ my ($db, $val) = @_;
Subroutine __ANON__: called CC=13 SCAR=25.7
253  
254 34 ▶ my $obj = 'HASH' eq Scalar::Util::reftype( $db ) ? tied(%$db) : tied(@$db);
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq builtin::reftype($db) ? : 19 14 33
255 34 ▶ $obj ||= $db;
Condition66%2/3
$obj ||= $db
l!l&&r!l&&!r
2580
MC/DC0%0/2
$obj ||= $db
$obj $db
Truth table66%2/3
A ||= B
A= $objB= $db
ABresult
1X1
011
000
256  
257 34 my $r = $self->_check_legality( $val );
258 34 ▶ if ( 'HASH' eq $r ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq $r) { } 17 16 33
elsif ('ARRAY' eq $r) { } 16 0 16
259 18 ▶ while ( my ($k, $v) = each %$val ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$val) 36 15 51
260 37 my $r = $self->_check_legality( $v );
261 36 ▶ if ( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 18 17 35
262 19 ▶ my $temp = 'HASH' eq $r ? {} : [];
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq $r ? : 9 9 18
263 19 ▶ if ( my $c = Scalar::Util::blessed( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = builtin::blessed($v)) 8 10 18
264 9 bless $temp, $c;
265 }
266 19 $obj->put( $k, $temp );
267 19 $recurse->( $temp, $v );
268 }
269 else {
270 18 $obj->put( $k, $v );
271 }
272 }
273 }
274 elsif ( 'ARRAY' eq $r ) {
275 17 foreach my $k ( 0 .. $#$val ) {
276 30 my $v = $val->[$k];
277 30 my $r = $self->_check_legality( $v );
278 28 ▶ if ( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 7 20 27
279 8 ▶ my $temp = 'HASH' eq $r ? {} : [];
Branch: 1 branch
BranchTrueFalseTotal
'HASH' eq $r ? : 2 5 7
280 8 ▶ if ( my $c = Scalar::Util::blessed( $v ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = builtin::blessed($v)) 3 4 7
281 4 bless $temp, $c;
282 }
283 8 $obj->put( $k, $temp );
284 8 $recurse->( $temp, $v );
285 }
286 else {
287 21 $obj->put( $k, $v );
288 }
289 }
290 }
291 9 };
292 9 $recurse->( $self, $struct );
293  
294 6 return 1;
295 }
296  
297 #XXX Need to keep track of who has a fh to this file in order to
298 #XXX close them all prior to optimize on Win32/cygwin
299 # Rebuild entire database into new file, then move
300 # it back on top of original.
301 sub optimize {
302 3 ▶ my $self = shift->_get_self;
Subroutine optimize: called CC=5 SCAR=16.2
Pod documented
303  
304 # Optimizing is only something we need to do when we're working with our
305 # own file format. Otherwise, let the other guy do the optimizations.
306 3 ▶ return unless $self->_engine->isa( 'DBM::Deep::Engine::File' );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_engine->isa("DBM::Deep::Engine::File") 0 2 2
307  
308 #XXX Need to create a new test for this
309 # if ($self->_engine->storage->{links} > 1) {
310 # $self->_throw_error("Cannot optimize: reference count is greater than 1");
311 # }
312  
313 #XXX Do we have to lock the tempfile?
314  
315 #XXX Should we use tempfile() here instead of a hard-coded name?
316 3 my $temp_filename = $self->_engine->storage->{file} . '.tmp';
317 my $db_temp = __PACKAGE__->new(
318 file => $temp_filename,
319 type => $self->_type,
320  
321 # Bring over all the parameters that we need to bring over
322 3 ( map { $_ => $self->_engine->$_ } qw(
323 byte_size max_buckets data_sector_size num_txns
324 )),
325 );
326  
327 3 $self->lock_exclusive;
328 3 $self->_engine->clear_cache;
329 3 $self->_copy_node( $db_temp );
330 3 $self->unlock;
331 3 $db_temp->_engine->storage->close;
332 3 undef $db_temp;
333  
334 ##
335 # Attempt to copy user, group and permissions over to new file
336 ##
337 3 $self->_engine->storage->copy_stats( $temp_filename );
338  
339 # q.v. perlport for more information on this variable
340 3 ▶ if ( $^O eq 'MSWin32' || $^O eq 'cygwin' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32" or $^O eq "cygwin") 0 2 2
Condition33%1/3
$^O eq "MSWin32" or $^O eq "cygwin"
l!l&&r!l&&!r
002
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
341 ##
342 # Potential race condition when optmizing on Win32 with locking.
343 # The Windows filesystem requires that the filehandle be closed
344 # before it is overwritten with rename(). This could be redone
345 # with a soft copy.
346 ##
347 1 $self->unlock;
348 1 $self->_engine->storage->close;
349 }
350  
351 3 ▶ if (!rename $temp_filename, $self->_engine->storage->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $temp_filename, $self->_engine->storage->{"file"}) 0 2 2
352 1 unlink $temp_filename;
353 1 $self->unlock;
354 1 $self->_throw_error("Optimize failed: Cannot copy temp file over original: $!");
355 }
356  
357 3 $self->unlock;
358 3 $self->_engine->storage->close;
359  
360 3 $self->_engine->storage->open;
361 3 $self->lock_exclusive;
362 3 $self->_engine->setup( $self );
363 3 $self->unlock;
364  
365 3 return 1;
366 }
367  
368 sub clone {
369 2 ▶ my $self = shift->_get_self;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
370  
371 2 return __PACKAGE__->new(
372 type => $self->_type,
373 base_offset => $self->_base_offset,
374 staleness => $self->_staleness,
375 engine => $self->_engine,
376 );
377 }
378  
379 sub supports {
380 10 ▶ my $self = shift->_get_self;
Subroutine supports: called CC=1 SCAR=0.0
Pod documented
381 10 return $self->_engine->supports( @_ );
382 }
383  
384 #XXX Migrate this to the engine, where it really belongs and go through some
385 # API - stop poking in the innards of someone else..
386 {
387 my %is_legal_filter = map {
388 49 $_ => ~~1,
389 } qw(
390 store_key store_value
391 fetch_key fetch_value
392 );
393  
394 sub set_filter {
395 10 ▶ my $self = shift->_get_self;
Subroutine set_filter: called CC=2 SCAR=6.9
Pod documented
396 10 my $type = lc shift;
397 10 my $func = shift;
398  
399 10 ▶ if ( $is_legal_filter{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_legal_filter{$type}) 8 1 9
400 9 $self->_engine->storage->{"filter_$type"} = $func;
401 9 return 1;
402 }
403  
404 2 return;
405 }
406  
407 2 ▶ sub filter_store_key { $_[0]->set_filter( store_key => $_[1] ); }
Subroutine filter_store_key: called CC=1 SCAR=0.0
Pod documented
408 2 ▶ sub filter_store_value { $_[0]->set_filter( store_value => $_[1] ); }
Subroutine filter_store_value: called CC=1 SCAR=0.0
Pod documented
409 2 ▶ sub filter_fetch_key { $_[0]->set_filter( fetch_key => $_[1] ); }
Subroutine filter_fetch_key: called CC=1 SCAR=0.0
Pod documented
410 2 ▶ sub filter_fetch_value { $_[0]->set_filter( fetch_value => $_[1] ); }
Subroutine filter_fetch_value: called CC=1 SCAR=0.0
Pod documented
411 }
412  
413 49 sub begin_work {
414 277 ▶ my $self = shift->_get_self;
Subroutine begin_work: called CC=2 SCAR=6.9
Pod documented
415 277 $self->lock_exclusive;
416 277 my $rv = eval {
417 277 local $SIG{'__DIE__'};
418 277 $self->_engine->begin_work( $self, @_ );
419 };
420 277 my $e = $@;
421 277 $self->unlock;
422 277 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 275 276
423 276 return $rv;
424 }
425  
426 sub rollback {
427 13 ▶ my $self = shift->_get_self;
Subroutine rollback: called CC=2 SCAR=6.9
Pod documented
428  
429 13 $self->lock_exclusive;
430 13 my $rv = eval {
431 13 local $SIG{'__DIE__'};
432 13 $self->_engine->rollback( $self, @_ );
433 };
434 13 my $e = $@;
435 13 $self->unlock;
436 13 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 11 12
437 12 return $rv;
438 }
439  
440 sub commit {
441 11 ▶ my $self = shift->_get_self;
Subroutine commit: called CC=2 SCAR=6.9
Pod documented
442 11 $self->lock_exclusive;
443 11 my $rv = eval {
444 11 local $SIG{'__DIE__'};
445 11 $self->_engine->commit( $self, @_ );
446 };
447 11 my $e = $@;
448 11 $self->unlock;
449 11 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 1 9 10
450 10 return $rv;
451 }
452  
453 # Accessor methods
454 sub _engine {
455 43887 ▶ my $self = $_[0]->_get_self;
Subroutine _engine: called CC=1 SCAR=0.0
456 43887 return $self->{engine};
457 }
458  
459 sub _type {
460 391 ▶ my $self = $_[0]->_get_self;
Subroutine _type: called CC=1 SCAR=0.0
461 391 return $self->{type};
462 }
463  
464 sub _base_offset {
465 10554 ▶ my $self = $_[0]->_get_self;
Subroutine _base_offset: called CC=1 SCAR=0.0
466 10554 return $self->{base_offset};
467 }
468  
469 sub _staleness {
470 5335 ▶ my $self = $_[0]->_get_self;
Subroutine _staleness: called CC=1 SCAR=0.0
471 5335 return $self->{staleness};
472 }
473  
474 # Utility methods
475 sub _throw_error {
476 53 ▶ my $n = 0;
Subroutine _throw_error: called CC=2 SCAR=6.9
477 53 while( 1 ) {
478 166 my @caller = caller( ++$n );
479 166 ▶ next if $caller[0] =~ m/^DBM::Deep/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] =~ /^DBM::Deep/ 113 52 165
480  
481 53 die "DBM::Deep: $_[1] at $0 line $caller[2]\n";
482 }
483 }
484  
485 # Store single hash key/value or array element in database.
486 sub STORE {
487 1728 ▶ my $self = shift->_get_self;
Subroutine STORE: called CC=5 SCAR=16.1
488 1728 my ($key, $value) = @_;
489 1728 warn "STORE($self, '$key', '@{[defined$value?$value:'undef']}')\n" if DEBUG;
490  
491 1728 ▶ unless ( $self->_engine->storage->is_writable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_engine->storage->is_writable) 2 1725 1727
492 3 $self->_throw_error( 'Cannot write to a readonly filehandle' );
493 }
494  
495 1726 $self->lock_exclusive;
496  
497 # User may be storing a complex value, in which case we do not want it run
498 # through the filtering system.
499 1726 ▶ if ( !ref($value) && $self->_engine->storage->{filter_store_value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $value and $self->_engine->storage->{"filter_store_value"}) 2 1723 1725
Condition100%3/3
not ref $value and $self->_engine->storage->{"filter_store_value"}
!ll&&!rl&&r
12115122
MC/DC100%2/2
not ref $value and $self->_engine->storage->{"filter_store_value"}
not ref $value $self->_engine->storage->{"filter_store_value"}
Truth table100%3/3
not(A) and B
A= not ref $valueB= $self->_engine->storage->{"filter_store_value"}
ABresult
0X0
100
111
500 3 $value = $self->_engine->storage->{filter_store_value}->( $value );
501 }
502  
503 1726 eval {
504 1726 local $SIG{'__DIE__'};
505 1726 $self->_engine->write_value( $self, $key, $value );
506 1726 ▶ }; if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 10 1715 1725
507 11 $self->unlock;
508 11 die $e;
509 }
510  
511 1716 $self->unlock;
512  
513 1716 return 1;
514 }
515  
516 # Fetch single value or element given plain key or array index
517 sub FETCH {
518 3048 ▶ my $self = shift->_get_self;
Subroutine FETCH: called CC=4 SCAR=13.9
519 3048 my ($key) = @_;
520 3048 warn "FETCH($self, '$key')\n" if DEBUG;
521  
522 3048 $self->lock_shared;
523  
524 3048 my $result = $self->_engine->read_value( $self, $key );
525  
526 3048 $self->unlock;
527  
528 # Filters only apply to scalar values, so the ref check is making
529 # sure the fetched bucket is a scalar, not a child hash or array.
530 return ($result && !ref($result) && $self->_engine->storage->{filter_fetch_value})
531 3048 ▶ ? $self->_engine->storage->{filter_fetch_value}->($result)
Branch: 1 branch
BranchTrueFalseTotal
$result && !ref($result) && $self->_engine->storage->{'filter_fetch_value'} ? : 2 3045 3047
Condition100%6/6
$result and not ref $result and $self->_engine->storage->{"filter_fetch_value"}
!ll&&!rl&&r
23926532
$result and not ref $result
!ll&&!rl&&r
772315655
MC/DC100%3/3
$result and not ref $result and $self->_engine->storage->{"filter_fetch_value"}
$result not ref $result $self->_engine->storage->{"filter_fetch_value"}
Truth table100%4/4
A and not(B) and C
A= $resultB= not ref $resultC= $self->_engine->storage->{"filter_fetch_value"}
ABCresult
0XX0
10X0
1100
1111
532 : $result;
533 }
534  
535 # Delete single key/value pair or element given plain key or array index
536 sub DELETE {
537 50 ▶ my $self = shift->_get_self;
Subroutine DELETE: called CC=5 SCAR=16.1
538 50 my ($key) = @_;
539 50 warn "DELETE($self, '$key')\n" if DEBUG;
540  
541 50 ▶ unless ( $self->_engine->storage->is_writable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_engine->storage->is_writable) 0 49 49
542 1 $self->_throw_error( 'Cannot write to a readonly filehandle' );
543 }
544  
545 50 $self->lock_exclusive;
546  
547 ##
548 # Delete bucket
549 ##
550 50 my $value = $self->_engine->delete_key( $self, $key);
551  
552 50 ▶ if (defined $value && !ref($value) && $self->_engine->storage->{filter_fetch_value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $self->_engine->storage->{"filter_fetch_value"}) 1 48 49
Condition100%6/6
defined $value and not ref $value and $self->_engine->storage->{"filter_fetch_value"}
!ll&&!rl&&r
20281
defined $value and not ref $value
!ll&&!rl&&r
14629
MC/DC100%3/3
defined $value and not ref $value and $self->_engine->storage->{"filter_fetch_value"}
defined $value not ref $value $self->_engine->storage->{"filter_fetch_value"}
Truth table100%4/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $self->_engine->storage->{"filter_fetch_value"}
ABCresult
0XX0
10X0
1100
1111
553 2 $value = $self->_engine->storage->{filter_fetch_value}->($value);
554 }
555  
556 50 $self->unlock;
557  
558 50 return $value;
559 }
560  
561 # Check if a single key or element exists given plain key or array index
562 sub EXISTS {
563 129 ▶ my $self = shift->_get_self;
Subroutine EXISTS: called CC=1 SCAR=0.0
564 129 my ($key) = @_;
565 129 warn "EXISTS($self, '$key')\n" if DEBUG;
566  
567 129 $self->lock_shared;
568  
569 129 my $result = $self->_engine->key_exists( $self, $key );
570  
571 129 $self->unlock;
572  
573 129 return $result;
574 }
575  
576 # Clear all keys from hash, or all elements from array.
577 sub CLEAR {
578 203 ▶ my $self = shift->_get_self;
Subroutine CLEAR: called CC=4 SCAR=14.0
579 203 warn "CLEAR($self)\n" if DEBUG;
580  
581 203 my $engine = $self->_engine;
582 203 ▶ unless ( $engine->storage->is_writable ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($engine->storage->is_writable) 0 202 202
583 1 $self->_throw_error( 'Cannot write to a readonly filehandle' );
584 }
585  
586 203 $self->lock_exclusive;
587 203 eval {
588 203 local $SIG{'__DIE__'};
589 203 $engine->clear( $self );
590 };
591 203 my $e = $@;
592 203 ▶ warn "$e\n" if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 202 202
593  
594 203 $self->unlock;
595  
596 203 ▶ die $e if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 0 202 202
597  
598 203 return 1;
599 }
600  
601 # Public method aliases
602 78 ▶sub put { (shift)->STORE( @_ ) }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
603 96 ▶sub get { (shift)->FETCH( @_ ) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
604 8 ▶sub store { (shift)->STORE( @_ ) }
Subroutine store: called CC=1 SCAR=0.0
Pod documented
605 19 ▶sub fetch { (shift)->FETCH( @_ ) }
Subroutine fetch: called CC=1 SCAR=0.0
Pod documented
606 11 ▶sub delete { (shift)->DELETE( @_ ) }
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
607 15 ▶sub exists { (shift)->EXISTS( @_ ) }
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
608 7 ▶sub clear { (shift)->CLEAR( @_ ) }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
609  
610 3 ▶sub _dump_file {shift->_get_self->_engine->_dump_file;}
Subroutine _dump_file: called CC=1 SCAR=0.0
611  
612 49 1;
613 __END__
« blib/lib/DBM/Deep/Engine.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Scalar.pm »