From 6c819a6fd9beedd3d5ff5ec8d129bc354f95c45a Mon Sep 17 00:00:00 2001 From: orbiter Date: Thu, 6 Sep 2007 01:28:35 +0000 Subject: [PATCH] added cache to favicon display added better synchronization for simultanous search requests git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4076 6c8d7289-2bf4-0310-a012-ef5d649a1542 --- htroot/ViewImage.java | 137 ++++++++++-------- .../de/anomic/plasma/plasmaSearchEvent.java | 49 ++++--- .../de/anomic/plasma/plasmaSnippetCache.java | 9 +- .../de/anomic/plasma/plasmaSwitchboard.java | 4 +- 4 files changed, 113 insertions(+), 86 deletions(-) diff --git a/htroot/ViewImage.java b/htroot/ViewImage.java index b8abf6b22..65000d707 100644 --- a/htroot/ViewImage.java +++ b/htroot/ViewImage.java @@ -46,6 +46,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.util.HashMap; import de.anomic.http.httpHeader; import de.anomic.plasma.plasmaSnippetCache; @@ -58,6 +59,9 @@ import de.anomic.ymage.ymageImageParser; public class ViewImage { + private static HashMap iconcache = new HashMap(); + private static String defaulticon = "htroot/env/grafics/dfltfvcn.ico"; + public static Image respond(httpHeader header, serverObjects post, serverSwitch env) { plasmaSwitchboard sb = (plasmaSwitchboard)env; @@ -92,79 +96,92 @@ public class ViewImage { int timeout = post.getInt("timeout", 5000); // getting the image as stream - Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false); - byte[] imgb = null; - if (resource == null) { - if (urlString.endsWith(".ico")) { - // load default favicon dfltfvcn.ico + Image scaled = (Image) iconcache.get(urlString); + if (scaled == null) { + Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false); + byte[] imgb = null; + if (resource == null) { + if (urlString.endsWith(".ico")) { + // load default favicon dfltfvcn.ico + try { + imgb = serverFileUtils.read(new File(sb.getRootPath(), defaulticon)); + } catch (IOException e) { + return null; + } + } else { + return null; + } + } else { + InputStream imgStream = (InputStream) resource[0]; + if (imgStream == null) return null; + + // read image data try { - imgb = serverFileUtils.read(new File(sb.getRootPath(), "htroot/env/grafics/dfltfvcn.ico")); + imgb = serverFileUtils.read(imgStream); } catch (IOException e) { return null; + } finally { + try { + imgStream.close(); + } catch (Exception e) {/* ignore this */} } - } else { - return null; } - } else { - InputStream imgStream = (InputStream) resource[0]; - if (imgStream == null) return null; - - // read image data - try { - imgb = serverFileUtils.read(imgStream); - } catch (IOException e) { - return null; - } finally { - try { imgStream.close(); } catch (Exception e) {/* ignore this */} + + // read image + Image image = ymageImageParser.parse(urlString.toString(), imgb); + + if ((auth) && ((width == 0) || (height == 0)) && (maxwidth == 0) && (maxheight == 0)) return image; + + // find original size + int h = image.getHeight(null); + int w = image.getWidth(null); + + // System.out.println("DEBUG: get access to image " + + // url.toNormalform() + " is " + ((auth) ? "authorized" : "NOT + // authorized")); + + // in case of not-authorized access shrink the image to prevent + // copyright problems + // so that images are not larger than thumbnails + if ((!auth) && ((w > 16) || (h > 16))) { + maxwidth = (int) Math.min(64.0, w * 0.6); + maxheight = (int) Math.min(64.0, h * 0.6); } - } - - // read image - Image image = ymageImageParser.parse(urlString.toString(), imgb); - - if ((auth) && ((width == 0) || (height == 0)) && (maxwidth == 0) && (maxheight == 0)) return image; - // find original size - int h = image.getHeight(null); - int w = image.getWidth(null); - - //System.out.println("DEBUG: get access to image " + url.toNormalform() + " is " + ((auth) ? "authorized" : "NOT authorized")); - - // in case of not-authorized access shrink the image to prevent copyright problems - // so that images are not larger than thumbnails - if (!auth) { - maxwidth = (int) Math.min(64.0, w * 0.6); - maxheight = (int) Math.min(64.0, h * 0.6); - } - - // calculate width & height from maxwidth & maxheight - if ((maxwidth != 0) || (maxheight != 0)) { - double hs = (w <= maxwidth) ? 1.0 : ((double) maxwidth) / ((double) w); - double vs = (h <= maxheight) ? 1.0 : ((double) maxheight) / ((double) h); - double scale = Math.min(hs, vs); - if (!auth) scale = Math.min(scale, 0.6); // this is for copyright purpose - if (scale < 1.0) { - width = (int) (((double) w) * scale); - height = (int) (((double) h) * scale); + // calculate width & height from maxwidth & maxheight + if ((maxwidth != 0) || (maxheight != 0)) { + double hs = (w <= maxwidth) ? 1.0 : ((double) maxwidth) / ((double) w); + double vs = (h <= maxheight) ? 1.0 : ((double) maxheight) / ((double) h); + double scale = Math.min(hs, vs); + if (!auth) scale = Math.min(scale, 0.6); // this is for copyright purpose + if (scale < 1.0) { + width = (int) (((double) w) * scale); + height = (int) (((double) h) * scale); + } else { + width = w; + height = h; + } } else { width = w; height = h; } - } else { - width = w; - height = h; + + // check for minimum values + width = Math.max(width, 1); + height = Math.max(height, 1); + + // scale image + scaled = ((w == width) && (h == height)) ? image : image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING); + MediaTracker mediaTracker = new MediaTracker(new Container()); + mediaTracker.addImage(scaled, 0); + try {mediaTracker.waitForID(0);} catch (InterruptedException e) {} + + if ((height == 16) && (width == 16) && (resource != null)) { + // this might be a favicon, store image to cache for faster re-load later on + iconcache.put(urlString, scaled); + } } - // check for minimum values - width = Math.max(width, 1); - height = Math.max(height, 1); - - // scale image - Image scaled = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING); - MediaTracker mediaTracker = new MediaTracker(new Container()); - mediaTracker.addImage(scaled, 0); - try {mediaTracker.waitForID(0);} catch (InterruptedException e) {} - return scaled; } diff --git a/source/de/anomic/plasma/plasmaSearchEvent.java b/source/de/anomic/plasma/plasmaSearchEvent.java index 4064f43c6..099255636 100644 --- a/source/de/anomic/plasma/plasmaSearchEvent.java +++ b/source/de/anomic/plasma/plasmaSearchEvent.java @@ -421,7 +421,9 @@ public final class plasmaSearchEvent { } public static plasmaSearchEvent getEvent(String eventID) { - return (plasmaSearchEvent) lastEvents.get(eventID); + synchronized (lastEvents) { + return (plasmaSearchEvent) lastEvents.get(eventID); + } } public static plasmaSearchEvent getEvent(plasmaSearchQuery query, @@ -431,31 +433,34 @@ public final class plasmaSearchEvent { TreeMap preselectedPeerHashes, boolean generateAbstracts, TreeSet abstractSet) { - plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id()); - if (event == null) { - event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet); - } else { - //re-new the event time for this event, so it is not deleted next time too early - event.eventTime = System.currentTimeMillis(); - // replace the query, because this contains the current result offset - event.query = query; - } + synchronized (lastEvents) { + plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id()); + if (event == null) { + event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet); + } else { + //re-new the event time for this event, so it is not deleted next time too early + event.eventTime = System.currentTimeMillis(); + // replace the query, because this contains the current result offset + event.query = query; + } - // if worker threads had been alive, but did not succeed, start them again to fetch missing links - if ((query.onlineSnippetFetch) && - (!event.anyWorkerAlive()) && - (event.resultList.size() < query.neededResults()) && - ((event.getLocalCount() + event.getGlobalCount()) > event.resultList.size())) { - // set new timeout - event.eventTime = System.currentTimeMillis(); - // start worker threads to fetch urls and snippets - event.workerThreads = new resultWorker[workerThreadCount]; - for (int i = 0; i < workerThreadCount; i++) { - event.workerThreads[i] = event.deployWorker(i, 3 * event.process.getTargetTime()); + // if worker threads had been alive, but did not succeed, start them again to fetch missing links + if ((query.onlineSnippetFetch) && + (!event.anyWorkerAlive()) && + (event.resultList.size() < query.neededResults()) && + ((event.getLocalCount() + event.getGlobalCount()) > event.resultList.size())) { + // set new timeout + event.eventTime = System.currentTimeMillis(); + // start worker threads to fetch urls and snippets + event.workerThreads = new resultWorker[workerThreadCount]; + for (int i = 0; i < workerThreadCount; i++) { + event.workerThreads[i] = event.deployWorker(i, 3 * event.process.getTargetTime()); + } } + + return event; } - return event; } private resultWorker deployWorker(int id, long lifetime) { diff --git a/source/de/anomic/plasma/plasmaSnippetCache.java b/source/de/anomic/plasma/plasmaSnippetCache.java index 8664f8025..f13cc1711 100644 --- a/source/de/anomic/plasma/plasmaSnippetCache.java +++ b/source/de/anomic/plasma/plasmaSnippetCache.java @@ -590,8 +590,11 @@ public class plasmaSnippetCache { if (maxpos > maxLength) { // the string is too long, even if we cut it at the end // so cut it here at both ends at once - int newlen = maxpos - minpos + 10; + assert maxpos >= minpos; + int newlen = Math.max(10, maxpos - minpos + 10); int around = (maxLength - newlen) / 2; + assert minpos - around < sentence.length() : "maxpos = " + maxpos + ", minpos = " + minpos + ", around = " + around + ", sentence.length() = " + sentence.length(); + assert ((maxpos + around) <= sentence.length()) && ((maxpos + around) <= sentence.length()) : "maxpos = " + maxpos + ", minpos = " + minpos + ", around = " + around + ", sentence.length() = " + sentence.length(); sentence = "[..] " + sentence.substring(minpos - around, ((maxpos + around) > sentence.length()) ? sentence.length() : (maxpos + around)).trim() + " [..]"; minpos = around; maxpos = sentence.length() - around - 5; @@ -715,9 +718,11 @@ public class plasmaSnippetCache { Enumeration words = plasmaCondenser.wordTokenizer(sentence, "UTF-8", 0); int pos = 0; StringBuffer word; + String hash; while (words.hasMoreElements()) { word = (StringBuffer) words.nextElement(); - map.put(plasmaCondenser.word2hash(new String(word)), new Integer(pos)); + hash = plasmaCondenser.word2hash(new String(word)); + if (!map.containsKey(hash)) map.put(hash, new Integer(pos)); // dont overwrite old values, that leads to too far word distances pos += word.length() + 1; } return map; diff --git a/source/de/anomic/plasma/plasmaSwitchboard.java b/source/de/anomic/plasma/plasmaSwitchboard.java index 40b04d178..cfbda479f 100644 --- a/source/de/anomic/plasma/plasmaSwitchboard.java +++ b/source/de/anomic/plasma/plasmaSwitchboard.java @@ -1647,14 +1647,14 @@ public final class plasmaSwitchboard extends serverAbstractSwitch implements ser } return hasDoneSomething; } - + /* synchronized public void htEntryStoreEnqueued(plasmaHTCache.Entry entry) { if (plasmaHTCache.full()) htEntryStoreProcess(entry); else plasmaHTCache.push(entry); } - + */ synchronized public boolean htEntryStoreProcess(plasmaHTCache.Entry entry) { if (entry == null) return false;