Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Add a copy constructor to MapWritable, and use it in CrawlDatum.set to ensure a deep copy of metaData (NUTCH-323).

Andrzej Bialecki9c9e4d92b0d1
diff --git a/src/java/org/apache/nutch/crawl/CrawlDatum.java b/src/java/org/apache/nutch/crawl/CrawlDatum.java
index c475a2180..753aca20f 100644
--- a/src/java/org/apache/nutch/crawl/CrawlDatum.java
+++ b/src/java/org/apache/nutch/crawl/CrawlDatum.java
@@ -210,7 +210,7 @@ public class CrawlDatum implements WritableComparable, Cloneable {
this.score = that.score;
this.modifiedTime = that.modifiedTime;
this.signature = that.signature;
- this.metaData = that.metaData;
+ this.metaData = new MapWritable(that.metaData); // make a deep copy
}
diff --git a/src/java/org/apache/nutch/crawl/MapWritable.java b/src/java/org/apache/nutch/crawl/MapWritable.java
index f59eaf470..c90345e2e 100644
--- a/src/java/org/apache/nutch/crawl/MapWritable.java
+++ b/src/java/org/apache/nutch/crawl/MapWritable.java
@@ -31,6 +31,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.io.BytesWritable;
+import org.apache.hadoop.io.DataInputBuffer;
+import org.apache.hadoop.io.DataOutputBuffer;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
@@ -39,6 +41,7 @@ import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.ObjectWritable;
import org.apache.hadoop.io.UTF8;
import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.util.StringUtils;
/**
* A writable map, with a similar behavior as <code>java.util.HashMap</code>.
@@ -97,6 +100,29 @@ public class MapWritable implements Writable {
CLASS_ID_MAP.put(clazz, byteId);
ID_CLASS_MAP.put(byteId, clazz);
}
+
+ public MapWritable() { }
+
+ /**
+ * Copy constructor. This constructor makes a deep copy, using serialization /
+ * deserialization to break any possible references to contained objects.
+ *
+ * @param map map to copy from
+ */
+ public MapWritable(MapWritable map) {
+ if (map != null) {
+ try {
+ DataOutputBuffer dob = new DataOutputBuffer();
+ map.write(dob);
+ DataInputBuffer dib = new DataInputBuffer();
+ dib.reset(dob.getData(), dob.getLength());
+ readFields(dib);
+ } catch (IOException e) {
+ throw new IllegalArgumentException("this map cannot be copied: " +
+ StringUtils.stringifyException(e));
+ }
+ }
+ }
public void clear() {
fOld = fFirst;