Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuDirCache.java @ 166:6132c29ac9f9
Servlet Version 1.16b2 with new DigilibParameter code.
- more generic class for request parameters
- like already done for DiglibConfig
- changes in JSPs for new request stuff
- changes in ImageSize class so size=0 is "wildcard"
- missing dw and dh parameters now treated as wildcards
- changed package name in imageinfo class
| author | robcast |
|---|---|
| date | Wed, 29 Oct 2003 22:45:51 +0100 |
| parents | e743b853efca |
| children | 67ff8c7fecb9 |
| 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; |
| 159 | 26 import java.util.Iterator; |
| 27 import java.util.LinkedList; | |
| 28 import java.util.List; | |
| 86 | 29 |
| 30 /** | |
| 31 * @author casties | |
| 32 */ | |
| 91 | 33 public class DocuDirCache { |
| 86 | 34 |
| 159 | 35 /** HashMap of directories */ |
| 91 | 36 private HashMap map = null; |
| 159 | 37 /** names of base directories */ |
| 86 | 38 private String[] baseDirNames = null; |
| 159 | 39 /** array of allowed file classes (image/text) */ |
| 40 private int[] fileClasses = null; | |
| 41 /** number of files in the whole cache (approximate) */ | |
| 86 | 42 private long numFiles = 0; |
| 159 | 43 /** number of cache hits */ |
| 86 | 44 private long hits = 0; |
| 159 | 45 /** number of cache misses */ |
| 86 | 46 private long misses = 0; |
| 47 | |
| 159 | 48 /** Constructor with array of base directory names and file classes. |
| 49 * | |
| 50 * @param bd base directory names | |
| 51 */ | |
| 52 public DocuDirCache(String[] bd, int[] fileClasses) { | |
| 53 baseDirNames = bd; | |
| 54 map = new HashMap(); | |
| 55 this.fileClasses = fileClasses; | |
| 56 } | |
| 86 | 57 /** Constructor with array of base directory names. |
| 58 * | |
| 59 * @param bd base directory names | |
| 60 */ | |
| 61 public DocuDirCache(String[] bd) { | |
| 62 baseDirNames = bd; | |
| 91 | 63 map = new HashMap(); |
| 159 | 64 // default file class is CLASS_IMAGE |
| 65 fileClasses = new int[1]; | |
| 66 fileClasses[0] = FileOps.CLASS_IMAGE; | |
| 91 | 67 } |
| 68 | |
| 69 /** The number of directories in the cache. | |
| 70 * @return | |
| 71 */ | |
| 72 public int size() { | |
| 73 return (map != null) ? map.size() : 0; | |
| 86 | 74 } |
| 75 | |
| 76 /** Add a DocuDirectory to the cache. | |
| 77 * | |
| 78 * @param newdir | |
| 79 */ | |
| 80 public void put(DocuDirectory newdir) { | |
| 81 String s = newdir.getDirName(); | |
| 91 | 82 if (map.containsKey(s)) { |
| 159 | 83 System.out.println("Baah, duplicate key in DocuDirCache.put!"); |
| 86 | 84 } else { |
| 91 | 85 map.put(s, newdir); |
| 86 | 86 numFiles += newdir.size(); |
| 87 } | |
| 88 } | |
| 89 | |
| 151 | 90 /** Add a directory to the cache and check its parents. |
| 91 * | |
| 92 * @param newDir | |
| 93 */ | |
| 94 public void putDir(DocuDirectory newDir) { | |
| 95 put(newDir); | |
| 96 String parent = newDir.getParentDirName(); | |
| 97 if (parent != null) { | |
| 98 // check the parent in the cache | |
| 99 DocuDirectory pd = (DocuDirectory)map.get(parent); | |
| 100 if (pd == null) { | |
| 101 // the parent is unknown | |
| 102 pd = new DocuDirectory(parent, this); | |
| 103 putDir(pd); | |
| 104 } | |
| 105 newDir.setParent(pd); | |
| 106 } | |
| 107 } | |
| 108 | |
| 159 | 109 /** Get a list with all children of a directory. |
| 110 * | |
| 111 * Returns a List of DocuDirectory's. | |
| 112 * Returns an empty List if the directory has no children. | |
| 113 * If recurse is false then only direct children are returned. | |
| 114 * | |
| 115 * @param dirname | |
| 116 * @param recurse find all children and their children. | |
| 117 * @return | |
| 118 */ | |
| 119 public List getChildren(String dirname, boolean recurse) { | |
| 120 List l = new LinkedList(); | |
| 121 for (Iterator i = map.keySet().iterator(); i.hasNext(); ) { | |
| 122 DocuDirectory dd = (DocuDirectory)i.next(); | |
| 123 if (recurse) { | |
| 124 if (dd.getDirName().startsWith(dirname)) { | |
| 125 l.add(dd); | |
| 126 } | |
| 127 } else { | |
| 128 if (dd.getParentDirName().equals(dirname)) { | |
| 129 l.add(dd); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 return l; | |
| 134 } | |
| 135 | |
| 136 /** Returns the ImageFileset with the pathname <code>fn</code> and the | |
| 137 * index <code>in</code> and the class <code>fc</code>. | |
| 91 | 138 * |
| 139 * If <code>fn</code> is a file then the corresponding Fileset is | |
| 140 * returned and the index is ignored. | |
| 141 * | |
| 142 * @param fn digilib pathname | |
| 143 * @param in file index | |
| 144 * @return | |
| 145 */ | |
| 159 | 146 public DocuDirent getFile(String fn, int in, int fc) { |
| 86 | 147 DocuDirectory dd; |
| 148 // file number is 1-based, vector index is 0-based | |
| 149 int n = in - 1; | |
| 150 // first, assume fn is a directory and look in the cache | |
| 91 | 151 dd = (DocuDirectory) map.get(fn); |
| 152 if (dd == null) { | |
| 153 // cache miss | |
| 154 misses++; | |
| 155 // see if it's a directory | |
| 152 | 156 File f = new File(baseDirNames[0], fn); |
| 91 | 157 if (f.isDirectory()) { |
| 151 | 158 dd = new DocuDirectory(fn, this); |
| 91 | 159 if (dd.isValid()) { |
| 160 // add to the cache | |
| 151 | 161 putDir(dd); |
| 91 | 162 } |
| 163 } else { | |
| 164 // maybe it's a file | |
| 165 if (f.canRead()) { | |
| 116 | 166 // get the parent directory string (like we store it in the cache) |
| 167 String d = fn.substring(0, fn.lastIndexOf("/")); | |
| 91 | 168 // try it in the cache |
| 169 dd = (DocuDirectory) map.get(d); | |
| 170 if (dd == null) { | |
| 171 // try to read from disk | |
| 151 | 172 dd = new DocuDirectory(d, this); |
| 91 | 173 if (dd.isValid()) { |
| 174 // add to the cache | |
| 151 | 175 putDir(dd); |
| 91 | 176 } else { |
| 177 // invalid path | |
| 178 return null; | |
| 179 } | |
| 180 } else { | |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
181 // then it was not a real cache miss |
| 91 | 182 misses--; |
| 183 } | |
| 184 // get the file's index | |
| 159 | 185 n = dd.indexOf(f.getName(), fc); |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
186 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
187 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
188 return null; |
| 91 | 189 } |
| 190 } | |
| 191 } else { | |
| 192 // cache hit | |
| 193 hits++; | |
| 194 } | |
| 195 dd.refresh(); | |
| 196 if (dd.isValid()) { | |
| 197 try { | |
| 159 | 198 return dd.get(n, fc); |
| 91 | 199 } catch (ArrayIndexOutOfBoundsException e) { |
| 200 } | |
| 201 } | |
| 202 return null; | |
| 203 } | |
| 204 | |
| 205 /** Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
| 206 * | |
| 207 * If <code>fn</code> is a file then its parent directory is returned. | |
| 208 * | |
| 209 * @param fn digilib pathname | |
| 210 * @return | |
| 211 */ | |
| 212 public DocuDirectory getDirectory(String fn) { | |
| 213 DocuDirectory dd; | |
| 214 // first, assume fn is a directory and look in the cache | |
| 215 dd = (DocuDirectory) map.get(fn); | |
| 86 | 216 if (dd == null) { |
| 217 // cache miss | |
| 218 misses++; | |
| 219 // see if it's a directory | |
| 152 | 220 File f = new File(baseDirNames[0], fn); |
| 86 | 221 if (f.isDirectory()) { |
| 151 | 222 dd = new DocuDirectory(fn, this); |
| 86 | 223 if (dd.isValid()) { |
| 224 // add to the cache | |
| 151 | 225 putDir(dd); |
| 86 | 226 } |
| 227 } else { | |
| 228 // maybe it's a file | |
| 229 if (f.canRead()) { | |
| 230 // try the parent directory in the cache | |
| 91 | 231 dd = (DocuDirectory) map.get(f.getParent()); |
| 86 | 232 if (dd == null) { |
| 233 // try to read from disk | |
| 151 | 234 dd = new DocuDirectory(f.getParent(), this); |
| 86 | 235 if (dd.isValid()) { |
| 236 // add to the cache | |
| 151 | 237 putDir(dd); |
| 86 | 238 } else { |
| 239 // invalid path | |
| 240 return null; | |
| 241 } | |
| 91 | 242 } else { |
| 243 // not a real cache miss then | |
| 244 misses--; | |
| 86 | 245 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
246 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
247 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
248 return null; |
| 86 | 249 } |
| 250 } | |
| 251 } else { | |
| 252 // cache hit | |
| 253 hits++; | |
| 254 } | |
| 255 dd.refresh(); | |
| 256 if (dd.isValid()) { | |
| 257 return dd; | |
| 258 } | |
| 259 return null; | |
| 260 } | |
| 261 | |
| 262 /** | |
| 263 * @return String[] | |
| 264 */ | |
| 265 public String[] getBaseDirNames() { | |
| 266 return baseDirNames; | |
| 267 } | |
| 268 | |
| 269 /** | |
| 270 * @return long | |
| 271 */ | |
| 272 public long getNumFiles() { | |
| 273 return numFiles; | |
| 274 } | |
| 275 | |
| 276 /** | |
| 277 * Sets the baseDirNames. | |
| 278 * @param baseDirNames The baseDirNames to set | |
| 279 */ | |
| 280 public void setBaseDirNames(String[] baseDirNames) { | |
| 281 this.baseDirNames = baseDirNames; | |
| 282 } | |
| 283 | |
| 284 /** | |
| 285 * @return long | |
| 286 */ | |
| 287 public long getHits() { | |
| 288 return hits; | |
| 289 } | |
| 290 | |
| 291 /** | |
| 292 * @return long | |
| 293 */ | |
| 294 public long getMisses() { | |
| 295 return misses; | |
| 296 } | |
| 297 | |
| 159 | 298 /** |
| 299 * @return | |
| 300 */ | |
| 301 public int[] getFileClasses() { | |
| 302 return fileClasses; | |
| 303 } | |
| 304 | |
| 305 /** | |
| 306 * @param fileClasses | |
| 307 */ | |
| 308 public void setFileClasses(int[] fileClasses) { | |
| 309 this.fileClasses = fileClasses; | |
| 310 } | |
| 311 | |
| 86 | 312 } |
