changeset 282:87dca7119596

Servlet version 1.22b1 - more fast searching (hopefully all working now) - some simple synchronisation - some reshuffling of methods to eliminate cruft
author robcast
date Fri, 15 Oct 2004 16:59:47 +0200
parents 60a064f27d25
children b09009d7fcfc
files servlet/src/digilib/image/JAIImageLoaderDocuImage.java servlet/src/digilib/io/Directory.java servlet/src/digilib/io/DocuDirCache.java servlet/src/digilib/io/DocuDirectory.java
diffstat 4 files changed, 54 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Fri Oct 15 16:59:47 2004 +0200
+++ b/servlet/src/digilib/image/JAIImageLoaderDocuImage.java	Fri Oct 15 16:59:47 2004 +0200
@@ -92,36 +92,40 @@
 	}
 
 	/* Get an ImageReader for the image file. */
-	public void preloadImage(ImageFile f) throws IOException {
+	public ImageReader getReader(ImageFile f) throws IOException {
 		logger.debug("preloadImage: "+f.getFile());
 		//System.gc();
 		RandomAccessFile rf = new RandomAccessFile(f.getFile(), "r");
 		ImageInputStream istream = new FileImageInputStream(rf);
-		Iterator readers = ImageIO.getImageReaders(istream);
-		//Iterator readers = ImageIO.getImageReadersByMIMEType(f.getMimetype());
-		reader = (ImageReader) readers.next();
-		if (reader == null) {
+		//Iterator readers = ImageIO.getImageReaders(istream);
+		Iterator readers = ImageIO.getImageReadersByMIMEType(f.getMimetype());
+		if (! readers.hasNext()) {
 			throw new FileOpException("Unable to load File!");
 		}
+		reader = (ImageReader) readers.next();
 		logger.debug("JAIImageIO: this reader: " + reader.getClass());
+		while (readers.hasNext()) {
+			logger.debug("  next reader: " + readers.next().getClass());
+		}
 		reader.setInput(istream);
+		return reader;
 	}
 
 	/* Load an image file into the Object. */
 	public void loadSubimage(ImageFile f, Rectangle region, int prescale)
 		throws FileOpException {
 		logger.debug("loadSubimage: "+f.getFile());
-		//System.gc();
+		System.gc();
 		try {
 			if ((reader == null) || (imgFile != f.getFile())) {
-				preloadImage(f);
+				getReader(f);
 			}
-			ImageInputStream istream = (ImageInputStream) reader.getInput();
 			ImageReadParam readParam = reader.getDefaultReadParam();
 			readParam.setSourceRegion(region);
 			readParam.setSourceSubsampling(prescale, prescale, 0, 0);
 			img = reader.read(0, readParam);
 			/* JAI imageread seems to ignore the readParam :-(
+			ImageInputStream istream = (ImageInputStream) reader.getInput();
 			ParameterBlockJAI pb = new ParameterBlockJAI("imageread");
 			pb.setParameter("Input", istream);
 			pb.setParameter("ReadParam", readParam);
--- a/servlet/src/digilib/io/Directory.java	Fri Oct 15 16:59:47 2004 +0200
+++ b/servlet/src/digilib/io/Directory.java	Fri Oct 15 16:59:47 2004 +0200
@@ -35,9 +35,9 @@
 	protected Logger logger = Logger.getLogger(this.getClass());
 
 	/** File object pointing to the directory */
-	File dir = null;
+	protected File dir = null;
 	/** parent directory */
-	Directory parent = null;
+	protected Directory parent = null;
 	/** list of filenames in the directory */
 	protected String[] list = null;
 
@@ -82,10 +82,10 @@
 	 */
 	public boolean readDir() {
 		if (dir != null) {
-			logger.debug("start reading dir");
+			logger.debug("reading dir: "+dir.getPath());
 			list = dir.list();
 			Arrays.sort(list);
-			logger.debug("done reading dir");
+			logger.debug("  done");
 		}
 		return (list != null);
 	}
--- a/servlet/src/digilib/io/DocuDirCache.java	Fri Oct 15 16:59:47 2004 +0200
+++ b/servlet/src/digilib/io/DocuDirCache.java	Fri Oct 15 16:59:47 2004 +0200
@@ -40,20 +40,28 @@
 
 	/** general logger for this class */
 	Logger logger = Logger.getLogger(this.getClass());
+
 	/** HashMap of directories */
 	Map map = null;
+
 	/** names of base directories */
 	String[] baseDirNames = null;
+
 	/** array of allowed file classes (image/text) */
 	private int[] fileClasses = null;
+
 	/** number of files in the whole cache (approximate) */
 	long numFiles = 0;
+
 	/** number of cache hits */
 	long hits = 0;
+
 	/** number of cache misses */
 	long misses = 0;
+
 	/** use safe (but slow) indexing */
 	boolean safeDirIndex = false;
+
 	/** the root directory element */
 	public static Directory ROOT = null;
 
@@ -63,12 +71,14 @@
 	 * @param bd
 	 *            base directory names
 	 */
-	public DocuDirCache(String[] bd, int[] fileClasses, DigilibConfiguration dlConfig) {
+	public DocuDirCache(String[] bd, int[] fileClasses,
+			DigilibConfiguration dlConfig) {
 		baseDirNames = bd;
 		map = new HashMap();
 		this.fileClasses = fileClasses;
 		safeDirIndex = dlConfig.getAsBoolean("safe-dir-index");
 	}
+
 	/**
 	 * Constructor with array of base directory names.
 	 * 
@@ -112,7 +122,7 @@
 	 * 
 	 * @param newDir
 	 */
-	public void putDir(DocuDirectory newDir) {
+	public synchronized void putDir(DocuDirectory newDir) {
 		put(newDir);
 		String parent = FileOps.parent(newDir.getDirName());
 		if (parent != "") {
@@ -130,9 +140,9 @@
 	/**
 	 * Get a list with all children of a directory.
 	 * 
-	 * Returns a List of DocuDirectory's. Returns an empty List if the
-	 * directory has no children. If recurse is false then only direct children
-	 * are returned.
+	 * Returns a List of DocuDirectory's. Returns an empty List if the directory
+	 * has no children. If recurse is false then only direct children are
+	 * returned.
 	 * 
 	 * @param dirname
 	 * @param recurse
@@ -158,8 +168,8 @@
 	}
 
 	/**
-	 * Returns the DocuDirent with the pathname <code>fn</code> and the
-	 * index <code>in</code> and the class <code>fc</code>.
+	 * Returns the DocuDirent with the pathname <code>fn</code> and the index
+	 * <code>in</code> and the class <code>fc</code>.
 	 * 
 	 * If <code>fn</code> is a file then the corresponding DocuDirent is
 	 * returned and the index is ignored.
@@ -169,7 +179,7 @@
 	 * @param in
 	 *            file index
 	 * @param fc
-	 * 			  file class
+	 *            file class
 	 * @return
 	 */
 	public DocuDirent getFile(String fn, int in, int fc) {
--- a/servlet/src/digilib/io/DocuDirectory.java	Fri Oct 15 16:59:47 2004 +0200
+++ b/servlet/src/digilib/io/DocuDirectory.java	Fri Oct 15 16:59:47 2004 +0200
@@ -166,7 +166,7 @@
 	 * 
 	 * @return boolean the directory exists
 	 */
-	public boolean readDir() {
+	public synchronized boolean readDir() {
 		// check directory first
 		checkDir();
 		if (!isValid) {
@@ -204,7 +204,9 @@
 			File d = new File(baseDirNames[j], dirName);
 			if (d.isDirectory()) {
 				dirs[j] = new Directory(d);
+				logger.debug("  reading scaled directory " + d.getPath());
 				dirs[j].readDir();
+				logger.debug("    done");
 			}
 		}
 
@@ -290,13 +292,9 @@
 					unresolvedFileMeta = fileMeta;
 				}
 			} catch (SAXException e) {
-				if (cache != null) {
-					cache.logger.warn("error parsing index.meta", e);
-				}
+				logger.warn("error parsing index.meta", e);
 			} catch (IOException e) {
-				if (cache != null) {
-					cache.logger.warn("error reading index.meta", e);
-				}
+				logger.warn("error reading index.meta", e);
 			}
 		}
 		readParentMeta();
@@ -425,16 +423,23 @@
 			return idx;
 		} else {
 			// try closest matches without extension
-			idx = -idx;
+			idx = -idx - 1;
 			String fb = FileOps.basename(fn);
-			DocuDirent fs = (DocuDirent) list[fc].get(idx - 1);
-			if (FileOps.basename(fs.getName()).equals(fb)) {
-				// basename matches
+			DocuDirent fs;
+			if ((idx < list.length)
+					&& (FileOps.basename(((DocuDirent) list[fc].get(idx))
+							.getName()).equals(fb))) {
+				// idx matches
+				return idx;
+			} else if ((idx > 0)
+					&& (FileOps.basename(((DocuDirent) list[fc].get(idx - 1))
+							.getName()).equals(fb))) {
+				// idx-1 matches
 				return idx - 1;
-			}
-			fs = (DocuDirent) list[fc].get(idx + 1);
-			if (FileOps.basename(fs.getName()).equals(fb)) {
-				// basename matches
+			} else if ((idx + 1 < list.length)
+					&& (FileOps.basename(((DocuDirent) list[fc].get(idx - 1))
+							.getName()).equals(fb))) {
+				// idx+1 matches
 				return idx + 1;
 			}