Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuDirCache.java @ 116:55bc0e928ac5
Servlet Version 1.9b4
Fixed bug with single file access on windows.
author | robcast |
---|---|
date | Tue, 27 May 2003 21:41:04 +0200 |
parents | 226624784fe3 |
children | bc8df0133c04 |
rev | line source |
---|---|
86 | 1 /* DocuDirCache.java |
2 | |
3 Digital Image Library servlet components | |
4 | |
5 Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
6 | |
7 This program is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Please read license.txt for the full details. A copy of the GPL | |
13 may be found at http://www.gnu.org/copyleft/lgpl.html | |
14 | |
15 You should have received a copy of the GNU General Public License | |
16 along with this program; if not, write to the Free Software | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 * Created on 03.03.2003 | |
20 */ | |
21 | |
22 package digilib.io; | |
23 | |
24 import java.io.File; | |
91 | 25 import java.util.HashMap; |
86 | 26 |
27 /** | |
28 * @author casties | |
29 */ | |
91 | 30 public class DocuDirCache { |
86 | 31 |
91 | 32 // HashMap of directories |
33 private HashMap map = null; | |
86 | 34 // names of base directories |
35 private String[] baseDirNames = null; | |
91 | 36 // number of files in the whole cache (approximate) |
86 | 37 private long numFiles = 0; |
38 // number of cache hits | |
39 private long hits = 0; | |
40 // number of cache misses | |
41 private long misses = 0; | |
42 | |
43 /** Constructor with array of base directory names. | |
44 * | |
45 * @param bd base directory names | |
46 */ | |
47 public DocuDirCache(String[] bd) { | |
48 baseDirNames = bd; | |
91 | 49 map = new HashMap(); |
50 } | |
51 | |
52 /** The number of directories in the cache. | |
53 * @return | |
54 */ | |
55 public int size() { | |
56 return (map != null) ? map.size() : 0; | |
86 | 57 } |
58 | |
59 /** Add a DocuDirectory to the cache. | |
60 * | |
61 * @param newdir | |
62 */ | |
63 public void put(DocuDirectory newdir) { | |
64 String s = newdir.getDirName(); | |
91 | 65 if (map.containsKey(s)) { |
86 | 66 System.out.println("Baah, duplicate key in DocuDirectory.put!"); |
67 } else { | |
91 | 68 map.put(s, newdir); |
86 | 69 numFiles += newdir.size(); |
70 } | |
71 } | |
72 | |
91 | 73 /** Returns the DocuFileset with the pathname <code>fn</code> and the |
74 * index <code>in</code>. | |
75 * | |
76 * If <code>fn</code> is a file then the corresponding Fileset is | |
77 * returned and the index is ignored. | |
78 * | |
79 * @param fn digilib pathname | |
80 * @param in file index | |
81 * @return | |
82 */ | |
86 | 83 public DocuFileset getFileset(String fn, int in) { |
84 DocuDirectory dd; | |
85 // file number is 1-based, vector index is 0-based | |
86 int n = in - 1; | |
87 // first, assume fn is a directory and look in the cache | |
91 | 88 dd = (DocuDirectory) map.get(fn); |
89 if (dd == null) { | |
90 // cache miss | |
91 misses++; | |
92 // see if it's a directory | |
93 File f = new File(baseDirNames[0] + fn); | |
94 if (f.isDirectory()) { | |
95 dd = new DocuDirectory(fn, baseDirNames); | |
96 if (dd.isValid()) { | |
97 // add to the cache | |
98 put(dd); | |
99 } | |
100 } else { | |
101 // maybe it's a file | |
102 if (f.canRead()) { | |
116 | 103 // get the parent directory string (like we store it in the cache) |
104 String d = fn.substring(0, fn.lastIndexOf("/")); | |
91 | 105 // try it in the cache |
106 dd = (DocuDirectory) map.get(d); | |
107 if (dd == null) { | |
108 // try to read from disk | |
109 dd = new DocuDirectory(d, baseDirNames); | |
110 if (dd.isValid()) { | |
111 // add to the cache | |
112 put(dd); | |
113 } else { | |
114 // invalid path | |
115 return null; | |
116 } | |
117 } else { | |
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
118 // then it was not a real cache miss |
91 | 119 misses--; |
120 } | |
121 // get the file's index | |
122 n = dd.indexOf(f.getName()); | |
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
123 } else { |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
124 // it's not even a file :-( |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
125 return null; |
91 | 126 } |
127 } | |
128 } else { | |
129 // cache hit | |
130 hits++; | |
131 } | |
132 dd.refresh(); | |
133 if (dd.isValid()) { | |
134 try { | |
135 return dd.get(n); | |
136 } catch (ArrayIndexOutOfBoundsException e) { | |
137 } | |
138 } | |
139 return null; | |
140 } | |
141 | |
142 /** Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
143 * | |
144 * If <code>fn</code> is a file then its parent directory is returned. | |
145 * | |
146 * @param fn digilib pathname | |
147 * @return | |
148 */ | |
149 public DocuDirectory getDirectory(String fn) { | |
150 DocuDirectory dd; | |
151 // first, assume fn is a directory and look in the cache | |
152 dd = (DocuDirectory) map.get(fn); | |
86 | 153 if (dd == null) { |
154 // cache miss | |
155 misses++; | |
156 // see if it's a directory | |
157 File f = new File(baseDirNames[0] + fn); | |
158 if (f.isDirectory()) { | |
159 dd = new DocuDirectory(fn, baseDirNames); | |
160 if (dd.isValid()) { | |
161 // add to the cache | |
162 put(dd); | |
163 } | |
164 } else { | |
165 // maybe it's a file | |
166 if (f.canRead()) { | |
167 // try the parent directory in the cache | |
91 | 168 dd = (DocuDirectory) map.get(f.getParent()); |
86 | 169 if (dd == null) { |
170 // try to read from disk | |
171 dd = new DocuDirectory(f.getParent(), baseDirNames); | |
172 if (dd.isValid()) { | |
173 // add to the cache | |
174 put(dd); | |
175 } else { | |
176 // invalid path | |
177 return null; | |
178 } | |
91 | 179 } else { |
180 // not a real cache miss then | |
181 misses--; | |
86 | 182 } |
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
183 } else { |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
184 // it's not even a file :-( |
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
185 return null; |
86 | 186 } |
187 } | |
188 } else { | |
189 // cache hit | |
190 hits++; | |
191 } | |
192 dd.refresh(); | |
193 if (dd.isValid()) { | |
194 return dd; | |
195 } | |
196 return null; | |
197 } | |
198 | |
199 /** | |
200 * @return String[] | |
201 */ | |
202 public String[] getBaseDirNames() { | |
203 return baseDirNames; | |
204 } | |
205 | |
206 /** | |
207 * @return long | |
208 */ | |
209 public long getNumFiles() { | |
210 return numFiles; | |
211 } | |
212 | |
213 /** | |
214 * Sets the baseDirNames. | |
215 * @param baseDirNames The baseDirNames to set | |
216 */ | |
217 public void setBaseDirNames(String[] baseDirNames) { | |
218 this.baseDirNames = baseDirNames; | |
219 } | |
220 | |
221 /** | |
222 * @return long | |
223 */ | |
224 public long getHits() { | |
225 return hits; | |
226 } | |
227 | |
228 /** | |
229 * @return long | |
230 */ | |
231 public long getMisses() { | |
232 return misses; | |
233 } | |
234 | |
235 } |