File Coverage

blib/lib/DBM/Deep/Sector/File/Index.pm
Criterion Covered Total %
statement 35 42 83.3
branch 6 10 60.0
condition 2 6 33.3
subroutine 8 10 80.0
pod 0 5 0.0
total 51 73 69.8


line stmt bran cond sub pod time code
1             package DBM::Deep::Sector::File::Index;
2              
3 50     50   362 use strict;
  50         103  
  50         1728  
4 50     50   280 use warnings FATAL => 'all';
  50         113  
  50         1850  
5              
6 50     50   307 use base qw( DBM::Deep::Sector::File );
  50         119  
  50         32746  
7              
8             my $STALE_SIZE = 2;
9              
10             # Please refer to the pack() documentation for further information
11             my %StP = (
12             1 => 'C', # Unsigned char value (no order needed as it's just one byte)
13             2 => 'n', # Unsigned short in "network" (big-endian) order
14             4 => 'N', # Unsigned long in "network" (big-endian) order
15             8 => 'Q', # Usigned quad (no order specified, presumably machine-dependent)
16             );
17              
18             sub _init {
19 4102     4102   6166 my $self = shift;
20              
21 4102         9234 my $engine = $self->engine;
22              
23 4102 100       8429 unless ( $self->offset ) {
24 10         41 my $leftover = $self->size - $self->base_size;
25              
26 10         43 $self->{offset} = $engine->_request_index_sector( $self->size );
27 10         50 $engine->storage->print_at( $self->offset, $engine->SIG_INDEX ); # Sector type
28             # Skip staleness counter
29 10         71 $engine->storage->print_at( $self->offset + $self->base_size,
30             chr(0) x $leftover, # Zero-fill the rest
31             );
32             }
33              
34 4102         7687 return $self;
35             }
36              
37             #XXX Change here
38             sub size {
39 23     23 0 41 my $self = shift;
40 23 100       83 unless ( $self->{size} ) {
41 13         42 my $e = $self->engine;
42 13         61 $self->{size} = $self->base_size + $e->byte_size * $e->hash_chars;
43             }
44 23         86 return $self->{size};
45             }
46              
47 0     0 0 0 sub free_meth { return '_add_free_index_sector' }
48              
49             sub free {
50 0     0 0 0 my $self = shift;
51 0         0 my $e = $self->engine;
52              
53 0         0 for my $i ( 0 .. $e->hash_chars - 1 ) {
54 0 0       0 my $l = $self->get_entry( $i ) or next;
55 0         0 $e->load_sector( $l )->free;
56             }
57              
58 0         0 $self->SUPER::free();
59             }
60              
61             sub _loc_for {
62 8339     8339   13060 my $self = shift;
63 8339         12811 my ($idx) = @_;
64 8339         15551 return $self->offset + $self->base_size + $idx * $self->engine->byte_size;
65             }
66              
67             sub get_entry {
68 7914     7914 0 11876 my $self = shift;
69 7914         13761 my ($idx) = @_;
70              
71 7914         15505 my $e = $self->engine;
72              
73 7914 50 33     24153 DBM::Deep->_throw_error( "get_entry: Out of range ($idx)" )
74             if $idx < 0 || $idx >= $e->hash_chars;
75              
76             return unpack(
77 7914         17296 $StP{$e->byte_size},
78             $e->storage->read_at( $self->_loc_for( $idx ), $e->byte_size ),
79             );
80             }
81              
82             sub set_entry {
83 425     425 0 755 my $self = shift;
84 425         916 my ($idx, $loc) = @_;
85              
86 425         877 my $e = $self->engine;
87              
88 425 50 33     1537 DBM::Deep->_throw_error( "set_entry: Out of range ($idx)" )
89             if $idx < 0 || $idx >= $e->hash_chars;
90              
91             $self->engine->storage->print_at(
92             $self->_loc_for( $idx ),
93 425         959 pack( $StP{$e->byte_size}, $loc ),
94             );
95             }
96              
97             1;
98             __END__