| 1 | package Tie::StorableDir; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use File::Spec::Functions; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use IO::Dir; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Scalar::Util qw(weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use Tie::StorableDir::Slot; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 4 | our @ISA = qw(Tie::Hash); | |||||||||||||||||||||||||||
| 17 | 4 | our $VERSION = 0.075; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # if $not_exiting = 0, we don't save anything. This is set at the end of the | ||||||||||||||||||||||||||||
| 20 | # END {} block lower. This prevents gc ordering problems from trashing the data. | ||||||||||||||||||||||||||||
| 21 | 4 | our $not_exiting = 1; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | our %instances; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 53 | ▶ | sub _path_encode { | ||||||||||||||||||||||||||
|
Subroutine
_path_encode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 53 | my $path = shift; | |||||||||||||||||||||||||||
| 27 | 53 | $path =~ s{([^0-9a-zA-Z. -])}{sprintf "_%02x", ord $1}ge; | |||||||||||||||||||||||||||
| 28 | 53 | return 'k'.$path; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 7 | ▶ | sub _path_decode { | ||||||||||||||||||||||||||
|
Subroutine
_path_decode: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 7 | my $path = shift; | |||||||||||||||||||||||||||
| 33 | 7 | ▶ | $path =~ s/^k// or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 6 | $path =~ s{_([0-9a-zA-Z]{2})}{chr hex $1}ge; | |||||||||||||||||||||||||||
| 35 | 6 | return $path; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 10 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 39 | 10 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 40 | 10 | ▶ | $class = ref $class || $class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||
| 41 | 10 | my $self = {}; | |||||||||||||||||||||||||||
| 42 | 10 | bless $self, $class; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 10 | ▶ | if (!exists $opts{dirname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1 | croak "Missing required parameter dirname"; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 9 | ▶ | if (!-d $opts{dirname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | croak "dirname '$opts{dirname}' is not a directory."; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 9 | $self->{dirname} = File::Spec->rel2abs(delete $opts{dirname}); | |||||||||||||||||||||||||||
| 51 | 9 | $self->{backedkeys} = {}; | |||||||||||||||||||||||||||
| 52 | 9 | ▶ | if (%opts) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | carp "One or more unrecognized options"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 8 | $instances{$self} = $self; | |||||||||||||||||||||||||||
| 56 | 8 | return $self; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 27 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=7 SCAR=20.6
| |||||||||||||||||||||||||||||
| 60 | 27 | my ($self, $key, $value) = @_; | |||||||||||||||||||||||||||
| 61 | 27 | ▶ | unless ($not_exiting && defined $self->{dirname}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Tie::StorableDir::not_exiting and defined $self->{"dirname"}
MC/DC0%0/2
$Tie::StorableDir::not_exiting and defined $self->{"dirname"}
$Tie::StorableDir::not_exiting
defined $self->{"dirname"}
Truth table33%1/3
A and B
A= $Tie::StorableDir::not_exitingB= defined $self->{"dirname"}
| |||||||||||||||||||||||||||||
| 62 | 0 | carp "Exiting; STORE ignored."; | |||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 27 | my $ekey = _path_encode($key); | |||||||||||||||||||||||||||
| 66 | 27 | my $path = catfile($self->{dirname}, $ekey); | |||||||||||||||||||||||||||
| 67 | 27 | eval { | |||||||||||||||||||||||||||
| 68 | 27 | ▶ | store \$value, $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | or die $!; | ||||||||||||||||||||||||||||
| 70 | }; | ||||||||||||||||||||||||||||
| 71 | 27 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 1 | croak "Error storing: $!"; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 26 | ▶ | if (defined $self->{backedkeys}{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 17 | my $slot = $self->{backedkeys}{$key}; | |||||||||||||||||||||||||||
| 76 | 17 | ▶ | $slot->disconnect if defined $slot; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 17 | delete $self->{backedkeys}{$key}; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 25 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 82 | 25 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 83 | 25 | ▶ | if (defined $self->{backedkeys}{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 2 | my $slot = $self->{backedkeys}{$key}; | |||||||||||||||||||||||||||
| 85 | 2 | return $slot->getvalue; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 23 | my $ekey = _path_encode($key); | |||||||||||||||||||||||||||
| 88 | 23 | my $path = catfile($self->{dirname}, $ekey); | |||||||||||||||||||||||||||
| 89 | 23 | ▶ | return undef if (!-e $path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 23 | my $ref; | |||||||||||||||||||||||||||
| 91 | 23 | eval { | |||||||||||||||||||||||||||
| 92 | 23 | $ref = retrieve($path); | |||||||||||||||||||||||||||
| 93 | }; | ||||||||||||||||||||||||||||
| 94 | 23 | ▶ | if (!defined $ref && $@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $ref and $@
MC/DC50%1/2
not defined $ref and $@
not defined $ref
$@
Truth table66%2/3
not(A) and B
A= not defined $refB= $@
| |||||||||||||||||||||||||||||
| 95 | 1 | croak "Error retrieving: $@"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 22 | ▶ | if (!ref $$ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 4 | return $$ref; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 18 | my $slot = new Tie::StorableDir::Slot($key, $$ref, $self); | |||||||||||||||||||||||||||
| 101 | 18 | my $v = $slot->getvalue; | |||||||||||||||||||||||||||
| 102 | 18 | $self->{backedkeys}{$key} = $slot; | |||||||||||||||||||||||||||
| 103 | 18 | weaken($self->{backedkeys}{$key}); | |||||||||||||||||||||||||||
| 104 | 18 | return $v; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | 2 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 109 | 2 | $key = _path_encode($key); | |||||||||||||||||||||||||||
| 110 | 2 | my $path = catfile($self->{dirname}, $key); | |||||||||||||||||||||||||||
| 111 | 2 | return -e $path; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 6 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 116 | 6 | delete $self->{iterator}; | |||||||||||||||||||||||||||
| 117 | 6 | return $self->NEXTKEY; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 12 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 121 | 12 | my ($self) = @_; | |||||||||||||||||||||||||||
| 122 | 12 | ▶ | if (!defined $self->{iterator}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | $self->{iterator} = new IO::Dir($self->{dirname}) | ||||||||||||||||||||||||||||
| 124 | 6 | ▶ | or croak "Cannot open directory for read: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 11 | while (1) { | |||||||||||||||||||||||||||
| 127 | 24 | $! = 0; | |||||||||||||||||||||||||||
| 128 | 24 | my $ent = $self->{iterator}->read; | |||||||||||||||||||||||||||
| 129 | 24 | ▶ | if (!defined $ent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 5 | ▶ | if ($! != 0 && !($! =~ /file desc/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$! != 0 and not $! =~ /file desc/
MC/DC0%0/2
$! != 0 and not $! =~ /file desc/
$! != 0
not $! =~ /file desc/
Truth table33%1/3
A and not(B)
A= $! != 0B= not $! =~ /file desc/
| |||||||||||||||||||||||||||||
| 131 | 0 | croak "Cannot read directory entry: $!"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 5 | delete $self->{iterator}; | |||||||||||||||||||||||||||
| 134 | 5 | return undef; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 19 | my $path = catfile($self->{dirname}, $ent); | |||||||||||||||||||||||||||
| 137 | 19 | ▶ | next if (!-r $path || !-f $path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-r $path and -f $path
MC/DC100%2/2
-r $path and -f $path
-r $path
-f $path
Truth table100%3/3
A and B
A= -r $pathB= -f $path
| |||||||||||||||||||||||||||||
| 138 | 7 | my $key = _path_decode($ent); | |||||||||||||||||||||||||||
| 139 | 7 | ▶ | next unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 6 | return $key; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||
| 145 | 1 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 146 | 1 | my $oldv = $self->FETCH($key); | |||||||||||||||||||||||||||
| 147 | 1 | my $path = catfile($self->{dirname}, _path_encode($key)); | |||||||||||||||||||||||||||
| 148 | 1 | ▶ | return undef if (!-e $path); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | unlink $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | or croak "Cannot unlink key: $!"; | ||||||||||||||||||||||||||||
| 151 | 1 | ▶ | if (defined $self->{backedkeys}{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 1 | my $slot = $self->{backedkeys}{$key}; | |||||||||||||||||||||||||||
| 153 | 1 | ▶ | $slot->disconnect if defined $slot; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 1 | delete $self->{backedkeys}{$key}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 1 | return $oldv; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | ▶ | sub CLEAR { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=7 SCAR=21.0
| |||||||||||||||||||||||||||||
| 160 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 161 | my $dirh = new IO::Dir($self->{dirname}) | ||||||||||||||||||||||||||||
| 162 | 1 | ▶ | or croak "Cannot open directory: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 1 | ▶ | while (defined($_ = $dirh->read)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 7 | my $path = catfile($self->{dirname}, $_); | |||||||||||||||||||||||||||
| 165 | 7 | ▶ | next unless -f $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 5 | ▶ | unlink $path | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | or croak "Cannot unlink $path: $!"; | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 1 | for (values %{$self->{backedkeys}}) { | |||||||||||||||||||||||||||
| 170 | 0 | my $slot = $_; | |||||||||||||||||||||||||||
| 171 | 0 | ▶ | $slot->disconnect if defined $slot; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 1 | $self->{backedkeys} = {}; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub SCALAR { | ||||||||||||||||||||||||||
|
Subroutine
SCALAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 177 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 178 | 0 | return $self; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | ▶ | sub UNTIE { | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 182 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 183 | 1 | for (values %{$self->{backedkeys}}) { | |||||||||||||||||||||||||||
| 184 | 1 | ▶ | next unless defined $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 1 | $_->writeback; | |||||||||||||||||||||||||||
| 186 | 1 | $_->disconnect; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 1 | delete $self->{backedkeys}; | |||||||||||||||||||||||||||
| 189 | 1 | delete $self->{dirname}; | |||||||||||||||||||||||||||
| 190 | 1 | delete $instances{$self}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 195 | 3 | delete $instances{$self}; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | ▶ | END { | ||||||||||||||||||||||||||
|
Subroutine
END: called CC=4 SCAR=17.9
| |||||||||||||||||||||||||||||
| 199 | 4 | for (values %instances) { | |||||||||||||||||||||||||||
| 200 | 7 | for (values %{$_->{backedkeys}}) { | |||||||||||||||||||||||||||
| 201 | 0 | ▶ | next unless defined $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 0 | $_->writeback; | |||||||||||||||||||||||||||
| 203 | 0 | $_->disconnect; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 7 | delete $_->{backedkeys}; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 4 | $not_exiting = 0; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 4 | 1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||