blib/lib/DBM/Deep/Engine.pm

filter: Statementstmt 100% 92 / 92 Branchbran 92.8% 13 / 14 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 37 / 37 Podpod 100% 23 / 23 totaltotal 99.4% 171 / 172 CC 10 SCAR 4.4
CC
10
Cov
99%
_descend
6.0
get_next_key
3.0
unlock
2.0
CRAP 1.5
« blib/lib/DBM/Deep/Iterator/File/Index.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Null.pm »
1 package DBM::Deep::Engine;
2  
3 63 ▶use 5.008_004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 57 ▶use DBM::Deep::Iterator ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # File-wide notes:
12 # * Every method in here assumes that the storage has been appropriately
13 # safeguarded. This can be anything from flock() to some sort of manual
14 # mutex. But, it's the caller's responsibility to make sure that this has
15 # been done.
16  
17 sub SIG_HASH () { 'H' }
18 sub SIG_ARRAY () { 'A' }
19  
20 =head1 NAME
21  
22 DBM::Deep::Engine - mediate mapping between DBM::Deep objects and storage medium
23  
24 =head1 PURPOSE
25  
26 This is an internal-use-only object for L<DBM::Deep>. It mediates the low-level
27 mapping between the L<DBM::Deep> objects and the storage medium.
28  
29 The purpose of this documentation is to provide low-level documentation for
30 developers. It is B<not> intended to be used by the general public. This
31 documentation and what it documents can and will change without notice.
32  
33 =head1 OVERVIEW
34  
35 The engine exposes an API to the DBM::Deep objects (DBM::Deep, DBM::Deep::Array,
36 and DBM::Deep::Hash) for their use to access the actual stored values. This API
37 is the following:
38  
39 =over 4
40  
41 =item * new
42  
43 =item * read_value
44  
45 =item * get_classname
46  
47 =item * make_reference
48  
49 =item * key_exists
50  
51 =item * delete_key
52  
53 =item * write_value
54  
55 =item * get_next_key
56  
57 =item * setup
58  
59 =item * clear
60  
61 =item * begin_work
62  
63 =item * commit
64  
65 =item * rollback
66  
67 =item * lock_exclusive
68  
69 =item * lock_shared
70  
71 =item * unlock
72  
73 =back
74  
75 They are explained in their own sections below. These methods, in turn, may
76 provide some bounds-checking, but primarily act to instantiate objects in the
77 Engine::Sector::* hierarchy and dispatch to them.
78  
79 =head1 TRANSACTIONS
80  
81 Transactions in DBM::Deep are implemented using a variant of MVCC. This attempts
82 to keep the amount of actual work done against the file low while still providing
83 Atomicity, Consistency, and Isolation. Durability, unfortunately, cannot be done
84 with only one file.
85  
86 =head2 STALENESS
87  
88 If another process uses a transaction slot and writes stuff to it, then
89 terminates, the data that process wrote is still within the file. In order to
90 address this, there is also a transaction staleness counter associated within
91 every write. Each time a transaction is started, that process increments that
92 transaction's staleness counter. If, when it reads a value, the staleness
93 counters aren't identical, DBM::Deep will consider the value on disk to be stale
94 and discard it.
95  
96 =head2 DURABILITY
97  
98 The fourth leg of ACID is Durability, the guarantee that when a commit returns,
99 the data will be there the next time you read from it. This should be regardless
100 of any crashes or powerdowns in between the commit and subsequent read.
101 DBM::Deep does provide that guarantee; once the commit returns, all of the data
102 has been transferred from the transaction shadow to the HEAD. The issue arises
103 with partial commits - a commit that is interrupted in some fashion. In keeping
104 with DBM::Deep's "tradition" of very light error-checking and non-existent
105 error-handling, there is no way to recover from a partial commit. (This is
106 probably a failure in Consistency as well as Durability.)
107  
108 Other DBMSes use transaction logs (a separate file, generally) to achieve
109 Durability. As DBM::Deep is a single-file, we would have to do something
110 similar to what SQLite and BDB do in terms of committing using synchronized
111 writes. To do this, we would have to use a much higher RAM footprint and some
112 serious programming that makes my head hurt just to think about it.
113  
114 =cut
115  
116 =head1 METHODS
117  
118 =head2 read_value( $obj, $key )
119  
120 This takes an object that provides _base_offset() and a string. It returns the
121 value stored in the corresponding Sector::Value's data section.
122  
123 =cut
124  
125 3 ▶sub read_value { die "read_value must be implemented in a child class" }
Subroutine read_value: called CC=1 SCAR=0.0
Pod documented
126  
127 =head2 get_classname( $obj )
128  
129 This takes an object that provides _base_offset() and returns the classname (if
130 any) associated with it.
131  
132 It delegates to Sector::Reference::get_classname() for the heavy lifting.
133  
134 It performs a staleness check.
135  
136 =cut
137  
138 2 ▶sub get_classname { die "get_classname must be implemented in a child class" }
Subroutine get_classname: called CC=1 SCAR=0.0
Pod documented
139  
140 =head2 make_reference( $obj, $old_key, $new_key )
141  
142 This takes an object that provides _base_offset() and two strings. The
143 strings correspond to the old key and new key, respectively. This operation
144 is equivalent to (given C<< $db->{foo} = []; >>) C<< $db->{bar} = $db->{foo} >>.
145  
146 This returns nothing.
147  
148 =cut
149  
150 2 ▶sub make_reference { die "make_reference must be implemented in a child class" }
Subroutine make_reference: called CC=1 SCAR=0.0
Pod documented
151  
152 =head2 key_exists( $obj, $key )
153  
154 This takes an object that provides _base_offset() and a string for
155 the key to be checked. This returns 1 for true and "" for false.
156  
157 =cut
158  
159 3 ▶sub key_exists { die "key_exists must be implemented in a child class" }
Subroutine key_exists: called CC=1 SCAR=0.0
Pod documented
160  
161 =head2 delete_key( $obj, $key )
162  
163 This takes an object that provides _base_offset() and a string for
164 the key to be deleted. This returns the result of the Sector::Reference
165 delete_key() method.
166  
167 =cut
168  
169 3 ▶sub delete_key { die "delete_key must be implemented in a child class" }
Subroutine delete_key: called CC=1 SCAR=0.0
Pod documented
170  
171 =head2 write_value( $obj, $key, $value )
172  
173 This takes an object that provides _base_offset(), a string for the
174 key, and a value. This value can be anything storable within L<DBM::Deep>.
175  
176 This returns 1 upon success.
177  
178 =cut
179  
180 3 ▶sub write_value { die "write_value must be implemented in a child class" }
Subroutine write_value: called CC=1 SCAR=0.0
Pod documented
181  
182 =head2 setup( $obj )
183  
184 This takes an object that provides _base_offset(). It will do everything needed
185 in order to properly initialize all values for necessary functioning. If this is
186 called upon an already initialized object, this will also reset the inode.
187  
188 This returns 1.
189  
190 =cut
191  
192 3 ▶sub setup { die "setup must be implemented in a child class" }
Subroutine setup: called CC=1 SCAR=0.0
Pod documented
193  
194 =head2 begin_work( $obj )
195  
196 This takes an object that provides _base_offset(). It will set up all necessary
197 bookkeeping in order to run all work within a transaction.
198  
199 If $obj is already within a transaction, an error will be thrown. If there are
200 no more available transactions, an error will be thrown.
201  
202 This returns undef.
203  
204 =cut
205  
206 3 ▶sub begin_work { die "begin_work must be implemented in a child class" }
Subroutine begin_work: called CC=1 SCAR=0.0
Pod documented
207  
208 =head2 rollback( $obj )
209  
210 This takes an object that provides _base_offset(). It will revert all
211 actions taken within the running transaction.
212  
213 If $obj is not within a transaction, an error will be thrown.
214  
215 This returns 1.
216  
217 =cut
218  
219 3 ▶sub rollback { die "rollback must be implemented in a child class" }
Subroutine rollback: called CC=1 SCAR=0.0
Pod documented
220  
221 =head2 commit( $obj )
222  
223 This takes an object that provides _base_offset(). It will apply all
224 actions taken within the transaction to the HEAD.
225  
226 If $obj is not within a transaction, an error will be thrown.
227  
228 This returns 1.
229  
230 =cut
231  
232 3 ▶sub commit { die "commit must be implemented in a child class" }
Subroutine commit: called CC=1 SCAR=0.0
Pod documented
233  
234 =head2 get_next_key( $obj, $prev_key )
235  
236 This takes an object that provides _base_offset() and an optional string
237 representing the prior key returned via a prior invocation of this method.
238  
239 This method delegates to C<< DBM::Deep::Iterator->get_next_key() >>.
240  
241 =cut
242  
243 # XXX Add staleness here
244 sub get_next_key {
245 547 ▶ my $self = shift;
Subroutine get_next_key: called CC=3 SCAR=11.0
Pod documented
246 547 my ($obj, $prev_key) = @_;
247  
248 # XXX Need to add logic about resetting the iterator if any key in the
249 # reference has changed
250 547 ▶ unless ( defined $prev_key ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $prev_key) 162 383 545
251 164 ▶ eval "use " . $self->iterator_class; die $@ if $@;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 161 161
252 163 $obj->{iterator} = $self->iterator_class->new({
253 base_offset => $obj->_base_offset,
254 engine => $self,
255 });
256 }
257  
258 545 return $obj->{iterator}->get_next_key( $obj );
259 }
260  
261 =head2 lock_exclusive()
262  
263 This takes an object that provides _base_offset(). It will guarantee that
264 the storage has taken precautions to be safe for a write.
265  
266 This returns nothing.
267  
268 =cut
269  
270 sub lock_exclusive {
271 5905 ▶ my $self = shift;
Subroutine lock_exclusive: called CC=1 SCAR=0.0
Pod documented
272 5905 my ($obj) = @_;
273 5905 return $self->storage->lock_exclusive( $obj );
274 }
275  
276 =head2 lock_shared()
277  
278 This takes an object that provides _base_offset(). It will guarantee that
279 the storage has taken precautions to be safe for a read.
280  
281 This returns nothing.
282  
283 =cut
284  
285 sub lock_shared {
286 4655 ▶ my $self = shift;
Subroutine lock_shared: called CC=1 SCAR=0.0
Pod documented
287 4655 my ($obj) = @_;
288 4655 return $self->storage->lock_shared( $obj );
289 }
290  
291 =head2 unlock()
292  
293 This takes an object that provides _base_offset(). It will guarantee that
294 the storage has released the most recently-taken lock.
295  
296 This returns nothing.
297  
298 =cut
299  
300 sub unlock {
301 10553 ▶ my $self = shift;
Subroutine unlock: called CC=2 SCAR=6.9
Pod documented
302 10553 my ($obj) = @_;
303  
304 10553 my $rv = $self->storage->unlock( $obj );
305  
306 10549 ▶ $self->flush if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 5536 5011 10547
307  
308 10547 return $rv;
309 }
310  
311 =head1 INTERNAL METHODS
312  
313 The following methods are internal-use-only to DBM::Deep::Engine and its
314 child classes.
315  
316 =cut
317  
318 =head2 flush()
319  
320 This takes no arguments. It will do everything necessary to flush all things to
321 disk. This is usually called during unlock() and setup().
322  
323 This returns nothing.
324  
325 =cut
326  
327 sub flush {
328 5538 ▶ my $self = shift;
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
329  
330 # Why do we need to have the storage flush? Shouldn't autoflush take care of
331 # things? -RobK, 2008-06-26
332 5538 $self->storage->flush;
333  
334 5536 return;
335 }
336  
337 =head2 load_sector( $loc )
338  
339 This takes an id/location/offset and loads the sector based on the engine's
340 defined sector type.
341  
342 =cut
343  
344 23177 ▶sub load_sector { $_[0]->sector_type->load( @_ ) }
Subroutine load_sector: called CC=1 SCAR=0.0
Pod documented
345  
346 =head2 clear( $obj )
347  
348 This takes an object that provides _base_offset() and deletes all its
349 elements, returning nothing.
350  
351 =cut
352  
353 3 ▶sub clear { die "clear must be implemented in a child class" }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
354  
355 =head2 cache / clear_cache
356  
357 This is the cache of loaded Reference sectors.
358  
359 =cut
360  
361 2394 ▶sub cache { $_[0]{cache} ||= {} }
Subroutine cache: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"cache"} ||= {}
l!l
234547
MC/DC100%1/1
$_[0]{"cache"} ||= {}
$_[0]{"cache"}
Truth table100%2/2
A
A= $_[0]{"cache"}
Aresult
11
00
362 4 ▶sub clear_cache { %{$_[0]->cache} = () }
Subroutine clear_cache: called CC=1 SCAR=0.0
Pod documented
363  
364 =head2 supports( $option )
365  
366 This returns a boolean depending on if this instance of DBM::Dep supports
367 that feature. C<$option> can be one of:
368  
369 =over 4
370  
371 =item * transactions
372  
373 =item * singletons
374  
375 =back
376  
377 Any other value will return false.
378  
379 =cut
380  
381 3 ▶sub supports { die "supports must be implemented in a child class" }
Subroutine supports: called CC=1 SCAR=0.0
Pod documented
382  
383 =head1 ACCESSORS
384  
385 The following are readonly attributes.
386  
387 =over 4
388  
389 =item * storage
390  
391 =item * sector_type
392  
393 =item * iterator_class
394  
395 =back
396  
397 =cut
398  
399 187637 ▶sub storage { $_[0]{storage} }
Subroutine storage: called CC=1 SCAR=0.0
Pod documented
400  
401 3 ▶sub sector_type { die "sector_type must be implemented in a child class" }
Subroutine sector_type: called CC=1 SCAR=0.0
Pod documented
402 3 ▶sub iterator_class { die "iterator_class must be implemented in a child class" }
Subroutine iterator_class: called CC=1 SCAR=0.0
Pod documented
403  
404 # This code is to make sure we write all the values in the $value to the
405 # disk and to make sure all changes to $value after the assignment are
406 # reflected on disk. This may be counter-intuitive at first, but it is
407 # correct dwimmery.
408 # NOTE - simply tying $value won't perform a STORE on each value. Hence,
409 # the copy to a temp value.
410 sub _descend {
411 799 ▶ my $self = shift;
Subroutine _descend: called CC=6 SCAR=17.9
412 799 my ($value, $value_sector) = @_;
413 799 ▶ my $r = Scalar::Util::reftype( $value ) || '';
Condition100%2/2
builtin::reftype($value) || ''
l!l
214583
MC/DC100%1/1
builtin::reftype($value) || ''
builtin::reftype($value)
Truth table100%2/2
A
A= builtin::reftype($value)
Aresult
11
00
414  
415 799 ▶ if ( $r eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($r eq 'ARRAY') { } 47 750 797
elsif ($r eq 'HASH') { } 167 583 750
416 49 my @temp = @$value;
417 49 tie @$value, 'DBM::Deep', {
418 base_offset => $value_sector->offset,
419 staleness => $value_sector->staleness,
420 storage => $self->storage,
421 engine => $self,
422 };
423 49 @$value = @temp;
424 49 ▶ bless $value, 'DBM::Deep::Array' unless Scalar::Util::blessed( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 35 12 47
425 }
426 elsif ( $r eq 'HASH' ) {
427 169 my %temp = %$value;
428 169 tie %$value, 'DBM::Deep', {
429 base_offset => $value_sector->offset,
430 staleness => $value_sector->staleness,
431 storage => $self->storage,
432 engine => $self,
433 };
434 169 %$value = %temp;
435 169 ▶ bless $value, 'DBM::Deep::Hash' unless Scalar::Util::blessed( $value );
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 157 10 167
436 }
437  
438 799 return;
439 }
440  
441 57 1;
442 __END__
« blib/lib/DBM/Deep/Iterator/File/Index.pm ↑ Summary blib/lib/DBM/Deep/Sector/File/Null.pm »