changeset 472:f8ca069517a2

Bugfix for images not found in dir: added sorting for ArrayLists of ImageFilesets
author hertzhaft
date Fri, 07 Apr 2006 20:39:07 +0200
parents d623c6fee3c9
children fca40a188a22
files servlet/src/digilib/io/DocuDirCache.java servlet/src/digilib/io/DocuDirectory.java servlet/src/digilib/io/ImageFileset.java
diffstat 3 files changed, 31 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/servlet/src/digilib/io/DocuDirCache.java	Wed Feb 15 00:09:50 2006 +0100
+++ b/servlet/src/digilib/io/DocuDirCache.java	Fri Apr 07 20:39:07 2006 +0200
@@ -188,6 +188,8 @@
 		int n = in - 1;
 		// first, assume fn is a directory and look in the cache
 		dd = (DocuDirectory) map.get(fn);
+        // logger.debug("fn: " + fn);
+        // logger.debug("dd: " + dd);
 		if (dd == null) {
 			// cache miss
 			misses++;
@@ -196,6 +198,7 @@
 			 */
 			File f = new File(baseDirNames[0], fn);
 			if (f.isDirectory()) {
+                // logger.debug(fn + " is a dir");
 				dd = new DocuDirectory(fn, this);
 				if (dd.isValid()) {
 					// add to the cache
@@ -209,12 +212,14 @@
 				// cache)
 				String d = FileOps.parent(fn);
 				// try it in the cache
+                // logger.debug(fn + " is a file in dir " + d);
 				dd = (DocuDirectory) map.get(d);
 				if (dd == null) {
 					// try to read from disk
 					dd = new DocuDirectory(d, this);
 					if (dd.isValid()) {
 						// add to the cache
+                        // logger.debug(dd + " is valid");
 						putDir(dd);
 					} else {
 						// invalid path
@@ -226,16 +231,20 @@
 				}
 				// get the file's index
 				n = dd.indexOf(f.getName(), fc);
+                // logger.debug(f.getName() + ", index is " + n + ", fc = " + fc);
 			}
 		} else {
 			// cache hit
 			hits++;
 		}
 		dd.refresh();
+        // logger.debug(dd + " refreshed");
 		if (dd.isValid()) {
 			try {
+                // logger.debug(dd + " is valid");
 				return dd.get(n, fc);
 			} catch (IndexOutOfBoundsException e) {
+                logger.debug(fn + ": index out of bounds");
 			}
 		}
 		return null;
--- a/servlet/src/digilib/io/DocuDirectory.java	Wed Feb 15 00:09:50 2006 +0100
+++ b/servlet/src/digilib/io/DocuDirectory.java	Fri Apr 07 20:39:07 2006 +0200
@@ -30,6 +30,8 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.log4j.Logger;
+
 import org.xml.sax.SAXException;
 
 /**
@@ -175,7 +177,7 @@
 		// first file extension to try for scaled directories
 		String scalext = null;
 		// read all filenames
-		//logger.debug("reading directory " + dir.getPath());
+		logger.debug("reading directory " + dir.getPath());
 		/*
 		 * using ReadableFileFilter is safer (we won't get directories with file
 		 * extensions) but slower.
@@ -204,7 +206,7 @@
 			File d = new File(baseDirNames[j], dirName);
 			if (d.isDirectory()) {
 				dirs[j] = new Directory(d);
-				//logger.debug("  reading scaled directory " + d.getPath());
+				logger.debug("  reading scaled directory " + d.getPath());
 				dirs[j].readDir();
 				//logger.debug("    done");
 			}
@@ -224,16 +226,20 @@
 				// create new list
 				list[fileClass] = new ArrayList(numFiles);
 				// sort the file names alphabetically and iterate the list
-				Arrays.sort(fileList);
+				// Arrays.sort(fileList); // not needed <hertzhaft>
 				Map hints = FileOps.newHints(FileOps.HINT_BASEDIRS, dirs);
 				hints.put(FileOps.HINT_FILEEXT, scalext);
 				for (int i = 0; i < numFiles; i++) {
 					DocuDirent f = FileOps.fileForClass(fileClass, fileList[i],
 							hints);
 					// add the file to our list
+                    // logger.debug(f.getName());
+
 					list[fileClass].add(f);
 					f.setParent(this);
 				}
+                // we need to sort the ArrayList, not the Array, for binarysearch to work
+                Collections.sort(list[fileClass]);
 			}
 		}
 		// clear the scaled directories
@@ -418,11 +424,14 @@
 		if (fileList == null) {
 			return -1;
 		}
+        
 		// search for exact match
+        // OBS: fileList needs to be sorted first (see )! <hertzhaft>
 		int idx = Collections.binarySearch(fileList, fn);
 		if (idx >= 0) {
 			return idx;
 		} else {
+            logger.debug(fn + " not found by binarysearch");
 			// try closest matches without extension
 			idx = -idx - 1;
 			if ((idx < fileList.size())
--- a/servlet/src/digilib/io/ImageFileset.java	Wed Feb 15 00:09:50 2006 +0100
+++ b/servlet/src/digilib/io/ImageFileset.java	Fri Apr 07 20:39:07 2006 +0200
@@ -109,6 +109,16 @@
 	}
 
 	/**
+	 * Compares to a String (for binarySearch)
+     * or to another ImageFileset (for sort)
+	 */
+	public int compareTo(Object arg0) {
+		return (arg0 instanceof ImageFileset)
+            ? getName().compareTo(((ImageFileset) arg0).getName())
+            : getName().compareTo((String) arg0);
+	}
+
+	/**
 	 * Get the ImageFile at the index.
 	 * 
 	 *