From a61f44f9e442790e17a6eee4924ea11bc8fb0950 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 7 Jun 2012 13:16:38 +0200 Subject: [PATCH] lazy initialization of block rank table. this causes that the table is not initialized when there is no search is done. the effect is most strong if YaCy is started headless which causes no browser pop-up which otherwise would load the search page and therefore trigger the initialization of the table. --- htroot/yacyinteractive.java | 3 +++ htroot/yacysearch.java | 2 ++ htroot/yacysearch_location.java | 4 +++- source/net/yacy/search/ranking/BlockRank.java | 16 +++++++++++++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/htroot/yacyinteractive.java b/htroot/yacyinteractive.java index 959dbec48..845ca476b 100644 --- a/htroot/yacyinteractive.java +++ b/htroot/yacyinteractive.java @@ -27,6 +27,7 @@ import net.yacy.cora.protocol.RequestHeader; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; +import net.yacy.search.ranking.BlockRank; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; @@ -37,6 +38,8 @@ public class yacyinteractive { public static serverObjects respond(final RequestHeader header, serverObjects post, final serverSwitch env) { final Switchboard sb = (Switchboard) env; final serverObjects prop = new serverObjects(); + BlockRank.ensureLoaded(); + prop.put("topmenu", sb.getConfigBool("publicTopmenu", true) ? 1 : 0); final String promoteSearchPageGreeting = (env.getConfigBool(SwitchboardConstants.GREETING_NETWORK_NAME, false)) ? diff --git a/htroot/yacysearch.java b/htroot/yacysearch.java index 182030eea..31fe9d53a 100644 --- a/htroot/yacysearch.java +++ b/htroot/yacysearch.java @@ -78,6 +78,7 @@ import net.yacy.search.query.AccessTracker; import net.yacy.search.query.QueryParams; import net.yacy.search.query.SearchEvent; import net.yacy.search.query.SearchEventCache; +import net.yacy.search.ranking.BlockRank; import net.yacy.search.ranking.RankingProfile; import de.anomic.data.DidYouMean; import de.anomic.data.UserDB; @@ -96,6 +97,7 @@ public class yacysearch { final Switchboard sb = (Switchboard) env; sb.localSearchLastAccess = System.currentTimeMillis(); + BlockRank.ensureLoaded(); final boolean searchAllowed = sb.getConfigBool("publicSearchpage", true) || sb.verifyAuthentication(header); diff --git a/htroot/yacysearch_location.java b/htroot/yacysearch_location.java index 68b3ac080..2a4c1cbf4 100644 --- a/htroot/yacysearch_location.java +++ b/htroot/yacysearch_location.java @@ -31,6 +31,7 @@ import net.yacy.document.LibraryProvider; import net.yacy.document.geolocalization.GeoLocation; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; +import net.yacy.search.ranking.BlockRank; import de.anomic.server.serverCore; import de.anomic.server.serverObjects; import de.anomic.server.serverSwitch; @@ -40,7 +41,8 @@ public class yacysearch_location { public static serverObjects respond(final RequestHeader header, final serverObjects post, final serverSwitch env) { final Switchboard sb = (Switchboard) env; final serverObjects prop = new serverObjects(); - + BlockRank.ensureLoaded(); + prop.put("kml", 0); if (header.get(HeaderFramework.CONNECTION_PROP_EXT, "").equals("kml") || diff --git a/source/net/yacy/search/ranking/BlockRank.java b/source/net/yacy/search/ranking/BlockRank.java index 24b146e22..4e4abeafc 100644 --- a/source/net/yacy/search/ranking/BlockRank.java +++ b/source/net/yacy/search/ranking/BlockRank.java @@ -56,7 +56,8 @@ import net.yacy.search.index.Segment; public class BlockRank { public static BinSearch[] ybrTables = null; // block-rank tables - + private static File rankingPath; + private static int count; /** * collect host index information from other peers. All peers in the seed database are asked @@ -214,6 +215,7 @@ public class BlockRank { byte[] hosth = new byte[6]; String hosths, hostn; HostStat hs; + ensureLoaded(); for (int ybr = 0; ybr < ybrTables.length; ybr++) { row: for (int i = 0; i < ybrTables[ybr].size(); i++) { hosth = ybrTables[ybr].get(i, hosth); @@ -240,11 +242,18 @@ public class BlockRank { * @param rankingPath * @param count */ - public static void loadBlockRankTable(final File rankingPath, final int count) { - if (!rankingPath.exists()) return; + public static void loadBlockRankTable(final File rankingPath0, final int count0) { + // lazy initialization to save memory during startup phase + rankingPath = rankingPath0; + count = count0; + } + + public static void ensureLoaded() { + if (ybrTables != null) return; ybrTables = new BinSearch[count]; String ybrName; File f; + Log.logInfo("BlockRank", "loading block rank table from " + rankingPath.toString()); try { for (int i = 0; i < count; i++) { ybrName = "YBR-4-" + Digest.encodeHex(i, 2) + ".idx"; @@ -287,6 +296,7 @@ public class BlockRank { * @return */ public static int ranking(final byte[] hash) { + ensureLoaded(); return ranking(hash, ybrTables); }