You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by ed...@apache.org on 2008/08/30 15:16:47 UTC

svn commit: r690498 - in /incubator/hama/trunk: CHANGES.txt src/java/org/apache/hama/Vector.java src/java/org/apache/hama/io/VectorWritable.java src/test/org/apache/hama/TestVector.java src/test/org/apache/hama/util/TestNumeric.java

Author: edwardyoon
Date: Sat Aug 30 06:16:46 2008
New Revision: 690498

URL: http://svn.apache.org/viewvc?rev=690498&view=rev
Log:
Add iterator() method to vector

Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/Vector.java
    incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
    incubator/hama/trunk/src/test/org/apache/hama/TestVector.java
    incubator/hama/trunk/src/test/org/apache/hama/util/TestNumeric.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=690498&r1=690497&r2=690498&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Sat Aug 30 06:16:46 2008
@@ -4,6 +4,7 @@
 
   NEW FEATURES
     
+    HAMA-49: Add iterator() method to vector (edwardyoon)
     HAMA-43: Color Hama Logo (Morakot via chanwit)
     HAMA-37: Add forrest build to Hudson patch build script (edwardyoon)
     HAMA-34: Hudson/Jira Intergration (edward yoon)

Modified: incubator/hama/trunk/src/java/org/apache/hama/Vector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Vector.java?rev=690498&r1=690497&r2=690498&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Sat Aug 30 06:16:46 2008
@@ -19,6 +19,10 @@
  */
 package org.apache.hama;
 
+import java.util.Iterator;
+
+import org.apache.hadoop.hbase.io.Cell;
+
 /**
  * Basic vector interface.
  */
@@ -125,4 +129,11 @@
     /** Largest entry in absolute value */
     Infinity
   }
+
+  /**
+   * Returns an iterator
+   * 
+   * @return iterator
+   */
+  public Iterator<Cell> iterator();
 }

Modified: incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java?rev=690498&r1=690497&r2=690498&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java Sat Aug 30 06:16:46 2008
@@ -1,199 +1,209 @@
-/**
- * Copyright 2007 The Apache Software Foundation
- *
- * 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.hama.io;
-
-import java.io.DataInput;
-import java.io.DataOutput;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-
-import org.apache.hadoop.hbase.HConstants;
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.io.HbaseMapWritable;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.hadoop.hbase.util.Writables;
-import org.apache.hadoop.io.Writable;
-import org.apache.hama.Vector;
-import org.apache.hama.util.Numeric;
-
-public class VectorWritable implements Writable, Map<byte[], Cell> {
-
-  public byte[] row;
-  public HbaseMapWritable<byte[], Cell> cells;
-
-  public Cell put(byte[] key, Cell value) {
-    throw new UnsupportedOperationException("VectorWritable is read-only!");
-  }
-
-  public Cell get(Object key) {
-    return this.cells.get(key);
-  }
-
-  public Cell remove(Object key) {
-    throw new UnsupportedOperationException("VectorWritable is read-only!");
-  }
-
-  public boolean containsKey(Object key) {
-    return cells.containsKey(key);
-  }
-
-  public boolean containsValue(Object value) {
-    throw new UnsupportedOperationException("Don't support containsValue!");
-  }
-
-  public boolean isEmpty() {
-    return cells.isEmpty();
-  }
-
-  public void clear() {
-    throw new UnsupportedOperationException("VectorDatum is read-only!");
-  }
-
-  public Set<byte[]> keySet() {
-    Set<byte[]> result = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
-    for (byte[] w : cells.keySet()) {
-      result.add(w);
-    }
-    return result;
-  }
-
-  public Set<Map.Entry<byte[], Cell>> entrySet() {
-    return Collections.unmodifiableSet(this.cells.entrySet());
-  }
-
-  public Collection<Cell> values() {
-    ArrayList<Cell> result = new ArrayList<Cell>();
-    for (Writable w : cells.values()) {
-      result.add((Cell) w);
-    }
-    return result;
-  }
-
-  public void readFields(final DataInput in) throws IOException {
-    this.row = Bytes.readByteArray(in);
-    this.cells.readFields(in);
-  }
-
-  public void write(final DataOutput out) throws IOException {
-    Bytes.writeByteArray(out, this.row);
-    this.cells.write(out);
-  }
-
-  public VectorWritable addition(byte[] bs, Vector v2) {
-    throw new UnsupportedOperationException("Not implemented yet");
-  }
-
-  public void putAll(Map<? extends byte[], ? extends Cell> m) {
-    throw new UnsupportedOperationException("Not implemented yet");
-  }
-
-  /**
-   * Get the Cell that corresponds to column
-   */
-  public Cell get(byte[] column) {
-    return this.cells.get(column);
-  }
-
-  /**
-   * Get the Cell that corresponds to column, using a String key
-   */
-  public Cell get(String key) {
-    return get(Bytes.toBytes(key));
-  }
-
-  /**
-   * Get the double value without timestamp
-   */
-  public double get(int key) {
-    return Numeric.bytesToDouble(get(Numeric.intToBytes(key)).getValue());
-  }
-
-  public int size() {
-    return this.cells.size();
-  }
-
-  @Override
-  public String toString() {
-    StringBuilder sb = new StringBuilder();
-    sb.append("row=");
-    sb.append(Bytes.toString(this.row));
-    sb.append(", cells={");
-    boolean moreThanOne = false;
-    for (Map.Entry<byte[], Cell> e : this.cells.entrySet()) {
-      if (moreThanOne) {
-        sb.append(", ");
-      } else {
-        moreThanOne = true;
-      }
-      sb.append("(column=");
-      sb.append(Bytes.toString(e.getKey()));
-      sb.append(", timestamp=");
-      sb.append(Long.toString(e.getValue().getTimestamp()));
-      sb.append(", value=");
-      byte[] value = e.getValue().getValue();
-      if (Bytes.equals(e.getKey(), HConstants.COL_REGIONINFO)) {
-        try {
-          sb.append(Writables.getHRegionInfo(value).toString());
-        } catch (IOException ioe) {
-          sb.append(ioe.toString());
-        }
-      } else {
-        sb.append(Bytes.toString(value));
-      }
-      sb.append(")");
-    }
-    sb.append("}");
-    return sb.toString();
-  }
-
-  /**
-   * 
-   * The inner class for an entry of row.
-   * 
-   */
-  public static class Entries implements Map.Entry<byte[], Cell> {
-
-    private final byte[] column;
-    private final Cell cell;
-
-    Entries(byte[] column, Cell cell) {
-      this.column = column;
-      this.cell = cell;
-    }
-
-    public Cell setValue(Cell c) {
-      throw new UnsupportedOperationException("VectorWritable is read-only!");
-    }
-
-    public byte[] getKey() {
-      byte[] key = column;
-      return key;
-    }
-
-    public Cell getValue() {
-      return cell;
-    }
-  }
-}
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * 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.hama.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.HbaseMapWritable;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.Writables;
+import org.apache.hadoop.io.Writable;
+import org.apache.hama.Vector;
+import org.apache.hama.util.Numeric;
+
+public class VectorWritable implements Writable, Map<byte[], Cell> {
+
+  public byte[] row;
+  public HbaseMapWritable<byte[], Cell> cells;
+
+  public Cell put(byte[] key, Cell value) {
+    throw new UnsupportedOperationException("VectorWritable is read-only!");
+  }
+
+  public Cell get(Object key) {
+    return this.cells.get(key);
+  }
+
+  public Cell remove(Object key) {
+    throw new UnsupportedOperationException("VectorWritable is read-only!");
+  }
+
+  public boolean containsKey(Object key) {
+    return cells.containsKey(key);
+  }
+
+  public boolean containsValue(Object value) {
+    throw new UnsupportedOperationException("Don't support containsValue!");
+  }
+
+  public boolean isEmpty() {
+    return cells.isEmpty();
+  }
+
+  public void clear() {
+    throw new UnsupportedOperationException("VectorDatum is read-only!");
+  }
+
+  public Set<byte[]> keySet() {
+    Set<byte[]> result = new TreeSet<byte[]>(Bytes.BYTES_COMPARATOR);
+    for (byte[] w : cells.keySet()) {
+      result.add(w);
+    }
+    return result;
+  }
+
+  public Set<Map.Entry<byte[], Cell>> entrySet() {
+    return Collections.unmodifiableSet(this.cells.entrySet());
+  }
+
+  public Collection<Cell> values() {
+    ArrayList<Cell> result = new ArrayList<Cell>();
+    for (Writable w : cells.values()) {
+      result.add((Cell) w);
+    }
+    return result;
+  }
+
+  public void readFields(final DataInput in) throws IOException {
+    this.row = Bytes.readByteArray(in);
+    this.cells.readFields(in);
+  }
+
+  public void write(final DataOutput out) throws IOException {
+    Bytes.writeByteArray(out, this.row);
+    this.cells.write(out);
+  }
+
+  public VectorWritable addition(byte[] bs, Vector v2) {
+    throw new UnsupportedOperationException("Not implemented yet");
+  }
+
+  public void putAll(Map<? extends byte[], ? extends Cell> m) {
+    throw new UnsupportedOperationException("Not implemented yet");
+  }
+
+  /**
+   * Get the Cell that corresponds to column
+   */
+  public Cell get(byte[] column) {
+    return this.cells.get(column);
+  }
+
+  /**
+   * Get the Cell that corresponds to column, using a String key
+   */
+  public Cell get(String key) {
+    return get(Bytes.toBytes(key));
+  }
+
+  /**
+   * Get the double value without timestamp
+   */
+  public double get(int key) {
+    return Numeric.bytesToDouble(get(Numeric.intToBytes(key)).getValue());
+  }
+
+  public int size() {
+    return this.cells.size();
+  }
+
+  @Override
+  public String toString() {
+    StringBuilder sb = new StringBuilder();
+    sb.append("row=");
+    sb.append(Bytes.toString(this.row));
+    sb.append(", cells={");
+    boolean moreThanOne = false;
+    for (Map.Entry<byte[], Cell> e : this.cells.entrySet()) {
+      if (moreThanOne) {
+        sb.append(", ");
+      } else {
+        moreThanOne = true;
+      }
+      sb.append("(column=");
+      sb.append(Bytes.toString(e.getKey()));
+      sb.append(", timestamp=");
+      sb.append(Long.toString(e.getValue().getTimestamp()));
+      sb.append(", value=");
+      byte[] value = e.getValue().getValue();
+      if (Bytes.equals(e.getKey(), HConstants.COL_REGIONINFO)) {
+        try {
+          sb.append(Writables.getHRegionInfo(value).toString());
+        } catch (IOException ioe) {
+          sb.append(ioe.toString());
+        }
+      } else {
+        sb.append(Bytes.toString(value));
+      }
+      sb.append(")");
+    }
+    sb.append("}");
+    return sb.toString();
+  }
+
+  /**
+   * Returns an Iterator.
+   * 
+   * @return iterator
+   */
+  public Iterator<Cell> iterator() {
+    return cells.values().iterator();
+  }
+
+  /**
+   * 
+   * The inner class for an entry of row.
+   * 
+   */
+  public static class Entries implements Map.Entry<byte[], Cell> {
+
+    private final byte[] column;
+    private final Cell cell;
+
+    Entries(byte[] column, Cell cell) {
+      this.column = column;
+      this.cell = cell;
+    }
+
+    public Cell setValue(Cell c) {
+      throw new UnsupportedOperationException("VectorWritable is read-only!");
+    }
+
+    public byte[] getKey() {
+      byte[] key = column;
+      return key;
+    }
+
+    public Cell getValue() {
+      return cell;
+    }
+  }
+}

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestVector.java?rev=690498&r1=690497&r2=690498&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestVector.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestVector.java Sat Aug 30 06:16:46 2008
@@ -19,12 +19,17 @@
  */
 package org.apache.hama;
 
+import java.util.Iterator;
+
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hama.util.Numeric;
+
 public class TestVector extends HamaTestCase {
-  private final double cosine = 0.6978227007909176;
-  private final double norm1 = 12.0;
-  private final double norm2 = 6.782329983125268;
+  private static final double cosine = 0.6978227007909176;
+  private static final double norm1 = 12.0;
+  private static final double norm2 = 6.782329983125268;
   private double[][] values = { { 2, 5, 1, 4 }, { 4, 1, 3, 3 } };
-  private final String m = "dotTest";
+  private static final String m = "dotTest";
 
   /**
    * Test vector
@@ -74,15 +79,29 @@
 
   private void scalingTest(Vector v2) {
     v2.scale(0.5);
-    
+
     for (int i = 0; i < v2.size(); i++) {
       assertEquals(values[1][i] * 0.5, v2.get(i));
     }
   }
-  
+
   public void testGetSet() {
     Vector v1 = new DenseVector();
     v1.set(0, 0.2);
     assertEquals(v1.get(0), 0.2);
   }
+
+  public void testIterator() {
+    Vector v1 = new DenseVector();
+    v1.set(0, 0.2);
+    v1.set(1, 0.5);
+    double[] result = { 0.2, 0.5 };
+    int i = 0;
+    Iterator<Cell> it = v1.iterator();
+    while (it.hasNext()) {
+      Cell c = it.next();
+      assertEquals(Numeric.bytesToDouble(c.getValue()), result[i]);
+      i++;
+    }
+  }
 }

Modified: incubator/hama/trunk/src/test/org/apache/hama/util/TestNumeric.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/util/TestNumeric.java?rev=690498&r1=690497&r2=690498&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/util/TestNumeric.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/util/TestNumeric.java Sat Aug 30 06:16:46 2008
@@ -1,54 +1,54 @@
-/**
- * Copyright 2007 The Apache Software Foundation
- *
- * 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.hama.util;
-
-import junit.framework.TestCase;
-
-import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.hama.Constants;
-
-public class TestNumeric extends TestCase {
-  final static int TEST_INT = 3;
-  final double TEST_DOUBLE = 0.4;
-
-  /**
-   * Integer conversion test
-   */
-  public void testInteger() {
-    assertEquals(Numeric.bytesToInt(Numeric.intToBytes(TEST_INT)), TEST_INT);
-  }
-
-  /**
-   * Double conversion test
-   */
-  public void testDouble() {
-    assertEquals(Numeric.bytesToDouble(Numeric.doubleToBytes(TEST_DOUBLE)),
-        TEST_DOUBLE);
-  }
-
-  /**
-   * Get the column index from hbase.
-   */
-  public void testGetColumnIndex() {
-    byte[] result = Numeric.getColumnIndex(3);
-    assertEquals(Bytes.toString(result), Constants.COLUMN
-        + Numeric.getColumnIndex(result));
-  }
-}
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * 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.hama.util;
+
+import junit.framework.TestCase;
+
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hama.Constants;
+
+public class TestNumeric extends TestCase {
+  final static int TEST_INT = 3;
+  final static double TEST_DOUBLE = 0.4;
+
+  /**
+   * Integer conversion test
+   */
+  public void testInteger() {
+    assertEquals(Numeric.bytesToInt(Numeric.intToBytes(TEST_INT)), TEST_INT);
+  }
+
+  /**
+   * Double conversion test
+   */
+  public void testDouble() {
+    assertEquals(Numeric.bytesToDouble(Numeric.doubleToBytes(TEST_DOUBLE)),
+        TEST_DOUBLE);
+  }
+
+  /**
+   * Get the column index from hbase.
+   */
+  public void testGetColumnIndex() {
+    byte[] result = Numeric.getColumnIndex(3);
+    assertEquals(Bytes.toString(result), Constants.COLUMN
+        + Numeric.getColumnIndex(result));
+  }
+}