This repository was archived by the owner on Jul 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCache.pm
executable file
·156 lines (130 loc) · 3.87 KB
/
Cache.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright SRA International
#
# Distributed under the OSI-approved BSD 3-Clause License.
# See http://ncip.github.com/pathway-interaction-database/LICENSE.txt for details.
package Cache;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(MakeCacheFile, FindCacheFile, $CACHE_FAIL);
my $CACHE_FAIL = 0;
use constant MAX_CACHE_ID => 500;
use constant MAX_TRIES => 30;
use constant FIRST_CACHE_ID => 1;
######################################################################
sub new {
my ($class, $cache_path, $cache_prefix) = @_;
my $self = {};
$self->{CACHE_PATH} = $cache_path;
$self->{CACHE_PREFIX} = $cache_prefix;
$self->{CACHE_PATH} =~ s/\/$//;
if (not (-e $self->{CACHE_PATH})) {
die "cache path $self->{CACHE_PATH} does not exist";
}
if (not (-w $self->{CACHE_PATH})) {
die "cache path $self->{CACHE_PATH} is not writable";
}
my $status = "not_ok";
my $sequence_file = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.txt";
my $temp_file1 = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.tmp1";
my $temp_file2 = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.tmp2";
for (my $tries = 1; $tries <= MAX_TRIES; $tries++) {
if (-e $sequence_file) {
$status = "ok";
last;
} else {
sleep 1;
}
}
if ($status eq "ok") {
return bless $self;
} else {
if (-e $temp_file1) {
unlink $temp_file1;
}
if (-e $temp_file2) {
unlink $temp_file2;
}
print STDERR "Unlinked both files\n";
if (open(OUT, ">$sequence_file")) {
print OUT "1";
close OUT;
chmod 0666, $sequence_file;
return bless $self;
} else {
die "Could not create PW.txt";
}
}
}
######################################################################
sub MakeCacheFile {
my ($self) = @_;
my $cache_id = GetCacheId($self);
if ($cache_id == $CACHE_FAIL) {
return ($CACHE_FAIL, "");
} else {
my $fname = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.$cache_id";
return ($cache_id, $fname);
}
}
######################################################################
sub FindCacheFile {
my ($self, $cache_id) = @_;
if ($cache_id == $CACHE_FAIL) {
return "";
} elsif( -e "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.$cache_id" ) {
return "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.$cache_id";
} else {
return "";
}
}
######################################################################
sub GetCacheId {
my ($self) = @_;
my $sequence_file = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.txt";
my $temp_file1 = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.tmp1";
my $temp_file2 = "$self->{CACHE_PATH}/$self->{CACHE_PREFIX}.tmp2";
my $id = $CACHE_FAIL;
for (my $tries = 1; $tries <= MAX_TRIES; $tries++) {
if (-e $sequence_file) {
if (rename $sequence_file, $temp_file1) {
if (open(IN, $temp_file1)) {
while (<IN>) {
$id = $_;
}
close $temp_file1;
if ($id >= MAX_CACHE_ID) {
$id = FIRST_CACHE_ID;
} else {
$id = $id + 1;
}
if (open(OUT, ">$temp_file2")) {
print OUT $id;
close OUT;
unlink $temp_file1;
rename $temp_file2, $sequence_file;
chmod 0666, $sequence_file;
} else {
$id = $CACHE_FAIL;
print STDERR "Cannot open $temp_file2\n";
rename $temp_file1, $sequence_file;
chmod 0666, $sequence_file;
}
last;
} else {
print STDERR "Cannot open $temp_file1\n";
rename $temp_file1, $sequence_file;
chmod 0666, $sequence_file;
last;
}
} else {
print STDERR "Cannot rename $sequence_file to $temp_file1\n";
last;
}
} else {
sleep 1;
}
}
return $id;
}
######################################################################
1;