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,79 +96,92 @@ 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
Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false); Image scaled = (Image) iconcache.get(urlString);
byte[] imgb = null; if (scaled == null) {
if (resource == null) { Object[] resource = plasmaSnippetCache.getResource(url, true, timeout, false);
if (urlString.endsWith(".ico")) { byte[] imgb = null;
// load default favicon dfltfvcn.ico 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 { try {
imgb = serverFileUtils.read(new File(sb.getRootPath(), "htroot/env/grafics/dfltfvcn.ico")); imgb = serverFileUtils.read(imgStream);
} catch (IOException e) { } catch (IOException e) {
return null; return null;
} finally {
try {
imgStream.close();
} catch (Exception e) {/* ignore this */}
} }
} else {
return null;
} }
} else {
InputStream imgStream = (InputStream) resource[0]; // read image
if (imgStream == null) return null; Image image = ymageImageParser.parse(urlString.toString(), imgb);
// read image data if ((auth) && ((width == 0) || (height == 0)) && (maxwidth == 0) && (maxheight == 0)) return image;
try {
imgb = serverFileUtils.read(imgStream); // find original size
} catch (IOException e) { int h = image.getHeight(null);
return null; int w = image.getWidth(null);
} finally {
try { imgStream.close(); } catch (Exception e) {/* ignore this */} // 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 // calculate width & height from maxwidth & maxheight
int h = image.getHeight(null); if ((maxwidth != 0) || (maxheight != 0)) {
int w = image.getWidth(null); double hs = (w <= maxwidth) ? 1.0 : ((double) maxwidth) / ((double) w);
double vs = (h <= maxheight) ? 1.0 : ((double) maxheight) / ((double) h);
//System.out.println("DEBUG: get access to image " + url.toNormalform() + " is " + ((auth) ? "authorized" : "NOT authorized")); double scale = Math.min(hs, vs);
if (!auth) scale = Math.min(scale, 0.6); // this is for copyright purpose
// in case of not-authorized access shrink the image to prevent copyright problems if (scale < 1.0) {
// so that images are not larger than thumbnails width = (int) (((double) w) * scale);
if (!auth) { height = (int) (((double) h) * scale);
maxwidth = (int) Math.min(64.0, w * 0.6); } else {
maxheight = (int) Math.min(64.0, h * 0.6); width = w;
} height = h;
}
// 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 { } else {
width = w; width = w;
height = h; height = h;
} }
} else {
width = w; // check for minimum values
height = h; 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; return scaled;
} }

@ -421,7 +421,9 @@ public final class plasmaSearchEvent {
} }
public static plasmaSearchEvent getEvent(String eventID) { public static plasmaSearchEvent getEvent(String eventID) {
return (plasmaSearchEvent) lastEvents.get(eventID); synchronized (lastEvents) {
return (plasmaSearchEvent) lastEvents.get(eventID);
}
} }
public static plasmaSearchEvent getEvent(plasmaSearchQuery query, public static plasmaSearchEvent getEvent(plasmaSearchQuery query,
@ -431,31 +433,34 @@ public final class plasmaSearchEvent {
TreeMap preselectedPeerHashes, TreeMap preselectedPeerHashes,
boolean generateAbstracts, boolean generateAbstracts,
TreeSet abstractSet) { TreeSet abstractSet) {
plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id()); synchronized (lastEvents) {
if (event == null) { plasmaSearchEvent event = (plasmaSearchEvent) lastEvents.get(query.id());
event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet); if (event == null) {
} else { event = new plasmaSearchEvent(query, ranking, localTiming, wordIndex, preselectedPeerHashes, generateAbstracts, abstractSet);
//re-new the event time for this event, so it is not deleted next time too early } else {
event.eventTime = System.currentTimeMillis(); //re-new the event time for this event, so it is not deleted next time too early
// replace the query, because this contains the current result offset event.eventTime = System.currentTimeMillis();
event.query = query; // 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 worker threads had been alive, but did not succeed, start them again to fetch missing links
if ((query.onlineSnippetFetch) && if ((query.onlineSnippetFetch) &&
(!event.anyWorkerAlive()) && (!event.anyWorkerAlive()) &&
(event.resultList.size() < query.neededResults()) && (event.resultList.size() < query.neededResults()) &&
((event.getLocalCount() + event.getGlobalCount()) > event.resultList.size())) { ((event.getLocalCount() + event.getGlobalCount()) > event.resultList.size())) {
// set new timeout // set new timeout
event.eventTime = System.currentTimeMillis(); event.eventTime = System.currentTimeMillis();
// start worker threads to fetch urls and snippets // start worker threads to fetch urls and snippets
event.workerThreads = new resultWorker[workerThreadCount]; event.workerThreads = new resultWorker[workerThreadCount];
for (int i = 0; i < workerThreadCount; i++) { for (int i = 0; i < workerThreadCount; i++) {
event.workerThreads[i] = event.deployWorker(i, 3 * event.process.getTargetTime()); event.workerThreads[i] = event.deployWorker(i, 3 * event.process.getTargetTime());
}
} }
return event;
} }
return event;
} }
private resultWorker deployWorker(int id, long lifetime) { private resultWorker deployWorker(int id, long lifetime) {

@ -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