Mercurial > hg > digilib-old
comparison servlet/src/digilib/io/DocuDirCache.java @ 563:686086d6e6d6 digilibPDF
more refactoring and rearranging
more enums
pdf and image generation still works
author | robcast |
---|---|
date | Fri, 17 Dec 2010 20:42:25 +0100 |
parents | 88ec23c2b2fb |
children | 4f5aaa0de456 |
comparison
equal
deleted
inserted
replaced
562:baaa5bab5e16 | 563:686086d6e6d6 |
---|---|
28 import java.util.List; | 28 import java.util.List; |
29 import java.util.Map; | 29 import java.util.Map; |
30 | 30 |
31 import org.apache.log4j.Logger; | 31 import org.apache.log4j.Logger; |
32 | 32 |
33 import digilib.io.FileOps.FileClass; | |
33 import digilib.servlet.DigilibConfiguration; | 34 import digilib.servlet.DigilibConfiguration; |
34 | 35 |
35 /** | 36 /** |
36 * @author casties | 37 * @author casties |
37 */ | 38 */ |
45 | 46 |
46 /** names of base directories */ | 47 /** names of base directories */ |
47 String[] baseDirNames = null; | 48 String[] baseDirNames = null; |
48 | 49 |
49 /** array of allowed file classes (image/text) */ | 50 /** array of allowed file classes (image/text) */ |
50 private int[] fileClasses = null; | 51 private FileClass[] fileClasses = null; |
51 | 52 |
52 /** number of files in the whole cache (approximate) */ | 53 /** number of files in the whole cache (approximate) */ |
53 long numFiles = 0; | 54 long numFiles = 0; |
54 | 55 |
55 /** number of cache hits */ | 56 /** number of cache hits */ |
65 * Constructor with array of base directory names and file classes. | 66 * Constructor with array of base directory names and file classes. |
66 * | 67 * |
67 * @param bd | 68 * @param bd |
68 * base directory names | 69 * base directory names |
69 */ | 70 */ |
70 public DocuDirCache(String[] bd, int[] fileClasses, | 71 public DocuDirCache(String[] bd, FileClass[] fcs, |
71 DigilibConfiguration dlConfig) { | 72 DigilibConfiguration dlConfig) { |
72 baseDirNames = bd; | 73 baseDirNames = bd; |
73 map = new HashMap<String, DocuDirectory>(); | 74 map = new HashMap<String, DocuDirectory>(); |
74 this.fileClasses = fileClasses; | 75 this.fileClasses = fcs; |
75 } | 76 } |
76 | 77 |
77 /** | 78 /** |
78 * Constructor with array of base directory names. | 79 * Constructor with array of base directory names. |
79 * | 80 * |
82 */ | 83 */ |
83 public DocuDirCache(String[] bd) { | 84 public DocuDirCache(String[] bd) { |
84 baseDirNames = bd; | 85 baseDirNames = bd; |
85 map = new HashMap<String, DocuDirectory>(); | 86 map = new HashMap<String, DocuDirectory>(); |
86 // default file class is CLASS_IMAGE | 87 // default file class is CLASS_IMAGE |
87 fileClasses = new int[1]; | 88 fileClasses = new FileClass[] { FileClass.IMAGE }; |
88 fileClasses[0] = FileOps.CLASS_IMAGE; | |
89 } | 89 } |
90 | 90 |
91 /** | 91 /** |
92 * The number of directories in the cache. | 92 * The number of directories in the cache. |
93 * | 93 * |
120 public synchronized void putDir(DocuDirectory newDir) { | 120 public synchronized void putDir(DocuDirectory newDir) { |
121 put(newDir); | 121 put(newDir); |
122 String parent = FileOps.parent(newDir.getDirName()); | 122 String parent = FileOps.parent(newDir.getDirName()); |
123 if (parent != "") { | 123 if (parent != "") { |
124 // check the parent in the cache | 124 // check the parent in the cache |
125 DocuDirectory pd = (DocuDirectory) map.get(parent); | 125 DocuDirectory pd = map.get(parent); |
126 if (pd == null) { | 126 if (pd == null) { |
127 // the parent is unknown | 127 // the parent is unknown |
128 pd = new DocuDirectory(parent, this); | 128 pd = new DocuDirectory(parent, this); |
129 putDir(pd); | 129 putDir(pd); |
130 } | 130 } |
173 * file index | 173 * file index |
174 * @param fc | 174 * @param fc |
175 * file class | 175 * file class |
176 * @return | 176 * @return |
177 */ | 177 */ |
178 public DocuDirent getFile(String fn, int in, int fc) { | 178 public DocuDirent getFile(String fn, int in, FileClass fc) { |
179 DocuDirectory dd; | 179 DocuDirectory dd; |
180 // file number is 1-based, vector index is 0-based | 180 // file number is 1-based, vector index is 0-based |
181 int n = in - 1; | 181 int n = in - 1; |
182 // first, assume fn is a directory and look in the cache | 182 // first, assume fn is a directory and look in the cache |
183 dd = (DocuDirectory) map.get(fn); | 183 dd = map.get(fn); |
184 // logger.debug("fn: " + fn); | 184 // logger.debug("fn: " + fn); |
185 // logger.debug("dd: " + dd); | 185 // logger.debug("dd: " + dd); |
186 if (dd == null) { | 186 if (dd == null) { |
187 // cache miss | 187 // cache miss |
188 misses++; | 188 misses++; |
204 // get the parent directory string (like we store it in the | 204 // get the parent directory string (like we store it in the |
205 // cache) | 205 // cache) |
206 String d = FileOps.parent(fn); | 206 String d = FileOps.parent(fn); |
207 // try it in the cache | 207 // try it in the cache |
208 // logger.debug(fn + " is a file in dir " + d); | 208 // logger.debug(fn + " is a file in dir " + d); |
209 dd = (DocuDirectory) map.get(d); | 209 dd = map.get(d); |
210 if (dd == null) { | 210 if (dd == null) { |
211 // try to read from disk | 211 // try to read from disk |
212 dd = new DocuDirectory(d, this); | 212 dd = new DocuDirectory(d, this); |
213 if (dd.isValid()) { | 213 if (dd.isValid()) { |
214 // add to the cache | 214 // add to the cache |
253 * @return | 253 * @return |
254 */ | 254 */ |
255 public DocuDirectory getDirectory(String fn) { | 255 public DocuDirectory getDirectory(String fn) { |
256 DocuDirectory dd; | 256 DocuDirectory dd; |
257 // first, assume fn is a directory and look in the cache | 257 // first, assume fn is a directory and look in the cache |
258 dd = (DocuDirectory) map.get(fn); | 258 dd = map.get(fn); |
259 if (dd == null) { | 259 if (dd == null) { |
260 // cache miss | 260 // cache miss |
261 misses++; | 261 misses++; |
262 // see if it's a directory | 262 // see if it's a directory |
263 File f = new File(baseDirNames[0], fn); | 263 File f = new File(baseDirNames[0], fn); |
269 } | 269 } |
270 } else { | 270 } else { |
271 // maybe it's a file | 271 // maybe it's a file |
272 if (f.canRead()) { | 272 if (f.canRead()) { |
273 // try the parent directory in the cache | 273 // try the parent directory in the cache |
274 dd = (DocuDirectory) map.get(f.getParent()); | 274 dd = map.get(f.getParent()); |
275 if (dd == null) { | 275 if (dd == null) { |
276 // try to read from disk | 276 // try to read from disk |
277 dd = new DocuDirectory(f.getParent(), this); | 277 dd = new DocuDirectory(f.getParent(), this); |
278 if (dd.isValid()) { | 278 if (dd.isValid()) { |
279 // add to the cache | 279 // add to the cache |
341 } | 341 } |
342 | 342 |
343 /** | 343 /** |
344 * @return | 344 * @return |
345 */ | 345 */ |
346 public int[] getFileClasses() { | 346 public FileClass[] getFileClasses() { |
347 return fileClasses; | 347 return fileClasses; |
348 } | 348 } |
349 | 349 |
350 /** | 350 /** |
351 * @param fileClasses | 351 * @param fileClasses |
352 */ | 352 */ |
353 public void setFileClasses(int[] fileClasses) { | 353 public void setFileClasses(FileClass[] fileClasses) { |
354 this.fileClasses = fileClasses; | 354 this.fileClasses = fileClasses; |
355 } | 355 } |
356 | 356 |
357 } | 357 } |