ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/IndexedFileStore.pm
Revision: 1.4.6.1
Committed: Fri Feb 27 15:34:56 2004 UTC (21 years, 2 months ago) by sashby
Content type: text/plain
Branch: SCRAM_V1_BRANCH
CVS Tags: V1_pre0, SCRAM_V1, SCRAMV1_IMPORT
Branch point for: V1_pre1
Changes since 1.4: +1 -0 lines
Log Message:
First import of new SCRAM packages into CMS cvs repos.

File Contents

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