incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r688720 [1/2] - in /incubator/hama/trunk: ./ src/examples/org/apache/hama/examples/ src/java/org/apache/hama/ src/java/org/apache/hama/algebra/ src/java/org/apache/hama/io/ src/java/org/apache/hama/mapred/ src/test/org/apache/hama/ src/test...
Date Mon, 25 Aug 2008 13:27:05 GMT
Author: edwardyoon
Date: Mon Aug 25 06:27:04 2008
New Revision: 688720

URL: http://svn.apache.org/viewvc?rev=688720&view=rev
Log:
Rename MatrixInterface to Matrix.

Added:
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
    incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java
Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java
    incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
    incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java
    incubator/hama/trunk/src/java/org/apache/hama/Vector.java
    incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java
    incubator/hama/trunk/src/java/org/apache/hama/io/VectorWritable.java
    incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java
    incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java
    incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java
    incubator/hama/trunk/src/test/org/apache/hama/TestVector.java
    incubator/hama/trunk/src/test/org/apache/hama/mapred/TestMatrixMapReduce.java

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Mon Aug 25 06:27:04 2008
@@ -19,6 +19,7 @@
     
   IMPROVEMENTS
   
+    HAMA-40: Rename MatrixInterface to Matrix (edwardyoon)
     HAMA-14: Using Java 6 (edwardyoon)
     HAMA-22: Add IRC channel information to website (edwardyoon)
     HAMA-41: Add some tests (edwardyoon)

Modified: incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java (original)
+++ incubator/hama/trunk/src/examples/org/apache/hama/examples/MatrixAddition.java Mon Aug 25 06:27:04 2008
@@ -1,67 +1,68 @@
-/**
- * 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.examples;
-
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-
-public class MatrixAddition {
-
-  public static void main(String[] args) {
-    if (args.length < 2) {
-      System.out.println("addition <row_m> <column_n>");
-      System.exit(-1);
-    }
-
-    int row = Integer.parseInt(args[0]);
-    int column = Integer.parseInt(args[1]);
-
-    HamaConfiguration conf = new HamaConfiguration();
-
-    Matrix a = Matrix.random(conf, row, column);
-    Matrix b = Matrix.random(conf, row, column);
-
-    Matrix c = a.add(b);
-
-    System.out.println("\nMatrix A");
-    System.out.println("----------------------");
-    for(int i =  0; i < row; i++) {
-      for(int j =  0; j < row; j++) {
-        System.out.println(a.get(i, j));
-      }
-    }
-    
-    System.out.println("\nMatrix B");
-    System.out.println("----------------------");
-    for(int i =  0; i < row; i++) {
-      for(int j =  0; j < row; j++) {
-        System.out.println(b.get(i, j));
-      }
-    }
-    
-    System.out.println("\nC = A + B");
-    System.out.println("----------------------");
-    for(int i =  0; i < row; i++) {
-      for(int j =  0; j < row; j++) {
-        System.out.println(c.get(i, j));
-      }
-    }
-  }
-}
+/**
+ * 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.examples;
+
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+
+public class MatrixAddition {
+
+  public static void main(String[] args) {
+    if (args.length < 2) {
+      System.out.println("addition <row_m> <column_n>");
+      System.exit(-1);
+    }
+
+    int row = Integer.parseInt(args[0]);
+    int column = Integer.parseInt(args[1]);
+
+    HamaConfiguration conf = new HamaConfiguration();
+
+    Matrix a = DenseMatrix.random(conf, row, column);
+    Matrix b = DenseMatrix.random(conf, row, column);
+
+    Matrix c = a.add(b);
+
+    System.out.println("\nMatrix A");
+    System.out.println("----------------------");
+    for(int i =  0; i < row; i++) {
+      for(int j =  0; j < row; j++) {
+        System.out.println(a.get(i, j));
+      }
+    }
+    
+    System.out.println("\nMatrix B");
+    System.out.println("----------------------");
+    for(int i =  0; i < row; i++) {
+      for(int j =  0; j < row; j++) {
+        System.out.println(b.get(i, j));
+      }
+    }
+    
+    System.out.println("\nC = A + B");
+    System.out.println("----------------------");
+    for(int i =  0; i < row; i++) {
+      for(int j =  0; j < row; j++) {
+        System.out.println(c.get(i, j));
+      }
+    }
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java Mon Aug 25 06:27:04 2008
@@ -1,172 +1,153 @@
-/**
- * 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;
-
-import java.io.IOException;
-
-import org.apache.hadoop.hbase.HColumnDescriptor;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.MasterNotRunningException;
-import org.apache.hadoop.hbase.client.HBaseAdmin;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.io.BatchUpdate;
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.hadoop.io.Text;
-import org.apache.hama.util.Numeric;
-import org.apache.log4j.Logger;
-
-/**
- * Methods of the matrix classes
- */
-public abstract class AbstractMatrix implements MatrixInterface {
-  static final Logger LOG = Logger.getLogger(AbstractMatrix.class);
-
-  /** Hama Configuration */
-  protected HamaConfiguration config;
-  /** Hbase admin object */
-  protected HBaseAdmin admin;
-  /** The name of Matrix */
-  protected String matrixName;
-  /** Hbase table object */
-  protected HTable table;
-  /** Matrix attribute description */
-  protected HTableDescriptor tableDesc;
-
-  /**
-   * Sets the job configuration
-   * 
-   * @param conf configuration object
-   */
-  public void setConfiguration(HamaConfiguration conf) {
-    config = (HamaConfiguration) conf;
-    try {
-      admin = new HBaseAdmin(config);
-    } catch (MasterNotRunningException e) {
-      LOG.error(e, e);
-    }
-  }
-
-  /**
-   * Create matrix space
-   */
-  protected void create() {
-    try {
-      tableDesc.addFamily(new HColumnDescriptor(Constants.METADATA));
-      LOG.info("Initializing the matrix storage.");
-      admin.createTable(tableDesc);
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-  }
-
-  /** {@inheritDoc} */
-  public double get(int i, int j) {
-    Cell c;
-    double result = -1;
-    try {
-      c = table
-          .get(Bytes.toBytes(String.valueOf(i)), Numeric.getColumnIndex(j));
-      if (c != null) {
-        result = Numeric.bytesToDouble(c.getValue());
-      }
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-    return result;
-  }
-
-  /** {@inheritDoc} */
-  public Vector getRow(int row) {
-    try {
-      return new Vector(row, table.getRow(String.valueOf(row)));
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-    return null;
-  }
-
-  public Vector getRow(byte[] row) {
-    try {
-      return new Vector(Numeric.bytesToInt(row), table.getRow(row));
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-    return null;
-  }
-
-  /** {@inheritDoc} */
-  public int getRows() {
-    Cell rows = null;
-    try {
-      rows = table.get(Constants.METADATA, Constants.METADATA_ROWS);
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-
-    return Bytes.toInt(rows.getValue());
-  }
-
-  /** {@inheritDoc} */
-  public int getColumns() {
-    Cell columns = null;
-    try {
-      columns = table.get(Constants.METADATA, Constants.METADATA_COLUMNS);
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-    return Bytes.toInt(columns.getValue());
-  }
-
-  /** {@inheritDoc} */
-  public void set(int i, int j, double value) {
-    BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i)));
-    b.put(new Text(Constants.COLUMN + String.valueOf(j)), Numeric
-        .doubleToBytes(value));
-    try {
-      table.commit(b);
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-  }
-
-  /** {@inheritDoc} */
-  public void add(int i, int j, double value) {
-    // TODO Auto-generated method stub
-  }
-
-  /** {@inheritDoc} */
-  public void setDimension(int rows, int columns) {
-    BatchUpdate b = new BatchUpdate(Constants.METADATA);
-    b.put(Constants.METADATA_ROWS, Bytes.toBytes(rows));
-    b.put(Constants.METADATA_COLUMNS, Bytes.toBytes(columns));
-
-    try {
-      table.commit(b);
-    } catch (IOException e) {
-      LOG.error(e, e);
-    }
-  }
-
-  /** {@inheritDoc} */
-  public String getName() {
-    return (matrixName != null) ? matrixName.toString() : null;
-  }
-}
+/**
+ * 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;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.MasterNotRunningException;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.io.Text;
+import org.apache.hama.util.Numeric;
+import org.apache.log4j.Logger;
+
+/**
+ * Methods of the matrix classes
+ */
+public abstract class AbstractMatrix implements Matrix {
+  static final Logger LOG = Logger.getLogger(AbstractMatrix.class);
+
+  /** Hama Configuration */
+  protected HamaConfiguration config;
+  /** Hbase admin object */
+  protected HBaseAdmin admin;
+  /** The name of Matrix */
+  protected String matrixName;
+  /** Hbase table object */
+  protected HTable table;
+  /** Matrix attribute description */
+  protected HTableDescriptor tableDesc;
+
+  /**
+   * Sets the job configuration
+   * 
+   * @param conf configuration object
+   */
+  public void setConfiguration(HamaConfiguration conf) {
+    config = (HamaConfiguration) conf;
+    try {
+      admin = new HBaseAdmin(config);
+    } catch (MasterNotRunningException e) {
+      LOG.error(e, e);
+    }
+  }
+
+  /**
+   * Create matrix space
+   */
+  protected void create() {
+    try {
+      tableDesc.addFamily(new HColumnDescriptor(Constants.METADATA));
+      LOG.info("Initializing the matrix storage.");
+      admin.createTable(tableDesc);
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+  }
+
+  /** {@inheritDoc} */
+  public double get(int i, int j) {
+    Cell c;
+    double result = -1;
+    try {
+      c = table
+          .get(Bytes.toBytes(String.valueOf(i)), Numeric.getColumnIndex(j));
+      if (c != null) {
+        result = Numeric.bytesToDouble(c.getValue());
+      }
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+    return result;
+  }
+
+  /** {@inheritDoc} */
+  public int getRows() {
+    Cell rows = null;
+    try {
+      rows = table.get(Constants.METADATA, Constants.METADATA_ROWS);
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+
+    return Bytes.toInt(rows.getValue());
+  }
+
+  /** {@inheritDoc} */
+  public int getColumns() {
+    Cell columns = null;
+    try {
+      columns = table.get(Constants.METADATA, Constants.METADATA_COLUMNS);
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+    return Bytes.toInt(columns.getValue());
+  }
+
+  /** {@inheritDoc} */
+  public void set(int i, int j, double value) {
+    BatchUpdate b = new BatchUpdate(new Text(String.valueOf(i)));
+    b.put(new Text(Constants.COLUMN + String.valueOf(j)), Numeric
+        .doubleToBytes(value));
+    try {
+      table.commit(b);
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+  }
+
+  /** {@inheritDoc} */
+  public void add(int i, int j, double value) {
+    // TODO Auto-generated method stub
+  }
+
+  /** {@inheritDoc} */
+  public void setDimension(int rows, int columns) {
+    BatchUpdate b = new BatchUpdate(Constants.METADATA);
+    b.put(Constants.METADATA_ROWS, Bytes.toBytes(rows));
+    b.put(Constants.METADATA_COLUMNS, Bytes.toBytes(columns));
+
+    try {
+      table.commit(b);
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+  }
+
+  /** {@inheritDoc} */
+  public String getName() {
+    return (matrixName != null) ? matrixName : null;
+  }
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java?rev=688720&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Mon Aug 25 06:27:04 2008
@@ -0,0 +1,185 @@
+/**
+ * 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;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.HColumnDescriptor;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.mapred.JobClient;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hama.algebra.AdditionMap;
+import org.apache.hama.algebra.AdditionReduce;
+import org.apache.hama.mapred.DenseMap;
+import org.apache.hama.mapred.MatrixReduce;
+import org.apache.hama.util.RandomVariable;
+
+public class DenseMatrix extends AbstractMatrix implements Matrix {
+
+  /**
+   * Construct
+   * 
+   * @param conf configuration object
+   */
+  public DenseMatrix(HamaConfiguration conf) {
+    setConfiguration(conf);
+  }
+
+  /**
+   * Construct an matrix
+   * 
+   * @param conf configuration object
+   * @param matrixName the name of the matrix
+   */
+  public DenseMatrix(HamaConfiguration conf, String matrixName) {
+    try {
+      setConfiguration(conf);
+      this.matrixName = matrixName;
+
+      if (!admin.tableExists(matrixName)) {
+        tableDesc = new HTableDescriptor(matrixName);
+        tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
+        create();
+      }
+
+      table = new HTable(config, matrixName);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+  /**
+   * Construct an m-by-n constant matrix.
+   * 
+   * @param conf configuration object
+   * @param m the number of rows.
+   * @param n the number of columns.
+   * @param s fill the matrix with this scalar value.
+   */
+  public DenseMatrix(HamaConfiguration conf, int m, int n, double s) {
+    try {
+      setConfiguration(conf);
+      matrixName = RandomVariable.randMatrixName();
+
+      if (!admin.tableExists(matrixName)) {
+        tableDesc = new HTableDescriptor(matrixName);
+        tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
+        create();
+      }
+
+      table = new HTable(config, matrixName);
+
+      for (int i = 0; i < m; i++) {
+        for (int j = 0; j < n; j++) {
+          set(i, j, s);
+        }
+      }
+
+      setDimension(m, n);
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+  }
+
+  /**
+   * Generate matrix with random elements
+   * 
+   * @param conf configuration object
+   * @param m the number of rows.
+   * @param n the number of columns.
+   * @return an m-by-n matrix with uniformly distributed random elements.
+   */
+  public static Matrix random(HamaConfiguration conf, int m, int n) {
+    String name = RandomVariable.randMatrixName();
+    Matrix rand = new DenseMatrix(conf, name);
+    for (int i = 0; i < m; i++) {
+      for (int j = 0; j < n; j++) {
+        rand.set(i, j, RandomVariable.rand());
+      }
+    }
+
+    rand.setDimension(m, n);
+    LOG.info("Create the " + m + " * " + n + " random matrix : " + name);
+    return rand;
+  }
+
+  public Matrix add(Matrix B) {
+    String output = RandomVariable.randMatrixName();
+    Matrix C = new DenseMatrix(config, output);
+
+    JobConf jobConf = new JobConf(config);
+    jobConf.setJobName("addition MR job");
+
+    DenseMap.initJob(this.getName(), B.getName(), AdditionMap.class,
+        ImmutableBytesWritable.class, DenseVector.class, jobConf);
+    MatrixReduce.initJob(C.getName(), AdditionReduce.class, jobConf);
+
+    try {
+      JobClient.runJob(jobConf);
+    } catch (IOException e) {
+      // TODO Auto-generated catch block
+      e.printStackTrace();
+    }
+
+    return C;
+  }
+
+  public Matrix add(double alpha, Matrix B) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public DenseVector getRow(int row) {
+    try {
+      return new DenseVector(row, table.getRow(String.valueOf(row)));
+    } catch (IOException e) {
+      LOG.error(e, e);
+    }
+    return null;
+  }
+  
+  public Matrix mult(Matrix B) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public Matrix multAdd(double alpha, Matrix B, Matrix C) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public double norm(Norm type) {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+
+  public Matrix set(double alpha, Matrix B) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public Matrix set(Matrix B) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+}

Added: incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java?rev=688720&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseVector.java Mon Aug 25 06:27:04 2008
@@ -0,0 +1,154 @@
+package org.apache.hama;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.HbaseMapWritable;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hama.io.VectorWritable;
+import org.apache.hama.util.Numeric;
+import org.apache.log4j.Logger;
+
+public class DenseVector extends VectorWritable implements Vector {
+  static final Logger LOG = Logger.getLogger(Vector.class);
+
+  public DenseVector() {
+    this(null, new HbaseMapWritable<byte[], Cell>());
+  }
+
+  public DenseVector(final byte[] rowKey, final HbaseMapWritable<byte[], Cell> m) {
+    this.row = row;
+    this.cells = m;
+  }
+
+  public DenseVector(int row, RowResult rowResult) {
+    this.cells = new HbaseMapWritable<byte[], Cell>();
+    this.row = Numeric.intToBytes(row);
+    for (Map.Entry<byte[], Cell> f : rowResult.entrySet()) {
+      this.cells.put(f.getKey(), f.getValue());
+    }
+  }
+
+  /**
+   * Get the row for this Vector
+   */
+  public byte[] getRow() {
+    return row;
+  }
+
+  public HbaseMapWritable<byte[], Cell> getCells() {
+    return cells;
+  }
+
+  public void add(int index, double value) {
+    // TODO Auto-generated method stub
+
+  }
+
+  public Vector add(double alpha, Vector v) {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public Vector add(Vector v2) {
+    HbaseMapWritable<byte[], Cell> trunk = new HbaseMapWritable<byte[], Cell>();
+    for (int i = 0; i < this.size(); i++) {
+      double value = (this.get(i) + v2.get(i));
+      Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis());
+      trunk.put(Bytes.toBytes("column:" + i), cValue);
+    }
+
+    return new DenseVector(row, trunk);
+  }
+
+  public double dot(Vector v) {
+    double cosine = 0.0;
+    double q_i, d_i;
+    for (int i = 0; i < Math.min(this.size(), v.size()); i++) {
+      q_i = v.get(i);
+      d_i = this.get(i);
+      cosine += q_i * d_i;
+    }
+    return cosine / (this.getNorm2() * ((DenseVector) v).getNorm2());
+  }
+
+  public Vector scale(double alpha) {
+    Set<byte[]> keySet = cells.keySet();
+    Iterator<byte[]> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      byte[] key = it.next();
+      double oValue = Numeric.bytesToDouble(get(key).getValue());
+      double nValue = oValue * alpha;
+      Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis());
+      cells.put(key, cValue);
+    }
+
+    return this;
+  }
+
+  public double get(int index) {
+    return Numeric.bytesToDouble(cells.get(Numeric.getColumnIndex(index))
+        .getValue());
+  }
+
+  public double norm(Norm type) {
+    if (type == Norm.One)
+      return getNorm1();
+    else if (type == Norm.Two)
+      return getNorm2();
+    else if (type == Norm.TwoRobust)
+      return getNorm2Robust();
+    else
+      return getNormInf();
+  }
+
+  public void set(int index, double value) {
+    Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis());
+    cells.put(Numeric.getColumnIndex(index), cValue);
+  }
+
+  public DenseVector set(Vector v) {
+    return new DenseVector(((DenseVector) v).getRow(), ((DenseVector) v).getCells());
+  }
+
+  public double getNorm1() {
+    double sum = 0.0;
+
+    Set<byte[]> keySet = cells.keySet();
+    Iterator<byte[]> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      sum += Numeric.bytesToDouble(get(it.next()).getValue());
+    }
+
+    return sum;
+  }
+
+  public double getNorm2() {
+    double square_sum = 0.0;
+
+    Set<byte[]> keySet = cells.keySet();
+    Iterator<byte[]> it = keySet.iterator();
+
+    while (it.hasNext()) {
+      double value = Numeric.bytesToDouble(get(it.next()).getValue());
+      square_sum += value * value;
+    }
+
+    return Math.sqrt(square_sum);
+  }
+
+  public double getNorm2Robust() {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+
+  public double getNormInf() {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+}
\ No newline at end of file

Modified: incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Matrix.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Mon Aug 25 06:27:04 2008
@@ -1,178 +1,157 @@
-/**
- * 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;
-
-import java.io.IOException;
-
-import org.apache.hadoop.hbase.HColumnDescriptor;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.mapred.JobClient;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hama.algebra.AdditionMap;
-import org.apache.hama.algebra.AdditionReduce;
-import org.apache.hama.mapred.MatrixMap;
-import org.apache.hama.mapred.MatrixReduce;
-import org.apache.hama.util.RandomVariable;
-
-/**
- * A library for mathematical operations on matrices of double.
- */
-public class Matrix extends AbstractMatrix {
-
-  /**
-   * Construct
-   * 
-   * @param conf configuration object
-   */
-  public Matrix(HamaConfiguration conf) {
-    setConfiguration(conf);
-  }
-
-  /**
-   * Construct an matrix
-   * 
-   * @param conf configuration object
-   * @param matrixName the name of the matrix
-   */
-  public Matrix(HamaConfiguration conf, String matrixName) {
-    try {
-      setConfiguration(conf);
-      this.matrixName = matrixName;
-
-      if (!admin.tableExists(matrixName)) {
-        tableDesc = new HTableDescriptor(matrixName.toString());
-        tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN.toString()));
-        create();
-      }
-
-      table = new HTable(config, matrixName);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Construct an m-by-n constant matrix.
-   * 
-   * @param conf configuration object
-   * @param m the number of rows.
-   * @param n the number of columns.
-   * @param s fill the matrix with this scalar value.
-   */
-  public Matrix(HamaConfiguration conf, int m, int n, double s) {
-    try {
-      setConfiguration(conf);
-      matrixName = RandomVariable.randMatrixName();
-
-      if (!admin.tableExists(matrixName)) {
-        tableDesc = new HTableDescriptor(matrixName.toString());
-        tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN.toString()));
-        create();
-      }
-
-      table = new HTable(config, matrixName);
-
-      for (int i = 0; i < m; i++) {
-        for (int j = 0; j < n; j++) {
-          set(i, j, s);
-        }
-      }
-
-      setDimension(m, n);
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * Generate matrix with random elements
-   * 
-   * @param conf configuration object
-   * @param m the number of rows.
-   * @param n the number of columns.
-   * @return an m-by-n matrix with uniformly distributed random elements.
-   */
-  public static Matrix random(HamaConfiguration conf, int m, int n) {
-    String name = RandomVariable.randMatrixName();
-    Matrix rand = new Matrix(conf, name);
-    for (int i = 0; i < m; i++) {
-      for (int j = 0; j < n; j++) {
-        rand.set(i, j, RandomVariable.rand());
-      }
-    }
-
-    rand.setDimension(m, n);
-    LOG.info("Create the " + m + " * " + n + " random matrix : " + name);
-    return rand;
-  }
-
-  public Matrix add(Matrix B) {
-    String output = RandomVariable.randMatrixName();
-    Matrix C = new Matrix(config, output);
-
-    JobConf jobConf = new JobConf(config);
-    jobConf.setJobName("addition MR job");
-
-    MatrixMap.initJob(this.getName(), B.getName(), AdditionMap.class,
-        ImmutableBytesWritable.class, Vector.class, jobConf);
-    MatrixReduce.initJob(C.getName(), AdditionReduce.class, jobConf);
-
-    try {
-      JobClient.runJob(jobConf);
-    } catch (IOException e) {
-      // TODO Auto-generated catch block
-      e.printStackTrace();
-    }
-
-    return C;
-  }
-
-  public Matrix add(double alpha, Matrix B) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Matrix mult(Matrix B) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Matrix multAdd(double alpha, Matrix B, Matrix C) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public double norm(Norm type) {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-
-  public Matrix set(double alpha, Matrix B) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Matrix set(Matrix B) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-}
+/**
+ * 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;
+
+/**
+ * Basic matrix interface.
+ */
+public interface Matrix {
+  /**
+   * Gets the double value of (i, j)
+   * 
+   * @param i ith row of the matrix
+   * @param j jth column of the matrix
+   * @return the value of entry
+   */
+  public double get(int i, int j);
+
+  /**
+   * Gets the vector of row
+   * 
+   * @param row the row index of the matrix
+   * @return the feature vector of row
+   */
+  public Vector getRow(int row);
+
+  /**
+   * Get a number of row of the matrix from the meta-data column
+   * 
+   * @return a number of rows of the matrix
+   */
+  public int getRows();
+
+  /**
+   * Get a number of column of the matrix from the meta-data column
+   * 
+   * @return a number of columns of the matrix
+   */
+  public int getColumns();
+
+  /**
+   * Sets the double value of (i, j)
+   * 
+   * @param i ith row of the matrix
+   * @param j jth column of the matrix
+   * @param value the value of entry
+   */
+  public void set(int i, int j, double value);
+
+  /**
+   * A=alpha*B
+   * 
+   * @param alpha
+   * @param B
+   * @return A
+   */
+  public Matrix set(double alpha, Matrix B);
+
+  /**
+   * A=B
+   * 
+   * @param B
+   * @return A
+   */
+  public Matrix set(Matrix B);
+
+  /**
+   * Sets the dimension of matrix
+   * 
+   * @param rows the number of rows
+   * @param columns the number of columns
+   */
+  public void setDimension(int rows, int columns);
+
+  /**
+   * A(i, j) += value
+   * 
+   * @param i
+   * @param j
+   * @param value
+   */
+  public void add(int i, int j, double value);
+
+  /**
+   * A = B + A
+   * 
+   * @param B
+   * @return A
+   */
+  public Matrix add(Matrix B);
+
+  /**
+   * A = alpha*B + A
+   * 
+   * @param alpha
+   * @param B
+   * @return A
+   */
+  public Matrix add(double alpha, Matrix B);
+
+  /**
+   * C = A*B
+   * 
+   * @param B
+   * @return C
+   */
+  public Matrix mult(Matrix B);
+
+  /**
+   * C = alpha*A*B + C
+   * 
+   * @param alpha
+   * @param B
+   * @param C
+   * @return C
+   */
+  public Matrix multAdd(double alpha, Matrix B, Matrix C);
+
+  /**
+   * Computes the given norm of the matrix
+   * 
+   * @param type
+   * @return norm of the matrix
+   */
+  public double norm(Norm type);
+
+  /**
+   * Supported matrix-norms.
+   */
+  enum Norm {
+    /** Largest entry in absolute value */
+    Infinity
+  }
+
+  /**
+   * Return the matrix name
+   * 
+   * @return the name of the matrix
+   */
+  public String getName();
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/MatrixInterface.java Mon Aug 25 06:27:04 2008
@@ -19,9 +19,7 @@
  */
 package org.apache.hama;
 
-/**
- * Basic matrix interface.
- */
+@Deprecated
 public interface MatrixInterface {
 
   /**

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=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Vector.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Vector.java Mon Aug 25 06:27:04 2008
@@ -1,173 +1,128 @@
-/**
- * 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;
-
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.io.HbaseMapWritable;
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.hbase.util.Bytes;
-import org.apache.hama.io.VectorWritable;
-import org.apache.hama.util.Numeric;
-import org.apache.log4j.Logger;
-
-public class Vector extends VectorWritable implements VectorInterface {
-  static final Logger LOG = Logger.getLogger(Vector.class);
-
-  public Vector() {
-    this(null, new HbaseMapWritable<byte[], Cell>());
-  }
-
-  public Vector(final byte[] row, final HbaseMapWritable<byte[], Cell> m) {
-    this.row = row;
-    this.cells = m;
-  }
-
-  public Vector(int row, RowResult rowResult) {
-    this.cells = new HbaseMapWritable<byte[], Cell>();
-    this.row = Numeric.intToBytes(row);
-    for (Map.Entry<byte[], Cell> f : rowResult.entrySet()) {
-      this.cells.put(f.getKey(), f.getValue());
-    }
-  }
-
-  /**
-   * Get the row for this Vector
-   */
-  public byte[] getRow() {
-    return row;
-  }
-
-  public HbaseMapWritable<byte[], Cell> getCells() {
-    return cells;
-  }
-
-  public void add(int index, double value) {
-    // TODO Auto-generated method stub
-
-  }
-
-  public Vector add(double alpha, Vector v) {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Vector add(Vector v2) {
-    HbaseMapWritable<byte[], Cell> trunk = new HbaseMapWritable<byte[], Cell>();
-    for (int i = 0; i < this.size(); i++) {
-      double value = (this.get(i) + v2.get(i));
-      Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis());
-      trunk.put(Bytes.toBytes("column:" + i), cValue);
-    }
-
-    return new Vector(row, trunk);
-  }
-
-  public double dot(Vector v) {
-    double cosine = 0.0;
-    double q_i, d_i;
-    for (int i = 0; i < Math.min(this.size(), v.size()); i++) {
-      q_i = v.get(i);
-      d_i = this.get(i);
-      cosine += q_i * d_i;
-    }
-    return cosine / (this.getNorm2() * v.getNorm2());
-  }
-
-  public Vector scale(double alpha) {
-    Set<byte[]> keySet = cells.keySet();
-    Iterator<byte[]> it = keySet.iterator();
-
-    while (it.hasNext()) {
-      byte[] key = it.next();
-      double oValue = Numeric.bytesToDouble(get(key).getValue());
-      double nValue = oValue * alpha;
-      Cell cValue = new Cell(String.valueOf(nValue), System.currentTimeMillis());
-      cells.put(key, cValue);
-    }
-
-    return this;
-  }
-
-  public double get(int index) {
-    return Numeric.bytesToDouble(cells.get(Numeric.getColumnIndex(index))
-        .getValue());
-  }
-
-  public double norm(Norm type) {
-    if (type == Norm.One)
-      return getNorm1();
-    else if (type == Norm.Two)
-      return getNorm2();
-    else if (type == Norm.TwoRobust)
-      return getNorm2Robust();
-    else
-      return getNormInf();
-  }
-
-  public void set(int index, double value) {
-    Cell cValue = new Cell(String.valueOf(value), System.currentTimeMillis());
-    cells.put(Numeric.getColumnIndex(index), cValue);
-  }
-
-  public Vector set(Vector v) {
-    return new Vector(v.getRow(), v.getCells());
-  }
-
-  public double getNorm1() {
-    double sum = 0.0;
-
-    Set<byte[]> keySet = cells.keySet();
-    Iterator<byte[]> it = keySet.iterator();
-
-    while (it.hasNext()) {
-      sum += Numeric.bytesToDouble(get(it.next()).getValue());
-    }
-
-    return sum;
-  }
-
-  public double getNorm2() {
-    double square_sum = 0.0;
-
-    Set<byte[]> keySet = cells.keySet();
-    Iterator<byte[]> it = keySet.iterator();
-
-    while (it.hasNext()) {
-      double value = Numeric.bytesToDouble(get(it.next()).getValue());
-      square_sum += value * value;
-    }
-
-    return Math.sqrt(square_sum);
-  }
-
-  public double getNorm2Robust() {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-
-  public double getNormInf() {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-}
+/**
+ * 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;
+
+/**
+ * Basic vector interface.
+ */
+public interface Vector {
+
+  /**
+   * Size of the vector
+   * 
+   * @return size of the vector
+   */
+  public int size();
+
+  /**
+   * Gets the value of index
+   * 
+   * @param index
+   * @return v(index)
+   */
+  public double get(int index);
+
+  /**
+   * Sets the value of index
+   * 
+   * @param index
+   * @param value
+   */
+  public void set(int index, double value);
+
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public Vector set(Vector v);
+
+  /**
+   * Adds the value to v(index)
+   * 
+   * @param index
+   * @param value
+   */
+  public void add(int index, double value);
+
+  /**
+   * x = alpha*v + x
+   * 
+   * @param alpha
+   * @param v
+   * @return x = alpha*v + x
+   */
+  public Vector add(double alpha, Vector v);
+
+  /**
+   * x = v + x
+   * 
+   * @param v
+   * @return x = v + x
+   */
+  public Vector add(Vector v);
+
+  /**
+   * x dot v
+   * 
+   * @param v
+   * @return x dot v
+   */
+  public double dot(Vector v);
+
+  /**
+   * v = alpha*v 
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public Vector scale(double alpha);
+  
+  
+  /**
+   * Computes the given norm of the vector
+   * 
+   * @param type
+   * @return norm of the vector
+   */
+  public double norm(Norm type);
+
+  /**
+   * Supported vector-norms.
+   */
+  enum Norm {
+
+    /** Sum of the absolute values of the entries */
+    One,
+
+    /** The root of sum of squares */
+    Two,
+
+    /**
+     * As the 2 norm may overflow, an overflow resistant version is also
+     * available. Note that it may be slower.
+     */
+    TwoRobust,
+
+    /** Largest entry in absolute value */
+    Infinity
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/VectorInterface.java Mon Aug 25 06:27:04 2008
@@ -1,128 +1,126 @@
-/**
- * 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;
-
-/**
- * Basic vector interface.
- */
-public interface VectorInterface {
-
-  /**
-   * Size of the vector
-   * 
-   * @return size of the vector
-   */
-  public int size();
-
-  /**
-   * Gets the value of index
-   * 
-   * @param index
-   * @return v(index)
-   */
-  public double get(int index);
-
-  /**
-   * Sets the value of index
-   * 
-   * @param index
-   * @param value
-   */
-  public void set(int index, double value);
-
-  /**
-   * Sets the vector
-   * 
-   * @param v
-   * @return x = v
-   */
-  public Vector set(Vector v);
-
-  /**
-   * Adds the value to v(index)
-   * 
-   * @param index
-   * @param value
-   */
-  public void add(int index, double value);
-
-  /**
-   * x = alpha*v + x
-   * 
-   * @param alpha
-   * @param v
-   * @return x = alpha*v + x
-   */
-  public Vector add(double alpha, Vector v);
-
-  /**
-   * x = v + x
-   * 
-   * @param v
-   * @return x = v + x
-   */
-  public Vector add(Vector v);
-
-  /**
-   * x dot v
-   * 
-   * @param v
-   * @return x dot v
-   */
-  public double dot(Vector v);
-
-  /**
-   * v = alpha*v 
-   * 
-   * @param alpha
-   * @return v = alpha*v
-   */
-  public Vector scale(double alpha);
-  
-  
-  /**
-   * Computes the given norm of the vector
-   * 
-   * @param type
-   * @return norm of the vector
-   */
-  public double norm(Norm type);
-
-  /**
-   * Supported vector-norms.
-   */
-  enum Norm {
-
-    /** Sum of the absolute values of the entries */
-    One,
-
-    /** The root of sum of squares */
-    Two,
-
-    /**
-     * As the 2 norm may overflow, an overflow resistant version is also
-     * available. Note that it may be slower.
-     */
-    TwoRobust,
-
-    /** Largest entry in absolute value */
-    Infinity
-  }
-}
+/**
+ * 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;
+
+@Deprecated
+public interface VectorInterface {
+
+  /**
+   * Size of the vector
+   * 
+   * @return size of the vector
+   */
+  public int size();
+
+  /**
+   * Gets the value of index
+   * 
+   * @param index
+   * @return v(index)
+   */
+  public double get(int index);
+
+  /**
+   * Sets the value of index
+   * 
+   * @param index
+   * @param value
+   */
+  public void set(int index, double value);
+
+  /**
+   * Sets the vector
+   * 
+   * @param v
+   * @return x = v
+   */
+  public Vector set(Vector v);
+
+  /**
+   * Adds the value to v(index)
+   * 
+   * @param index
+   * @param value
+   */
+  public void add(int index, double value);
+
+  /**
+   * x = alpha*v + x
+   * 
+   * @param alpha
+   * @param v
+   * @return x = alpha*v + x
+   */
+  public Vector add(double alpha, Vector v);
+
+  /**
+   * x = v + x
+   * 
+   * @param v
+   * @return x = v + x
+   */
+  public Vector add(Vector v);
+
+  /**
+   * x dot v
+   * 
+   * @param v
+   * @return x dot v
+   */
+  public double dot(Vector v);
+
+  /**
+   * v = alpha*v 
+   * 
+   * @param alpha
+   * @return v = alpha*v
+   */
+  public Vector scale(double alpha);
+  
+  
+  /**
+   * Computes the given norm of the vector
+   * 
+   * @param type
+   * @return norm of the vector
+   */
+  public double norm(Norm type);
+
+  /**
+   * Supported vector-norms.
+   */
+  enum Norm {
+
+    /** Sum of the absolute values of the entries */
+    One,
+
+    /** The root of sum of squares */
+    Two,
+
+    /**
+     * As the 2 norm may overflow, an overflow resistant version is also
+     * available. Note that it may be slower.
+     */
+    TwoRobust,
+
+    /** Largest entry in absolute value */
+    Infinity
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionMap.java Mon Aug 25 06:27:04 2008
@@ -1,41 +1,43 @@
-/**
- * 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.algebra;
-
-import java.io.IOException;
-
-import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.Vector;
-import org.apache.hama.mapred.MatrixMap;
-
-public class AdditionMap extends MatrixMap<ImmutableBytesWritable, Vector> {
-
-  public void map(ImmutableBytesWritable key, Vector value,
-      OutputCollector<ImmutableBytesWritable, Vector> output,
-      Reporter reporter) throws IOException {
-    
-    Vector v1 = B.getRow(key.get());
-    output.collect(key, v1.add(value));
-    
-  }
-
-}
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.DenseVector;
+import org.apache.hama.Vector;
+import org.apache.hama.mapred.DenseMap;
+import org.apache.hama.util.Numeric;
+
+public class AdditionMap extends DenseMap<ImmutableBytesWritable, DenseVector> {
+
+  @Override
+  public void map(ImmutableBytesWritable key, Vector value,
+      OutputCollector<ImmutableBytesWritable, DenseVector> output,
+      Reporter reporter) throws IOException {
+
+    Vector v1 = MATRIX_B.getRow(Numeric.bytesToInt(key.get()));
+    output.collect(key, (DenseVector) v1.add(value));
+
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/AdditionReduce.java Mon Aug 25 06:27:04 2008
@@ -1,51 +1,51 @@
-/**
- * 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.algebra;
-
-import java.io.IOException;
-import java.util.Iterator;
-import java.util.Map;
-
-import org.apache.hadoop.hbase.io.BatchUpdate;
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.Vector;
-import org.apache.hama.mapred.MatrixReduce;
-
-public class AdditionReduce extends
-    MatrixReduce<ImmutableBytesWritable, Vector> {
-
-  @Override
-  public void reduce(ImmutableBytesWritable key, Iterator<Vector> values,
-      OutputCollector<ImmutableBytesWritable, BatchUpdate> output,
-      Reporter reporter) throws IOException {
-
-    BatchUpdate b = new BatchUpdate(key.get());
-    Vector vector = values.next();
-    for (Map.Entry<byte[], Cell> f : vector.entrySet()) {
-      b.put(f.getKey(), f.getValue().getValue());
-    }
-
-    output.collect(key, b);
-  }
-
-}
+/**
+ * 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.algebra;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.hadoop.hbase.io.BatchUpdate;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.DenseVector;
+import org.apache.hama.mapred.MatrixReduce;
+
+public class AdditionReduce extends
+    MatrixReduce<ImmutableBytesWritable, DenseVector> {
+
+  @Override
+  public void reduce(ImmutableBytesWritable key, Iterator<DenseVector> values,
+      OutputCollector<ImmutableBytesWritable, BatchUpdate> output,
+      Reporter reporter) throws IOException {
+
+    BatchUpdate b = new BatchUpdate(key.get());
+    DenseVector vector = values.next();
+    for (Map.Entry<byte[], Cell> f : vector.entrySet()) {
+      b.put(f.getKey(), f.getValue().getValue());
+    }
+
+    output.collect(key, b);
+  }
+
+}

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=688720&r1=688719&r2=688720&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 Mon Aug 25 06:27:04 2008
@@ -152,15 +152,15 @@
       sb.append(", timestamp=");
       sb.append(Long.toString(e.getValue().getTimestamp()));
       sb.append(", value=");
-      byte[] v = e.getValue().getValue();
+      byte[] value = e.getValue().getValue();
       if (Bytes.equals(e.getKey(), HConstants.COL_REGIONINFO)) {
         try {
-          sb.append(Writables.getHRegionInfo(v).toString());
+          sb.append(Writables.getHRegionInfo(value).toString());
         } catch (IOException ioe) {
           sb.append(ioe.toString());
         }
       } else {
-        sb.append(v);
+        sb.append(value);
       }
       sb.append(")");
     }
@@ -178,8 +178,8 @@
     private final byte[] column;
     private final Cell cell;
 
-    Entries(byte[] row, Cell cell) {
-      this.column = row;
+    Entries(byte[] column, Cell cell) {
+      this.column = column;
       this.cell = cell;
     }
 

Added: incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java?rev=688720&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/DenseMap.java Mon Aug 25 06:27:04 2008
@@ -0,0 +1,43 @@
+package org.apache.hama.mapred;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.Vector;
+
+@SuppressWarnings("unchecked")
+public abstract class DenseMap<K extends WritableComparable, V extends Writable>
+    extends MapReduceBase implements
+    Mapper<ImmutableBytesWritable, Vector, K, V> {
+  public static Matrix MATRIX_B;
+
+  public static void initJob(String matrixA, String matrixB,
+      Class<? extends DenseMap> mapper,
+      Class<? extends WritableComparable> outputKeyClass,
+      Class<? extends Writable> outputValueClass, JobConf job) {
+
+    job.setInputFormat(MatrixInputFormat.class);
+    job.setMapOutputValueClass(outputValueClass);
+    job.setMapOutputKeyClass(outputKeyClass);
+    job.setMapperClass(mapper);
+    FileInputFormat.addInputPaths(job, matrixA);
+
+    MATRIX_B = new DenseMatrix(new HamaConfiguration(), matrixB);
+    job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  public abstract void map(ImmutableBytesWritable key, Vector value,
+      OutputCollector<K, V> output, Reporter reporter) throws IOException;
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixInputFormatBase.java Mon Aug 25 06:27:04 2008
@@ -1,274 +1,292 @@
-package org.apache.hama.mapred;
-
-import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.hadoop.hbase.HConstants;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.client.Scanner;
-import org.apache.hadoop.hbase.filter.RowFilterInterface;
-import org.apache.hadoop.hbase.filter.RowFilterSet;
-import org.apache.hadoop.hbase.filter.StopRowFilter;
-import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.hbase.mapred.TableSplit;
-import org.apache.hadoop.hbase.regionserver.HRegion;
-import org.apache.hadoop.hbase.util.Writables;
-import org.apache.hadoop.mapred.InputFormat;
-import org.apache.hadoop.mapred.InputSplit;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.RecordReader;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.Vector;
-import org.apache.hama.io.VectorWritable;
-
-public abstract class MatrixInputFormatBase implements
-    InputFormat<ImmutableBytesWritable, Vector> {
-  private final Log LOG = LogFactory.getLog(MatrixInputFormatBase.class);
-  private byte[][] inputColumns;
-  private HTable table;
-  private TableRecordReader tableRecordReader;
-  private RowFilterInterface rowFilter;
-
-  /**
-   * Iterate over an HBase table data, return (Text, VectorResult) pairs
-   */
-  protected class TableRecordReader implements
-      RecordReader<ImmutableBytesWritable, Vector> {
-    private byte[] startRow;
-    private byte[] endRow;
-    private RowFilterInterface trrRowFilter;
-    private Scanner scanner;
-    private HTable htable;
-    private byte[][] trrInputColumns;
-
-    /**
-     * Build the scanner. Not done in constructor to allow for extension.
-     * 
-     * @throws IOException
-     */
-    public void init() throws IOException {
-      if ((endRow != null) && (endRow.length > 0)) {
-        if (trrRowFilter != null) {
-          final Set<RowFilterInterface> rowFiltersSet = new HashSet<RowFilterInterface>();
-          rowFiltersSet.add(new StopRowFilter(endRow));
-          rowFiltersSet.add(trrRowFilter);
-          this.scanner = this.htable.getScanner(trrInputColumns, startRow,
-              new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL,
-                  rowFiltersSet));
-        } else {
-          this.scanner = this.htable.getScanner(trrInputColumns, startRow,
-              endRow);
-        }
-      } else {
-        this.scanner = this.htable.getScanner(trrInputColumns, startRow,
-            trrRowFilter);
-      }
-    }
-
-    /**
-     * @param htable the {@link HTable} to scan.
-     */
-    public void setHTable(HTable htable) {
-      this.htable = htable;
-    }
-
-    /**
-     * @param inputColumns the columns to be placed in {@link VectorWritable}.
-     */
-    public void setInputColumns(final byte[][] inputColumns) {
-      this.trrInputColumns = inputColumns;
-    }
-
-    /**
-     * @param startRow the first row in the split
-     */
-    public void setStartRow(final byte[] startRow) {
-      this.startRow = startRow;
-    }
-
-    /**
-     * 
-     * @param endRow the last row in the split
-     */
-    public void setEndRow(final byte[] endRow) {
-      this.endRow = endRow;
-    }
-
-    /**
-     * @param rowFilter the {@link RowFilterInterface} to be used.
-     */
-    public void setRowFilter(RowFilterInterface rowFilter) {
-      this.trrRowFilter = rowFilter;
-    }
-
-    /** {@inheritDoc} */
-    public void close() throws IOException {
-      this.scanner.close();
-    }
-
-    /**
-     * @return ImmutableBytesWritable
-     * 
-     * @see org.apache.hadoop.mapred.RecordReader#createKey()
-     */
-    public ImmutableBytesWritable createKey() {
-      return new ImmutableBytesWritable();
-    }
-
-    /**
-     * @return VectorResult
-     * 
-     * @see org.apache.hadoop.mapred.RecordReader#createValue()
-     */
-    public Vector createValue() {
-      return new Vector();
-    }
-
-    /** {@inheritDoc} */
-    public long getPos() {
-      // This should be the ordinal tuple in the range;
-      // not clear how to calculate...
-      return 0;
-    }
-
-    /** {@inheritDoc} */
-    public float getProgress() {
-      // Depends on the total number of tuples and getPos
-      return 0;
-    }
-
-    /**
-     * @param key HStoreKey as input key.
-     * @param value MapWritable as input value
-     * 
-     *          Converts Scanner.next() to Text, VectorResult
-     * 
-     * @return true if there was more data
-     * @throws IOException
-     */
-    @SuppressWarnings("unchecked")
-    public boolean next(ImmutableBytesWritable key, Vector value)
-        throws IOException {
-      RowResult result = this.scanner.next();
-      boolean hasMore = result != null && result.size() > 0;
-      if (hasMore) {
-        key.set(result.getRow());
-        Writables.copyWritable(result, value);
-      }
-      return hasMore;
-    }
-  }
-
-  /**
-   * Builds a TableRecordReader. If no TableRecordReader was provided, uses the
-   * default.
-   * 
-   * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit,
-   *      JobConf, Reporter)
-   */
-  public RecordReader<ImmutableBytesWritable, Vector> getRecordReader(
-      InputSplit split, @SuppressWarnings("unused") JobConf job,
-      @SuppressWarnings("unused") Reporter reporter) throws IOException {
-    TableSplit tSplit = (TableSplit) split;
-    TableRecordReader trr = this.tableRecordReader;
-    // if no table record reader was provided use default
-    if (trr == null) {
-      trr = new TableRecordReader();
-    }
-    trr.setStartRow(tSplit.getStartRow());
-    trr.setEndRow(tSplit.getEndRow());
-    trr.setHTable(this.table);
-    trr.setInputColumns(this.inputColumns);
-    trr.setRowFilter(this.rowFilter);
-    trr.init();
-    return trr;
-  }
-
-  /**
-   * Calculates the splits that will serve as input for the map tasks.
-   * <ul>
-   * Splits are created in number equal to the smallest between numSplits and
-   * the number of {@link HRegion}s in the table. If the number of splits is
-   * smaller than the number of {@link HRegion}s then splits are spanned across
-   * multiple {@link HRegion}s and are grouped the most evenly possible. In the
-   * case splits are uneven the bigger splits are placed first in the
-   * {@link InputSplit} array.
-   * 
-   * @param job the map task {@link JobConf}
-   * @param numSplits a hint to calculate the number of splits
-   * 
-   * @return the input splits
-   * 
-   * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf,
-   *      int)
-   */
-  public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
-    byte[][] startKeys = this.table.getStartKeys();
-    if (startKeys == null || startKeys.length == 0) {
-      throw new IOException("Expecting at least one region");
-    }
-    if (this.table == null) {
-      throw new IOException("No table was provided");
-    }
-    if (this.inputColumns == null || this.inputColumns.length == 0) {
-      throw new IOException("Expecting at least one column");
-    }
-    int realNumSplits = numSplits > startKeys.length ? startKeys.length
-        : numSplits;
-    InputSplit[] splits = new InputSplit[realNumSplits];
-    int middle = startKeys.length / realNumSplits;
-    int startPos = 0;
-    for (int i = 0; i < realNumSplits; i++) {
-      int lastPos = startPos + middle;
-      lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos;
-      splits[i] = new TableSplit(this.table.getTableName(),
-          startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos]
-              : HConstants.EMPTY_START_ROW);
-      if (LOG.isDebugEnabled()) {
-        LOG.debug("split: " + i + "->" + splits[i]);
-      }
-      startPos = lastPos;
-    }
-    return splits;
-
-  }
-
-  /**
-   * @param inputColumns to be passed in {@link VectorWritable} to the map task.
-   */
-  protected void setInputColums(byte[][] inputColumns) {
-    this.inputColumns = inputColumns;
-  }
-
-  /**
-   * Allows subclasses to set the {@link HTable}.
-   * 
-   * @param table to get the data from
-   */
-  protected void setHTable(HTable table) {
-    this.table = table;
-  }
-
-  /**
-   * Allows subclasses to set the {@link TableRecordReader}.
-   * 
-   * @param tableRecordReader to provide other {@link TableRecordReader}
-   *          implementations.
-   */
-  protected void setTableRecordReader(TableRecordReader tableRecordReader) {
-    this.tableRecordReader = tableRecordReader;
-  }
-
-  /**
-   * Allows subclasses to set the {@link RowFilterInterface} to be used.
-   * 
-   * @param rowFilter
-   */
-  protected void setRowFilter(RowFilterInterface rowFilter) {
-    this.rowFilter = rowFilter;
-  }
-}
+/**
+ * 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.mapred;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Scanner;
+import org.apache.hadoop.hbase.filter.RowFilterInterface;
+import org.apache.hadoop.hbase.filter.RowFilterSet;
+import org.apache.hadoop.hbase.filter.StopRowFilter;
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.hbase.mapred.TableSplit;
+import org.apache.hadoop.hbase.regionserver.HRegion;
+import org.apache.hadoop.hbase.util.Writables;
+import org.apache.hadoop.mapred.InputFormat;
+import org.apache.hadoop.mapred.InputSplit;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.RecordReader;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.DenseVector;
+
+public abstract class MatrixInputFormatBase implements
+    InputFormat<ImmutableBytesWritable, DenseVector> {
+  private final Log LOG = LogFactory.getLog(MatrixInputFormatBase.class);
+  private byte[][] inputColumns;
+  private HTable table;
+  private TableRecordReader tableRecordReader;
+  private RowFilterInterface rowFilter;
+
+  /**
+   * Iterate over an HBase table data, return (Text, DenseVector) pairs
+   */
+  protected class TableRecordReader implements
+      RecordReader<ImmutableBytesWritable, DenseVector> {
+    private byte[] startRow;
+    private byte[] endRow;
+    private RowFilterInterface trrRowFilter;
+    private Scanner scanner;
+    private HTable htable;
+    private byte[][] trrInputColumns;
+
+    /**
+     * Build the scanner. Not done in constructor to allow for extension.
+     * 
+     * @throws IOException
+     */
+    public void init() throws IOException {
+      if ((endRow != null) && (endRow.length > 0)) {
+        if (trrRowFilter != null) {
+          final Set<RowFilterInterface> rowFiltersSet = new HashSet<RowFilterInterface>();
+          rowFiltersSet.add(new StopRowFilter(endRow));
+          rowFiltersSet.add(trrRowFilter);
+          this.scanner = this.htable.getScanner(trrInputColumns, startRow,
+              new RowFilterSet(RowFilterSet.Operator.MUST_PASS_ALL,
+                  rowFiltersSet));
+        } else {
+          this.scanner = this.htable.getScanner(trrInputColumns, startRow,
+              endRow);
+        }
+      } else {
+        this.scanner = this.htable.getScanner(trrInputColumns, startRow,
+            trrRowFilter);
+      }
+    }
+
+    /**
+     * @param htable the {@link HTable} to scan.
+     */
+    public void setHTable(HTable htable) {
+      this.htable = htable;
+    }
+
+    /**
+     * @param inputColumns the columns to be placed in {@link DenseVector}.
+     */
+    public void setInputColumns(final byte[][] inputColumns) {
+      this.trrInputColumns = inputColumns;
+    }
+
+    /**
+     * @param startRow the first row in the split
+     */
+    public void setStartRow(final byte[] startRow) {
+      this.startRow = startRow;
+    }
+
+    /**
+     * 
+     * @param endRow the last row in the split
+     */
+    public void setEndRow(final byte[] endRow) {
+      this.endRow = endRow;
+    }
+
+    /**
+     * @param rowFilter the {@link RowFilterInterface} to be used.
+     */
+    public void setRowFilter(RowFilterInterface rowFilter) {
+      this.trrRowFilter = rowFilter;
+    }
+
+    /** {@inheritDoc} */
+    public void close() throws IOException {
+      this.scanner.close();
+    }
+
+    /**
+     * @return ImmutableBytesWritable
+     * 
+     * @see org.apache.hadoop.mapred.RecordReader#createKey()
+     */
+    public ImmutableBytesWritable createKey() {
+      return new ImmutableBytesWritable();
+    }
+
+    /**
+     * @return DenseVector
+     * 
+     * @see org.apache.hadoop.mapred.RecordReader#createValue()
+     */
+    public DenseVector createValue() {
+      return new DenseVector();
+    }
+
+    /** {@inheritDoc} */
+    public long getPos() {
+      // This should be the ordinal tuple in the range;
+      // not clear how to calculate...
+      return 0;
+    }
+
+    /** {@inheritDoc} */
+    public float getProgress() {
+      // Depends on the total number of tuples and getPos
+      return 0;
+    }
+
+    /**
+     * @param key HStoreKey as input key.
+     * @param value MapWritable as input value
+     * 
+     *          Converts Scanner.next() to Text, DenseVector
+     * 
+     * @return true if there was more data
+     * @throws IOException
+     */
+    @SuppressWarnings("unchecked")
+    public boolean next(ImmutableBytesWritable key, DenseVector value)
+        throws IOException {
+      RowResult result = this.scanner.next();
+      boolean hasMore = result != null && result.size() > 0;
+      if (hasMore) {
+        key.set(result.getRow());
+        Writables.copyWritable(result, value);
+      }
+      return hasMore;
+    }
+  }
+
+  /**
+   * Builds a TableRecordReader. If no TableRecordReader was provided, uses the
+   * default.
+   * 
+   * @see org.apache.hadoop.mapred.InputFormat#getRecordReader(InputSplit,
+   *      JobConf, Reporter)
+   */
+  public RecordReader<ImmutableBytesWritable, DenseVector> getRecordReader(
+      InputSplit split, @SuppressWarnings("unused") JobConf job,
+      @SuppressWarnings("unused") Reporter reporter) throws IOException {
+    TableSplit tSplit = (TableSplit) split;
+    TableRecordReader trr = this.tableRecordReader;
+    // if no table record reader was provided use default
+    if (trr == null) {
+      trr = new TableRecordReader();
+    }
+    trr.setStartRow(tSplit.getStartRow());
+    trr.setEndRow(tSplit.getEndRow());
+    trr.setHTable(this.table);
+    trr.setInputColumns(this.inputColumns);
+    trr.setRowFilter(this.rowFilter);
+    trr.init();
+    return trr;
+  }
+
+  /**
+   * Calculates the splits that will serve as input for the map tasks.
+   * <ul>
+   * Splits are created in number equal to the smallest between numSplits and
+   * the number of {@link HRegion}s in the table. If the number of splits is
+   * smaller than the number of {@link HRegion}s then splits are spanned across
+   * multiple {@link HRegion}s and are grouped the most evenly possible. In the
+   * case splits are uneven the bigger splits are placed first in the
+   * {@link InputSplit} array.
+   * 
+   * @param job the map task {@link JobConf}
+   * @param numSplits a hint to calculate the number of splits
+   * 
+   * @return the input splits
+   * 
+   * @see org.apache.hadoop.mapred.InputFormat#getSplits(org.apache.hadoop.mapred.JobConf,
+   *      int)
+   */
+  public InputSplit[] getSplits(JobConf job, int numSplits) throws IOException {
+    byte[][] startKeys = this.table.getStartKeys();
+    if (startKeys == null || startKeys.length == 0) {
+      throw new IOException("Expecting at least one region");
+    }
+    if (this.table == null) {
+      throw new IOException("No table was provided");
+    }
+    if (this.inputColumns == null || this.inputColumns.length == 0) {
+      throw new IOException("Expecting at least one column");
+    }
+    int realNumSplits = numSplits > startKeys.length ? startKeys.length
+        : numSplits;
+    InputSplit[] splits = new InputSplit[realNumSplits];
+    int middle = startKeys.length / realNumSplits;
+    int startPos = 0;
+    for (int i = 0; i < realNumSplits; i++) {
+      int lastPos = startPos + middle;
+      lastPos = startKeys.length % realNumSplits > i ? lastPos + 1 : lastPos;
+      splits[i] = new TableSplit(this.table.getTableName(),
+          startKeys[startPos], ((i + 1) < realNumSplits) ? startKeys[lastPos]
+              : HConstants.EMPTY_START_ROW);
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("split: " + i + "->" + splits[i]);
+      }
+      startPos = lastPos;
+    }
+    return splits;
+
+  }
+
+  /**
+   * @param inputColumns to be passed in {@link DenseVector} to the map task.
+   */
+  protected void setInputColums(byte[][] inputColumns) {
+    this.inputColumns = inputColumns;
+  }
+
+  /**
+   * Allows subclasses to set the {@link HTable}.
+   * 
+   * @param table to get the data from
+   */
+  protected void setHTable(HTable table) {
+    this.table = table;
+  }
+
+  /**
+   * Allows subclasses to set the {@link TableRecordReader}.
+   * 
+   * @param tableRecordReader to provide other {@link TableRecordReader}
+   *          implementations.
+   */
+  protected void setTableRecordReader(TableRecordReader tableRecordReader) {
+    this.tableRecordReader = tableRecordReader;
+  }
+
+  /**
+   * Allows subclasses to set the {@link RowFilterInterface} to be used.
+   * 
+   * @param rowFilter
+   */
+  protected void setRowFilter(RowFilterInterface rowFilter) {
+    this.rowFilter = rowFilter;
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/mapred/MatrixMap.java Mon Aug 25 06:27:04 2008
@@ -1,61 +1,62 @@
-/**
- * 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.mapred;
-
-import java.io.IOException;
-
-import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
-import org.apache.hadoop.io.Writable;
-import org.apache.hadoop.io.WritableComparable;
-import org.apache.hadoop.mapred.FileInputFormat;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.MapReduceBase;
-import org.apache.hadoop.mapred.Mapper;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.Constants;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-import org.apache.hama.Vector;
-
-@SuppressWarnings("unchecked")
-public abstract class MatrixMap<K extends WritableComparable, V extends Writable>
-    extends MapReduceBase implements
-    Mapper<ImmutableBytesWritable, Vector, K, V> {
-  protected static Matrix B;
-
-  public static void initJob(String matrixA, String matrixB,
-      Class<? extends MatrixMap> mapper,
-      Class<? extends WritableComparable> outputKeyClass,
-      Class<? extends Writable> outputValueClass, JobConf job) {
-
-    job.setInputFormat(MatrixInputFormat.class);
-    job.setMapOutputValueClass(outputValueClass);
-    job.setMapOutputKeyClass(outputKeyClass);
-    job.setMapperClass(mapper);
-    FileInputFormat.addInputPaths(job, matrixA);
-
-    B = new Matrix(new HamaConfiguration(), matrixB);
-    job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN);
-  }
-
-  public abstract void map(ImmutableBytesWritable key, Vector value,
-      OutputCollector<K, V> output, Reporter reporter) throws IOException;
-}
+/**
+ * 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.mapred;
+
+import java.io.IOException;
+
+import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
+import org.apache.hadoop.io.Writable;
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.mapred.FileInputFormat;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.MapReduceBase;
+import org.apache.hadoop.mapred.Mapper;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.Constants;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.Vector;
+
+@Deprecated
+public abstract class MatrixMap<K extends WritableComparable, V extends Writable>
+    extends MapReduceBase implements
+    Mapper<ImmutableBytesWritable, Vector, K, V> {
+  protected static Matrix MATRIX_B;
+
+  public static void initJob(String matrixA, String matrixB,
+      Class<? extends DenseMap> mapper,
+      Class<? extends WritableComparable> outputKeyClass,
+      Class<? extends Writable> outputValueClass, JobConf job) {
+
+    job.setInputFormat(MatrixInputFormat.class);
+    job.setMapOutputValueClass(outputValueClass);
+    job.setMapOutputKeyClass(outputKeyClass);
+    job.setMapperClass(mapper);
+    FileInputFormat.addInputPaths(job, matrixA);
+
+    MATRIX_B = new DenseMatrix(new HamaConfiguration(), matrixB);
+    job.set(MatrixInputFormat.COLUMN_LIST, Constants.COLUMN);
+  }
+
+  public abstract void map(ImmutableBytesWritable key, Vector value,
+      OutputCollector<K, V> output, Reporter reporter) throws IOException;
+}

Modified: incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java?rev=688720&r1=688719&r2=688720&view=diff
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java (original)
+++ incubator/hama/trunk/src/test/org/apache/hama/TestMatrix.java Mon Aug 25 06:27:04 2008
@@ -28,7 +28,7 @@
    * Random matrix creation test
    */
   public void testRandomMatrix() {
-    Matrix rand = Matrix.random(conf, SIZE, SIZE);
+    Matrix rand = DenseMatrix.random(conf, SIZE, SIZE);
     assertTrue(rand.getRows() == SIZE);
   }
 }



Mime
View raw message