File Coverage

perllib/Arch/SharedCache.pm
Criterion Covered Total %
statement 56 66 84.8
branch 18 36 50.0
condition 7 16 43.7
subroutine 9 10 90.0
pod 4 6 66.6
total 94 134 70.1


line stmt bran cond sub pod time code
1             # Arch Perl library, Copyright (C) 2004 Mikhael Goikhman
2             #
3             # This program is free software; you can redistribute it and/or modify
4             # it under the terms of the GNU General Public License as published by
5             # the Free Software Foundation; either version 2 of the License, or
6             # (at your option) any later version.
7             #
8             # This program is distributed in the hope that it will be useful,
9             # but WITHOUT ANY WARRANTY; without even the implied warranty of
10             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11             # GNU General Public License for more details.
12             #
13             # You should have received a copy of the GNU General Public License
14             # along with this program; if not, write to the Free Software
15             # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16              
17 16     16   498 use 5.005;
  16         74  
  16         710  
18 16     16   107 use strict;
  16         32  
  16         968  
19              
20             package Arch::SharedCache;
21              
22 16     16   101 use base 'Arch::SharedIndex';
  16         31  
  16         13892  
23 16     16   12104 use Arch::Util qw(save_file load_file);
  16         47  
  16         13847  
24              
25             sub new ($%) {
26 24     24 1 968 my $class = shift;
27 24         501 my %init = @_;
28              
29 24 50       325 my $dir = $init{dir} or die "No cache directory given\n";
30 24 100       2490 unless (-d $dir) {
31 15 50       2355 mkdir($dir, 0777) or die "Can't create cache directory $dir: $!\n";
32             }
33 24 50       706 -d $dir or die "No cache directory ($dir)\n";
34              
35 24   50     988 my $index_file = $init{index_file} || $init{file} || '.index';
36 24 50       460 $index_file = "$dir/$index_file" unless $index_file =~ m!^\.?/!;
37              
38 24         636 my $self = $class->SUPER::new(
39             # default to a more readable serialization output
40             perl_data_indent => 1,
41             perl_data_pair => " => ",
42             %init,
43             file => $index_file,
44             );
45 24         193 $self->{dir} = $dir;
46 24   50     407 $self->{generic_filenames} = $init{generic_filenames} || 0;
47              
48 24         165 return $self;
49             }
50              
51             sub generate_unique_token ($) {
52 0     0 0 0 my $self = shift;
53 0         0 my $dir = $self->{dir};
54 0         0 my $prefix = time() . "-";
55 0         0 my $token = $prefix . "000000";
56 0 0       0 return $token unless -e "$dir/$token";
57 0         0 my $tries = 1000000;
58 0   0     0 do {
59 0         0 $token = $prefix . sprintf("%06d", rand(1000000));
60             } while -e "$dir/$token" && --$tries;
61 0 0       0 die "Failed to acquire unused file name $dir/$prefix*\n" unless $tries;
62 0         0 return $token;
63             }
64              
65             sub file_name_by_token ($$) {
66 4413     4413 0 5772 my $self = shift;
67 4413         5719 my $token = shift;
68 4413         19479 $token =~ s!/!%!g;
69 4413         14870 return "$self->{dir}/$token";
70             }
71              
72             sub delete_value ($$$) {
73 116     116 1 303 my $self = shift;
74 116         177 my ($key, $token) = @_;
75 116 50       329 $token = $key if $token eq "";
76 116         310 my $file_name = $self->file_name_by_token($token);
77 116 50       2564 return unless -e $file_name;
78 116 50       32827 unlink($file_name) or warn "Can't unlink $file_name: $!\n";
79             }
80              
81             sub fetch_value ($$$) {
82 2170     2170 1 3334 my $self = shift;
83 2170         5550 my ($key, $token) = @_;
84 2170 50       8459 $token = $key if $token eq "";
85 2170         5859 my $file_name = $self->file_name_by_token($token);
86 2170         3630 my $value = eval { load_file($file_name); };
  2170         8510  
87 2170 50       5713 warn $@ if $@;
88 2170         8192 $self->decode_value(\$value);
89 2170         14186 return $value;
90             }
91              
92             sub store_value ($$$) {
93 2127     2127 1 2698 my $self = shift;
94 2127         3394 my ($key, $token, $value) = @_;
95 2127 100 66     6611 $token = $key
96             if defined $token && $token eq "";
97 2127 50 66     7204 $token = $key
98             if !defined $token && !$self->{generic_filenames};
99 2127 50 33     8801 $token = $self->generate_unique_token
100             if !defined $token || $token eq "";
101 2127         4374 my $file_name = $self->file_name_by_token($token);
102 2127         6380 $self->encode_value(\$value);
103 2127         66125 eval { save_file($file_name, \$value); };
  2127         7565  
104 2127 50       5219 warn $@ if $@;
105 2127 50       5590 $token = "" if $key eq $token;
106 2127 50       3903 $token = undef if $@;
107 2127         7458 return $token;
108             }
109              
110             1;
111              
112             __END__