Author: markus
Date: Wed Jan 16 11:10:09 2013
New Revision: 1433900

URL: http://svn.apache.org/viewvc?rev=1433900&view=rev
Log:
Implement read/write in NutchField

Modified:
    nutch/trunk/CHANGES.txt
    nutch/trunk/src/java/org/apache/nutch/indexer/NutchField.java

Modified: nutch/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/nutch/trunk/CHANGES.txt?rev=1433900&r1=1433899&r2=1433900&view=diff
==============================================================================
--- nutch/trunk/CHANGES.txt (original)
+++ nutch/trunk/CHANGES.txt Wed Jan 16 11:10:09 2013
@@ -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)

Modified: nutch/trunk/src/java/org/apache/nutch/indexer/NutchField.java
URL: 
http://svn.apache.org/viewvc/nutch/trunk/src/java/org/apache/nutch/indexer/NutchField.java?rev=1433900&r1=1433899&r2=1433900&view=diff
==============================================================================
--- nutch/trunk/src/java/org/apache/nutch/indexer/NutchField.java (original)
+++ nutch/trunk/src/java/org/apache/nutch/indexer/NutchField.java Wed Jan 16 
11:10:09 2013
@@ -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 Writa
   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 Writa
     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();
   }
 
 }


Reply via email to