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
pull/1/head
orbiter 18 years ago
parent d69013f66a
commit 6c819a6fd9

@ -46,6 +46,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.HashMap;
import de.anomic.http.httpHeader; import de.anomic.http.httpHeader;
import de.anomic.plasma.plasmaSnippetCache; import de.anomic.plasma.plasmaSnippetCache;
@ -58,6 +59,9 @@ import de.anomic.ymage.ymageImageParser;
public class ViewImage { 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) { public static Image respond(httpHeader header, serverObjects post, serverSwitch env) {
plasmaSwitchboard sb = (plasmaSwitchboard)env; plasmaSwitchboard sb = (plasmaSwitchboard)env;
@ -92,13 +96,15 @@ public class ViewImage {
int timeout = post.getInt("timeout", 5000); int timeout = post.getInt("timeout", 5000);
// getting the image as stream // getting the image as stream
Image scaled = (Image) iconcache.get(urlString);
if (scaled == null) {
Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false); Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false);
byte[] imgb = null; byte[] imgb = null;
if (resource == null) { if (resource == null) {
if (urlString.endsWith(".ico")) { if (urlString.endsWith(".ico")) {
// load default favicon dfltfvcn.ico // load default favicon dfltfvcn.ico
try { try {
imgb = serverFileUtils.read(new File(sb.getRootPath(), "htroot/env/grafics/dfltfvcn.ico")); imgb = serverFileUtils.read(new File(sb.getRootPath(), defaulticon));
} catch (IOException e) { } catch (IOException e) {
return null; return null;
} }
@ -115,7 +121,9 @@ public class ViewImage {
} catch (IOException e) { } catch (IOException e) {
return null; return null;
} finally { } finally {
try { imgStream.close(); } catch (Exception e) {/* ignore this */} try {
imgStream.close();
} catch (Exception e) {/* ignore this */}
} }
} }
@ -128,11 +136,14 @@ public class ViewImage {
int h = image.getHeight(null); int h = image.getHeight(null);
int w = image.getWidth(null); int w = image.getWidth(null);
//System.out.println("DEBUG: get access to image " + url.toNormalform() + " is " + ((auth) ? "authorized" : "NOT authorized")); // 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 // in case of not-authorized access shrink the image to prevent
// copyright problems
// so that images are not larger than thumbnails // so that images are not larger than thumbnails
if (!auth) { if ((!auth) && ((w > 16) || (h > 16))) {
maxwidth = (int) Math.min(64.0, w * 0.6); maxwidth = (int) Math.min(64.0, w * 0.6);
maxheight = (int) Math.min(64.0, h * 0.6); maxheight = (int) Math.min(64.0, h * 0.6);
} }
@ -160,11 +171,17 @@ public class ViewImage {
height = Math.max(height, 1); height = Math.max(height, 1);
// scale image // scale image
Image scaled = image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING); scaled = ((w == width) && (h == height)) ? image : image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
MediaTracker mediaTracker = new MediaTracker(new Container()); MediaTracker mediaTracker = new MediaTracker(new Container());
mediaTracker.addImage(scaled, 0); mediaTracker.addImage(scaled, 0);
try {mediaTracker.waitForID(0);} catch (InterruptedException e) {} 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);
}
}
return scaled; return scaled;
} }

@ -421,8 +421,10 @@ public final class plasmaSearchEvent {
} }
public static plasmaSearchEvent getEvent(String eventID) { public static plasmaSearchEvent getEvent(String eventID) {
synchronized (lastEvents) {
return (plasmaSearchEvent) lastEvents.get(eventID); return (plasmaSearchEvent) lastEvents.get(eventID);
} }
}
public static plasmaSearchEvent getEvent(plasmaSearchQuery query, public static plasmaSearchEvent getEvent(plasmaSearchQuery query,
plasmaSearchRankingProfile ranking, plasmaSearchRankingProfile ranking,
@ -431,6 +433,7 @@ public final class plasmaSearchEvent {
TreeMap preselectedPeerHashes, TreeMap preselectedPeerHashes,
boolean generateAbstracts, boolean generateAbstracts,
TreeSet abstractSet) { TreeSet abstractSet) {
synchronized (lastEvents) {
plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id()); plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id());
if (event == null) { if (event == null) {
event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet); event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet);
@ -458,6 +461,8 @@ public final class plasmaSearchEvent {
return event; return event;
} }
}
private resultWorker deployWorker(int id, long lifetime) { private resultWorker deployWorker(int id, long lifetime) {
resultWorker worker = new resultWorker(id, lifetime); resultWorker worker = new resultWorker(id, lifetime);
worker.start(); worker.start();

@ -590,8 +590,11 @@ public class plasmaSnippetCache {
if (maxpos > maxLength) { if (maxpos > maxLength) {
// the string is too long, even if we cut it at the end // the string is too long, even if we cut it at the end
// so cut it here at both ends at once // 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; 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() + " [..]"; sentence = "[..] " + sentence.substring(minpos - around, ((maxpos + around) > sentence.length()) ? sentence.length() : (maxpos + around)).trim() + " [..]";
minpos = around; minpos = around;
maxpos = sentence.length() - around - 5; maxpos = sentence.length() - around - 5;
@ -715,9 +718,11 @@ public class plasmaSnippetCache {
Enumeration words = plasmaCondenser.wordTokenizer(sentence, "UTF-8", 0); Enumeration words = plasmaCondenser.wordTokenizer(sentence, "UTF-8", 0);
int pos = 0; int pos = 0;
StringBuffer word; StringBuffer word;
String hash;
while (words.hasMoreElements()) { while (words.hasMoreElements()) {
word = (StringBuffer) words.nextElement(); 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; pos += word.length() + 1;
} }
return map; return map;

@ -1647,14 +1647,14 @@ public final class plasmaSwitchboard extends serverAbstractSwitch implements ser
} }
return hasDoneSomething; return hasDoneSomething;
} }
/*
synchronized public void htEntryStoreEnqueued(plasmaHTCache.Entry entry) { synchronized public void htEntryStoreEnqueued(plasmaHTCache.Entry entry) {
if (plasmaHTCache.full()) if (plasmaHTCache.full())
htEntryStoreProcess(entry); htEntryStoreProcess(entry);
else else
plasmaHTCache.push(entry); plasmaHTCache.push(entry);
} }
*/
synchronized public boolean htEntryStoreProcess(plasmaHTCache.Entry entry) { synchronized public boolean htEntryStoreProcess(plasmaHTCache.Entry entry) {
if (entry == null) return false; if (entry == null) return false;

Loading…
Cancel
Save