File Coverage

blib/lib/Tie/MLDBM.pm
Criterion Covered Total %
statement 12 72 16.6
branch 0 12 0.0
condition n/a
subroutine 4 12 33.3
pod 0 1 0.0
total 16 97 16.4


line stmt bran cond sub pod time code
1             package Tie::MLDBM;
2              
3 1     1   75217 use Carp;
  1         3  
  1         80  
4 1     1   1017 use Tie::Hash;
  1         1052  
  1         28  
5              
6 1     1   7 use strict;
  1         7  
  1         45  
7 1     1   5 use vars qw/ @ISA $AUTOLOAD $VERSION /;
  1         2  
  1         1023  
8              
9             @ISA = qw/ Tie::Hash /;
10             $VERSION = '1.04';
11              
12              
13             sub AUTOLOAD {
14 0     0     my ( $self, @params ) = @_;
15              
16             # Parse method name from $AUTOLOAD variable and validate this method against
17             # a list of methods allowed to be accessed through the AUTOLOAD subroutine.
18              
19 0           my $method = $AUTOLOAD;
20 0           $method =~ s/.*:://;
21              
22 0           my @methods = qw/ EXISTS FIRSTKEY NEXTKEY /;
23              
24 0 0         unless ( grep /\Q$method\E/, @methods ) {
25              
26 0           croak( __PACKAGE__, '->AUTOLOAD : Unsupported method for tied object - ', $method );
27              
28             }
29              
30 0           $self->{'Modules'}->{'Lock'}->lock_shared;
31              
32 0           my $rv = $self->{ 'Store' }->$method( @params );
33              
34 0           $self->{'Modules'}->{'Lock'}->unlock;
35              
36 0           return $rv;
37             }
38              
39              
40             sub CLEAR {
41 0     0     my ( $self, @params ) = @_;
42              
43             # Acquire an exclusive lock, execute the CLEAR method on the tied hash and
44             # synchronise the tied hash. The synchronisation of this hash is performed
45             # by the Sync method of this class by way of re-opening the tied hash object
46             # and storing this object reference
47              
48 0           $self->{'Modules'}->{'Lock'}->lock_exclusive;
49              
50 0           my $rv = $self->{'Store'}->CLEAR( @params );
51              
52 0           $self->{'Modules'}->{'Lock'}->unlock;
53              
54 0           $self->Sync;
55              
56 0           return $rv;
57             }
58              
59              
60             sub DELETE {
61 0     0     my ( $self, @params ) = @_;
62              
63             # Acquire an exclusive lock and execute the DELETE method on the tied hash.
64              
65 0           $self->{'Modules'}->{'Lock'}->lock_exclusive;
66              
67 0           my $rv = $self->{'Store'}->DELETE( @params );
68              
69 0           $self->{'Modules'}->{'Lock'}->unlock;
70              
71 0           return $rv;
72             }
73              
74              
75             sub FETCH {
76 0     0     my ( $self, $key ) = @_;
77              
78             # Retrieve the value indexed by the passed key argument from the second-level
79             # tied hash. This fetched value is then deserialised using the serialisation
80             # component module of the Tie::MLDBM framework and returned to the calling
81             # process.
82              
83 0           $self->{'Modules'}->{'Lock'}->lock_shared;
84              
85 0           my $value = $self->{'Store'}->FETCH( $key );
86              
87 0           $self->{'Modules'}->{'Lock'}->unlock;
88              
89 0           return $self->{'Modules'}->{'Serialise'}->deserialise( $value );
90             }
91              
92              
93             sub STORE {
94 0     0     my ( $self, $key, $value ) = @_;
95              
96             # Serialise the passed value argument using the serialisation component
97             # module of the Tie::MLDBM framework. The result of this serialisation is
98             # stored in the second-level tied hash.
99              
100 0           $value = $self->{'Modules'}->{'Serialise'}->serialise( $value );
101            
102 0           $self->{'Modules'}->{'Lock'}->lock_exclusive;
103              
104 0           my $rv = $self->{'Store'}->STORE( $key, $value );
105              
106 0           $self->{'Modules'}->{'Lock'}->unlock;
107              
108 0           return $rv;
109             }
110              
111              
112             sub TIEHASH {
113 0     0     my ( $class, $args, @params ) = @_;
114 0           my $self = bless {}, $class;
115              
116             # The first argument to the TIEHASH object constructor should be a hash
117             # reference which contains configuration options for this framework. There
118             # is no strict checking of the elements of the passed hash so as to allow for
119             # the expansion of this framework and definition of additional configuration
120             # options for framework components.
121              
122 0 0         unless ( ref $args eq 'HASH' ) {
123              
124 0           croak( __PACKAGE__, '->TIEHASH : First argument to TIEHASH constructor should be a hash reference' );
125              
126             }
127              
128             # The following simply cleans up the keys of the passed argument hash so that
129             # all keys are word-like with an uppercase first character and all lowercase
130             # for the remaining characters.
131             #
132             # The result is stored in $self->{ 'Config' } so that these arguments can be
133             # accessed by component modules.
134              
135 0           $self->{'Config'} = { map { ucfirst lc $_ => delete ${$args}{$_} } keys %{$args} };
  0            
  0            
  0            
136              
137             # The %modules hash contains a list of configuration parameters which may be
138             # specified within the hash reference argument to the TIEHASH object
139             # constructor.
140             #
141             # The hash of configuration parameters are then iterated through and if any
142             # of the options specified in the %modules hash are present, the component
143             # module to which the configuration option refers (at this level of the
144             # Tie::MLDBM framework) is called upon.
145              
146 0           my %modules = (
147             'Lock' => 'Null',
148             'Serialise' => undef,
149             'Store' => undef
150             );
151              
152 0           foreach my $arg ( keys %modules ) {
153              
154 0 0         if ( exists $self->{'Config'}->{$arg} ) {
155              
156 0           $modules{$arg} = join '::', __PACKAGE__, $arg, $self->{'Config'}->{$arg};
157 0 0         eval "require $modules{$arg}" or
158             croak( __PACKAGE__, '->TIEHASH : Cannot include framework component module ', $modules{$arg}, ' - ', $! );
159              
160             }
161              
162             }
163              
164 0           $self->{'Modules'} = \%modules;
165              
166             # The arguments passed to the TIEHASH method of this class are stored for
167             # re-use at a later stage after locking or CLEAR operations where the tied
168             # hash is synchronised.
169              
170 0           $self->{'Args'} = [ @params ];
171              
172             # Create a second-level tie to the underlying storage mechanism for
173             # serialised data structures and store the tied object within the blessed
174             # package object.
175              
176 0           my $db;
177 0 0         $db = $self->{'Modules'}->{'Store'}->TIEHASH( @{ $self->{'Args'} } ) or
  0            
178             croak( __PACKAGE__, '->TIEHASH : Failed to tie second level hash object - ', $! );
179 0           $self->{'Store'} = $db;
180              
181 0           return $self;
182             }
183              
184              
185             sub Sync {
186 0     0 0   my ( $self ) = @_;
187              
188             # The synchronisation of the tied hash is carried out in the same manner by
189             # which this is achieved in MLDBM::Sync. This calls for the re-opening of
190             # the tied hash object and storing this object reference in the Tie::MLDBM
191             # class object.
192              
193 0           my $db;
194 0 0         $db = $self->{'Modules'}->{'Store'}->TIEHASH( @{ $self->{'Args'} } ) or
  0            
195             croak( __PACKAGE__, '->Sync : Failed to tie second level hash object - ', $! );
196 0           $self->{'Store'} = $db;
197              
198 0           return $self;
199             }
200              
201              
202             sub UNTIE {
203 0     0     my ( $self, @params ) = @_;
204 0           return $self->{'Store'}->UNTIE( @params );
205             }
206              
207              
208             1;
209              
210              
211             __END__