File Coverage

blib/lib/DBM/Deep/Hash.pm
Criterion Covered Total %
statement 66 66 100.0
branch 25 26 96.1
condition 6 6 100.0
subroutine 18 18 100.0
pod 2 2 100.0
total 117 118 99.1


line stmt bran cond sub pod time code
1             package DBM::Deep::Hash;
2              
3 54     54   1149 use 5.008_004;
  54         198  
4              
5 54     54   297 use strict;
  54         109  
  54         1894  
6 54     54   327 use warnings FATAL => 'all';
  54         104  
  54         4257  
7 54     54   295 no warnings 'recursion';
  54         214  
  54         2598  
8              
9 54     54   363 use base 'DBM::Deep';
  54         110  
  54         58057  
10              
11             sub _get_self {
12             # See the note in Array.pm as to why this is commented out.
13             # eval { local $SIG{'__DIE__'}; tied( %{$_[0]} ) } || $_[0]
14              
15             # During global destruction %{$_[0]} might get tied to undef, so we
16             # need to check that case if tied returns false.
17 67790 50   67790   118153 tied %{$_[0]} or local *@, eval { exists $_[0]{_}; 1 } ? $_[0] : undef
  66338 100       106113  
  66338         212469  
  67790         220153  
18             }
19              
20 21     21   59 sub _repr { return {} }
21              
22             sub TIEHASH {
23 790     790   1523 my $class = shift;
24 790         3046 my $args = $class->_get_args( @_ );
25            
26 788         2662 $args->{type} = $class->TYPE_HASH;
27              
28 788         2861 return $class->_init($args);
29             }
30              
31             sub FETCH {
32 2634     2634   156286 my $self = shift->_get_self;
33 2634 100       7108 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
34             my $key = ($self->_engine->storage->{filter_store_key})
35 2632 100       7684 ? $self->_engine->storage->{filter_store_key}->($_[0])
36             : $_[0];
37              
38 2632         9114 return $self->SUPER::FETCH( $key, $_[0] );
39             }
40              
41             sub STORE {
42 1868     1868   54515 my $self = shift->_get_self;
43 1868 100       7891 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
44             my $key = ($self->_engine->storage->{filter_store_key})
45 1866 100       4768 ? $self->_engine->storage->{filter_store_key}->($_[0])
46             : $_[0];
47 1866         3549 my $value = $_[1];
48              
49 1866         6510 return $self->SUPER::STORE( $key, $value, $_[0] );
50             }
51              
52             sub EXISTS {
53 125     125   40130 my $self = shift->_get_self;
54 125 100       353 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
55             my $key = ($self->_engine->storage->{filter_store_key})
56 123 100       322 ? $self->_engine->storage->{filter_store_key}->($_[0])
57             : $_[0];
58              
59 123         500 return $self->SUPER::EXISTS( $key );
60             }
61              
62             sub DELETE {
63 48     48   7382 my $self = shift->_get_self;
64 48 100       189 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
65             my $key = ($self->_engine->storage->{filter_store_key})
66 46 100       177 ? $self->_engine->storage->{filter_store_key}->($_[0])
67             : $_[0];
68              
69 46         285 return $self->SUPER::DELETE( $key, $_[0] );
70             }
71              
72             # Locate and return first key (in no particular order)
73             sub FIRSTKEY {
74 162     162   113802 my $self = shift->_get_self;
75              
76 162         713 $self->lock_shared;
77            
78 162         634 my $result = $self->_engine->get_next_key( $self );
79            
80 159         748 $self->unlock;
81            
82             return ($result && $self->_engine->storage->{filter_fetch_key})
83 159 100 100     900 ? $self->_engine->storage->{filter_fetch_key}->($result)
84             : $result;
85             }
86              
87             # Return next key (in no particular order), given previous one
88             sub NEXTKEY {
89 383     383   1944 my $self = shift->_get_self;
90              
91             my $prev_key = ($self->_engine->storage->{filter_store_key})
92 383 100       1129 ? $self->_engine->storage->{filter_store_key}->($_[0])
93             : $_[0];
94              
95 383         1293 $self->lock_shared;
96            
97 383         1218 my $result = $self->_engine->get_next_key( $self, $prev_key );
98            
99 383         1769 $self->unlock;
100            
101             return ($result && $self->_engine->storage->{filter_fetch_key})
102 383 100 100     3036 ? $self->_engine->storage->{filter_fetch_key}->($result)
103             : $result;
104             }
105              
106 33     33 1 4430 sub first_key { (shift)->FIRSTKEY(@_) }
107 52     52 1 2794 sub next_key { (shift)->NEXTKEY(@_) }
108              
109             sub _clear {
110 1     1   2 my $self = shift;
111              
112 1         4 while ( defined(my $key = $self->first_key) ) {
113 1         2 do {
114 2         7 $self->_engine->delete_key( $self, $key, $key );
115             } while defined($key = $self->next_key($key));
116             }
117              
118 1         4 return;
119             }
120              
121             sub _copy_node {
122 23     23   48 my $self = shift;
123 23         51 my ($db_temp) = @_;
124              
125 23         83 my $key = $self->first_key();
126 23         72 while (defined $key) {
127 41         178 my $value = $self->get($key);
128 41         343 $self->_copy_value( \$db_temp->{$key}, $value );
129 41         158 $key = $self->next_key($key);
130             }
131              
132 23         64 return 1;
133             }
134              
135             1;
136             __END__