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 50     50   1061 use 5.008_004;
  50         188  
4              
5 50     50   327 use strict;
  50         108  
  50         1527  
6 50     50   303 use warnings FATAL => 'all';
  50         94  
  50         2230  
7 50     50   327 no warnings 'recursion';
  50         96  
  50         2148  
8              
9 50     50   319 use base 'DBM::Deep';
  50         101  
  50         51899  
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 63097 50   63097   93855 tied %{$_[0]} or local *@, eval { exists $_[0]{_}; 1 } ? $_[0] : undef
  61651 100       96460  
  61651         191404  
  63097         215248  
18             }
19              
20 21     21   52 sub _repr { return {} }
21              
22             sub TIEHASH {
23 778     778   1636 my $class = shift;
24 778         2627 my $args = $class->_get_args( @_ );
25            
26 776         2480 $args->{type} = $class->TYPE_HASH;
27              
28 776         2595 return $class->_init($args);
29             }
30              
31             sub FETCH {
32 2619     2619   99445 my $self = shift->_get_self;
33 2619 100       6232 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
34             my $key = ($self->_engine->storage->{filter_store_key})
35 2617 100       6214 ? $self->_engine->storage->{filter_store_key}->($_[0])
36             : $_[0];
37              
38 2617         7511 return $self->SUPER::FETCH( $key, $_[0] );
39             }
40              
41             sub STORE {
42 1502     1502   41880 my $self = shift->_get_self;
43 1502 100       3525 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
44             my $key = ($self->_engine->storage->{filter_store_key})
45 1500 100       3702 ? $self->_engine->storage->{filter_store_key}->($_[0])
46             : $_[0];
47 1500         2620 my $value = $_[1];
48              
49 1500         4607 return $self->SUPER::STORE( $key, $value, $_[0] );
50             }
51              
52             sub EXISTS {
53 125     125   54061 my $self = shift->_get_self;
54 125 100       422 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       400 ? $self->_engine->storage->{filter_store_key}->($_[0])
57             : $_[0];
58              
59 123         492 return $self->SUPER::EXISTS( $key );
60             }
61              
62             sub DELETE {
63 46     46   4133 my $self = shift->_get_self;
64 46 100       156 DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0];
65             my $key = ($self->_engine->storage->{filter_store_key})
66 44 100       141 ? $self->_engine->storage->{filter_store_key}->($_[0])
67             : $_[0];
68              
69 44         234 return $self->SUPER::DELETE( $key, $_[0] );
70             }
71              
72             # Locate and return first key (in no particular order)
73             sub FIRSTKEY {
74 162     162   106526 my $self = shift->_get_self;
75              
76 162         659 $self->lock_shared;
77            
78 162         644 my $result = $self->_engine->get_next_key( $self );
79            
80 159         781 $self->unlock;
81            
82             return ($result && $self->_engine->storage->{filter_fetch_key})
83 159 100 100     888 ? $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   1098 my $self = shift->_get_self;
90              
91             my $prev_key = ($self->_engine->storage->{filter_store_key})
92 383 100       950 ? $self->_engine->storage->{filter_store_key}->($_[0])
93             : $_[0];
94              
95 383         1253 $self->lock_shared;
96            
97 383         1320 my $result = $self->_engine->get_next_key( $self, $prev_key );
98            
99 383         1522 $self->unlock;
100            
101             return ($result && $self->_engine->storage->{filter_fetch_key})
102 383 100 100     2654 ? $self->_engine->storage->{filter_fetch_key}->($result)
103             : $result;
104             }
105              
106 33     33 1 3515 sub first_key { (shift)->FIRSTKEY(@_) }
107 52     52 1 1733 sub next_key { (shift)->NEXTKEY(@_) }
108              
109             sub _clear {
110 1     1   2 my $self = shift;
111              
112 1         6 while ( defined(my $key = $self->first_key) ) {
113 1         3 do {
114 2         9 $self->_engine->delete_key( $self, $key, $key );
115             } while defined($key = $self->next_key($key));
116             }
117              
118 1         6 return;
119             }
120              
121             sub _copy_node {
122 23     23   47 my $self = shift;
123 23         49 my ($db_temp) = @_;
124              
125 23         66 my $key = $self->first_key();
126 23         79 while (defined $key) {
127 41         210 my $value = $self->get($key);
128 41         258 $self->_copy_value( \$db_temp->{$key}, $value );
129 41         118 $key = $self->next_key($key);
130             }
131              
132 23         64 return 1;
133             }
134              
135             1;
136             __END__