Mercurial > hg > digilib-old
diff servlet/src/digilib/io/DocuDirCache.java @ 91:a398fc09ba71
New version 1.8b4.
DocuFile classes use new Collection classes and has-a instead of is-a
list and map relations.
author | robcast |
---|---|
date | Mon, 17 Mar 2003 15:59:12 +0100 |
parents | 997ba69afb81 |
children | 226624784fe3 |
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirCache.java Mon Mar 17 15:57:54 2003 +0100 +++ b/servlet/src/digilib/io/DocuDirCache.java Mon Mar 17 15:59:12 2003 +0100 @@ -22,48 +22,38 @@ package digilib.io; import java.io.File; -import java.util.Hashtable; +import java.util.HashMap; /** * @author casties */ -public class DocuDirCache extends Hashtable { +public class DocuDirCache { + // HashMap of directories + private HashMap map = null; // names of base directories private String[] baseDirNames = null; - // number of files in the whole cache (not reliable) + // number of files in the whole cache (approximate) private long numFiles = 0; // number of cache hits private long hits = 0; // number of cache misses private long misses = 0; - /* - * inherited constructors - */ - public DocuDirCache(int initialCapacity, float loadFactor) { - super(initialCapacity, loadFactor); - } - - public DocuDirCache(int initialCapacity) { - super(initialCapacity); - } - - public DocuDirCache() { - super(); - } - - /* - * new and exiting stuff - */ - /** Constructor with array of base directory names. * * @param bd base directory names */ public DocuDirCache(String[] bd) { - super(); baseDirNames = bd; + map = new HashMap(); + } + + /** The number of directories in the cache. + * @return + */ + public int size() { + return (map != null) ? map.size() : 0; } /** Add a DocuDirectory to the cache. @@ -72,20 +62,91 @@ */ public void put(DocuDirectory newdir) { String s = newdir.getDirName(); - if (containsKey(s)) { + if (map.containsKey(s)) { System.out.println("Baah, duplicate key in DocuDirectory.put!"); } else { - super.put(s, newdir); + map.put(s, newdir); numFiles += newdir.size(); } } + /** Returns the DocuFileset with the pathname <code>fn</code> and the + * index <code>in</code>. + * + * If <code>fn</code> is a file then the corresponding Fileset is + * returned and the index is ignored. + * + * @param fn digilib pathname + * @param in file index + * @return + */ public DocuFileset getFileset(String fn, int in) { DocuDirectory dd; // file number is 1-based, vector index is 0-based int n = in - 1; // first, assume fn is a directory and look in the cache - dd = (DocuDirectory) get(fn); + dd = (DocuDirectory) map.get(fn); + if (dd == null) { + // cache miss + misses++; + // see if it's a directory + File f = new File(baseDirNames[0] + fn); + if (f.isDirectory()) { + dd = new DocuDirectory(fn, baseDirNames); + if (dd.isValid()) { + // add to the cache + put(dd); + } + } else { + // maybe it's a file + if (f.canRead()) { + // get the parent directory + String d = fn.substring(0, fn.lastIndexOf(File.separator)); + // try it in the cache + dd = (DocuDirectory) map.get(d); + if (dd == null) { + // try to read from disk + dd = new DocuDirectory(d, baseDirNames); + if (dd.isValid()) { + // add to the cache + put(dd); + } else { + // invalid path + return null; + } + } else { + // not a real cache miss then + misses--; + } + // get the file's index + n = dd.indexOf(f.getName()); + } + } + } else { + // cache hit + hits++; + } + dd.refresh(); + if (dd.isValid()) { + try { + return dd.get(n); + } catch (ArrayIndexOutOfBoundsException e) { + } + } + return null; + } + + /** Returns the DocuDirectory indicated by the pathname <code>fn</code>. + * + * If <code>fn</code> is a file then its parent directory is returned. + * + * @param fn digilib pathname + * @return + */ + public DocuDirectory getDirectory(String fn) { + DocuDirectory dd; + // first, assume fn is a directory and look in the cache + dd = (DocuDirectory) map.get(fn); if (dd == null) { // cache miss misses++; @@ -101,7 +162,7 @@ // maybe it's a file if (f.canRead()) { // try the parent directory in the cache - dd = (DocuDirectory) get(f.getParent()); + dd = (DocuDirectory) map.get(f.getParent()); if (dd == null) { // try to read from disk dd = new DocuDirectory(f.getParent(), baseDirNames); @@ -112,55 +173,9 @@ // invalid path return null; } - } - // get the file's index - n = dd.indexOf(f.getName()); - } - } - } else { - // cache hit - hits++; - } - dd.refresh(); - if (dd.isValid()) { - try { - return (DocuFileset) dd.elementAt(n); - } catch (ArrayIndexOutOfBoundsException e) { - } - } - return null; - } - - public DocuDirectory getDirectory(String fn) { - DocuDirectory dd; - // first, assume fn is a directory and look in the cache - dd = (DocuDirectory) get(fn); - if (dd == null) { - // cache miss - misses++; - // see if it's a directory - File f = new File(baseDirNames[0] + fn); - if (f.isDirectory()) { - dd = new DocuDirectory(fn, baseDirNames); - if (dd.isValid()) { - // add to the cache - put(dd); - } - } else { - // maybe it's a file - if (f.canRead()) { - // try the parent directory in the cache - dd = (DocuDirectory) get(f.getParent()); - if (dd == null) { - // try to read from disk - dd = new DocuDirectory(f.getParent(), baseDirNames); - if (dd.isValid()) { - // add to the cache - put(dd); - } else { - // invalid path - return null; - } + } else { + // not a real cache miss then + misses--; } } }