annotate servlet/src/digilib/io/DocuDirCache.java @ 339:6d2032b6121d gen2_1

new directory and cache work
author robcast
date Wed, 17 Nov 2004 18:17:34 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
1 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
2 * DocuDirCache.java
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
3 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
4 * Digital Image Library servlet components
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
5 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
6 * Copyright (C) 2003 Robert Casties (robcast@mail.berlios.de)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
7 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
8 * This program is free software; you can redistribute it and/or modify it
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the Free
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
10 * Software Foundation; either version 2 of the License, or (at your option)
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
11 * any later version.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
12 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
13 * Please read license.txt for the full details. A copy of the GPL may be found
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
14 * at http://www.gnu.org/copyleft/lgpl.html
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
15 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
16 * You should have received a copy of the GNU General Public License along with
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
18 * Place, Suite 330, Boston, MA 02111-1307 USA
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
19 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
20 * Created on 03.03.2003
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
21 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
22
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
23 package digilib.io;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
24
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
25 import java.io.File;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
26 import java.util.HashMap;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
27 import java.util.Map;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
28
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
29 import org.apache.log4j.Logger;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
30
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
31 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
32 * Cache of digilib directories.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
33 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
34 * @author casties
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
35 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
36 public class DocuDirCache {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
37
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
38 /** general logger for this class */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
39 protected static Logger logger = Logger.getLogger(DocuDirCache.class);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
40
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
41 /** Map of directories */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
42 Map map = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
43
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
44 /** number of files in the whole cache (approximate) */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
45 long numFiles = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
46
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
47 /** number of cache hits */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
48 long hits = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
49
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
50 /** number of cache misses */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
51 long misses = 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
52
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
53 /** the root directory element */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
54 public DigiDirectory rootDir = null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
55
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
56 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
57 * Default constructor.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
58 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
59 public DocuDirCache() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
60 map = new HashMap();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
61 // add root directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
62 rootDir = new DigiDirectory(FileOps.getBaseDirs()[0], "", null);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
63 map.put("", rootDir);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
64 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
65
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
66 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
67 * The number of directories in the cache.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
68 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
69 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
70 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
71 public int size() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
72 return (map != null) ? map.size() : 0;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
73 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
74
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
75 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
76 * Add a DocuDirectory to the cache.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
77 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
78 * @param newdir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
79 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
80 public void put(DigiDirectory newdir) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
81 String s = newdir.getDLPath();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
82 Object oldkey = map.put(s, newdir);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
83 if (oldkey != null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
84 logger.warn("Duplicate key in DocuDirCache.put -- overwritten!");
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
85 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
86 numFiles += newdir.getSize();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
87 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
88
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
89 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
90 * Add a directory to the cache and check its parents.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
91 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
92 * @param newDir
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
93 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
94 public synchronized void putDir(DigiDirectory newDir) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
95 put(newDir);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
96 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
97
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
98 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
99 * Returns a DigiDirectory from the cache.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
100 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
101 * @param path
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
102 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
103 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
104 public DigiDirectory get(String path) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
105 return (DigiDirectory) map.get(path);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
106 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
107
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
108 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
109 * Returns the DigiDirectory indicated by the pathname <code>fn</code>.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
110 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
111 * If <code>fn</code> is a file then its parent directory is returned.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
112 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
113 * @param fn
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
114 * digilib pathname
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
115 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
116 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
117 public DigiDirectory getDirectory(String pn) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
118 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
119 * first, assume pn is a directory and look in the cache
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
120 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
121 DigiDirectory dd = (DigiDirectory) map.get(pn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
122 if (dd != null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
123 // cache hit
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
124 hits++;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
125 return dd;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
126 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
127 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
128 * maybe it's a file? try the parent directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
129 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
130 String dn = FileOps.dlParent(pn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
131 // try it in the cache
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
132 dd = (DigiDirectory) map.get(dn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
133 if (dd != null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
134 // cache hit
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
135 hits++;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
136 return dd;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
137 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
138 // cache miss
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
139 misses++;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
140 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
141 * try to read from disk
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
142 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
143 File f = FileOps.getRealFile(pn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
144 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
145 * is it a directory?
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
146 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
147 if (f.isDirectory()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
148 dd = new DigiDirectory(f, pn, null);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
149 // add to the cache
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
150 putDir(dd);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
151 return dd;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
152 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
153 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
154 * then maybe a file? try the parent as a directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
155 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
156 File d = FileOps.getRealFile(dn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
157 if (d.isDirectory()) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
158 dd = new DigiDirectory(d, dn, null);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
159 // add to the cache
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
160 putDir(dd);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
161 return dd;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
162 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
163 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
164 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
165 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
166 /*
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
167 * otherwise it's crap
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
168 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
169 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
170 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
171
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
172 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
173 * Returns the DigiDirent with the pathname <code>fn</code> and the index
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
174 * <code>in</code> and the class <code>fc</code>.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
175 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
176 * If <code>fn</code> is a file then the corresponding DocuDirent is
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
177 * returned and the index is ignored.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
178 *
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
179 * @param pn
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
180 * digilib pathname
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
181 * @param in
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
182 * file index
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
183 * @param fc
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
184 * file class
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
185 * @return
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
186 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
187 public DigiDirent getFile(String pn, int in, int fc) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
188 // file number is 1-based, vector index is 0-based
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
189 int n = in - 1;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
190 // get the (parent) directory
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
191 DigiDirectory dd = getDirectory(pn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
192 if (dd != null) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
193 // get the directory's name
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
194 String dn = dd.getDLPath();
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
195 if (dn.equals(pn)) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
196 // return the file at the index
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
197 return dd.get(n, fc);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
198 } else {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
199 // then the last part must be the filename
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
200 String fn = FileOps.dlName(pn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
201 return dd.get(fn);
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
202 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
203 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
204 return null;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
205 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
206
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
207 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
208 * @return long
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
209 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
210 public long getNumFiles() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
211 return numFiles;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
212 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
213
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
214 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
215 * @return long
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
216 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
217 public long getHits() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
218 return hits;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
219 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
220
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
221 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
222 * @return long
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
223 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
224 public long getMisses() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
225 return misses;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
226 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
227
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
228 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
229 * @return Returns the rootDir.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
230 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
231 public DigiDirectory getRootDir() {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
232 return rootDir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
233 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
234 /**
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
235 * @param rootDir The rootDir to set.
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
236 */
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
237 public void setRootDir(DigiDirectory rootDir) {
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
238 this.rootDir = rootDir;
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
239 }
6d2032b6121d new directory and cache work
robcast
parents:
diff changeset
240 }