Mercurial > hg > digilib-old
annotate common/src/main/java/digilib/io/DocuDirCache.java @ 957:c4bd6f984aee
don't set position of scaler div, just size.
| author | robcast |
|---|---|
| date | Fri, 13 Jan 2012 14:07:57 +0100 |
| parents | 7bcc6765c209 |
| children |
| 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; | |
| 159 | 26 import java.util.LinkedList; |
| 27 import java.util.List; | |
| 758 | 28 import java.util.concurrent.ConcurrentHashMap; |
| 759 | 29 import java.util.concurrent.ConcurrentMap; |
| 937 | 30 import java.util.concurrent.atomic.AtomicInteger; |
| 86 | 31 |
| 181 | 32 import org.apache.log4j.Logger; |
| 33 | |
| 563 | 34 import digilib.io.FileOps.FileClass; |
| 259 | 35 import digilib.servlet.DigilibConfiguration; |
| 36 | |
| 86 | 37 /** |
| 38 * @author casties | |
| 39 */ | |
| 91 | 40 public class DocuDirCache { |
| 86 | 41 |
| 181 | 42 /** general logger for this class */ |
| 270 | 43 Logger logger = Logger.getLogger(this.getClass()); |
| 282 | 44 |
| 159 | 45 /** HashMap of directories */ |
| 759 | 46 ConcurrentMap<String, DocuDirectory> map = new ConcurrentHashMap<String, DocuDirectory>(); |
| 282 | 47 |
| 159 | 48 /** names of base directories */ |
| 270 | 49 String[] baseDirNames = null; |
| 282 | 50 |
| 159 | 51 /** array of allowed file classes (image/text) */ |
| 563 | 52 private FileClass[] fileClasses = null; |
| 282 | 53 |
| 159 | 54 /** number of files in the whole cache (approximate) */ |
| 937 | 55 protected AtomicInteger numImgFiles = new AtomicInteger(0); |
| 282 | 56 |
| 159 | 57 /** number of cache hits */ |
| 937 | 58 protected AtomicInteger hits = new AtomicInteger(0); |
| 282 | 59 |
| 159 | 60 /** number of cache misses */ |
| 937 | 61 protected AtomicInteger misses = new AtomicInteger(0); |
| 282 | 62 |
| 270 | 63 /** the root directory element */ |
| 64 public static Directory ROOT = null; | |
| 86 | 65 |
| 176 | 66 /** |
| 67 * Constructor with array of base directory names and file classes. | |
| 68 * | |
| 69 * @param bd | |
| 70 * base directory names | |
| 159 | 71 */ |
| 563 | 72 public DocuDirCache(String[] bd, FileClass[] fcs, |
| 282 | 73 DigilibConfiguration dlConfig) { |
| 159 | 74 baseDirNames = bd; |
| 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; | |
| 159 | 86 // default file class is CLASS_IMAGE |
| 563 | 87 fileClasses = new FileClass[] { FileClass.IMAGE }; |
| 91 | 88 } |
| 89 | |
| 176 | 90 /** |
| 91 * The number of directories in the cache. | |
| 92 * | |
| 91 | 93 * @return |
| 94 */ | |
| 95 public int size() { | |
| 96 return (map != null) ? map.size() : 0; | |
| 86 | 97 } |
| 98 | |
| 176 | 99 /** |
| 100 * Add a DocuDirectory to the cache. | |
| 759 | 101 * Always returns the correct Object from the cache, |
| 102 * either newdir one or another one. | |
| 86 | 103 * |
| 104 * @param newdir | |
| 759 | 105 * @return dir |
| 86 | 106 */ |
| 759 | 107 public DocuDirectory put(DocuDirectory newdir) { |
| 86 | 108 String s = newdir.getDirName(); |
| 750 | 109 logger.debug("DocuDirCache.put for "+s+" in "+this); |
| 759 | 110 DocuDirectory olddir = map.putIfAbsent(s, newdir); |
| 111 if (olddir != null) { | |
| 197 | 112 logger.warn("Duplicate key in DocuDirCache.put -- ignoring!"); |
| 759 | 113 return olddir; |
| 86 | 114 } |
| 937 | 115 numImgFiles.addAndGet(newdir.size(FileClass.IMAGE)); |
| 759 | 116 return newdir; |
| 86 | 117 } |
| 118 | |
| 176 | 119 /** |
| 120 * Add a directory to the cache and check its parents. | |
| 759 | 121 * Always returns the correct Object from the cache, |
| 122 * either newDir one or another one. | |
| 123 * | |
| 151 | 124 * @param newDir |
| 759 | 125 * @return dir |
| 151 | 126 */ |
| 759 | 127 public DocuDirectory putDir(DocuDirectory newDir) { |
| 128 DocuDirectory dd = put(newDir); | |
| 129 if (dd.getParent() == null) { | |
| 130 // no parent link yet | |
| 131 String parent = FileOps.parent(newDir.getDirName()); | |
| 132 if (parent != "") { | |
| 133 // check the parent in the cache | |
| 134 DocuDirectory pd = map.get(parent); | |
| 135 if (pd == null) { | |
| 136 // the parent is unknown | |
| 137 pd = new DocuDirectory(parent, this); | |
| 138 pd = putDir(pd); | |
| 139 } | |
| 140 newDir.setParent(pd); | |
| 151 | 141 } |
| 142 } | |
| 759 | 143 return dd; |
| 151 | 144 } |
| 145 | |
| 176 | 146 /** |
| 147 * Get a list with all children of a directory. | |
| 159 | 148 * |
| 282 | 149 * Returns a List of DocuDirectory's. Returns an empty List if the directory |
| 150 * has no children. If recurse is false then only direct children are | |
| 151 * returned. | |
| 159 | 152 * |
| 153 * @param dirname | |
| 176 | 154 * @param recurse |
| 155 * find all children and their children. | |
| 159 | 156 * @return |
| 157 */ | |
| 531 | 158 public List<DocuDirectory> getChildren(String dirname, boolean recurse) { |
| 159 List<DocuDirectory> l = new LinkedList<DocuDirectory>(); | |
| 160 for (DocuDirectory dd: map.values()) { | |
| 159 | 161 if (recurse) { |
| 162 if (dd.getDirName().startsWith(dirname)) { | |
| 163 l.add(dd); | |
| 164 } | |
| 165 } else { | |
| 270 | 166 if (FileOps.parent(dd.getDirName()).equals(dirname)) { |
| 159 | 167 l.add(dd); |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 return l; | |
| 172 } | |
| 173 | |
| 176 | 174 /** |
| 282 | 175 * Returns the DocuDirent with the pathname <code>fn</code> and the index |
| 176 * <code>in</code> and the class <code>fc</code>. | |
| 91 | 177 * |
| 187 | 178 * If <code>fn</code> is a file then the corresponding DocuDirent is |
| 91 | 179 * returned and the index is ignored. |
| 180 * | |
| 176 | 181 * @param fn |
| 182 * digilib pathname | |
| 183 * @param in | |
| 184 * file index | |
| 246 | 185 * @param fc |
| 282 | 186 * file class |
| 176 | 187 * @return |
| 91 | 188 */ |
| 563 | 189 public DocuDirent getFile(String fn, int in, FileClass fc) { |
| 86 | 190 DocuDirectory dd; |
| 191 // file number is 1-based, vector index is 0-based | |
| 192 int n = in - 1; | |
| 193 // first, assume fn is a directory and look in the cache | |
| 563 | 194 dd = map.get(fn); |
| 91 | 195 if (dd == null) { |
| 196 // cache miss | |
| 937 | 197 misses.incrementAndGet(); |
| 176 | 198 /* |
| 287 | 199 * see if fn is a directory |
| 176 | 200 */ |
| 152 | 201 File f = new File(baseDirNames[0], fn); |
| 91 | 202 if (f.isDirectory()) { |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
203 // logger.debug(fn + " is a dir"); |
| 151 | 204 dd = new DocuDirectory(fn, this); |
| 91 | 205 if (dd.isValid()) { |
| 206 // add to the cache | |
| 759 | 207 dd = putDir(dd); |
| 91 | 208 } |
| 209 } else { | |
| 176 | 210 /* |
| 211 * maybe it's a file | |
| 212 */ | |
| 213 // get the parent directory string (like we store it in the | |
| 214 // cache) | |
| 246 | 215 String d = FileOps.parent(fn); |
| 176 | 216 // 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
|
217 // logger.debug(fn + " is a file in dir " + d); |
| 563 | 218 dd = map.get(d); |
| 176 | 219 if (dd == null) { |
| 220 // try to read from disk | |
| 221 dd = new DocuDirectory(d, this); | |
| 222 if (dd.isValid()) { | |
| 223 // add to the cache | |
|
472
f8ca069517a2
Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
hertzhaft
parents:
287
diff
changeset
|
224 // logger.debug(dd + " is valid"); |
| 759 | 225 dd = putDir(dd); |
| 91 | 226 } else { |
| 176 | 227 // invalid path |
| 228 return null; | |
| 91 | 229 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
230 } else { |
| 176 | 231 // it was not a real cache miss |
| 937 | 232 misses.decrementAndGet(); |
| 91 | 233 } |
| 176 | 234 // get the file's index |
| 235 n = dd.indexOf(f.getName(), fc); | |
| 91 | 236 } |
| 237 } else { | |
| 238 // cache hit | |
| 937 | 239 hits.incrementAndGet(); |
| 91 | 240 } |
| 241 dd.refresh(); | |
| 242 if (dd.isValid()) { | |
| 243 try { | |
| 159 | 244 return dd.get(n, fc); |
| 187 | 245 } catch (IndexOutOfBoundsException e) { |
|
473
fca40a188a22
modified DocuDirent.compareTo instead of ImageFileset.compareTo
hertzhaft
parents:
472
diff
changeset
|
246 // logger.debug(fn + " not found in directory"); |
| 91 | 247 } |
| 248 } | |
| 249 return null; | |
| 250 } | |
| 251 | |
| 176 | 252 /** |
| 253 * Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
| 91 | 254 * |
| 255 * If <code>fn</code> is a file then its parent directory is returned. | |
| 256 * | |
| 176 | 257 * @param fn |
| 258 * digilib pathname | |
| 91 | 259 * @return |
| 260 */ | |
| 261 public DocuDirectory getDirectory(String fn) { | |
| 262 DocuDirectory dd; | |
| 263 // first, assume fn is a directory and look in the cache | |
| 563 | 264 dd = map.get(fn); |
| 86 | 265 if (dd == null) { |
| 266 // cache miss | |
| 937 | 267 misses.incrementAndGet(); |
| 86 | 268 // see if it's a directory |
| 152 | 269 File f = new File(baseDirNames[0], fn); |
| 86 | 270 if (f.isDirectory()) { |
| 151 | 271 dd = new DocuDirectory(fn, this); |
| 86 | 272 if (dd.isValid()) { |
| 273 // add to the cache | |
| 759 | 274 dd = putDir(dd); |
| 86 | 275 } |
| 276 } else { | |
| 277 // maybe it's a file | |
| 278 if (f.canRead()) { | |
| 279 // try the parent directory in the cache | |
| 563 | 280 dd = map.get(f.getParent()); |
| 86 | 281 if (dd == null) { |
| 282 // try to read from disk | |
| 151 | 283 dd = new DocuDirectory(f.getParent(), this); |
| 86 | 284 if (dd.isValid()) { |
| 285 // add to the cache | |
| 759 | 286 dd = putDir(dd); |
| 86 | 287 } else { |
| 288 // invalid path | |
| 289 return null; | |
| 290 } | |
| 91 | 291 } else { |
| 292 // not a real cache miss then | |
| 937 | 293 misses.decrementAndGet(); |
| 86 | 294 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
295 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
296 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
297 return null; |
| 86 | 298 } |
| 299 } | |
| 300 } else { | |
| 301 // cache hit | |
| 937 | 302 hits.incrementAndGet(); |
| 86 | 303 } |
| 304 dd.refresh(); | |
| 305 if (dd.isValid()) { | |
| 306 return dd; | |
| 307 } | |
| 308 return null; | |
| 309 } | |
| 310 | |
| 311 /** | |
| 312 * @return String[] | |
| 313 */ | |
| 314 public String[] getBaseDirNames() { | |
| 315 return baseDirNames; | |
| 316 } | |
| 317 | |
| 318 /** | |
| 319 * Sets the baseDirNames. | |
| 176 | 320 * |
| 321 * @param baseDirNames | |
| 322 * The baseDirNames to set | |
| 86 | 323 */ |
| 324 public void setBaseDirNames(String[] baseDirNames) { | |
| 325 this.baseDirNames = baseDirNames; | |
| 326 } | |
| 327 | |
| 937 | 328 /** |
| 329 * @return long | |
| 330 */ | |
| 331 public int getNumFiles() { | |
| 332 return numImgFiles.get(); | |
| 333 } | |
| 334 | |
| 86 | 335 /** |
| 336 * @return long | |
| 337 */ | |
| 937 | 338 public int getHits() { |
| 339 return hits.get(); | |
| 86 | 340 } |
| 341 | |
| 342 /** | |
| 343 * @return long | |
| 344 */ | |
| 937 | 345 public int getMisses() { |
| 346 return misses.get(); | |
| 86 | 347 } |
| 348 | |
| 159 | 349 /** |
| 350 * @return | |
| 351 */ | |
| 563 | 352 public FileClass[] getFileClasses() { |
| 159 | 353 return fileClasses; |
| 354 } | |
| 355 | |
| 356 /** | |
| 357 * @param fileClasses | |
| 358 */ | |
| 563 | 359 public void setFileClasses(FileClass[] fileClasses) { |
| 159 | 360 this.fileClasses = fileClasses; |
| 361 } | |
| 176 | 362 |
| 86 | 363 } |
