ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/IndexedFileStore.pm
Revision: 1.1
Committed: Thu Nov 11 10:53:07 1999 UTC (25 years, 6 months ago) by williamc
Content type: text/plain
Branch: MAIN
Log Message:
fully functional - transfered from URLcache

File Contents

# User Rev Content
1 williamc 1.1 #
2     # IndexedFileStore.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     #
8     # Interface
9     # ---------
10     # new(storedir) : A new IndexedFileStore object based in storedir
11     # store(key,file) : store a key/file combination
12     # filename(key) : return the filename corresponding to a given key
13     # - generate one if it dosn't already exist
14     # file(key) : return the filename only if it already exists
15     # delete(key) : remove from cache key
16     # clear() : clear cache
17     # filestore() : Return the directory to download files to
18     # updatenumber(key) : return an integer number indicating update sequence
19     # - this number increased by one each time store is
20     # called
21    
22     package Utilities::IndexedFileStore;
23     require 5.004;
24     use Utilities::HashDB;
25    
26     sub new {
27     my $class=shift;
28     $self={};
29     bless $self, $class;
30     $self->_init(@_);
31     return $self;
32     }
33    
34     sub store {
35     my $self=shift;
36     my $key=shift;
37     my $file=shift;
38    
39     $self->{keyDB}->deletedata($key);
40     $self->{keyDB}->setdata($file,$key);
41     $self->{keyDB}->store($self->{cacheindex});
42    
43     # Keep a track of changes
44     my ($sequencenumber)=$self->{keyDBupdate}->getdata($key);
45     if ( ! defined $sequencenumber ) { $sequencenumber=0 };
46     $sequencenumber=$sequencenumber+1;
47     $self->{keyDBupdate}->deletedata($key);
48     $self->{keyDBupdate}->setdata($sequencenumber,$key);
49     $self->{keyDBupdate}->store($self->{cacheseqindex});
50     }
51    
52     sub updatenumber {
53     my $self=shift;
54     my $key=shift;
55    
56     my ($rv)=$self->{keyDBupdate}->getdata($key);
57     return ((defined $rv)?$rv:0);
58     }
59    
60     sub filestore {
61     my $self=shift;
62     return $self->{cachedir};
63     }
64    
65     sub filename {
66     my $self=shift;
67     my $key=shift;
68    
69     my $filenumber;
70    
71     my $file=$self->file($key);
72     if ( $file eq "" ) {
73     # need to generate a new filename - a random number will do
74     srand();
75     do {
76     $filenumber=int(rand 99999999)+1;
77     $file=$self->filestore()."/".$filenumber;
78     } until ( ! ( -e $file ) );
79     }
80     return $file;
81     }
82    
83     sub file {
84     my $self=shift;
85     my $key=shift;
86    
87     my @found=$self->{keyDB}->getdata($key);
88     return ( ($#found == -1)?"":$found[0]);
89     }
90    
91     sub clear {
92     my $self=shift;
93     foreach $item ( $self->{keyDB}->match() ) {
94     $self->delete($item->keys());
95     }
96     }
97    
98     sub delete {
99     my $self=shift;
100     my $key=shift;
101     unlink ($self->{keyDB}->getdata($URL));
102     $self->{keyDB}->deletedata($key);
103     $self->{keyDB}->store($self->{cacheindex});
104     }
105    
106     sub _init {
107     my $self=shift;
108     $self->{cachetop}=shift;
109     $self->{cachedir}=$self->{cachetop}."/files";
110     AddDir::adddir($self->{cachedir});
111    
112     $self->{cacheindex}=$self->{cachetop}."/index.db";
113     $self->{cacheseqindex}=$self->{cachetop}."/seqnumb.db";
114     $self->{keyDB}=Utilities::HashDB->new();
115     $self->{keyDBupdate}=Utilities::HashDB->new();
116     if ( -f $self->{cacheindex} ) {
117     $self->{keyDB}->restore($self->{cacheindex});
118     }
119     else {
120     AddDir::adddir($self->{cachedir});
121     }
122     if ( -f $self->{cacheseqindex} ) {
123     $self->{keyDBupdate}->restore($self->{cacheseqindex});
124     }
125     }