Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuDirCache.java @ 812:b484631f37c1 jquery
better intersect function
| author | hertzhaft |
|---|---|
| date | Mon, 21 Feb 2011 01:00:26 +0100 |
| parents | 4f5aaa0de456 |
| children | 485b85f6e097 |
| rev | line source |
|---|---|
| 176 | 1 /* |
| 2 * DocuDirCache.java | |
| 3 * | |
| 4 * Digital Image Library servlet components | |
| 5 * | |
| 6 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de) | |
| 7 * | |
| 8 * This program is free software; you can redistribute it and/or modify it | |
| 9 * under the terms of the GNU General Public License as published by the Free | |
| 10 * Software Foundation; either version 2 of the License, or (at your option) | |
| 11 * any later version. | |
| 12 * | |
| 13 * Please read license.txt for the full details. A copy of the GPL may be found | |
| 14 * at http://www.gnu.org/copyleft/lgpl.html | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License along with | |
| 17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
| 18 * Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 * | |
| 86 | 20 * Created on 03.03.2003 |
| 21 */ | |
| 22 | |
| 23 package digilib.io; | |
| 24 | |
| 25 import java.io.File; | |
| 91 | 26 import java.util.HashMap; |
| 159 | 27 import java.util.LinkedList; |
| 28 import java.util.List; | |
| 270 | 29 import java.util.Map; |
| 86 | 30 |
| 181 | 31 import org.apache.log4j.Logger; |
| 32 | |
| 563 | 33 import digilib.io.FileOps.FileClass; |
| 259 | 34 import digilib.servlet.DigilibConfiguration; |
| 35 | |
| 86 | 36 /** |
| 37 * @author casties | |
| 38 */ | |
| 91 | 39 public class DocuDirCache { |
| 86 | 40 |
| 181 | 41 /** general logger for this class */ |
| 270 | 42 Logger logger = Logger.getLogger(this.getClass()); |
| 282 | 43 |
| 159 | 44 /** HashMap of directories */ |
| 531 | 45 Map<String, DocuDirectory> map = null; |
| 282 | 46 |
| 159 | 47 /** names of base directories */ |
| 270 | 48 String[] baseDirNames = null; |
| 282 | 49 |
| 159 | 50 /** array of allowed file classes (image/text) */ |
| 563 | 51 private FileClass[] fileClasses = null; |
| 282 | 52 |
| 159 | 53 /** number of files in the whole cache (approximate) */ |
| 270 | 54 long numFiles = 0; |
| 282 | 55 |
| 159 | 56 /** number of cache hits */ |
| 270 | 57 long hits = 0; |
| 282 | 58 |
| 159 | 59 /** number of cache misses */ |
| 270 | 60 long misses = 0; |
| 282 | 61 |
| 270 | 62 /** the root directory element */ |
| 63 public static Directory ROOT = null; | |
| 86 | 64 |
| 176 | 65 /** |
| 66 * Constructor with array of base directory names and file classes. | |
| 67 * | |
| 68 * @param bd | |
| 69 * base directory names | |
| 159 | 70 */ |
| 563 | 71 public DocuDirCache(String[] bd, FileClass[] fcs, |
| 282 | 72 DigilibConfiguration dlConfig) { |
| 159 | 73 baseDirNames = bd; |
| 531 | 74 map = new HashMap<String, DocuDirectory>(); |
| 563 | 75 this.fileClasses = fcs; |
| 159 | 76 } |
| 282 | 77 |
| 176 | 78 /** |
| 79 * Constructor with array of base directory names. | |
| 80 * | |
| 81 * @param bd | |
| 82 * base directory names | |
| 86 | 83 */ |
| 84 public DocuDirCache(String[] bd) { | |
| 85 baseDirNames = bd; | |
| 531 | 86 map = new HashMap<String, DocuDirectory>(); |
| 159 | 87 // default file class is CLASS_IMAGE |
| 563 | 88 fileClasses = new FileClass[] { FileClass.IMAGE }; |
| 91 | 89 } |
| 90 | |
| 176 | 91 /** |
| 92 * The number of directories in the cache. | |
| 93 * | |
| 91 | 94 * @return |
| 95 */ | |
| 96 public int size() { | |
| 97 return (map != null) ? map.size() : 0; | |
| 86 | 98 } |
| 99 | |
| 176 | 100 /** |
| 101 * Add a DocuDirectory to the cache. | |
| 86 | 102 * |
| 103 * @param newdir | |
| 104 */ | |
| 105 public void put(DocuDirectory newdir) { | |
| 106 String s = newdir.getDirName(); | |
| 750 | 107 logger.debug("DocuDirCache.put for "+s+" in "+this); |
| 91 | 108 if (map.containsKey(s)) { |
| 197 | 109 logger.warn("Duplicate key in DocuDirCache.put -- ignoring!"); |
| 86 | 110 } else { |
| 91 | 111 map.put(s, newdir); |
| 86 | 112 numFiles += newdir.size(); |
| 113 } | |
| 114 } | |
| 115 | |
| 176 | 116 /** |
| 117 * Add a directory to the cache and check its parents. | |
| 151 | 118 * |
| 119 * @param newDir | |
| 120 */ | |
| 750 | 121 public void putDir(DocuDirectory newDir) { |
| 151 | 122 put(newDir); |
| 270 | 123 String parent = FileOps.parent(newDir.getDirName()); |
| 124 if (parent != "") { | |
| 151 | 125 // check the parent in the cache |
| 563 | 126 DocuDirectory pd = map.get(parent); |
| 151 | 127 if (pd == null) { |
| 128 // the parent is unknown | |
| 129 pd = new DocuDirectory(parent, this); | |
| 130 putDir(pd); | |
| 131 } | |
| 132 newDir.setParent(pd); | |
| 133 } | |
| 134 } | |
| 135 | |
| 176 | 136 /** |
| 137 * Get a list with all children of a directory. | |
| 159 | 138 * |
| 282 | 139 * Returns a List of DocuDirectory's. Returns an empty List if the directory |
| 140 * has no children. If recurse is false then only direct children are | |
| 141 * returned. | |
| 159 | 142 * |
| 143 * @param dirname | |
| 176 | 144 * @param recurse |
| 145 * find all children and their children. | |
| 159 | 146 * @return |
| 147 */ | |
| 531 | 148 public List<DocuDirectory> getChildren(String dirname, boolean recurse) { |
| 149 List<DocuDirectory> l = new LinkedList<DocuDirectory>(); | |
| 150 for (DocuDirectory dd: map.values()) { | |
| 159 | 151 if (recurse) { |
| 152 if (dd.getDirName().startsWith(dirname)) { | |
| 153 l.add(dd); | |
| 154 } | |
| 155 } else { | |
| 270 | 156 if (FileOps.parent(dd.getDirName()).equals(dirname)) { |
| 159 | 157 l.add(dd); |
| 158 } | |
| 159 } | |
| 160 } | |
| 161 return l; | |
| 162 } | |
| 163 | |
| 176 | 164 /** |
| 282 | 165 * Returns the DocuDirent with the pathname <code>fn</code> and the index |
| 166 * <code>in</code> and the class <code>fc</code>. | |
| 91 | 167 * |
| 187 | 168 * If <code>fn</code> is a file then the corresponding DocuDirent is |
| 91 | 169 * returned and the index is ignored. |
| 170 * | |
| 176 | 171 * @param fn |
| 172 * digilib pathname | |
| 173 * @param in | |
| 174 * file index | |
| 246 | 175 * @param fc |
| 282 | 176 * file class |
| 176 | 177 * @return |
| 91 | 178 */ |
| 563 | 179 public DocuDirent getFile(String fn, int in, FileClass fc) { |
| 86 | 180 DocuDirectory dd; |
| 181 // file number is 1-based, vector index is 0-based | |
| 182 int n = in - 1; | |
| 183 // first, assume fn is a directory and look in the cache | |
| 563 | 184 dd = map.get(fn); |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
185 // logger.debug("fn: " + fn); |
|
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
186 // logger.debug("dd: " + dd); |
| 91 | 187 if (dd == null) { |
| 188 // cache miss | |
| 189 misses++; | |
| 176 | 190 /* |
| 287 | 191 * see if fn is a directory |
| 176 | 192 */ |
| 152 | 193 File f = new File(baseDirNames[0], fn); |
| 91 | 194 if (f.isDirectory()) { |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
195 // logger.debug(fn + " is a dir"); |
| 151 | 196 dd = new DocuDirectory(fn, this); |
| 91 | 197 if (dd.isValid()) { |
| 198 // add to the cache | |
| 151 | 199 putDir(dd); |
| 91 | 200 } |
| 201 } else { | |
| 176 | 202 /* |
| 203 * maybe it's a file | |
| 204 */ | |
| 205 // get the parent directory string (like we store it in the | |
| 206 // cache) | |
| 246 | 207 String d = FileOps.parent(fn); |
| 176 | 208 // try it in the cache |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
209 // logger.debug(fn + " is a file in dir " + d); |
| 563 | 210 dd = map.get(d); |
| 176 | 211 if (dd == null) { |
| 212 // try to read from disk | |
| 213 dd = new DocuDirectory(d, this); | |
| 214 if (dd.isValid()) { | |
| 215 // add to the cache | |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
216 // logger.debug(dd + " is valid"); |
| 176 | 217 putDir(dd); |
| 91 | 218 } else { |
| 176 | 219 // invalid path |
| 220 return null; | |
| 91 | 221 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
222 } else { |
| 176 | 223 // it was not a real cache miss |
| 224 misses--; | |
| 91 | 225 } |
| 176 | 226 // get the file's index |
| 227 n = dd.indexOf(f.getName(), fc); | |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
228 // logger.debug(f.getName() + ", index is " + n + ", fc = " + fc); |
| 91 | 229 } |
| 230 } else { | |
| 231 // cache hit | |
| 232 hits++; | |
| 233 } | |
| 234 dd.refresh(); | |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
235 // logger.debug(dd + " refreshed"); |
| 91 | 236 if (dd.isValid()) { |
| 237 try { | |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
238 // logger.debug(dd + " is valid"); |
| 159 | 239 return dd.get(n, fc); |
| 187 | 240 } catch (IndexOutOfBoundsException e) { |
|
473
fca40a188a22
modified DocuDirent.compareTo instead of ImageFileset.compareTo
hertzhaft
parents:
472
diff
changeset
|
241 // logger.debug(fn + " not found in directory"); |
| 91 | 242 } |
| 243 } | |
| 244 return null; | |
| 245 } | |
| 246 | |
| 176 | 247 /** |
| 248 * Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
| 91 | 249 * |
| 250 * If <code>fn</code> is a file then its parent directory is returned. | |
| 251 * | |
| 176 | 252 * @param fn |
| 253 * digilib pathname | |
| 91 | 254 * @return |
| 255 */ | |
| 256 public DocuDirectory getDirectory(String fn) { | |
| 257 DocuDirectory dd; | |
| 258 // first, assume fn is a directory and look in the cache | |
| 563 | 259 dd = map.get(fn); |
| 86 | 260 if (dd == null) { |
| 261 // cache miss | |
| 262 misses++; | |
| 263 // see if it's a directory | |
| 152 | 264 File f = new File(baseDirNames[0], fn); |
| 86 | 265 if (f.isDirectory()) { |
| 151 | 266 dd = new DocuDirectory(fn, this); |
| 86 | 267 if (dd.isValid()) { |
| 268 // add to the cache | |
| 151 | 269 putDir(dd); |
| 86 | 270 } |
| 271 } else { | |
| 272 // maybe it's a file | |
| 273 if (f.canRead()) { | |
| 274 // try the parent directory in the cache | |
| 563 | 275 dd = map.get(f.getParent()); |
| 86 | 276 if (dd == null) { |
| 277 // try to read from disk | |
| 151 | 278 dd = new DocuDirectory(f.getParent(), this); |
| 86 | 279 if (dd.isValid()) { |
| 280 // add to the cache | |
| 151 | 281 putDir(dd); |
| 86 | 282 } else { |
| 283 // invalid path | |
| 284 return null; | |
| 285 } | |
| 91 | 286 } else { |
| 287 // not a real cache miss then | |
| 288 misses--; | |
| 86 | 289 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
290 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
291 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
292 return null; |
| 86 | 293 } |
| 294 } | |
| 295 } else { | |
| 296 // cache hit | |
| 297 hits++; | |
| 298 } | |
| 299 dd.refresh(); | |
| 300 if (dd.isValid()) { | |
| 301 return dd; | |
| 302 } | |
| 303 return null; | |
| 304 } | |
| 305 | |
| 306 /** | |
| 307 * @return String[] | |
| 308 */ | |
| 309 public String[] getBaseDirNames() { | |
| 310 return baseDirNames; | |
| 311 } | |
| 312 | |
| 313 /** | |
| 314 * @return long | |
| 315 */ | |
| 316 public long getNumFiles() { | |
| 317 return numFiles; | |
| 318 } | |
| 319 | |
| 320 /** | |
| 321 * Sets the baseDirNames. | |
| 176 | 322 * |
| 323 * @param baseDirNames | |
| 324 * The baseDirNames to set | |
| 86 | 325 */ |
| 326 public void setBaseDirNames(String[] baseDirNames) { | |
| 327 this.baseDirNames = baseDirNames; | |
| 328 } | |
| 329 | |
| 330 /** | |
| 331 * @return long | |
| 332 */ | |
| 333 public long getHits() { | |
| 334 return hits; | |
| 335 } | |
| 336 | |
| 337 /** | |
| 338 * @return long | |
| 339 */ | |
| 340 public long getMisses() { | |
| 341 return misses; | |
| 342 } | |
| 343 | |
| 159 | 344 /** |
| 345 * @return | |
| 346 */ | |
| 563 | 347 public FileClass[] getFileClasses() { |
| 159 | 348 return fileClasses; |
| 349 } | |
| 350 | |
| 351 /** | |
| 352 * @param fileClasses | |
| 353 */ | |
| 563 | 354 public void setFileClasses(FileClass[] fileClasses) { |
| 159 | 355 this.fileClasses = fileClasses; |
| 356 } | |
| 176 | 357 |
| 86 | 358 } |
