comparison servlet/src/digilib/io/DocuDirCache.java @ 86:997ba69afb81

New version 1.8b1. With directory and file information cache. With enhanceRGB method for color correction.
author robcast
date Sun, 09 Mar 2003 21:37:27 +0100
parents
children a398fc09ba71
comparison
equal deleted inserted replaced
85:4e6757e8ccd4 86:997ba69afb81
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;
25 import java.util.Hashtable;
26
27 /**
28 * @author casties
29 */
30 public class DocuDirCache extends Hashtable {
31
32 // names of base directories
33 private String[] baseDirNames = null;
34 // number of files in the whole cache (not reliable)
35 private long numFiles = 0;
36 // number of cache hits
37 private long hits = 0;
38 // number of cache misses
39 private long misses = 0;
40
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.
61 *
62 * @param bd base directory names
63 */
64 public DocuDirCache(String[] bd) {
65 super();
66 baseDirNames = bd;
67 }
68
69 /** Add a DocuDirectory to the cache.
70 *
71 * @param newdir
72 */
73 public void put(DocuDirectory newdir) {
74 String s = newdir.getDirName();
75 if (containsKey(s)) {
76 System.out.println("Baah, duplicate key in DocuDirectory.put!");
77 } else {
78 super.put(s, newdir);
79 numFiles += newdir.size();
80 }
81 }
82
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
88 dd = (DocuDirectory) 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 // try the parent directory in the cache
104 dd = (DocuDirectory) get(f.getParent());
105 if (dd == null) {
106 // try to read from disk
107 dd = new DocuDirectory(f.getParent(), baseDirNames);
108 if (dd.isValid()) {
109 // add to the cache
110 put(dd);
111 } else {
112 // invalid path
113 return null;
114 }
115 }
116 // get the file's index
117 n = dd.indexOf(f.getName());
118 }
119 }
120 } else {
121 // cache hit
122 hits++;
123 }
124 dd.refresh();
125 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;
174 }
175 return null;
176 }
177
178 /**
179 * @return String[]
180 */
181 public String[] getBaseDirNames() {
182 return baseDirNames;
183 }
184
185 /**
186 * @return long
187 */
188 public long getNumFiles() {
189 return numFiles;
190 }
191
192 /**
193 * Sets the baseDirNames.
194 * @param baseDirNames The baseDirNames to set
195 */
196 public void setBaseDirNames(String[] baseDirNames) {
197 this.baseDirNames = baseDirNames;
198 }
199
200 /**
201 * @return long
202 */
203 public long getHits() {
204 return hits;
205 }
206
207 /**
208 * @return long
209 */
210 public long getMisses() {
211 return misses;
212 }
213
214 }