ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/IndexedFileStore.pm
Revision: 1.2
Committed: Mon Nov 15 16:39:13 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.1: +7 -1 lines
Log Message:
make portable

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 williamc 1.2 $file=~s/^$self->filestore()//;
40 williamc 1.1 $self->{keyDB}->deletedata($key);
41     $self->{keyDB}->setdata($file,$key);
42     $self->{keyDB}->store($self->{cacheindex});
43    
44     # Keep a track of changes
45     my ($sequencenumber)=$self->{keyDBupdate}->getdata($key);
46     if ( ! defined $sequencenumber ) { $sequencenumber=0 };
47     $sequencenumber=$sequencenumber+1;
48     $self->{keyDBupdate}->deletedata($key);
49     $self->{keyDBupdate}->setdata($sequencenumber,$key);
50     $self->{keyDBupdate}->store($self->{cacheseqindex});
51     }
52    
53     sub updatenumber {
54     my $self=shift;
55     my $key=shift;
56    
57     my ($rv)=$self->{keyDBupdate}->getdata($key);
58     return ((defined $rv)?$rv:0);
59     }
60    
61     sub filestore {
62     my $self=shift;
63     return $self->{cachedir};
64     }
65    
66     sub filename {
67     my $self=shift;
68     my $key=shift;
69    
70     my $filenumber;
71    
72     my $file=$self->file($key);
73     if ( $file eq "" ) {
74     # need to generate a new filename - a random number will do
75     srand();
76     do {
77     $filenumber=int(rand 99999999)+1;
78     $file=$self->filestore()."/".$filenumber;
79     } until ( ! ( -e $file ) );
80     }
81     return $file;
82     }
83    
84     sub file {
85     my $self=shift;
86     my $key=shift;
87    
88     my @found=$self->{keyDB}->getdata($key);
89 williamc 1.2 my $file=( ($#found == -1)?"":$found[0]);
90     # expand any files to include the path (unless complete path names)
91     if ( ($file ne "") && ($file!~/^\//) ) {
92     $file=$self->filestore()."/".$file;
93     }
94     return $file;
95 williamc 1.1 }
96    
97     sub clear {
98     my $self=shift;
99     foreach $item ( $self->{keyDB}->match() ) {
100     $self->delete($item->keys());
101     }
102     }
103    
104     sub delete {
105     my $self=shift;
106     my $key=shift;
107     unlink ($self->{keyDB}->getdata($URL));
108     $self->{keyDB}->deletedata($key);
109     $self->{keyDB}->store($self->{cacheindex});
110     }
111    
112     sub _init {
113     my $self=shift;
114     $self->{cachetop}=shift;
115     $self->{cachedir}=$self->{cachetop}."/files";
116     AddDir::adddir($self->{cachedir});
117    
118     $self->{cacheindex}=$self->{cachetop}."/index.db";
119     $self->{cacheseqindex}=$self->{cachetop}."/seqnumb.db";
120     $self->{keyDB}=Utilities::HashDB->new();
121     $self->{keyDBupdate}=Utilities::HashDB->new();
122     if ( -f $self->{cacheindex} ) {
123     $self->{keyDB}->restore($self->{cacheindex});
124     }
125     else {
126     AddDir::adddir($self->{cachedir});
127     }
128     if ( -f $self->{cacheseqindex} ) {
129     $self->{keyDBupdate}->restore($self->{cacheseqindex});
130     }
131     }