comparison common/src/main/java/digilib/io/DocuDirCache.java @ 937:7bcc6765c209

improved runtime statistics
author robcast
date Wed, 21 Dec 2011 19:18:16 +0100
parents 7779b37d1d05
children
comparison
equal deleted inserted replaced
936:172079f9a398 937:7bcc6765c209
25 import java.io.File; 25 import java.io.File;
26 import java.util.LinkedList; 26 import java.util.LinkedList;
27 import java.util.List; 27 import java.util.List;
28 import java.util.concurrent.ConcurrentHashMap; 28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap; 29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.atomic.AtomicInteger;
30 31
31 import org.apache.log4j.Logger; 32 import org.apache.log4j.Logger;
32 33
33 import digilib.io.FileOps.FileClass; 34 import digilib.io.FileOps.FileClass;
34 import digilib.servlet.DigilibConfiguration; 35 import digilib.servlet.DigilibConfiguration;
49 50
50 /** array of allowed file classes (image/text) */ 51 /** array of allowed file classes (image/text) */
51 private FileClass[] fileClasses = null; 52 private FileClass[] fileClasses = null;
52 53
53 /** number of files in the whole cache (approximate) */ 54 /** number of files in the whole cache (approximate) */
54 long numFiles = 0; 55 protected AtomicInteger numImgFiles = new AtomicInteger(0);
55 56
56 /** number of cache hits */ 57 /** number of cache hits */
57 long hits = 0; 58 protected AtomicInteger hits = new AtomicInteger(0);
58 59
59 /** number of cache misses */ 60 /** number of cache misses */
60 long misses = 0; 61 protected AtomicInteger misses = new AtomicInteger(0);
61 62
62 /** the root directory element */ 63 /** the root directory element */
63 public static Directory ROOT = null; 64 public static Directory ROOT = null;
64 65
65 /** 66 /**
109 DocuDirectory olddir = map.putIfAbsent(s, newdir); 110 DocuDirectory olddir = map.putIfAbsent(s, newdir);
110 if (olddir != null) { 111 if (olddir != null) {
111 logger.warn("Duplicate key in DocuDirCache.put -- ignoring!"); 112 logger.warn("Duplicate key in DocuDirCache.put -- ignoring!");
112 return olddir; 113 return olddir;
113 } 114 }
114 numFiles += newdir.size(); 115 numImgFiles.addAndGet(newdir.size(FileClass.IMAGE));
115 return newdir; 116 return newdir;
116 } 117 }
117 118
118 /** 119 /**
119 * Add a directory to the cache and check its parents. 120 * Add a directory to the cache and check its parents.
191 int n = in - 1; 192 int n = in - 1;
192 // first, assume fn is a directory and look in the cache 193 // first, assume fn is a directory and look in the cache
193 dd = map.get(fn); 194 dd = map.get(fn);
194 if (dd == null) { 195 if (dd == null) {
195 // cache miss 196 // cache miss
196 misses++; 197 misses.incrementAndGet();
197 /* 198 /*
198 * see if fn is a directory 199 * see if fn is a directory
199 */ 200 */
200 File f = new File(baseDirNames[0], fn); 201 File f = new File(baseDirNames[0], fn);
201 if (f.isDirectory()) { 202 if (f.isDirectory()) {
226 // invalid path 227 // invalid path
227 return null; 228 return null;
228 } 229 }
229 } else { 230 } else {
230 // it was not a real cache miss 231 // it was not a real cache miss
231 misses--; 232 misses.decrementAndGet();
232 } 233 }
233 // get the file's index 234 // get the file's index
234 n = dd.indexOf(f.getName(), fc); 235 n = dd.indexOf(f.getName(), fc);
235 } 236 }
236 } else { 237 } else {
237 // cache hit 238 // cache hit
238 hits++; 239 hits.incrementAndGet();
239 } 240 }
240 dd.refresh(); 241 dd.refresh();
241 if (dd.isValid()) { 242 if (dd.isValid()) {
242 try { 243 try {
243 return dd.get(n, fc); 244 return dd.get(n, fc);
261 DocuDirectory dd; 262 DocuDirectory dd;
262 // first, assume fn is a directory and look in the cache 263 // first, assume fn is a directory and look in the cache
263 dd = map.get(fn); 264 dd = map.get(fn);
264 if (dd == null) { 265 if (dd == null) {
265 // cache miss 266 // cache miss
266 misses++; 267 misses.incrementAndGet();
267 // see if it's a directory 268 // see if it's a directory
268 File f = new File(baseDirNames[0], fn); 269 File f = new File(baseDirNames[0], fn);
269 if (f.isDirectory()) { 270 if (f.isDirectory()) {
270 dd = new DocuDirectory(fn, this); 271 dd = new DocuDirectory(fn, this);
271 if (dd.isValid()) { 272 if (dd.isValid()) {
287 // invalid path 288 // invalid path
288 return null; 289 return null;
289 } 290 }
290 } else { 291 } else {
291 // not a real cache miss then 292 // not a real cache miss then
292 misses--; 293 misses.decrementAndGet();
293 } 294 }
294 } else { 295 } else {
295 // it's not even a file :-( 296 // it's not even a file :-(
296 return null; 297 return null;
297 } 298 }
298 } 299 }
299 } else { 300 } else {
300 // cache hit 301 // cache hit
301 hits++; 302 hits.incrementAndGet();
302 } 303 }
303 dd.refresh(); 304 dd.refresh();
304 if (dd.isValid()) { 305 if (dd.isValid()) {
305 return dd; 306 return dd;
306 } 307 }
313 public String[] getBaseDirNames() { 314 public String[] getBaseDirNames() {
314 return baseDirNames; 315 return baseDirNames;
315 } 316 }
316 317
317 /** 318 /**
318 * @return long
319 */
320 public long getNumFiles() {
321 return numFiles;
322 }
323
324 /**
325 * Sets the baseDirNames. 319 * Sets the baseDirNames.
326 * 320 *
327 * @param baseDirNames 321 * @param baseDirNames
328 * The baseDirNames to set 322 * The baseDirNames to set
329 */ 323 */
330 public void setBaseDirNames(String[] baseDirNames) { 324 public void setBaseDirNames(String[] baseDirNames) {
331 this.baseDirNames = baseDirNames; 325 this.baseDirNames = baseDirNames;
332 } 326 }
333 327
328 /**
329 * @return long
330 */
331 public int getNumFiles() {
332 return numImgFiles.get();
333 }
334
334 /** 335 /**
335 * @return long 336 * @return long
336 */ 337 */
337 public long getHits() { 338 public int getHits() {
338 return hits; 339 return hits.get();
339 } 340 }
340 341
341 /** 342 /**
342 * @return long 343 * @return long
343 */ 344 */
344 public long getMisses() { 345 public int getMisses() {
345 return misses; 346 return misses.get();
346 } 347 }
347 348
348 /** 349 /**
349 * @return 350 * @return
350 */ 351 */