Mercurial > hg > digilib-old
annotate servlet/src/digilib/io/DocuDirCache.java @ 154:e4f095227510
Adapted to new DigilibConfig class.
| author | robcast |
|---|---|
| date | Wed, 03 Sep 2003 00:58:04 +0200 |
| parents | f4a5cfe37469 |
| children | e743b853efca |
| 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; |
| 86 | 26 |
| 27 /** | |
| 28 * @author casties | |
| 29 */ | |
| 91 | 30 public class DocuDirCache { |
| 86 | 31 |
| 91 | 32 // HashMap of directories |
| 33 private HashMap map = null; | |
| 86 | 34 // names of base directories |
| 35 private String[] baseDirNames = null; | |
| 91 | 36 // number of files in the whole cache (approximate) |
| 86 | 37 private long numFiles = 0; |
| 38 // number of cache hits | |
| 39 private long hits = 0; | |
| 40 // number of cache misses | |
| 41 private long misses = 0; | |
| 42 | |
| 43 /** Constructor with array of base directory names. | |
| 44 * | |
| 45 * @param bd base directory names | |
| 46 */ | |
| 47 public DocuDirCache(String[] bd) { | |
| 48 baseDirNames = bd; | |
| 91 | 49 map = new HashMap(); |
| 50 } | |
| 51 | |
| 52 /** The number of directories in the cache. | |
| 53 * @return | |
| 54 */ | |
| 55 public int size() { | |
| 56 return (map != null) ? map.size() : 0; | |
| 86 | 57 } |
| 58 | |
| 59 /** Add a DocuDirectory to the cache. | |
| 60 * | |
| 61 * @param newdir | |
| 62 */ | |
| 63 public void put(DocuDirectory newdir) { | |
| 64 String s = newdir.getDirName(); | |
| 91 | 65 if (map.containsKey(s)) { |
| 86 | 66 System.out.println("Baah, duplicate key in DocuDirectory.put!"); |
| 67 } else { | |
| 91 | 68 map.put(s, newdir); |
| 86 | 69 numFiles += newdir.size(); |
| 70 } | |
| 71 } | |
| 72 | |
| 151 | 73 /** Add a directory to the cache and check its parents. |
| 74 * | |
| 75 * @param newDir | |
| 76 */ | |
| 77 public void putDir(DocuDirectory newDir) { | |
| 78 put(newDir); | |
| 79 String parent = newDir.getParentDirName(); | |
| 80 if (parent != null) { | |
| 81 // check the parent in the cache | |
| 82 DocuDirectory pd = (DocuDirectory)map.get(parent); | |
| 83 if (pd == null) { | |
| 84 // the parent is unknown | |
| 85 pd = new DocuDirectory(parent, this); | |
| 86 putDir(pd); | |
| 87 } | |
| 88 newDir.setParent(pd); | |
| 89 } | |
| 90 // update dir in the end | |
| 91 newDir.readParentMeta(); | |
| 92 } | |
| 93 | |
| 91 | 94 /** Returns the DocuFileset with the pathname <code>fn</code> and the |
| 95 * index <code>in</code>. | |
| 96 * | |
| 97 * If <code>fn</code> is a file then the corresponding Fileset is | |
| 98 * returned and the index is ignored. | |
| 99 * | |
| 100 * @param fn digilib pathname | |
| 101 * @param in file index | |
| 102 * @return | |
| 103 */ | |
| 86 | 104 public DocuFileset getFileset(String fn, int in) { |
| 105 DocuDirectory dd; | |
| 106 // file number is 1-based, vector index is 0-based | |
| 107 int n = in - 1; | |
| 108 // first, assume fn is a directory and look in the cache | |
| 91 | 109 dd = (DocuDirectory) map.get(fn); |
| 110 if (dd == null) { | |
| 111 // cache miss | |
| 112 misses++; | |
| 113 // see if it's a directory | |
| 152 | 114 File f = new File(baseDirNames[0], fn); |
| 91 | 115 if (f.isDirectory()) { |
| 151 | 116 dd = new DocuDirectory(fn, this); |
| 91 | 117 if (dd.isValid()) { |
| 118 // add to the cache | |
| 151 | 119 putDir(dd); |
| 91 | 120 } |
| 121 } else { | |
| 122 // maybe it's a file | |
| 123 if (f.canRead()) { | |
| 116 | 124 // get the parent directory string (like we store it in the cache) |
| 125 String d = fn.substring(0, fn.lastIndexOf("/")); | |
| 91 | 126 // try it in the cache |
| 127 dd = (DocuDirectory) map.get(d); | |
| 128 if (dd == null) { | |
| 129 // try to read from disk | |
| 151 | 130 dd = new DocuDirectory(d, this); |
| 91 | 131 if (dd.isValid()) { |
| 132 // add to the cache | |
| 151 | 133 putDir(dd); |
| 91 | 134 } else { |
| 135 // invalid path | |
| 136 return null; | |
| 137 } | |
| 138 } else { | |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
139 // then it was not a real cache miss |
| 91 | 140 misses--; |
| 141 } | |
| 142 // get the file's index | |
| 143 n = dd.indexOf(f.getName()); | |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
144 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
145 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
146 return null; |
| 91 | 147 } |
| 148 } | |
| 149 } else { | |
| 150 // cache hit | |
| 151 hits++; | |
| 152 } | |
| 153 dd.refresh(); | |
| 154 if (dd.isValid()) { | |
| 155 try { | |
| 156 return dd.get(n); | |
| 157 } catch (ArrayIndexOutOfBoundsException e) { | |
| 158 } | |
| 159 } | |
| 160 return null; | |
| 161 } | |
| 162 | |
| 163 /** Returns the DocuDirectory indicated by the pathname <code>fn</code>. | |
| 164 * | |
| 165 * If <code>fn</code> is a file then its parent directory is returned. | |
| 166 * | |
| 167 * @param fn digilib pathname | |
| 168 * @return | |
| 169 */ | |
| 170 public DocuDirectory getDirectory(String fn) { | |
| 171 DocuDirectory dd; | |
| 172 // first, assume fn is a directory and look in the cache | |
| 173 dd = (DocuDirectory) map.get(fn); | |
| 86 | 174 if (dd == null) { |
| 175 // cache miss | |
| 176 misses++; | |
| 177 // see if it's a directory | |
| 152 | 178 File f = new File(baseDirNames[0], fn); |
| 86 | 179 if (f.isDirectory()) { |
| 151 | 180 dd = new DocuDirectory(fn, this); |
| 86 | 181 if (dd.isValid()) { |
| 182 // add to the cache | |
| 151 | 183 putDir(dd); |
| 86 | 184 } |
| 185 } else { | |
| 186 // maybe it's a file | |
| 187 if (f.canRead()) { | |
| 188 // try the parent directory in the cache | |
| 91 | 189 dd = (DocuDirectory) map.get(f.getParent()); |
| 86 | 190 if (dd == null) { |
| 191 // try to read from disk | |
| 151 | 192 dd = new DocuDirectory(f.getParent(), this); |
| 86 | 193 if (dd.isValid()) { |
| 194 // add to the cache | |
| 151 | 195 putDir(dd); |
| 86 | 196 } else { |
| 197 // invalid path | |
| 198 return null; | |
| 199 } | |
| 91 | 200 } else { |
| 201 // not a real cache miss then | |
| 202 misses--; | |
| 86 | 203 } |
|
99
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
204 } else { |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
205 // it's not even a file :-( |
|
226624784fe3
Small bug lead to null pointer exception when directory doesn't exist.
robcast
parents:
91
diff
changeset
|
206 return null; |
| 86 | 207 } |
| 208 } | |
| 209 } else { | |
| 210 // cache hit | |
| 211 hits++; | |
| 212 } | |
| 213 dd.refresh(); | |
| 214 if (dd.isValid()) { | |
| 215 return dd; | |
| 216 } | |
| 217 return null; | |
| 218 } | |
| 219 | |
| 220 /** | |
| 221 * @return String[] | |
| 222 */ | |
| 223 public String[] getBaseDirNames() { | |
| 224 return baseDirNames; | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * @return long | |
| 229 */ | |
| 230 public long getNumFiles() { | |
| 231 return numFiles; | |
| 232 } | |
| 233 | |
| 234 /** | |
| 235 * Sets the baseDirNames. | |
| 236 * @param baseDirNames The baseDirNames to set | |
| 237 */ | |
| 238 public void setBaseDirNames(String[] baseDirNames) { | |
| 239 this.baseDirNames = baseDirNames; | |
| 240 } | |
| 241 | |
| 242 /** | |
| 243 * @return long | |
| 244 */ | |
| 245 public long getHits() { | |
| 246 return hits; | |
| 247 } | |
| 248 | |
| 249 /** | |
| 250 * @return long | |
| 251 */ | |
| 252 public long getMisses() { | |
| 253 return misses; | |
| 254 } | |
| 255 | |
| 256 } |
