comparison servlet/src/digilib/io/DocuDirCache.java @ 159:e743b853efca

servlet version 1.16a4 - rather experimental - new Texter servlet for sending text - reads and caches text files in DocuDirCache - DocuFile renamed to ImageFile (-Set) - new TextFile
author robcast
date Tue, 16 Sep 2003 18:32:00 +0200
parents f4a5cfe37469
children 67ff8c7fecb9
comparison
equal deleted inserted replaced
158:e9a81ac446cb 159:e743b853efca
21 21
22 package digilib.io; 22 package digilib.io;
23 23
24 import java.io.File; 24 import java.io.File;
25 import java.util.HashMap; 25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.LinkedList;
28 import java.util.List;
26 29
27 /** 30 /**
28 * @author casties 31 * @author casties
29 */ 32 */
30 public class DocuDirCache { 33 public class DocuDirCache {
31 34
32 // HashMap of directories 35 /** HashMap of directories */
33 private HashMap map = null; 36 private HashMap map = null;
34 // names of base directories 37 /** names of base directories */
35 private String[] baseDirNames = null; 38 private String[] baseDirNames = null;
36 // number of files in the whole cache (approximate) 39 /** array of allowed file classes (image/text) */
40 private int[] fileClasses = null;
41 /** number of files in the whole cache (approximate) */
37 private long numFiles = 0; 42 private long numFiles = 0;
38 // number of cache hits 43 /** number of cache hits */
39 private long hits = 0; 44 private long hits = 0;
40 // number of cache misses 45 /** number of cache misses */
41 private long misses = 0; 46 private long misses = 0;
42 47
48 /** Constructor with array of base directory names and file classes.
49 *
50 * @param bd base directory names
51 */
52 public DocuDirCache(String[] bd, int[] fileClasses) {
53 baseDirNames = bd;
54 map = new HashMap();
55 this.fileClasses = fileClasses;
56 }
43 /** Constructor with array of base directory names. 57 /** Constructor with array of base directory names.
44 * 58 *
45 * @param bd base directory names 59 * @param bd base directory names
46 */ 60 */
47 public DocuDirCache(String[] bd) { 61 public DocuDirCache(String[] bd) {
48 baseDirNames = bd; 62 baseDirNames = bd;
49 map = new HashMap(); 63 map = new HashMap();
64 // default file class is CLASS_IMAGE
65 fileClasses = new int[1];
66 fileClasses[0] = FileOps.CLASS_IMAGE;
50 } 67 }
51 68
52 /** The number of directories in the cache. 69 /** The number of directories in the cache.
53 * @return 70 * @return
54 */ 71 */
61 * @param newdir 78 * @param newdir
62 */ 79 */
63 public void put(DocuDirectory newdir) { 80 public void put(DocuDirectory newdir) {
64 String s = newdir.getDirName(); 81 String s = newdir.getDirName();
65 if (map.containsKey(s)) { 82 if (map.containsKey(s)) {
66 System.out.println("Baah, duplicate key in DocuDirectory.put!"); 83 System.out.println("Baah, duplicate key in DocuDirCache.put!");
67 } else { 84 } else {
68 map.put(s, newdir); 85 map.put(s, newdir);
69 numFiles += newdir.size(); 86 numFiles += newdir.size();
70 } 87 }
71 } 88 }
85 pd = new DocuDirectory(parent, this); 102 pd = new DocuDirectory(parent, this);
86 putDir(pd); 103 putDir(pd);
87 } 104 }
88 newDir.setParent(pd); 105 newDir.setParent(pd);
89 } 106 }
90 // update dir in the end 107 }
91 newDir.readParentMeta(); 108
92 } 109 /** Get a list with all children of a directory.
93 110 *
94 /** Returns the DocuFileset with the pathname <code>fn</code> and the 111 * Returns a List of DocuDirectory's.
95 * index <code>in</code>. 112 * Returns an empty List if the directory has no children.
113 * If recurse is false then only direct children are returned.
114 *
115 * @param dirname
116 * @param recurse find all children and their children.
117 * @return
118 */
119 public List getChildren(String dirname, boolean recurse) {
120 List l = new LinkedList();
121 for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {
122 DocuDirectory dd = (DocuDirectory)i.next();
123 if (recurse) {
124 if (dd.getDirName().startsWith(dirname)) {
125 l.add(dd);
126 }
127 } else {
128 if (dd.getParentDirName().equals(dirname)) {
129 l.add(dd);
130 }
131 }
132 }
133 return l;
134 }
135
136 /** Returns the ImageFileset with the pathname <code>fn</code> and the
137 * index <code>in</code> and the class <code>fc</code>.
96 * 138 *
97 * If <code>fn</code> is a file then the corresponding Fileset is 139 * If <code>fn</code> is a file then the corresponding Fileset is
98 * returned and the index is ignored. 140 * returned and the index is ignored.
99 * 141 *
100 * @param fn digilib pathname 142 * @param fn digilib pathname
101 * @param in file index 143 * @param in file index
102 * @return 144 * @return
103 */ 145 */
104 public DocuFileset getFileset(String fn, int in) { 146 public DocuDirent getFile(String fn, int in, int fc) {
105 DocuDirectory dd; 147 DocuDirectory dd;
106 // file number is 1-based, vector index is 0-based 148 // file number is 1-based, vector index is 0-based
107 int n = in - 1; 149 int n = in - 1;
108 // first, assume fn is a directory and look in the cache 150 // first, assume fn is a directory and look in the cache
109 dd = (DocuDirectory) map.get(fn); 151 dd = (DocuDirectory) map.get(fn);
138 } else { 180 } else {
139 // then it was not a real cache miss 181 // then it was not a real cache miss
140 misses--; 182 misses--;
141 } 183 }
142 // get the file's index 184 // get the file's index
143 n = dd.indexOf(f.getName()); 185 n = dd.indexOf(f.getName(), fc);
144 } else { 186 } else {
145 // it's not even a file :-( 187 // it's not even a file :-(
146 return null; 188 return null;
147 } 189 }
148 } 190 }
151 hits++; 193 hits++;
152 } 194 }
153 dd.refresh(); 195 dd.refresh();
154 if (dd.isValid()) { 196 if (dd.isValid()) {
155 try { 197 try {
156 return dd.get(n); 198 return dd.get(n, fc);
157 } catch (ArrayIndexOutOfBoundsException e) { 199 } catch (ArrayIndexOutOfBoundsException e) {
158 } 200 }
159 } 201 }
160 return null; 202 return null;
161 } 203 }
251 */ 293 */
252 public long getMisses() { 294 public long getMisses() {
253 return misses; 295 return misses;
254 } 296 }
255 297
298 /**
299 * @return
300 */
301 public int[] getFileClasses() {
302 return fileClasses;
303 }
304
305 /**
306 * @param fileClasses
307 */
308 public void setFileClasses(int[] fileClasses) {
309 this.fileClasses = fileClasses;
310 }
311
256 } 312 }