1 |
sashby |
1.7 |
=head1 NAME
|
2 |
|
|
|
3 |
|
|
Utilities::IndexedFileStore - An indexed file storage object. Used during the project boot process.
|
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS
|
6 |
|
|
|
7 |
|
|
my $obj = Utilities::IndexedFileStore->new();
|
8 |
|
|
|
9 |
|
|
=head1 METHODS
|
10 |
|
|
|
11 |
|
|
=over
|
12 |
|
|
|
13 |
|
|
=cut
|
14 |
|
|
|
15 |
|
|
=item C<new(storedir)>
|
16 |
|
|
|
17 |
|
|
A new IndexedFileStore object based in storedir.
|
18 |
|
|
|
19 |
|
|
=item C<store(key,file)>
|
20 |
|
|
|
21 |
|
|
Store a key/file combination.
|
22 |
|
|
|
23 |
|
|
=item C<filename(key)>
|
24 |
|
|
|
25 |
|
|
Return the filename corresponding to a given key or
|
26 |
|
|
generate one if it doesnt already exist.
|
27 |
|
|
|
28 |
|
|
=item C<file(key)>
|
29 |
|
|
|
30 |
|
|
Return the filename only if it already exists.
|
31 |
|
|
|
32 |
|
|
=item C<delete(key)>
|
33 |
|
|
|
34 |
|
|
Remove key from cache.
|
35 |
|
|
|
36 |
|
|
=item C<clear()>
|
37 |
|
|
|
38 |
|
|
Clear the cache.
|
39 |
|
|
|
40 |
|
|
=item C<filestore()>
|
41 |
|
|
|
42 |
|
|
Return the directory to download files to.
|
43 |
|
|
|
44 |
|
|
=item C<updatenumber(key)>
|
45 |
|
|
|
46 |
|
|
Return an integer number indicating update sequence.
|
47 |
|
|
This number increases by one each time store is called.
|
48 |
|
|
|
49 |
|
|
=back
|
50 |
|
|
|
51 |
|
|
=head1 AUTHOR
|
52 |
|
|
|
53 |
|
|
Originally Written by Christopher Williams
|
54 |
|
|
|
55 |
|
|
=head1 MAINTAINER
|
56 |
|
|
|
57 |
|
|
Shaun ASHBY L<mailTo:Shaun.Ashby@cern.ch>
|
58 |
|
|
|
59 |
|
|
=cut
|
60 |
williamc |
1.1 |
|
61 |
|
|
package Utilities::IndexedFileStore;
|
62 |
|
|
require 5.004;
|
63 |
|
|
use Utilities::HashDB;
|
64 |
|
|
|
65 |
|
|
sub new {
|
66 |
|
|
my $class=shift;
|
67 |
|
|
$self={};
|
68 |
|
|
bless $self, $class;
|
69 |
|
|
$self->_init(@_);
|
70 |
|
|
return $self;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
sub store {
|
74 |
|
|
my $self=shift;
|
75 |
|
|
my $key=shift;
|
76 |
|
|
my $file=shift;
|
77 |
|
|
|
78 |
williamc |
1.3 |
# store as relative to filestore if applicable - relocatable stores
|
79 |
|
|
my $filestore=$self->filestore();
|
80 |
|
|
$file=~s/^\Q$filestore\E\///;
|
81 |
williamc |
1.1 |
$self->{keyDB}->deletedata($key);
|
82 |
|
|
$self->{keyDB}->setdata($file,$key);
|
83 |
|
|
$self->{keyDB}->store($self->{cacheindex});
|
84 |
|
|
|
85 |
|
|
# Keep a track of changes
|
86 |
|
|
my ($sequencenumber)=$self->{keyDBupdate}->getdata($key);
|
87 |
|
|
if ( ! defined $sequencenumber ) { $sequencenumber=0 };
|
88 |
|
|
$sequencenumber=$sequencenumber+1;
|
89 |
|
|
$self->{keyDBupdate}->deletedata($key);
|
90 |
|
|
$self->{keyDBupdate}->setdata($sequencenumber,$key);
|
91 |
|
|
$self->{keyDBupdate}->store($self->{cacheseqindex});
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
sub updatenumber {
|
95 |
|
|
my $self=shift;
|
96 |
|
|
my $key=shift;
|
97 |
|
|
|
98 |
|
|
my ($rv)=$self->{keyDBupdate}->getdata($key);
|
99 |
|
|
return ((defined $rv)?$rv:0);
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
sub filestore {
|
103 |
|
|
my $self=shift;
|
104 |
|
|
return $self->{cachedir};
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
sub filename {
|
108 |
|
|
my $self=shift;
|
109 |
|
|
my $key=shift;
|
110 |
|
|
|
111 |
|
|
my $filenumber;
|
112 |
|
|
|
113 |
|
|
my $file=$self->file($key);
|
114 |
|
|
if ( $file eq "" ) {
|
115 |
|
|
# need to generate a new filename - a random number will do
|
116 |
|
|
srand();
|
117 |
|
|
do {
|
118 |
|
|
$filenumber=int(rand 99999999)+1;
|
119 |
|
|
$file=$self->filestore()."/".$filenumber;
|
120 |
|
|
} until ( ! ( -e $file ) );
|
121 |
|
|
}
|
122 |
|
|
return $file;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
sub file {
|
126 |
|
|
my $self=shift;
|
127 |
|
|
my $key=shift;
|
128 |
|
|
|
129 |
|
|
my @found=$self->{keyDB}->getdata($key);
|
130 |
williamc |
1.2 |
my $file=( ($#found == -1)?"":$found[0]);
|
131 |
|
|
# expand any files to include the path (unless complete path names)
|
132 |
|
|
if ( ($file ne "") && ($file!~/^\//) ) {
|
133 |
|
|
$file=$self->filestore()."/".$file;
|
134 |
|
|
}
|
135 |
|
|
return $file;
|
136 |
williamc |
1.1 |
}
|
137 |
|
|
|
138 |
|
|
sub clear {
|
139 |
|
|
my $self=shift;
|
140 |
|
|
foreach $item ( $self->{keyDB}->match() ) {
|
141 |
|
|
$self->delete($item->keys());
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
sub delete {
|
146 |
|
|
my $self=shift;
|
147 |
|
|
my $key=shift;
|
148 |
|
|
unlink ($self->{keyDB}->getdata($URL));
|
149 |
|
|
$self->{keyDB}->deletedata($key);
|
150 |
|
|
$self->{keyDB}->store($self->{cacheindex});
|
151 |
|
|
}
|
152 |
|
|
|
153 |
williamc |
1.4 |
sub location {
|
154 |
|
|
my $self=shift;
|
155 |
|
|
return $self->{cachetop};
|
156 |
|
|
}
|
157 |
|
|
|
158 |
williamc |
1.1 |
sub _init {
|
159 |
|
|
my $self=shift;
|
160 |
|
|
$self->{cachetop}=shift;
|
161 |
|
|
$self->{cachedir}=$self->{cachetop}."/files";
|
162 |
sashby |
1.5 |
AddDir::adddir($self->{cachetop});
|
163 |
williamc |
1.1 |
AddDir::adddir($self->{cachedir});
|
164 |
|
|
|
165 |
|
|
$self->{cacheindex}=$self->{cachetop}."/index.db";
|
166 |
|
|
$self->{cacheseqindex}=$self->{cachetop}."/seqnumb.db";
|
167 |
|
|
$self->{keyDB}=Utilities::HashDB->new();
|
168 |
|
|
$self->{keyDBupdate}=Utilities::HashDB->new();
|
169 |
sashby |
1.6 |
|
170 |
williamc |
1.1 |
if ( -f $self->{cacheindex} ) {
|
171 |
|
|
$self->{keyDB}->restore($self->{cacheindex});
|
172 |
|
|
}
|
173 |
|
|
else {
|
174 |
|
|
AddDir::adddir($self->{cachedir});
|
175 |
|
|
}
|
176 |
|
|
if ( -f $self->{cacheseqindex} ) {
|
177 |
|
|
$self->{keyDBupdate}->restore($self->{cacheseqindex});
|
178 |
|
|
}
|
179 |
sashby |
1.6 |
|
180 |
|
|
my $mode = 0644;
|
181 |
|
|
chmod $mode, $self->{cacheindex}, $self->{cacheseqindex};
|
182 |
|
|
|
183 |
williamc |
1.1 |
}
|