comparison 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
comparison
equal deleted inserted replaced
90:8058d3b3466a 91:a398fc09ba71
20 */ 20 */
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.Hashtable; 25 import java.util.HashMap;
26 26
27 /** 27 /**
28 * @author casties 28 * @author casties
29 */ 29 */
30 public class DocuDirCache extends Hashtable { 30 public class DocuDirCache {
31 31
32 // HashMap of directories
33 private HashMap map = null;
32 // names of base directories 34 // names of base directories
33 private String[] baseDirNames = null; 35 private String[] baseDirNames = null;
34 // number of files in the whole cache (not reliable) 36 // number of files in the whole cache (approximate)
35 private long numFiles = 0; 37 private long numFiles = 0;
36 // number of cache hits 38 // number of cache hits
37 private long hits = 0; 39 private long hits = 0;
38 // number of cache misses 40 // number of cache misses
39 private long misses = 0; 41 private long misses = 0;
40 42
41 /*
42 * inherited constructors
43 */
44 public DocuDirCache(int initialCapacity, float loadFactor) {
45 super(initialCapacity, loadFactor);
46 }
47
48 public DocuDirCache(int initialCapacity) {
49 super(initialCapacity);
50 }
51
52 public DocuDirCache() {
53 super();
54 }
55
56 /*
57 * new and exiting stuff
58 */
59
60 /** Constructor with array of base directory names. 43 /** Constructor with array of base directory names.
61 * 44 *
62 * @param bd base directory names 45 * @param bd base directory names
63 */ 46 */
64 public DocuDirCache(String[] bd) { 47 public DocuDirCache(String[] bd) {
65 super();
66 baseDirNames = bd; 48 baseDirNames = bd;
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;
67 } 57 }
68 58
69 /** Add a DocuDirectory to the cache. 59 /** Add a DocuDirectory to the cache.
70 * 60 *
71 * @param newdir 61 * @param newdir
72 */ 62 */
73 public void put(DocuDirectory newdir) { 63 public void put(DocuDirectory newdir) {
74 String s = newdir.getDirName(); 64 String s = newdir.getDirName();
75 if (containsKey(s)) { 65 if (map.containsKey(s)) {
76 System.out.println("Baah, duplicate key in DocuDirectory.put!"); 66 System.out.println("Baah, duplicate key in DocuDirectory.put!");
77 } else { 67 } else {
78 super.put(s, newdir); 68 map.put(s, newdir);
79 numFiles += newdir.size(); 69 numFiles += newdir.size();
80 } 70 }
81 } 71 }
82 72
73 /** Returns the DocuFileset with the pathname <code>fn</code> and the
74 * index <code>in</code>.
75 *
76 * If <code>fn</code> is a file then the corresponding Fileset is
77 * returned and the index is ignored.
78 *
79 * @param fn digilib pathname
80 * @param in file index
81 * @return
82 */
83 public DocuFileset getFileset(String fn, int in) { 83 public DocuFileset getFileset(String fn, int in) {
84 DocuDirectory dd; 84 DocuDirectory dd;
85 // file number is 1-based, vector index is 0-based 85 // file number is 1-based, vector index is 0-based
86 int n = in - 1; 86 int n = in - 1;
87 // first, assume fn is a directory and look in the cache 87 // first, assume fn is a directory and look in the cache
88 dd = (DocuDirectory) get(fn); 88 dd = (DocuDirectory) map.get(fn);
89 if (dd == null) { 89 if (dd == null) {
90 // cache miss 90 // cache miss
91 misses++; 91 misses++;
92 // see if it's a directory 92 // see if it's a directory
93 File f = new File(baseDirNames[0] + fn); 93 File f = new File(baseDirNames[0] + fn);
98 put(dd); 98 put(dd);
99 } 99 }
100 } else { 100 } else {
101 // maybe it's a file 101 // maybe it's a file
102 if (f.canRead()) { 102 if (f.canRead()) {
103 // get the parent directory
104 String d = fn.substring(0, fn.lastIndexOf(File.separator));
105 // try it in the cache
106 dd = (DocuDirectory) map.get(d);
107 if (dd == null) {
108 // try to read from disk
109 dd = new DocuDirectory(d, baseDirNames);
110 if (dd.isValid()) {
111 // add to the cache
112 put(dd);
113 } else {
114 // invalid path
115 return null;
116 }
117 } else {
118 // not a real cache miss then
119 misses--;
120 }
121 // get the file's index
122 n = dd.indexOf(f.getName());
123 }
124 }
125 } else {
126 // cache hit
127 hits++;
128 }
129 dd.refresh();
130 if (dd.isValid()) {
131 try {
132 return dd.get(n);
133 } catch (ArrayIndexOutOfBoundsException e) {
134 }
135 }
136 return null;
137 }
138
139 /** Returns the DocuDirectory indicated by the pathname <code>fn</code>.
140 *
141 * If <code>fn</code> is a file then its parent directory is returned.
142 *
143 * @param fn digilib pathname
144 * @return
145 */
146 public DocuDirectory getDirectory(String fn) {
147 DocuDirectory dd;
148 // first, assume fn is a directory and look in the cache
149 dd = (DocuDirectory) map.get(fn);
150 if (dd == null) {
151 // cache miss
152 misses++;
153 // see if it's a directory
154 File f = new File(baseDirNames[0] + fn);
155 if (f.isDirectory()) {
156 dd = new DocuDirectory(fn, baseDirNames);
157 if (dd.isValid()) {
158 // add to the cache
159 put(dd);
160 }
161 } else {
162 // maybe it's a file
163 if (f.canRead()) {
103 // try the parent directory in the cache 164 // try the parent directory in the cache
104 dd = (DocuDirectory) get(f.getParent()); 165 dd = (DocuDirectory) map.get(f.getParent());
105 if (dd == null) { 166 if (dd == null) {
106 // try to read from disk 167 // try to read from disk
107 dd = new DocuDirectory(f.getParent(), baseDirNames); 168 dd = new DocuDirectory(f.getParent(), baseDirNames);
108 if (dd.isValid()) { 169 if (dd.isValid()) {
109 // add to the cache 170 // add to the cache
110 put(dd); 171 put(dd);
111 } else { 172 } else {
112 // invalid path 173 // invalid path
113 return null; 174 return null;
114 } 175 }
176 } else {
177 // not a real cache miss then
178 misses--;
115 } 179 }
116 // get the file's index
117 n = dd.indexOf(f.getName());
118 } 180 }
119 } 181 }
120 } else { 182 } else {
121 // cache hit 183 // cache hit
122 hits++; 184 hits++;
123 } 185 }
124 dd.refresh(); 186 dd.refresh();
125 if (dd.isValid()) { 187 if (dd.isValid()) {
126 try {
127 return (DocuFileset) dd.elementAt(n);
128 } catch (ArrayIndexOutOfBoundsException e) {
129 }
130 }
131 return null;
132 }
133
134 public DocuDirectory getDirectory(String fn) {
135 DocuDirectory dd;
136 // first, assume fn is a directory and look in the cache
137 dd = (DocuDirectory) get(fn);
138 if (dd == null) {
139 // cache miss
140 misses++;
141 // see if it's a directory
142 File f = new File(baseDirNames[0] + fn);
143 if (f.isDirectory()) {
144 dd = new DocuDirectory(fn, baseDirNames);
145 if (dd.isValid()) {
146 // add to the cache
147 put(dd);
148 }
149 } else {
150 // maybe it's a file
151 if (f.canRead()) {
152 // try the parent directory in the cache
153 dd = (DocuDirectory) get(f.getParent());
154 if (dd == null) {
155 // try to read from disk
156 dd = new DocuDirectory(f.getParent(), baseDirNames);
157 if (dd.isValid()) {
158 // add to the cache
159 put(dd);
160 } else {
161 // invalid path
162 return null;
163 }
164 }
165 }
166 }
167 } else {
168 // cache hit
169 hits++;
170 }
171 dd.refresh();
172 if (dd.isValid()) {
173 return dd; 188 return dd;
174 } 189 }
175 return null; 190 return null;
176 } 191 }
177 192