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
|
91
|
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 */
|
86
|
83 public DocuFileset getFileset(String fn, int in) {
|
|
84 DocuDirectory dd;
|
|
85 // file number is 1-based, vector index is 0-based
|
|
86 int n = in - 1;
|
|
87 // first, assume fn is a directory and look in the cache
|
91
|
88 dd = (DocuDirectory) map.get(fn);
|
|
89 if (dd == null) {
|
|
90 // cache miss
|
|
91 misses++;
|
|
92 // see if it's a directory
|
|
93 File f = new File(baseDirNames[0] + fn);
|
|
94 if (f.isDirectory()) {
|
|
95 dd = new DocuDirectory(fn, baseDirNames);
|
|
96 if (dd.isValid()) {
|
|
97 // add to the cache
|
|
98 put(dd);
|
|
99 }
|
|
100 } else {
|
|
101 // maybe it's a file
|
|
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);
|
86
|
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()) {
|
|
164 // try the parent directory in the cache
|
91
|
165 dd = (DocuDirectory) map.get(f.getParent());
|
86
|
166 if (dd == null) {
|
|
167 // try to read from disk
|
|
168 dd = new DocuDirectory(f.getParent(), baseDirNames);
|
|
169 if (dd.isValid()) {
|
|
170 // add to the cache
|
|
171 put(dd);
|
|
172 } else {
|
|
173 // invalid path
|
|
174 return null;
|
|
175 }
|
91
|
176 } else {
|
|
177 // not a real cache miss then
|
|
178 misses--;
|
86
|
179 }
|
|
180 }
|
|
181 }
|
|
182 } else {
|
|
183 // cache hit
|
|
184 hits++;
|
|
185 }
|
|
186 dd.refresh();
|
|
187 if (dd.isValid()) {
|
|
188 return dd;
|
|
189 }
|
|
190 return null;
|
|
191 }
|
|
192
|
|
193 /**
|
|
194 * @return String[]
|
|
195 */
|
|
196 public String[] getBaseDirNames() {
|
|
197 return baseDirNames;
|
|
198 }
|
|
199
|
|
200 /**
|
|
201 * @return long
|
|
202 */
|
|
203 public long getNumFiles() {
|
|
204 return numFiles;
|
|
205 }
|
|
206
|
|
207 /**
|
|
208 * Sets the baseDirNames.
|
|
209 * @param baseDirNames The baseDirNames to set
|
|
210 */
|
|
211 public void setBaseDirNames(String[] baseDirNames) {
|
|
212 this.baseDirNames = baseDirNames;
|
|
213 }
|
|
214
|
|
215 /**
|
|
216 * @return long
|
|
217 */
|
|
218 public long getHits() {
|
|
219 return hits;
|
|
220 }
|
|
221
|
|
222 /**
|
|
223 * @return long
|
|
224 */
|
|
225 public long getMisses() {
|
|
226 return misses;
|
|
227 }
|
|
228
|
|
229 }
|