UNSHIFT and PUSH now return the length of the new array
[dbsrgits/DBM-Deep.git] / lib / DBM / Deep / Hash.pm
CommitLineData
6fe26b29 1package DBM::Deep::Hash;
2
3use strict;
4
5use base 'DBM::Deep';
6
596e9574 7sub _get_self {
2ac02042 8 eval { tied( %{$_[0]} ) } || $_[0]
596e9574 9}
10
6fe26b29 11sub TIEHASH {
12 ##
13 # Tied hash constructor method, called by Perl's tie() function.
14 ##
15 my $class = shift;
16 my $args;
17 if (scalar(@_) > 1) { $args = {@_}; }
18 #XXX This use of ref() is bad and is a bug
19 elsif (ref($_[0])) { $args = $_[0]; }
20 else { $args = { file => shift }; }
21
22 $args->{type} = $class->TYPE_HASH;
23
24 return $class->_init($args);
25}
26
81d3d316 27sub STORE {
28 my $self = shift->_get_self;
29 my $key = ($self->root->{filter_store_key})
30 ? $self->root->{filter_store_key}->($_[0])
31 : $_[0];
32 my $value = $_[1];
33
34 return $self->SUPER::STORE( $key, $value );
35}
36
6fe26b29 37sub FIRSTKEY {
38 ##
39 # Locate and return first key (in no particular order)
40 ##
2ac02042 41 my $self = $_[0]->_get_self;
6fe26b29 42
43 ##
44 # Make sure file is open
45 ##
46 if (!defined($self->fh)) { $self->_open(); }
47
48 ##
49 # Request shared lock for reading
50 ##
51 $self->lock( $self->LOCK_SH );
52
53 my $result = $self->_get_next_key();
54
55 $self->unlock();
56
57 return ($result && $self->root->{filter_fetch_key})
58 ? $self->root->{filter_fetch_key}->($result)
59 : $result;
60}
61
62sub NEXTKEY {
63 ##
64 # Return next key (in no particular order), given previous one
65 ##
2ac02042 66 my $self = $_[0]->_get_self;
6fe26b29 67
68 my $prev_key = ($self->root->{filter_store_key})
69 ? $self->root->{filter_store_key}->($_[1])
70 : $_[1];
71
72 my $prev_md5 = $DBM::Deep::DIGEST_FUNC->($prev_key);
73
74 ##
75 # Make sure file is open
76 ##
77 if (!defined($self->fh)) { $self->_open(); }
78
79 ##
80 # Request shared lock for reading
81 ##
82 $self->lock( $self->LOCK_SH );
83
84 my $result = $self->_get_next_key( $prev_md5 );
85
86 $self->unlock();
87
88 return ($result && $self->root->{filter_fetch_key})
89 ? $self->root->{filter_fetch_key}->($result)
90 : $result;
91}
92
93##
94# Public method aliases
95##
96*first_key = *FIRSTKEY;
97*next_key = *NEXTKEY;
98
991;
100__END__