Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-501 - Implement a different caching mechanism for objects cached in configuration.

Tacettin Guneybebd9a8a784c
diff --git a/CHANGES.txt b/CHANGES.txt
index 816cb2db8..b29376cfc 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -151,6 +151,9 @@ Unreleased changes (1.0-dev)
51. NUTCH-488 - Avoid parsing uneccessary links and get a more relevant outlink
list. (Emmanuel Joke, Marcin Okraszewski via kubes)
+52. NUTCH-501 - Implement a different caching mechanism for objects cached in
+ configuration. (dogacan)
+
Release 0.9 - 2007-04-02
1. Changed log4j confiquration to log to stdout on commandline
diff --git a/src/java/org/apache/nutch/analysis/AnalyzerFactory.java b/src/java/org/apache/nutch/analysis/AnalyzerFactory.java
index 78b85eddf..f28060e7c 100644
--- a/src/java/org/apache/nutch/analysis/AnalyzerFactory.java
+++ b/src/java/org/apache/nutch/analysis/AnalyzerFactory.java
@@ -25,6 +25,7 @@ import org.apache.nutch.plugin.Extension;
import org.apache.nutch.plugin.ExtensionPoint;
import org.apache.nutch.plugin.PluginRuntimeException;
import org.apache.nutch.plugin.PluginRepository;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
@@ -56,10 +57,11 @@ public class AnalyzerFactory {
}
public static AnalyzerFactory get(Configuration conf) {
- AnalyzerFactory factory = (AnalyzerFactory) conf.getObject(KEY);
+ ObjectCache objectCache = ObjectCache.get(conf);
+ AnalyzerFactory factory = (AnalyzerFactory) objectCache.getObject(KEY);
if (factory == null) {
factory = new AnalyzerFactory(conf);
- conf.setObject(KEY, factory);
+ objectCache.setObject(KEY, factory);
}
return factory;
}
@@ -87,13 +89,13 @@ public class AnalyzerFactory {
}
private Extension getExtension(String lang) {
-
+ ObjectCache objectCache = ObjectCache.get(conf);
if (lang == null) { return null; }
- Extension extension = (Extension) this.conf.getObject(lang);
+ Extension extension = (Extension) objectCache.getObject(lang);
if (extension == null) {
extension = findExtension(lang);
if (extension != null) {
- this.conf.setObject(lang, extension);
+ objectCache.setObject(lang, extension);
}
}
return extension;
diff --git a/src/java/org/apache/nutch/analysis/CommonGrams.java b/src/java/org/apache/nutch/analysis/CommonGrams.java
index d6bf09b4a..91a4d252c 100644
--- a/src/java/org/apache/nutch/analysis/CommonGrams.java
+++ b/src/java/org/apache/nutch/analysis/CommonGrams.java
@@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.*;
import org.apache.nutch.util.NutchConfiguration;
+import org.apache.nutch.util.ObjectCache;
import org.apache.nutch.searcher.Query.*;
/** Construct n-grams for frequently occuring terms and phrases while indexing.
@@ -141,8 +142,9 @@ public class CommonGrams {
/** Construct using the provided config file. */
private void init(Configuration conf) {
+ ObjectCache objectCache = ObjectCache.get(conf);
// First, try to retrieve some commonTerms cached in configuration.
- commonTerms = (HashMap<String, HashSet<String>>) conf.getObject(KEY);
+ commonTerms = (HashMap<String, HashSet<String>>) objectCache.getObject(KEY);
if (commonTerms != null) { return; }
// Otherwise, read the terms.file
@@ -183,7 +185,7 @@ public class CommonGrams {
}
table.add(gram);
}
- conf.setObject(KEY, commonTerms);
+ objectCache.setObject(KEY, commonTerms);
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
diff --git a/src/java/org/apache/nutch/crawl/FetchScheduleFactory.java b/src/java/org/apache/nutch/crawl/FetchScheduleFactory.java
index c71e2bda0..cf54f141b 100755
--- a/src/java/org/apache/nutch/crawl/FetchScheduleFactory.java
+++ b/src/java/org/apache/nutch/crawl/FetchScheduleFactory.java
@@ -20,6 +20,7 @@ package org.apache.nutch.crawl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
+import org.apache.nutch.util.ObjectCache;
/** Creates and caches a {@link FetchSchedule} implementation. */
public class FetchScheduleFactory {
@@ -31,14 +32,15 @@ public class FetchScheduleFactory {
/** Return the FetchSchedule implementation. */
public static FetchSchedule getFetchSchedule(Configuration conf) {
String clazz = conf.get("db.fetch.schedule.class", DefaultFetchSchedule.class.getName());
- FetchSchedule impl = (FetchSchedule)conf.getObject(clazz);
+ ObjectCache objectCache = ObjectCache.get(conf);
+ FetchSchedule impl = (FetchSchedule)objectCache.getObject(clazz);
if (impl == null) {
try {
LOG.info("Using FetchSchedule impl: " + clazz);
Class implClass = Class.forName(clazz);
impl = (FetchSchedule)implClass.newInstance();
impl.setConf(conf);
- conf.setObject(clazz, impl);
+ objectCache.setObject(clazz, impl);
} catch (Exception e) {
throw new RuntimeException("Couldn't create " + clazz, e);
}
diff --git a/src/java/org/apache/nutch/crawl/SignatureFactory.java b/src/java/org/apache/nutch/crawl/SignatureFactory.java
index b726ac843..65d51690b 100644
--- a/src/java/org/apache/nutch/crawl/SignatureFactory.java
+++ b/src/java/org/apache/nutch/crawl/SignatureFactory.java
@@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
// Hadoop imports
import org.apache.hadoop.conf.Configuration;
+import org.apache.nutch.util.ObjectCache;
/**
* Factory class, which instantiates a Signature implementation according to the
@@ -39,7 +40,8 @@ public class SignatureFactory {
/** Return the default Signature implementation. */
public static Signature getSignature(Configuration conf) {
String clazz = conf.get("db.signature.class", MD5Signature.class.getName());
- Signature impl = (Signature)conf.getObject(clazz);
+ ObjectCache objectCache = ObjectCache.get(conf);
+ Signature impl = (Signature)objectCache.getObject(clazz);
if (impl == null) {
try {
if (LOG.isInfoEnabled()) {
@@ -48,7 +50,7 @@ public class SignatureFactory {
Class implClass = Class.forName(clazz);
impl = (Signature)implClass.newInstance();
impl.setConf(conf);
- conf.setObject(clazz, impl);
+ objectCache.setObject(clazz, impl);
} catch (Exception e) {
throw new RuntimeException("Couldn't create " + clazz, e);
}
diff --git a/src/java/org/apache/nutch/indexer/IndexingFilters.java b/src/java/org/apache/nutch/indexer/IndexingFilters.java
index 4517ee5bb..8a38a4106 100644
--- a/src/java/org/apache/nutch/indexer/IndexingFilters.java
+++ b/src/java/org/apache/nutch/indexer/IndexingFilters.java
@@ -28,6 +28,7 @@ import org.apache.lucene.document.Document;
import org.apache.nutch.plugin.*;
import org.apache.nutch.parse.Parse;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.Inlinks;
@@ -45,7 +46,8 @@ public class IndexingFilters {
public IndexingFilters(Configuration conf) {
/* Get indexingfilter.order property */
String order = conf.get(INDEXINGFILTER_ORDER);
- this.indexingFilters = (IndexingFilter[]) conf
+ ObjectCache objectCache = ObjectCache.get(conf);
+ this.indexingFilters = (IndexingFilter[]) objectCache
.getObject(IndexingFilter.class.getName());
if (this.indexingFilters == null) {
/*
@@ -78,7 +80,7 @@ public class IndexingFilters {
* indeterminate order
*/
if (orderedFilters == null) {
- conf.setObject(IndexingFilter.class.getName(),
+ objectCache.setObject(IndexingFilter.class.getName(),
filterMap.values().toArray(
new IndexingFilter[0]));
/* Otherwise run the filters in the required order */
@@ -91,13 +93,13 @@ public class IndexingFilters {
filters.add(filter);
}
}
- conf.setObject(IndexingFilter.class.getName(), filters
+ objectCache.setObject(IndexingFilter.class.getName(), filters
.toArray(new IndexingFilter[filters.size()]));
}
} catch (PluginRuntimeException e) {
throw new RuntimeException(e);
}
- this.indexingFilters = (IndexingFilter[]) conf
+ this.indexingFilters = (IndexingFilter[]) objectCache
.getObject(IndexingFilter.class.getName());
}
}
diff --git a/src/java/org/apache/nutch/net/URLFilters.java b/src/java/org/apache/nutch/net/URLFilters.java
index 74eab9968..ef52351ff 100644
--- a/src/java/org/apache/nutch/net/URLFilters.java
+++ b/src/java/org/apache/nutch/net/URLFilters.java
@@ -25,6 +25,7 @@ import org.apache.nutch.plugin.Extension;
import org.apache.nutch.plugin.ExtensionPoint;
import org.apache.nutch.plugin.PluginRuntimeException;
import org.apache.nutch.plugin.PluginRepository;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
/** Creates and caches {@link URLFilter} implementing plugins.*/
@@ -35,7 +36,8 @@ public class URLFilters {
public URLFilters(Configuration conf) {
String order = conf.get(URLFILTER_ORDER);
- this.filters = (URLFilter[]) conf.getObject(URLFilter.class.getName());
+ ObjectCache objectCache = ObjectCache.get(conf);
+ this.filters = (URLFilter[]) objectCache.getObject(URLFilter.class.getName());
if (this.filters == null) {
String[] orderedFilters = null;
@@ -58,7 +60,7 @@ public class URLFilters {
}
}
if (orderedFilters == null) {
- conf.setObject(URLFilter.class.getName(), filterMap.values().toArray(
+ objectCache.setObject(URLFilter.class.getName(), filterMap.values().toArray(
new URLFilter[0]));
} else {
ArrayList<URLFilter> filters = new ArrayList<URLFilter>();
@@ -68,13 +70,13 @@ public class URLFilters {
filters.add(filter);
}
}
- conf.setObject(URLFilter.class.getName(), filters
+ objectCache.setObject(URLFilter.class.getName(), filters
.toArray(new URLFilter[filters.size()]));
}
} catch (PluginRuntimeException e) {
throw new RuntimeException(e);
}
- this.filters = (URLFilter[]) conf.getObject(URLFilter.class.getName());
+ this.filters = (URLFilter[]) objectCache.getObject(URLFilter.class.getName());
}
}
diff --git a/src/java/org/apache/nutch/net/URLNormalizers.java b/src/java/org/apache/nutch/net/URLNormalizers.java
index e8afffe68..620bd511c 100644
--- a/src/java/org/apache/nutch/net/URLNormalizers.java
+++ b/src/java/org/apache/nutch/net/URLNormalizers.java
@@ -35,6 +35,7 @@ import org.apache.nutch.plugin.Extension;
import org.apache.nutch.plugin.ExtensionPoint;
import org.apache.nutch.plugin.PluginRepository;
import org.apache.nutch.plugin.PluginRuntimeException;
+import org.apache.nutch.util.ObjectCache;
/**
* This class uses a "chained filter" pattern to run defined normalizers.
@@ -115,18 +116,19 @@ public final class URLNormalizers {
this.conf = conf;
this.extensionPoint = PluginRepository.get(conf).getExtensionPoint(
URLNormalizer.X_POINT_ID);
-
+ ObjectCache objectCache = ObjectCache.get(conf);
+
if (this.extensionPoint == null) {
throw new RuntimeException("x point " + URLNormalizer.X_POINT_ID
+ " not found.");
}
- normalizers = (URLNormalizer[])conf.getObject(URLNormalizer.X_POINT_ID + "_" + scope);
+ normalizers = (URLNormalizer[])objectCache.getObject(URLNormalizer.X_POINT_ID + "_" + scope);
if (normalizers == null) {
normalizers = getURLNormalizers(scope);
}
if (normalizers == EMPTY_NORMALIZERS) {
- normalizers = (URLNormalizer[])conf.getObject(URLNormalizer.X_POINT_ID + "_" + SCOPE_DEFAULT);
+ normalizers = (URLNormalizer[])objectCache.getObject(URLNormalizer.X_POINT_ID + "_" + SCOPE_DEFAULT);
if (normalizers == null) {
normalizers = getURLNormalizers(SCOPE_DEFAULT);
}
@@ -148,6 +150,7 @@ public final class URLNormalizers {
*/
URLNormalizer[] getURLNormalizers(String scope) {
List<Extension> extensions = getExtensions(scope);
+ ObjectCache objectCache = ObjectCache.get(conf);
if (extensions == EMPTY_EXTENSION_LIST) {
return EMPTY_NORMALIZERS;
@@ -161,11 +164,11 @@ public final class URLNormalizers {
URLNormalizer normalizer = null;
try {
// check to see if we've cached this URLNormalizer instance yet
- normalizer = (URLNormalizer) this.conf.getObject(ext.getId());
+ normalizer = (URLNormalizer) objectCache.getObject(ext.getId());
if (normalizer == null) {
// go ahead and instantiate it and then cache it
normalizer = (URLNormalizer) ext.getExtensionInstance();
- this.conf.setObject(ext.getId(), normalizer);
+ objectCache.setObject(ext.getId(), normalizer);
}
normalizers.add(normalizer);
} catch (PluginRuntimeException e) {
@@ -191,9 +194,10 @@ public final class URLNormalizers {
* @throws PluginRuntimeException
*/
private List<Extension> getExtensions(String scope) {
-
- List<Extension> extensions = (List<Extension>) this.conf.getObject(URLNormalizer.X_POINT_ID + "_x_"
- + scope);
+ ObjectCache objectCache = ObjectCache.get(conf);
+ List<Extension> extensions =
+ (List<Extension>) objectCache.getObject(URLNormalizer.X_POINT_ID + "_x_"
+ + scope);
// Just compare the reference:
// if this is the empty list, we know we will find no extension.
@@ -204,11 +208,11 @@ public final class URLNormalizers {
if (extensions == null) {
extensions = findExtensions(scope);
if (extensions != null) {
- this.conf.setObject(URLNormalizer.X_POINT_ID + "_x_" + scope, extensions);
+ objectCache.setObject(URLNormalizer.X_POINT_ID + "_x_" + scope, extensions);
} else {
// Put the empty extension list into cache
// to remember we don't know any related extension.
- this.conf.setObject(URLNormalizer.X_POINT_ID + "_x_" + scope, EMPTY_EXTENSION_LIST);
+ objectCache.setObject(URLNormalizer.X_POINT_ID + "_x_" + scope, EMPTY_EXTENSION_LIST);
extensions = EMPTY_EXTENSION_LIST;
}
}
diff --git a/src/java/org/apache/nutch/parse/HtmlParseFilters.java b/src/java/org/apache/nutch/parse/HtmlParseFilters.java
index e3a54f359..90acaac6b 100644
--- a/src/java/org/apache/nutch/parse/HtmlParseFilters.java
+++ b/src/java/org/apache/nutch/parse/HtmlParseFilters.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
import org.apache.nutch.protocol.Content;
import org.apache.nutch.plugin.*;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
import org.w3c.dom.DocumentFragment;
@@ -31,7 +32,8 @@ public class HtmlParseFilters {
private HtmlParseFilter[] htmlParseFilters;
public HtmlParseFilters(Configuration conf) {
- this.htmlParseFilters = (HtmlParseFilter[]) conf.getObject(HtmlParseFilter.class.getName());
+ ObjectCache objectCache = ObjectCache.get(conf);
+ this.htmlParseFilters = (HtmlParseFilter[]) objectCache.getObject(HtmlParseFilter.class.getName());
if (htmlParseFilters == null) {
HashMap<String, HtmlParseFilter> filters =
new HashMap<String, HtmlParseFilter>();
@@ -48,11 +50,11 @@ public class HtmlParseFilters {
}
}
HtmlParseFilter[] htmlParseFilters = filters.values().toArray(new HtmlParseFilter[filters.size()]);
- conf.setObject(HtmlParseFilter.class.getName(), htmlParseFilters);
+ objectCache.setObject(HtmlParseFilter.class.getName(), htmlParseFilters);
} catch (PluginRuntimeException e) {
throw new RuntimeException(e);
}
- this.htmlParseFilters = (HtmlParseFilter[]) conf.getObject(HtmlParseFilter.class.getName());
+ this.htmlParseFilters = (HtmlParseFilter[]) objectCache.getObject(HtmlParseFilter.class.getName());
}
}
diff --git a/src/java/org/apache/nutch/parse/ParserFactory.java b/src/java/org/apache/nutch/parse/ParserFactory.java
index 9c32b1f48..35a1c3eb5 100644
--- a/src/java/org/apache/nutch/parse/ParserFactory.java
+++ b/src/java/org/apache/nutch/parse/ParserFactory.java
@@ -36,6 +36,7 @@ import org.apache.nutch.plugin.ExtensionPoint;
import org.apache.nutch.plugin.PluginRuntimeException;
import org.apache.nutch.plugin.PluginRepository;
import org.apache.nutch.util.LogUtil;
+import org.apache.nutch.util.ObjectCache;
// Tika imports
import org.apache.tika.mime.MimeType;
@@ -59,12 +60,13 @@ public final class ParserFactory {
public ParserFactory(Configuration conf) {
this.conf = conf;
+ ObjectCache objectCache = ObjectCache.get(conf);
this.extensionPoint = PluginRepository.get(conf).getExtensionPoint(
Parser.X_POINT_ID);
- this.parsePluginList = (ParsePluginList)conf.getObject(ParsePluginList.class.getName());
+ this.parsePluginList = (ParsePluginList)objectCache.getObject(ParsePluginList.class.getName());
if (this.parsePluginList == null) {
this.parsePluginList = new ParsePluginsReader().parse(conf);
- conf.setObject(ParsePluginList.class.getName(), this.parsePluginList);
+ objectCache.setObject(ParsePluginList.class.getName(), this.parsePluginList);
}
if (this.extensionPoint == null) {
@@ -107,6 +109,8 @@ public final class ParserFactory {
List<Parser> parsers = null;
List parserExts = null;
+ ObjectCache objectCache = ObjectCache.get(conf);
+
// TODO once the MimeTypes is available
// parsers = getExtensions(MimeUtils.map(contentType));
// if (parsers != null) {
@@ -126,11 +130,11 @@ public final class ParserFactory {
Parser p = null;
try {
//check to see if we've cached this parser instance yet
- p = (Parser) this.conf.getObject(ext.getId());
+ p = (Parser) objectCache.getObject(ext.getId());
if (p == null) {
// go ahead and instantiate it and then cache it
p = (Parser) ext.getExtensionInstance();
- this.conf.setObject(ext.getId(),p);
+ objectCache.setObject(ext.getId(),p);
}
parsers.add(p);
} catch (PluginRuntimeException e) {
@@ -172,6 +176,8 @@ public final class ParserFactory {
Extension[] extensions = this.extensionPoint.getExtensions();
Extension parserExt = null;
+ ObjectCache objectCache = ObjectCache.get(conf);
+
if (id != null) {
parserExt = getExtension(extensions, id);
}
@@ -184,14 +190,14 @@ public final class ParserFactory {
}
// first check the cache
- if (this.conf.getObject(parserExt.getId()) != null) {
- return (Parser) this.conf.getObject(parserExt.getId());
+ if (objectCache.getObject(parserExt.getId()) != null) {
+ return (Parser) objectCache.getObject(parserExt.getId());
// if not found in cache, instantiate the Parser
} else {
try {
Parser p = (Parser) parserExt.getExtensionInstance();
- this.conf.setObject(parserExt.getId(), p);
+ objectCache.setObject(parserExt.getId(), p);
return p;
} catch (PluginRuntimeException e) {
if (LOG.isWarnEnabled()) {
@@ -213,6 +219,7 @@ public final class ParserFactory {
*/
protected List<Extension> getExtensions(String contentType) {
+ ObjectCache objectCache = ObjectCache.get(conf);
// First of all, tries to clean the content-type
String type = null;
try {
@@ -225,7 +232,7 @@ public final class ParserFactory {
type = contentType;
}
- List<Extension> extensions = (List<Extension>) this.conf.getObject(type);
+ List<Extension> extensions = (List<Extension>) objectCache.getObject(type);
// Just compare the reference:
// if this is the empty list, we know we will find no extension.
@@ -236,11 +243,11 @@ public final class ParserFactory {
if (extensions == null) {
extensions = findExtensions(type);
if (extensions != null) {
- this.conf.setObject(type, extensions);
+ objectCache.setObject(type, extensions);
} else {
// Put the empty extension list into cache
// to remember we don't know any related extension.
- this.conf.setObject(type, EMPTY_EXTENSION_LIST);
+ objectCache.setObject(type, EMPTY_EXTENSION_LIST);
}
}
return extensions;
diff --git a/src/java/org/apache/nutch/protocol/ProtocolFactory.java b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
index e564e8d51..44d4ae9f7 100644
--- a/src/java/org/apache/nutch/protocol/ProtocolFactory.java
+++ b/src/java/org/apache/nutch/protocol/ProtocolFactory.java
@@ -25,6 +25,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.nutch.plugin.*;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
@@ -63,6 +64,7 @@ public class ProtocolFactory {
* when Protocol can not be found for urlString
*/
public Protocol getProtocol(String urlString) throws ProtocolNotFound {
+ ObjectCache objectCache = ObjectCache.get(conf);
try {
URL url = new URL(urlString);
String protocolName = url.getProtocol();
@@ -70,8 +72,8 @@ public class ProtocolFactory {
if (protocolName == null)
throw new ProtocolNotFound(urlString);
- if (conf.getObject(cacheId) != null) {
- return (Protocol) conf.getObject(cacheId);
+ if (objectCache.getObject(cacheId) != null) {
+ return (Protocol) objectCache.getObject(cacheId);
} else {
Extension extension = findExtension(protocolName);
if (extension == null) {
@@ -80,7 +82,7 @@ public class ProtocolFactory {
Protocol protocol = (Protocol) extension.getExtensionInstance();
- conf.setObject(cacheId, protocol);
+ objectCache.setObject(cacheId, protocol);
return protocol;
}
diff --git a/src/java/org/apache/nutch/scoring/ScoringFilters.java b/src/java/org/apache/nutch/scoring/ScoringFilters.java
index 69d1822f0..8eb3f4cdd 100644
--- a/src/java/org/apache/nutch/scoring/ScoringFilters.java
+++ b/src/java/org/apache/nutch/scoring/ScoringFilters.java
@@ -32,6 +32,7 @@ import org.apache.nutch.plugin.ExtensionPoint;
import org.apache.nutch.plugin.PluginRuntimeException;
import org.apache.nutch.plugin.PluginRepository;
import org.apache.nutch.protocol.Content;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
@@ -48,8 +49,9 @@ public class ScoringFilters extends Configured implements ScoringFilter {
public ScoringFilters(Configuration conf) {
super(conf);
+ ObjectCache objectCache = ObjectCache.get(conf);
String order = conf.get("scoring.filter.order");
- this.filters = (ScoringFilter[]) conf.getObject(ScoringFilter.class.getName());
+ this.filters = (ScoringFilter[]) objectCache.getObject(ScoringFilter.class.getName());
if (this.filters == null) {
String[] orderedFilters = null;
@@ -71,18 +73,18 @@ public class ScoringFilters extends Configured implements ScoringFilter {
}
}
if (orderedFilters == null) {
- conf.setObject(ScoringFilter.class.getName(), filterMap.values().toArray(new ScoringFilter[0]));
+ objectCache.setObject(ScoringFilter.class.getName(), filterMap.values().toArray(new ScoringFilter[0]));
} else {
ScoringFilter[] filter = new ScoringFilter[orderedFilters.length];
for (int i = 0; i < orderedFilters.length; i++) {
filter[i] = filterMap.get(orderedFilters[i]);
}
- conf.setObject(ScoringFilter.class.getName(), filter);
+ objectCache.setObject(ScoringFilter.class.getName(), filter);
}
} catch (PluginRuntimeException e) {
throw new RuntimeException(e);
}
- this.filters = (ScoringFilter[]) conf.getObject(ScoringFilter.class.getName());
+ this.filters = (ScoringFilter[]) objectCache.getObject(ScoringFilter.class.getName());
}
if (this.filters == null || this.filters.length == 0)
throw new RuntimeException("No scoring plugins - at least one scoring plugin is required!");
diff --git a/src/java/org/apache/nutch/searcher/QueryFilters.java b/src/java/org/apache/nutch/searcher/QueryFilters.java
index dd68fcf1b..b63e6e818 100644
--- a/src/java/org/apache/nutch/searcher/QueryFilters.java
+++ b/src/java/org/apache/nutch/searcher/QueryFilters.java
@@ -22,6 +22,7 @@ import org.apache.commons.logging.LogFactory;
import org.apache.nutch.plugin.*;
import org.apache.nutch.searcher.Query.Clause;
+import org.apache.nutch.util.ObjectCache;
import org.apache.hadoop.conf.Configuration;
import java.util.*;
@@ -50,7 +51,8 @@ public class QueryFilters {
}
public QueryFilters(Configuration conf) {
- this.queryFilters = (QueryFilter[]) conf.getObject(QueryFilter.class
+ ObjectCache objectCache = ObjectCache.get(conf);
+ this.queryFilters = (QueryFilter[]) objectCache.getObject(QueryFilter.class
.getName());
if (this.queryFilters == null) {
try {
@@ -76,20 +78,20 @@ public class QueryFilters {
filters[i] = (QueryFilter) extension.getExtensionInstance();
FIELD_NAMES.addAll(fieldNames);
FIELD_NAMES.addAll(rawFieldNames);
- conf.setObject("FIELD_NAMES", FIELD_NAMES);
+ objectCache.setObject("FIELD_NAMES", FIELD_NAMES);
RAW_FIELD_NAMES.addAll(rawFieldNames);
- conf.setObject("RAW_FIELD_NAMES", RAW_FIELD_NAMES);
+ objectCache.setObject("RAW_FIELD_NAMES", RAW_FIELD_NAMES);
}
- conf.setObject(QueryFilter.class.getName(), filters);
+ objectCache.setObject(QueryFilter.class.getName(), filters);
} catch (PluginRuntimeException e) {
throw new RuntimeException(e);
}
- this.queryFilters = (QueryFilter[]) conf.getObject(QueryFilter.class
+ this.queryFilters = (QueryFilter[]) objectCache.getObject(QueryFilter.class
.getName());
} else {
// cache already filled
- FIELD_NAMES = (HashSet) conf.getObject("FIELD_NAMES");
- RAW_FIELD_NAMES = (HashSet) conf.getObject("RAW_FIELD_NAMES");
+ FIELD_NAMES = (HashSet) objectCache.getObject("FIELD_NAMES");
+ RAW_FIELD_NAMES = (HashSet) objectCache.getObject("RAW_FIELD_NAMES");
}
}
diff --git a/src/java/org/apache/nutch/util/ObjectCache.java b/src/java/org/apache/nutch/util/ObjectCache.java
new file mode 100644
index 000000000..4d2d0f31b
--- /dev/null
+++ b/src/java/org/apache/nutch/util/ObjectCache.java
@@ -0,0 +1,58 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nutch.util;
+
+import java.util.HashMap;
+import java.util.WeakHashMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+
+public class ObjectCache {
+
+ private static final Log LOG = LogFactory.getLog(ObjectCache.class);
+
+ private static final WeakHashMap<Configuration, ObjectCache> CACHE =
+ new WeakHashMap<Configuration, ObjectCache>();
+
+ private final HashMap<String, Object> objectMap;
+
+ private ObjectCache() {
+ objectMap = new HashMap<String, Object>();
+ }
+
+ public static ObjectCache get(Configuration conf) {
+ ObjectCache objectCache = CACHE.get(conf);
+ if (objectCache == null) {
+ LOG.debug("No object cache found for conf=" + conf
+ + ", instantiating a new object cache");
+ objectCache = new ObjectCache();
+ CACHE.put(conf, objectCache);
+ }
+ return objectCache;
+ }
+
+ public Object getObject(String key) {
+ return objectMap.get(key);
+ }
+
+ public void setObject(String key, Object value) {
+ objectMap.put(key, value);
+ }
+}
+
diff --git a/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java b/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java
index cb971fc75..039b82c4d 100644
--- a/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java
+++ b/src/plugin/subcollection/src/java/org/apache/nutch/collection/CollectionManager.java
@@ -35,6 +35,7 @@ import org.apache.hadoop.conf.Configured;
import org.apache.nutch.util.DomUtil;
import org.apache.nutch.util.LogUtil;
import org.apache.nutch.util.NutchConfiguration;
+import org.apache.nutch.util.ObjectCache;
import org.apache.xerces.dom.DocumentImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -104,14 +105,15 @@ public class CollectionManager extends Configured {
public static CollectionManager getCollectionManager(Configuration conf) {
String key = "collectionmanager";
- CollectionManager impl = (CollectionManager)conf.getObject(key);
+ ObjectCache objectCache = ObjectCache.get(conf);
+ CollectionManager impl = (CollectionManager)objectCache.getObject(key);
if (impl == null) {
try {
if (LOG.isInfoEnabled()) {
LOG.info("Instantiating CollectionManager");
}
impl=new CollectionManager(conf);
- conf.setObject(key,impl);
+ objectCache.setObject(key,impl);
} catch (Exception e) {
throw new RuntimeException("Couldn't create CollectionManager",e);
}
diff --git a/src/test/org/apache/nutch/protocol/TestProtocolFactory.java b/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
index 157f17482..45432704d 100644
--- a/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
+++ b/src/test/org/apache/nutch/protocol/TestProtocolFactory.java
@@ -18,6 +18,7 @@ package org.apache.nutch.protocol;
import org.apache.hadoop.conf.Configuration;
import org.apache.nutch.util.NutchConfiguration;
+import org.apache.nutch.util.ObjectCache;
import junit.framework.TestCase;
@@ -56,7 +57,7 @@ public class TestProtocolFactory extends TestCase {
}
//cache key
- Object protocol=conf.getObject(Protocol.X_POINT_ID + "http");
+ Object protocol = ObjectCache.get(conf).getObject(Protocol.X_POINT_ID + "http");
assertNotNull(protocol);
assertEquals(httpProtocol, protocol);