Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-2950 Improve performance of UpdateHostDb - only create the homepage string if needed, rely on the parsed URL to select a URL as homepage candidate

Sebastian Nagel5086958be704
diff --git a/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java b/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java
index 3f94b5850..ca6797ac0 100644
--- a/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java
+++ b/src/java/org/apache/nutch/hostdb/UpdateHostDbMapper.java
@@ -173,11 +173,8 @@ public class UpdateHostDbMapper
// Get the protocol
String protocol = URLUtil.getProtocol(url);
- // Get the proposed homepage URL
- String homepage = protocol + "://" + buffer + "/";
-
// Check if the current key is equals the host
- if (keyStr.equals(homepage)) {
+ if (URLUtil.isHomePageOf(url, buffer)) {
// Check if this is a redirect to the real home page
if (crawlDatum.getStatus() == CrawlDatum.STATUS_DB_REDIR_PERM ||
crawlDatum.getStatus() == CrawlDatum.STATUS_DB_REDIR_TEMP) {
@@ -204,6 +201,8 @@ public class UpdateHostDbMapper
keyStr, args[0]);
}
} else {
+ // need to construct the URL anew as the hostname may be change by normalization
+ String homepage = protocol + "://" + buffer + "/";
hostDatum.setHomepageUrl(homepage);
context.write(host, new NutchWritable(hostDatum));
LOG.debug("UpdateHostDb: homepage: {}", homepage);
diff --git a/src/java/org/apache/nutch/util/URLUtil.java b/src/java/org/apache/nutch/util/URLUtil.java
index cebf1b499..822840e03 100644
--- a/src/java/org/apache/nutch/util/URLUtil.java
+++ b/src/java/org/apache/nutch/util/URLUtil.java
@@ -545,4 +545,22 @@ public class URLUtil {
ex.printStackTrace();
}
}
+
+ /**
+ * Test whether a URL is the home page or root page of a host. This is the
+ * case if the URL path is <code>/</code> and query, port, fragment, userinfo
+ * are empty resp. not given. In other words the URL is: <code>protocol://hostName/</code>
+ *
+ * @param url
+ * @param hostName
+ * @return true if the URL is the home or root page of the host
+ */
+ public static boolean isHomePageOf(URL url, String hostName) {
+ return url.getPath().equals("/") //
+ && url.getHost().equals(hostName) //
+ && url.getQuery() == null //
+ && url.getPort() == -1 //
+ && url.getRef() == null //
+ && url.getUserInfo() == null;
+ }
}