Commit | Line | Data |
---|---|---|
6fe26b29 | 1 | package DBM::Deep::Hash; |
2 | ||
2120a181 | 3 | use 5.006_000; |
460b1067 | 4 | |
6fe26b29 | 5 | use strict; |
065b45be | 6 | use warnings FATAL => 'all'; |
8385c429 | 7 | no warnings 'recursion'; |
29460253 | 8 | |
6fe26b29 | 9 | use base 'DBM::Deep'; |
10 | ||
596e9574 | 11 | sub _get_self { |
a4e2db58 | 12 | eval { local $SIG{'__DIE__'}; tied( %{$_[0]} ) } || $_[0] |
596e9574 | 13 | } |
14 | ||
e00d0eb3 | 15 | sub _repr { return {} } |
f9c33187 | 16 | |
6fe26b29 | 17 | sub TIEHASH { |
6fe26b29 | 18 | my $class = shift; |
0ca7ea98 | 19 | my $args = $class->_get_args( @_ ); |
6fe26b29 | 20 | |
21 | $args->{type} = $class->TYPE_HASH; | |
22 | ||
23 | return $class->_init($args); | |
24 | } | |
25 | ||
cb79ec85 | 26 | sub FETCH { |
27 | my $self = shift->_get_self; | |
2120a181 | 28 | DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; |
f1879fdc | 29 | my $key = ($self->_engine->storage->{filter_store_key}) |
30 | ? $self->_engine->storage->{filter_store_key}->($_[0]) | |
cb79ec85 | 31 | : $_[0]; |
32 | ||
359a01ac | 33 | return $self->SUPER::FETCH( $key, $_[0] ); |
cb79ec85 | 34 | } |
35 | ||
81d3d316 | 36 | sub STORE { |
37 | my $self = shift->_get_self; | |
2120a181 | 38 | DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; |
f1879fdc | 39 | my $key = ($self->_engine->storage->{filter_store_key}) |
40 | ? $self->_engine->storage->{filter_store_key}->($_[0]) | |
81d3d316 | 41 | : $_[0]; |
42 | my $value = $_[1]; | |
43 | ||
359a01ac | 44 | return $self->SUPER::STORE( $key, $value, $_[0] ); |
81d3d316 | 45 | } |
46 | ||
baa27ab6 | 47 | sub EXISTS { |
48 | my $self = shift->_get_self; | |
2120a181 | 49 | DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; |
f1879fdc | 50 | my $key = ($self->_engine->storage->{filter_store_key}) |
51 | ? $self->_engine->storage->{filter_store_key}->($_[0]) | |
baa27ab6 | 52 | : $_[0]; |
53 | ||
54 | return $self->SUPER::EXISTS( $key ); | |
55 | } | |
56 | ||
feaf1e6f | 57 | sub DELETE { |
58 | my $self = shift->_get_self; | |
2120a181 | 59 | DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; |
f1879fdc | 60 | my $key = ($self->_engine->storage->{filter_store_key}) |
61 | ? $self->_engine->storage->{filter_store_key}->($_[0]) | |
feaf1e6f | 62 | : $_[0]; |
63 | ||
359a01ac | 64 | return $self->SUPER::DELETE( $key, $_[0] ); |
feaf1e6f | 65 | } |
66 | ||
d426259c | 67 | # Locate and return first key (in no particular order) |
6fe26b29 | 68 | sub FIRSTKEY { |
eea0d863 | 69 | my $self = shift->_get_self; |
6fe26b29 | 70 | |
5c0756fc | 71 | $self->lock_shared; |
1cff45d7 | 72 | |
73 | my $result = $self->_engine->get_next_key( $self ); | |
74 | ||
9c87a079 | 75 | $self->unlock; |
1cff45d7 | 76 | |
f1879fdc | 77 | return ($result && $self->_engine->storage->{filter_fetch_key}) |
78 | ? $self->_engine->storage->{filter_fetch_key}->($result) | |
6fe26b29 | 79 | : $result; |
80 | } | |
81 | ||
d426259c | 82 | # Return next key (in no particular order), given previous one |
6fe26b29 | 83 | sub NEXTKEY { |
eea0d863 | 84 | my $self = shift->_get_self; |
6fe26b29 | 85 | |
f1879fdc | 86 | my $prev_key = ($self->_engine->storage->{filter_store_key}) |
87 | ? $self->_engine->storage->{filter_store_key}->($_[0]) | |
eea0d863 | 88 | : $_[0]; |
6fe26b29 | 89 | |
5c0756fc | 90 | $self->lock_shared; |
1cff45d7 | 91 | |
92 | my $result = $self->_engine->get_next_key( $self, $prev_key ); | |
93 | ||
9c87a079 | 94 | $self->unlock; |
95 | ||
f1879fdc | 96 | return ($result && $self->_engine->storage->{filter_fetch_key}) |
97 | ? $self->_engine->storage->{filter_fetch_key}->($result) | |
6fe26b29 | 98 | : $result; |
99 | } | |
100 | ||
f9c33187 | 101 | sub first_key { (shift)->FIRSTKEY(@_) } |
d426259c | 102 | sub next_key { (shift)->NEXTKEY(@_) } |
f9c33187 | 103 | |
cd5303b4 | 104 | sub _clear { |
39baa1fd | 105 | my $self = shift; |
cd5303b4 | 106 | |
e73f12ce | 107 | while ( defined(my $key = $self->first_key) ) { |
108 | do { | |
cd5303b4 | 109 | $self->_engine->delete_key( $self, $key, $key ); |
e73f12ce | 110 | } while defined($key = $self->next_key($key)); |
cd5303b4 | 111 | } |
112 | ||
113 | return; | |
114 | } | |
115 | ||
f9c33187 | 116 | sub _copy_node { |
898fd1fd | 117 | my $self = shift; |
f9c33187 | 118 | my ($db_temp) = @_; |
119 | ||
120 | my $key = $self->first_key(); | |
c11b7bfb | 121 | while (defined $key) { |
f9c33187 | 122 | my $value = $self->get($key); |
123 | $self->_copy_value( \$db_temp->{$key}, $value ); | |
124 | $key = $self->next_key($key); | |
125 | } | |
126 | ||
127 | return 1; | |
128 | } | |
6fe26b29 | 129 | |
130 | 1; | |
131 | __END__ |