Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Implement read/write in NutchField

Markus Jelsma789a174bd2b3
diff --git a/CHANGES.txt b/CHANGES.txt
index 79bf3839a..e41efe69f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -2,6 +2,8 @@ Nutch Change Log
(trunk): Current Development
+* NUTCH-1509 Implement read/write in NutchField (markus)
+
* NUTCH-1507 Remove FetcherOutput (markus)
* NUTCH-1506 Add UPDATE action to NutchIndexAction (markus)
diff --git a/src/java/org/apache/nutch/indexer/NutchField.java b/src/java/org/apache/nutch/indexer/NutchField.java
index 6a94fadb9..b754f0613 100644
--- a/src/java/org/apache/nutch/indexer/NutchField.java
+++ b/src/java/org/apache/nutch/indexer/NutchField.java
@@ -22,9 +22,10 @@ import java.io.DataOutput;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Date;
import java.util.List;
-import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.*;
/**
* This class represents a multi-valued field with a weight. Values are arbitrary
@@ -34,9 +35,7 @@ public class NutchField implements Writable {
private float weight;
private List<Object> values = new ArrayList<Object>();
- public NutchField() {
-
- }
+ public NutchField() { }
public NutchField(Object value) {
this(value, 1.0f);
@@ -72,10 +71,64 @@ public class NutchField implements Writable {
values.clear();
}
+ @Override
+ public Object clone() throws CloneNotSupportedException {
+ NutchField result = (NutchField)super.clone();
+ result.weight = weight;
+ result.values = values;
+
+ return result;
+ }
+
+ @Override
public void readFields(DataInput in) throws IOException {
+ weight = in.readFloat();
+ int count = in.readInt();
+ values = new ArrayList<Object>();
+ for (int i = 0; i < count; i++) {
+ String type = Text.readString(in);
+
+ if (type.equals("java.lang.String")) {
+ values.add(Text.readString(in));
+ } else if (type.equals("java.lang.Boolean")) {
+ values.add(in.readBoolean());
+ } else if (type.equals("java.lang.Integer")) {
+ values.add(in.readInt());
+ } else if (type.equals("java.lang.Float")) {
+ values.add(in.readFloat());
+ } else if (type.equals("java.util.Date")) {
+ values.add(new Date(in.readLong()));
+ }
+ }
}
+ @Override
public void write(DataOutput out) throws IOException {
+ out.writeFloat(weight);
+ out.writeInt(values.size());
+ for (Object value : values) {
+
+ Text.writeString(out, value.getClass().getName());
+
+ if (value instanceof Boolean) {
+ out.writeBoolean((Boolean)value);
+ } else if (value instanceof Integer) {
+ out.writeInt((Integer)value);
+ } else if (value instanceof Long) {
+ out.writeLong((Long)value);
+ } else if (value instanceof Float) {
+ out.writeFloat((Float)value);
+ } else if (value instanceof String) {
+ Text.writeString(out, (String)value);
+ } else if (value instanceof Date) {
+ Date date = (Date)value;
+ out.writeLong(date.getTime());
+ }
+ }
+ }
+
+ public String toString() {
+ return values.toString();
}
}