incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r708884 [1/2] - in /incubator/hama/trunk: ./ src/java/org/apache/hama/ src/java/org/apache/hama/algebra/ src/java/org/apache/hama/util/ src/test/org/apache/hama/ src/test/org/apache/hama/mapred/
Date Wed, 29 Oct 2008 11:59:40 GMT
Author: edwardyoon
Date: Wed Oct 29 04:59:40 2008
New Revision: 708884

URL: http://svn.apache.org/viewvc?rev=708884&view=rev
Log:
DenseMatrix.close should not delete the table that are aliased in HamaAdmin.

Modified:
    incubator/hama/trunk/CHANGES.txt
    incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/Constants.java
    incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java
    incubator/hama/trunk/src/java/org/apache/hama/HamaAdmin.java
    incubator/hama/trunk/src/java/org/apache/hama/HamaAdminImpl.java
    incubator/hama/trunk/src/java/org/apache/hama/Matrix.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/Add1DLayoutMap.java
    incubator/hama/trunk/src/java/org/apache/hama/algebra/Mult1DLayoutMap.java
    incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseMatrix.java
    incubator/hama/trunk/src/test/org/apache/hama/TestDenseVector.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=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Wed Oct 29 04:59:40 2008
@@ -7,17 +7,19 @@
     HAMA-92: Add subMatrix() to Matrix (edwardyoon)
     HAMA-83: Add a writable comparable for BlockID (edwardyoon) 
     HAMA-81: Add subVector(int i0, int i1) to Vector (edwardyoon)
-    Hama-80: Add identity(int m, int n) which returns identity matrix (edwardyoon) 
+    Hama-80: Add identity(int m, int n) 
+               which returns identity matrix (edwardyoon) 
     HAMA-62: Hama Shell Implementation (samuel via edwardyoon)
     HAMA-61: Load / save matrices from HTable (edwardyoon)
     HAMA-51: Add get/setRowAttribute() method (edwardyoon)
     HAMA-13: Matrix multiplication (edwardyoon)
-    HAMA-48: Add getColumn(int column) method which returns column vector (edwardyoon)
+    HAMA-48: Add getColumn(int column) method 
+               which returns column vector (edwardyoon)
     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)
-    HAMA-36: Add svn information to website (edward yoon)
+    HAMA-34: Hudson/Jira Intergration (edwardyoon)
+    HAMA-36: Add svn information to website (edwardyoon)
     HAMA-33: Add set() method (edwardyoon)
     HAMA-30: Add scaling method to Vector (edwardyoon)
     HAMA-25: Add matrix addition example (edwardyoon)
@@ -29,18 +31,23 @@
     
   IMPROVEMENTS
     
-    HAMA-89: Exception Handling of a ParseException (samuel)
-    HAMA-77: clear the matrices in hbase after quiting the shell (samuel via edwardyoon)
+    HAMA-88: DenseMatrix.close should not delete the table 
+               that are aliased in HamaAdmin. (samuel via edwardyoon)
+    HAMA-89: Exception Handling of a ParseException (samuel via edwardyoon)
+    HAMA-77: clear the matrices in hbase after 
+               quiting the shell (samuel via edwardyoon)
     HAMA-28: Implement Vector.add() method (edwardyoon)
     HAMA-78: Separate Interface and Implementation for HamaAdmin (edwardyoon)
     HAMA-76: Remove load() since it duplicated with constructor (edwardyoon)
-    HAMA-74: set the number of map/reduce task on the shell (samuel)
+    HAMA-74: set the number of map/reduce task 
+               on the shell (samuel via edwardyoon)
     HAMA-73: Add setNumMap/ReduceTasks() to HamaConfiguration (edwardyoon)
-    HAMA-60: Add an arguments for handle parallel degree to MatrixAddition (edwardyoon)
+    HAMA-60: Add an arguments for handle parallel degree
+               to MatrixAddition (edwardyoon)
     HAMA-68: Shell parser JUnit test cases (samuel via edwardyoon)
     HAMA-65: Remove the information of an inactive committers (edwardyoon) 
     HAMA-58: Remove duplicated code (edwardyoon)
-    HAMA-56: Add setRow(int row, Vector vector) method to matrix inteface (edwardyoon)
+    HAMA-56: Add setRow(int row, Vector vector) method to matrix (edwardyoon)
     HAMA-39: IO operations should throws an IOException (edwardyoon)
     HAMA-52: Fixture setup (edwardyoon)
     HAMA-42: Replace ImmutableBytesWritable to IntWritable (edwardyoon)
@@ -54,11 +61,11 @@
     HAMA-31: Refactor VectorWritable (edwardyoon)
     HAMA-29: Replace Matrix(Conf, Text) to Matrix(Conf, String) (edwardyoon)
     HAMA-27: Replace table.get(Text, Text) 
-               to table.get(String, String) (edwardyoon)
+             to table.get(String, String) (edwardyoon)
     HAMA-12: Matrix interface re-arrangement (edwardyoon)
     HAMA-21: Implement get(int index) method (edwardyoon)
     HAMA-19: Remove vector-norms enum to interface 
-               from implementation (edwardyoon)
+             from implementation (edwardyoon)
     HAMA-17: Vector to Writable conversion (edwardyoon)
     HAMA-15: Logo Alignment (edwardyoon) 
     HAMA-10: Refactor the mapred package 

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=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/AbstractMatrix.java Wed Oct 29 04:59:40 2008
@@ -26,6 +26,7 @@
 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.hama.io.VectorUpdate;
@@ -40,12 +41,14 @@
 
   protected HamaConfiguration config;
   protected HBaseAdmin admin;
+  // a matrix just need a table path to point to the table which stores matrix.
+  // let HamaAdmin manage Matrix Name space.
   protected String matrixPath;
-  protected String matrixName;
   protected HTable table;
   protected HTableDescriptor tableDesc;
-  public HamaAdmin store;
+  protected HamaAdmin hamaAdmin;
 
+  protected boolean closed = true;
   /**
    * Sets the job configuration
    * 
@@ -59,7 +62,7 @@
       LOG.error(e, e);
     }
 
-    store = new HamaAdminImpl(conf, admin);
+    hamaAdmin = new HamaAdminImpl(conf, admin);
   }
 
   /**
@@ -70,9 +73,22 @@
     if (!admin.tableExists(matrixPath)) {
       this.tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
       this.tableDesc.addFamily(new HColumnDescriptor(Constants.ATTRIBUTE));
+      this.tableDesc.addFamily(new HColumnDescriptor(Constants.ALIASEFAMILY));
 
       LOG.info("Initializing the matrix storage.");
       this.admin.createTable(this.tableDesc);
+      LOG.info("Create Matrix " + matrixPath);
+      
+      // connect to the table.
+      table = new HTable(config, matrixPath);
+      // Record the matrix type in METADATA_TYPE
+      BatchUpdate update = new BatchUpdate(Constants.METADATA);
+      update.put(Constants.METADATA_TYPE, Bytes.toBytes(this.getClass().getSimpleName()));
+
+      table.commit(update);
+      
+      // the new matrix's reference is 1.
+      setReference(1);
     }
   }
 
@@ -147,15 +163,68 @@
   }
 
   /** {@inheritDoc} */
-  public String getName() {
-    return (this.matrixName != null) ? this.matrixName : null;
+  public String getPath() {
+    return matrixPath;
   }
-
-  public void close() throws IOException {
-    store.delete(this.matrixName);
+  
+  protected void setReference(int reference) throws IOException {
+    BatchUpdate update = new BatchUpdate(Constants.METADATA);
+    update.put(Constants.METADATA_REFERENCE, Bytes.toBytes(reference));
+    table.commit(update);
+  }
+  
+  protected int incrementAndGetRef() throws IOException {
+    int reference = 1;
+    Cell rows = null;
+    rows = table.get(Constants.METADATA, Constants.METADATA_REFERENCE);
+    if(rows != null) {
+      reference = Bytes.toInt(rows.getValue());
+      reference++;
+    }
+    setReference(reference);
+    return reference;
+  }
+  
+  protected int decrementAndGetRef() throws IOException {
+    int reference = 0;
+    Cell rows = null;
+    rows = table.get(Constants.METADATA, Constants.METADATA_REFERENCE);
+    if(rows != null) {
+      reference = Bytes.toInt(rows.getValue());
+      if(reference>0) // reference==0, we need not to decrement it.
+        reference--;
+    }
+    setReference(reference);
+    return reference;
+  }
+  
+  protected boolean hasAliaseName() throws IOException {
+    Cell rows = null;
+    rows = table.get(Constants.METADATA, Constants.ALIASENAME);
+    return (rows != null) ? true : false;
   }
 
-  public boolean save(String name) throws IOException {
-    return store.save(this, name);
+  public void close() throws IOException {
+    if(closed) // have been closed
+      return;
+    int reference = decrementAndGetRef();
+    if(reference<=0) { // no reference again.
+      if(! hasAliaseName()) { // the table has not been aliased, we delete the table.
+        if (admin.isTableEnabled(matrixPath)) {
+          admin.disableTable(matrixPath);
+          admin.deleteTable(matrixPath);
+        }
+      }
+    } 
+    closed = true;
+  }
+
+  public boolean save(String aliasename) throws IOException {
+    // mark & update the aliase name in "alise:name" meta column.
+    // ! one matrix has only one aliasename now.
+    BatchUpdate update = new BatchUpdate(Constants.METADATA);
+    update.put(Constants.ALIASENAME, Bytes.toBytes(aliasename));
+    table.commit(update);
+    return hamaAdmin.save(this, aliasename);
   }
 }

Modified: incubator/hama/trunk/src/java/org/apache/hama/Constants.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/Constants.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Constants.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Constants.java Wed Oct 29 04:59:40 2008
@@ -1,62 +1,89 @@
-/**
- * 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;
-
-/**
- * Some constants used in the Hama
- */
-public class Constants {
-
-  /** Meta-columnFamily to store the matrix-info */
-  public final static String METADATA = "metadata";
-
-  /** Column index & attributes */
-  public final static String CINDEX = "cIndex";
-
-  /** The attribute column family */
-  public final static String ATTRIBUTE = "attribute:";
-
-  /** The number of the matrix rows */
-  public final static String METADATA_ROWS = "attribute:rows";
-
-  /** The number of the matrix columns */
-  public final static String METADATA_COLUMNS = "attribute:columns";
-
-  /** The type of the matrix */
-  public final static String METADATA_TYPE = "attribute:type";
-
-  /** Default columnFamily name */
-  public final static String COLUMN = "column:";
-
-  /** Temporary random matrices name prefix */
-  public final static String RANDOM = "rand";
-
-  /** Admin table name */
-  public final static String ADMINTABLE = "admin.table";
-
-  /** Matrix path columnFamily */
-  public static final String PATHCOLUMN = "path:";
-
-  /** Temporary Aliase name prefix in Hama Shell */
-  public static final String RANDOMALIASE = "_";
-
-  /** Matrix type information (e.g. DenseMatrix, SparseMatrix, .., etc) */
-  public static final String MATRIXTYPE = "type:";
-}
+/**
+ * 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;
+
+/**
+ * Some constants used in the Hama
+ */
+public class Constants {
+
+  /** Meta-columnFamily to store the matrix-info */
+  public final static String METADATA = "metadata";
+
+  /** Column index & attributes */
+  public final static String CINDEX = "cIndex";
+
+  /** The attribute column family */
+  public final static String ATTRIBUTE = "attribute:";
+
+  /** The number of the matrix rows */
+  public final static String METADATA_ROWS = "attribute:rows";
+
+  /** The number of the matrix columns */
+  public final static String METADATA_COLUMNS = "attribute:columns";
+
+  /** The type of the matrix */
+  public final static String METADATA_TYPE = "attribute:type";
+  
+  /** The reference of the matrix */
+  /** (1) when we create a Matrix object, we set up a connection to hbase table,
+   *      the reference of the table will be incremented.
+   *  (2) when we close a Matrix object, we disconnect the hbase table, 
+   *      the reference of the table will be decremented.
+   *      i)  if the reference of the table is not zero:
+   *          we should not delete the table, because some other matrix object
+   *          connect to the table.
+   *      ii) if the reference of the table is zero:
+   *          we need to know if the matrix table is aliased.
+   *          1) if the matrix table is aliased, we should not delete the table.
+   *          2) if the matrix table is not aliased, we need to delete the table.
+   */
+  public final static String METADATA_REFERENCE = "attribute:reference";
+  
+  /** The aliase names column family */
+  public final static String ALIASEFAMILY = "aliase:";
+  
+  /** The aliase names of the matrix, sperated by "," */
+  public final static String ALIASENAME = "aliase:name";
+
+  /** Default columnFamily name */
+  public final static String COLUMN = "column:";
+
+  /** Temporary random matrices name prefix */
+  public final static String RANDOM = "rand";
+
+  /** Admin table name */
+  public final static String ADMINTABLE = "admin.table";
+
+  /** Matrix path columnFamily */
+  public static final String PATHCOLUMN = "path:";
+
+  /** Temporary Aliase name prefix in Hama Shell */
+  public static final String RANDOMALIASE = "_";
+  
+  /** default matrix's path length (tablename length) */
+  public static final int DEFAULT_PATH_LENGTH = 5;
+  
+  /** default matrix's max path length (tablename length) */
+  public static final int DEFAULT_MAXPATHLEN = 10000;
+  
+  /** default try times to generate a suitable tablename */
+  public static final int DEFAULT_TRY_TIMES = 10000000;
+}

Modified: 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=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/DenseMatrix.java Wed Oct 29 04:59:40 2008
@@ -1,294 +1,370 @@
-/**
- * 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 java.util.Map;
-
-import org.apache.hadoop.hbase.HColumnDescriptor;
-import org.apache.hadoop.hbase.HConstants;
-import org.apache.hadoop.hbase.HTableDescriptor;
-import org.apache.hadoop.hbase.client.HTable;
-import org.apache.hadoop.hbase.client.Scanner;
-import org.apache.hadoop.hbase.io.Cell;
-import org.apache.hadoop.hbase.io.RowResult;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hama.algebra.Add1DLayoutMap;
-import org.apache.hama.algebra.Add1DLayoutReduce;
-import org.apache.hama.algebra.Mult1DLayoutMap;
-import org.apache.hama.algebra.Mult1DLayoutReduce;
-import org.apache.hama.io.VectorEntry;
-import org.apache.hama.io.VectorMapWritable;
-import org.apache.hama.io.VectorUpdate;
-import org.apache.hama.io.VectorWritable;
-import org.apache.hama.mapred.MatrixReduce;
-import org.apache.hama.util.JobManager;
-import org.apache.hama.util.Numeric;
-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 (store.matrixExists(matrixName)) {
-        this.matrixPath = store.getPath(matrixName);
-      } else {
-        this.matrixPath = matrixName;
-        tableDesc = new HTableDescriptor(matrixName);
-        create();
-      }
-
-      table = new HTable(config, this.matrixPath);
-    } 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);
-      matrixPath = RandomVariable.randMatrixName();
-
-      if (!admin.tableExists(matrixPath)) {
-        tableDesc = new HTableDescriptor(matrixPath);
-        tableDesc.addFamily(new HColumnDescriptor(Constants.COLUMN));
-        create();
-      }
-
-      table = new HTable(config, matrixPath);
-
-      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.
-   * @throws IOException
-   */
-  public static Matrix random(HamaConfiguration conf, int m, int n)
-      throws IOException {
-    String name = RandomVariable.randMatrixName();
-    Matrix rand = new DenseMatrix(conf, name);
-    for (int i = 0; i < m; i++) {
-      DenseVector vector = new DenseVector();
-      for (int j = 0; j < n; j++) {
-        vector.set(j, RandomVariable.rand());
-      }
-      rand.setRow(i, vector);
-    }
-
-    rand.setDimension(m, n);
-    LOG.info("Create the " + m + " * " + n + " random matrix : " + name);
-    return rand;
-  }
-
-  /**
-   * Generate identity matrix
-   * 
-   * @param conf configuration object
-   * @param m the number of rows.
-   * @param n the number of columns.
-   * @return an m-by-n matrix with ones on the diagonal and zeros elsewhere.
-   * @throws IOException
-   */
-  public static Matrix identity(HamaConfiguration conf, int m, int n)
-      throws IOException {
-    String name = RandomVariable.randMatrixName();
-    Matrix identity = new DenseMatrix(conf, name);
-
-    for (int i = 0; i < m; i++) {
-      DenseVector vector = new DenseVector();
-      for (int j = 0; j < n; j++) {
-        vector.set(j, (i == j ? 1.0 : 0.0));
-      }
-      identity.setRow(i, vector);
-    }
-
-    identity.setDimension(m, n);
-    LOG.info("Create the " + m + " * " + n + " identity matrix : " + name);
-    return identity;
-  }
-
-  public Matrix add(Matrix B) throws IOException {
-    String output = RandomVariable.randMatrixName();
-    Matrix result = new DenseMatrix(config, output);
-
-    JobConf jobConf = new JobConf(config);
-    jobConf.setJobName("addition MR job" + result.getName());
-
-    jobConf.setNumMapTasks(Integer.parseInt(config.get("mapred.map.tasks")));
-    jobConf.setNumReduceTasks(Integer.parseInt(config
-        .get("mapred.reduce.tasks")));
-
-    Add1DLayoutMap.initJob(this.getName(), B.getName(), Add1DLayoutMap.class,
-        IntWritable.class, VectorWritable.class, jobConf);
-    MatrixReduce.initJob(result.getName(), Add1DLayoutReduce.class, jobConf);
-
-    JobManager.execute(jobConf, result);
-    return result;
-  }
-
-  public Matrix add(double alpha, Matrix B) throws IOException {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public DenseVector getRow(int row) throws IOException {
-    VectorMapWritable<Integer, VectorEntry> values = new VectorMapWritable<Integer, VectorEntry>();
-    RowResult rowResult = table.getRow(String.valueOf(row));
-
-    for (Map.Entry<byte[], Cell> f : rowResult.entrySet()) {
-      VectorEntry entry = new VectorEntry(f.getValue());
-      values.put(Numeric.getColumnIndex(f.getKey()), entry);
-    }
-
-    return new DenseVector(values);
-  }
-
-  public Vector getColumn(int column) throws IOException {
-    byte[] columnKey = Numeric.getColumnIndex(column);
-    byte[][] c = { columnKey };
-    Scanner scan = table.getScanner(c, HConstants.EMPTY_START_ROW);
-
-    VectorMapWritable<Integer, VectorEntry> trunk = new VectorMapWritable<Integer, VectorEntry>();
-
-    for (RowResult row : scan) {
-      trunk.put(Numeric.bytesToInt(row.getRow()), new VectorEntry(row
-          .get(columnKey)));
-    }
-
-    return new DenseVector(trunk);
-  }
-
-  public Matrix mult(Matrix B) throws IOException {
-    String output = RandomVariable.randMatrixName();
-    Matrix result = new DenseMatrix(config, output);
-
-    JobConf jobConf = new JobConf(config);
-    jobConf.setJobName("multiplication MR job : " + result.getName());
-
-    jobConf.setNumMapTasks(Integer.parseInt(config.get("mapred.map.tasks")));
-    jobConf.setNumReduceTasks(Integer.parseInt(config
-        .get("mapred.reduce.tasks")));
-
-    Mult1DLayoutMap.initJob(this.getName(), B.getName(), Mult1DLayoutMap.class,
-        IntWritable.class, VectorWritable.class, jobConf);
-    MatrixReduce.initJob(result.getName(), Mult1DLayoutReduce.class, jobConf);
-    JobManager.execute(jobConf, result);
-    return result;
-  }
-
-  public Matrix multAdd(double alpha, Matrix B, Matrix C) throws IOException {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public double norm(Norm type) throws IOException {
-    // TODO Auto-generated method stub
-    return 0;
-  }
-
-  public Matrix set(double alpha, Matrix B) throws IOException {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public Matrix set(Matrix B) throws IOException {
-    // TODO Auto-generated method stub
-    return null;
-  }
-
-  public void setRow(int row, Vector vector) throws IOException {
-    VectorUpdate update = new VectorUpdate(row);
-    update.putAll(((DenseVector) vector).getEntries().entrySet());
-    table.commit(update.getBatchUpdate());
-  }
-
-  public void setColumn(int column, Vector vector) throws IOException {
-    // TODO Auto-generated method stub
-  }
-
-  public String getType() {
-    return this.getClass().getSimpleName();
-  }
-
-  public SubMatrix subMatrix(int i0, int i1, int j0, int j1) throws IOException {
-    int columnSize = (j1 - j0) + 1;
-    SubMatrix result = new SubMatrix((i1-i0) + 1, columnSize);
-    byte[][] c = new byte[columnSize][];
-    for (int i = 0; i < columnSize; i++) {
-      c[i] = Numeric.getColumnIndex(j0 + i);
-    }
-
-    Scanner scan = table.getScanner(c, Numeric.intToBytes(i0), Numeric
-        .intToBytes(i1 + 1));
-
-    int rKey = 0, cKey = 0;
-    for (RowResult row : scan) {
-      for (Map.Entry<byte[], Cell> e : row.entrySet()) {
-        result.set(rKey, cKey, Numeric.bytesToDouble(e.getValue().getValue()));
-        cKey++;
-      }
-      rKey++;
-      cKey = 0;
-    }
-
-    return 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;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.HTableDescriptor;
+import org.apache.hadoop.hbase.client.HTable;
+import org.apache.hadoop.hbase.client.Scanner;
+import org.apache.hadoop.hbase.io.Cell;
+import org.apache.hadoop.hbase.io.RowResult;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hama.algebra.Add1DLayoutMap;
+import org.apache.hama.algebra.Add1DLayoutReduce;
+import org.apache.hama.algebra.Mult1DLayoutMap;
+import org.apache.hama.algebra.Mult1DLayoutReduce;
+import org.apache.hama.io.VectorEntry;
+import org.apache.hama.io.VectorMapWritable;
+import org.apache.hama.io.VectorUpdate;
+import org.apache.hama.io.VectorWritable;
+import org.apache.hama.mapred.MatrixReduce;
+import org.apache.hama.util.JobManager;
+import org.apache.hama.util.Numeric;
+import org.apache.hama.util.RandomVariable;
+
+public class DenseMatrix extends AbstractMatrix implements Matrix {
+
+  static int tryPathLength = Constants.DEFAULT_PATH_LENGTH;
+  static final String TABLE_PREFIX = DenseMatrix.class.getSimpleName() + "_";
+  
+  /**
+   * Construct a raw matrix.
+   * Just create a table in HBase, but didn't lay any schema ( such as
+   * dimensions: i, j ) on it.
+   * 
+   * @param conf configuration object
+   * @throws IOException 
+   *         throw the exception to let the user know what happend, if we
+   *         didn't create the matrix successfully.
+   */
+  public DenseMatrix(HamaConfiguration conf) throws IOException {
+    setConfiguration(conf);
+    
+    tryToCreateTable();
+    
+    closed = false;
+  }
+
+  /**
+   * Create/load a matrix aliased as 'matrixName'.
+   * 
+   * @param conf configuration object
+   * @param matrixName the name of the matrix
+   * @param force if force is true, a new matrix will be created 
+   *              no matter 'matrixName' has aliased to an existed matrix;
+   *              otherwise, just try to load an existed matrix alised 
+   *              'matrixName'. 
+   * @throws IOException 
+   */
+  public DenseMatrix(HamaConfiguration conf, String matrixName, 
+      boolean force) throws IOException {
+    setConfiguration(conf);
+    // if force is set to true:
+    // 1) if this matrixName has aliase to other matrix, we will remove 
+    //    the old aliase, create a new matrix table, and aliase to it.
+    // 2) if this matrixName has no aliase to other matrix, we will create
+    //    a new matrix table, and alise to it.
+    //
+    // if force is set to false, we just try to load an existed matrix alised
+    // as 'matrixname'.
+    
+    boolean existed = hamaAdmin.matrixExists(matrixName);
+    
+    if (force) {
+      if(existed) {
+        // remove the old aliase
+        hamaAdmin.delete(matrixName);
+      }
+      // create a new matrix table.
+      tryToCreateTable();
+      // save the new aliase relationship
+      save(matrixName);
+    } else {
+      if(existed) {
+        // try to get the actual path of the table
+        matrixPath = hamaAdmin.getPath(matrixName);
+        // load the matrix
+        table = new HTable(conf, matrixPath);
+        // increment the reference
+        incrementAndGetRef();
+      } else {
+        throw new IOException("Try to load non-existed matrix alised as " + matrixName);
+      }
+    }
+
+    closed = false;
+  }
+  
+  /**
+   * Load a matrix from an existed matrix table whose tablename is 'matrixpath'
+   * 
+   * !! It is an internal used for map/reduce.
+   * 
+   * @param conf configuration object
+   * @param matrixpath 
+   * @throws IOException 
+   * @throws IOException 
+   */
+  public DenseMatrix(HamaConfiguration conf, String matrixpath) throws IOException {
+    setConfiguration(conf);
+    matrixPath = matrixpath;
+    // load the matrix
+    table = new HTable(conf, matrixPath);
+    // TODO: now we don't increment the reference of the table
+    //       for it's an internal use for map/reduce.
+    //       if we want to increment the reference of the table,
+    //       we don't know where to call Matrix.close in Add & Mul map/reduce
+    //       process to decrement the reference. It seems difficulty.
+  }
+
+  /**
+   * Create 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.
+   * @throws IOException 
+   *         throw the exception to let the user know what happend, if we
+   *         didn't create the matrix successfully.
+   */
+  public DenseMatrix(HamaConfiguration conf, int m, int n, double s) throws IOException {
+    setConfiguration(conf);
+    
+    tryToCreateTable();
+
+    closed = false;
+    
+    for (int i = 0; i < m; i++) {
+      for (int j = 0; j < n; j++) {
+        set(i, j, s);
+      }
+    }
+
+    setDimension(m, n);
+  }
+  
+  /** try to create a new matrix with a new random name.
+   *  try times will be (Integer.MAX_VALUE - 4) * DEFAULT_TRY_TIMES;
+   * @throws IOException
+   */
+  private void tryToCreateTable() throws IOException {
+    int tryTimes = Constants.DEFAULT_TRY_TIMES;
+    do {
+      matrixPath = TABLE_PREFIX + RandomVariable.randMatrixPath(tryPathLength);
+      
+      if (!admin.tableExists(matrixPath)) { // no table 'matrixPath' in hbase.
+        tableDesc = new HTableDescriptor(matrixPath);
+        create();
+        return;
+      }
+      
+      tryTimes--;
+      if(tryTimes <= 0) { // this loop has exhausted DEFAULT_TRY_TIMES.
+        tryPathLength++;
+        tryTimes = Constants.DEFAULT_TRY_TIMES;
+      }
+      
+    } while(tryPathLength <= Constants.DEFAULT_MAXPATHLEN);
+    // exhaustes the try times.
+    // throw out an IOException to let the user know what happened.
+    throw new IOException("Try too many times to create a table in hbase.");
+  }
+
+  /**
+   * 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.
+   * @throws IOException
+   */
+  public static Matrix random(HamaConfiguration conf, int m, int n)
+      throws IOException {
+    Matrix rand = new DenseMatrix(conf);
+    for (int i = 0; i < m; i++) {
+      DenseVector vector = new DenseVector();
+      for (int j = 0; j < n; j++) {
+        vector.set(j, RandomVariable.rand());
+      }
+      rand.setRow(i, vector);
+    }
+
+    rand.setDimension(m, n);
+    LOG.info("Create the " + m + " * " + n + " random matrix : " + rand.getPath());
+    return rand;
+  }
+
+  /**
+   * Generate identity matrix
+   * 
+   * @param conf configuration object
+   * @param m the number of rows.
+   * @param n the number of columns.
+   * @return an m-by-n matrix with ones on the diagonal and zeros elsewhere.
+   * @throws IOException
+   */
+  public static Matrix identity(HamaConfiguration conf, int m, int n)
+      throws IOException {
+    Matrix identity = new DenseMatrix(conf);
+
+    for (int i = 0; i < m; i++) {
+      DenseVector vector = new DenseVector();
+      for (int j = 0; j < n; j++) {
+        vector.set(j, (i == j ? 1.0 : 0.0));
+      }
+      identity.setRow(i, vector);
+    }
+
+    identity.setDimension(m, n);
+    LOG.info("Create the " + m + " * " + n + " identity matrix : " + identity.getPath());
+    return identity;
+  }
+
+  public Matrix add(Matrix B) throws IOException {
+    Matrix result = new DenseMatrix(config);
+
+    JobConf jobConf = new JobConf(config);
+    jobConf.setJobName("addition MR job" + result.getPath());
+
+    jobConf.setNumMapTasks(Integer.parseInt(config.get("mapred.map.tasks")));
+    jobConf.setNumReduceTasks(Integer.parseInt(config
+        .get("mapred.reduce.tasks")));
+
+    Add1DLayoutMap.initJob(this.getPath(), B.getPath(), Add1DLayoutMap.class,
+        IntWritable.class, VectorWritable.class, jobConf);
+    MatrixReduce.initJob(result.getPath(), Add1DLayoutReduce.class, jobConf);
+
+    JobManager.execute(jobConf, result);
+    return result;
+  }
+
+  public Matrix add(double alpha, Matrix B) throws IOException {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public DenseVector getRow(int row) throws IOException {
+    VectorMapWritable<Integer, VectorEntry> values = new VectorMapWritable<Integer, VectorEntry>();
+    RowResult rowResult = table.getRow(String.valueOf(row));
+
+    for (Map.Entry<byte[], Cell> f : rowResult.entrySet()) {
+      VectorEntry entry = new VectorEntry(f.getValue());
+      values.put(Numeric.getColumnIndex(f.getKey()), entry);
+    }
+
+    return new DenseVector(values);
+  }
+
+  public Vector getColumn(int column) throws IOException {
+    byte[] columnKey = Numeric.getColumnIndex(column);
+    byte[][] c = { columnKey };
+    Scanner scan = table.getScanner(c, HConstants.EMPTY_START_ROW);
+
+    VectorMapWritable<Integer, VectorEntry> trunk = new VectorMapWritable<Integer, VectorEntry>();
+
+    for (RowResult row : scan) {
+      trunk.put(Numeric.bytesToInt(row.getRow()), new VectorEntry(row
+          .get(columnKey)));
+    }
+
+    return new DenseVector(trunk);
+  }
+
+  public Matrix mult(Matrix B) throws IOException {
+    Matrix result = new DenseMatrix(config);
+
+    JobConf jobConf = new JobConf(config);
+    jobConf.setJobName("multiplication MR job : " + result.getPath());
+
+    jobConf.setNumMapTasks(Integer.parseInt(config.get("mapred.map.tasks")));
+    jobConf.setNumReduceTasks(Integer.parseInt(config
+        .get("mapred.reduce.tasks")));
+
+    Mult1DLayoutMap.initJob(this.getPath(), B.getPath(), Mult1DLayoutMap.class,
+        IntWritable.class, VectorWritable.class, jobConf);
+    MatrixReduce.initJob(result.getPath(), Mult1DLayoutReduce.class, jobConf);
+    JobManager.execute(jobConf, result);
+    return result;
+  }
+
+  public Matrix multAdd(double alpha, Matrix B, Matrix C) throws IOException {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public double norm(Norm type) throws IOException {
+    // TODO Auto-generated method stub
+    return 0;
+  }
+
+  public Matrix set(double alpha, Matrix B) throws IOException {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public Matrix set(Matrix B) throws IOException {
+    // TODO Auto-generated method stub
+    return null;
+  }
+
+  public void setRow(int row, Vector vector) throws IOException {
+    VectorUpdate update = new VectorUpdate(row);
+    update.putAll(((DenseVector) vector).getEntries().entrySet());
+    table.commit(update.getBatchUpdate());
+  }
+
+  public void setColumn(int column, Vector vector) throws IOException {
+    // TODO Auto-generated method stub
+  }
+
+  public String getType() {
+    return this.getClass().getSimpleName();
+  }
+
+  public SubMatrix subMatrix(int i0, int i1, int j0, int j1) throws IOException {
+    int columnSize = (j1 - j0) + 1;
+    SubMatrix result = new SubMatrix((i1-i0) + 1, columnSize);
+    byte[][] c = new byte[columnSize][];
+    for (int i = 0; i < columnSize; i++) {
+      c[i] = Numeric.getColumnIndex(j0 + i);
+    }
+
+    Scanner scan = table.getScanner(c, Numeric.intToBytes(i0), Numeric
+        .intToBytes(i1 + 1));
+
+    int rKey = 0, cKey = 0;
+    for (RowResult row : scan) {
+      for (Map.Entry<byte[], Cell> e : row.entrySet()) {
+        result.set(rKey, cKey, Numeric.bytesToDouble(e.getValue().getValue()));
+        cKey++;
+      }
+      rKey++;
+      cKey = 0;
+    }
+
+    return result;
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/HamaAdmin.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/HamaAdmin.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/HamaAdmin.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/HamaAdmin.java Wed Oct 29 04:59:40 2008
@@ -1,55 +1,55 @@
-/**
- * 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;
-
-public interface HamaAdmin {
-
-  /**
-   * Saves matrix as name 'AliaseName'
-   *
-   * @param matrix
-   * @param aliaseName
-   * @return true if it saved
-   */
-  public boolean save(Matrix matrix, String aliaseName); 
-
-  /**
-   * @param name
-   * @return return a physical path of matrix
-   */
-  public String getPath(String name);
-
-  /**
-   * @param matrixName
-   * @return true if matrix is exist
-   */
-  public boolean matrixExists(String matrixName);
-
-  /**
-   * Deletes matrix
-   * 
-   * @param matrixName
-   * @throws IOException
-   */
-  public void delete(String matrixName) 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;
+
+import java.io.IOException;
+
+public interface HamaAdmin {
+
+  /**
+   * Saves matrix as name 'AliaseName'
+   *
+   * @param matrix
+   * @param aliaseName
+   * @return true if it saved
+   */
+  public boolean save(Matrix matrix, String aliaseName); 
+
+  /**
+   * @param name
+   * @return return a physical path of matrix
+   */
+  public String getPath(String name);
+
+  /**
+   * @param matrixName
+   * @return true if matrix is exist
+   */
+  public boolean matrixExists(String matrixName);
+
+  /**
+   * Deletes matrix
+   * 
+   * @param matrixName
+   * @throws IOException
+   */
+  public void delete(String matrixName) throws IOException;
+  
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/HamaAdminImpl.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/HamaAdminImpl.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/HamaAdminImpl.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/HamaAdminImpl.java Wed Oct 29 04:59:40 2008
@@ -1,120 +1,166 @@
-/**
- * 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.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.log4j.Logger;
-
-public class HamaAdminImpl implements HamaAdmin {
-  static final Logger LOG = Logger.getLogger(HamaAdminImpl.class);
-  public HamaConfiguration conf;
-  public HBaseAdmin admin;
-  public HTable table;
-
-  public HamaAdminImpl(HamaConfiguration conf) {
-    this.conf = conf;
-    initialJob();
-  }
-
-  public HamaAdminImpl(HamaConfiguration conf, HBaseAdmin admin) {
-    this.conf = conf;
-    this.admin = admin;
-    initialJob();
-  }
-
-  public void initialJob() {
-    try {
-      if (!admin.tableExists(Constants.ADMINTABLE)) {
-        HTableDescriptor tableDesc = new HTableDescriptor(Constants.ADMINTABLE);
-        tableDesc.addFamily(new HColumnDescriptor(Constants.PATHCOLUMN));
-        tableDesc.addFamily(new HColumnDescriptor(Constants.MATRIXTYPE));
-        admin.createTable(tableDesc);
-      }
-
-      table = new HTable(conf, Constants.ADMINTABLE);
-    } catch (Exception e) {
-      e.printStackTrace();
-    }
-  }
-
-  /**
-   * @param name
-   * @return real table name
-   */
-  public String getPath(String name) {
-    try {
-      byte[] result = table.get(name, Constants.PATHCOLUMN).getValue();
-      return Bytes.toString(result);
-    } catch (IOException e) {
-      e.printStackTrace();
-      return null;
-    }
-  }
-
-  public boolean matrixExists(String matrixName) {
-    try {
-      Cell result = table.get(matrixName, Constants.PATHCOLUMN);
-      return (result == null) ? false : true;
-    } catch (IOException e) {
-      e.printStackTrace();
-      return false;
-    }
-  }
-
-  public boolean save(Matrix mat, String aliaseName) {
-    boolean result = false;
-
-    BatchUpdate update = new BatchUpdate(aliaseName);
-    update.put(Constants.PATHCOLUMN, Bytes.toBytes(mat.getName()));
-    // So, If we load a dense matrix, return the DenseMatrix.
-    update.put(Constants.MATRIXTYPE, Bytes.toBytes(mat.getType()));
-    
-    try {
-      table.commit(update);
-      result = true;
-    } catch (IOException e) {
-      e.printStackTrace();
-    }
-
-    return result;
-  }
-
-  public void delete(String matrixName) throws IOException {
-    String table;
-    if (matrixExists(matrixName)) {
-      table = getPath(matrixName);
-    } else {
-      table = matrixName;
-    }
-
-    if (admin.isTableEnabled(table)) {
-      admin.disableTable(table);
-      admin.deleteTable(table);
-    }
-  }
-}
+/**
+ * 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.log4j.Logger;
+
+/**
+ * An Implementation of {@link org.apache.hama.HamaAdmin} to manage 
+ * the matrix's namespace, and table allocation & garbage collection.
+ */
+public class HamaAdminImpl implements HamaAdmin {
+  static final Logger LOG = Logger.getLogger(HamaAdminImpl.class);
+  protected HamaConfiguration conf;
+  protected HBaseAdmin admin;
+  protected HTable table;
+
+  public HamaAdminImpl(HamaConfiguration conf) throws MasterNotRunningException {
+    this.conf = conf;
+    this.admin = new HBaseAdmin(conf);
+    initialJob();
+  }
+
+  public HamaAdminImpl(HamaConfiguration conf, HBaseAdmin admin) {
+    this.conf = conf;
+    this.admin = admin;
+    initialJob();
+  }
+
+  private void initialJob() {
+    try {
+      if (!admin.tableExists(Constants.ADMINTABLE)) {
+        HTableDescriptor tableDesc = new HTableDescriptor(Constants.ADMINTABLE);
+        tableDesc.addFamily(new HColumnDescriptor(Constants.PATHCOLUMN));
+        admin.createTable(tableDesc);
+      }
+
+      table = new HTable(conf, Constants.ADMINTABLE);
+    } catch (Exception e) {
+      e.printStackTrace();
+    }
+  }
+
+  /**
+   * @param name
+   * @return real table name
+   */
+  public String getPath(String name) {
+    try {
+      byte[] result = table.get(name, Constants.PATHCOLUMN).getValue();
+      return Bytes.toString(result);
+    } catch (IOException e) {
+      e.printStackTrace();
+      return null;
+    }
+  }
+
+  public boolean matrixExists(String matrixName) {
+    try {
+      Cell result = table.get(matrixName, Constants.PATHCOLUMN);
+      return (result == null) ? false : true;
+    } catch (IOException e) {
+      e.printStackTrace();
+      return false;
+    }
+  }
+
+  public boolean save(Matrix mat, String aliaseName) {
+    boolean result = false;
+
+    // we just store the name -> path(tablename) here.
+    // the matrix type is stored in its hbase table. we don't need to store again.
+    BatchUpdate update = new BatchUpdate(aliaseName);
+    update.put(Constants.PATHCOLUMN, Bytes.toBytes(mat.getPath()));
+    
+    try {
+      table.commit(update);
+      result = true;
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+    return result;
+  }
+
+  /** remove the entry of 'matrixName' in admin table. **/
+  private void removeEntry(String matrixName) throws IOException {
+    table.deleteAll(matrixName);
+  }
+  
+  private int getReference(String tableName) throws IOException {
+    HTable matrix = new HTable(conf, tableName);
+    
+    Cell rows = null;
+    rows = matrix.get(Constants.METADATA, Constants.METADATA_REFERENCE);
+    
+    return ( rows == null ) ? 0 : Bytes.toInt(rows.getValue());
+  }
+  
+  private void clearAliaseInfo(String tableName) throws IOException {
+    HTable matrix = new HTable(conf, tableName);
+    
+    matrix.deleteAll(Constants.METADATA, Constants.ALIASENAME);
+  }
+  
+  public void delete(String matrixName) throws IOException {
+    // we remove the aliase entry store in Admin table, and
+    // clear the aliase info store in matrix table.
+    // And check the reference of the matrix table:
+    // 1) if the reference of the matrix table is zero:
+    //    we delete the table.
+    // 2) if the reference of the matrix table is not zero:
+    //    we let the matrix who still reference the table to 
+    //    do the garbage collection.
+    if (matrixExists(matrixName)) {
+      String tablename = getPath(matrixName);
+      
+      // i) remove the aliase entry first.
+      removeEntry(matrixName);
+      
+      if(tablename == null) { // a matrixName point to a null table. we delete the entry.
+        return ; 
+      }
+      
+      if(!admin.tableExists(tablename)) { // have not specified table.
+        return;
+      }
+      
+      // ii) clear the aliase info store in matrix table.
+      clearAliaseInfo(tablename);
+      
+      if(getReference(tablename) <= 0) { // no reference, do gc!!
+        if (admin.isTableEnabled(tablename)) {
+          admin.disableTable(tablename);
+          admin.deleteTable(tablename);
+        }
+      }
+    }
+  }
+}

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=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/Matrix.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/Matrix.java Wed Oct 29 04:59:40 2008
@@ -85,11 +85,12 @@
   public String getColumnAttribute(int j) throws IOException;
 
   /**
-   * Return the matrix name
+   * Return the matrix path. 
+   * (in hbase, path is the tablename. in filesystem, path may be a file path.)
    * 
    * @return the name of the matrix
    */
-  public String getName();
+  public String getPath();
 
   /**
    * Sets the attribute of the row

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/Add1DLayoutMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/Add1DLayoutMap.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/Add1DLayoutMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/Add1DLayoutMap.java Wed Oct 29 04:59:40 2008
@@ -1,69 +1,73 @@
-/**
- * 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.io.IntWritable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.DenseVector;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-import org.apache.hama.Vector;
-import org.apache.hama.io.VectorWritable;
-import org.apache.hama.mapred.DenseMap;
-import org.apache.log4j.Logger;
-
-public class Add1DLayoutMap extends DenseMap<IntWritable, VectorWritable> {
-  static final Logger LOG = Logger.getLogger(Add1DLayoutMap.class);
-  protected Matrix matrix_b;
-  public static final String MATRIX_B = "hama.addition.matrix.b";
-
-  public void configure(JobConf job) {
-    matrix_b = new DenseMatrix(new HamaConfiguration(), job.get(MATRIX_B, ""));
-  }
-
-  public static void initJob(String matrix_a, String matrix_b,
-      Class<Add1DLayoutMap> map, Class<IntWritable> outputKeyClass,
-      Class<VectorWritable> outputValueClass, JobConf jobConf) {
-
-    jobConf.setMapOutputValueClass(outputValueClass);
-    jobConf.setMapOutputKeyClass(outputKeyClass);
-    jobConf.setMapperClass(map);
-    jobConf.set(MATRIX_B, matrix_b);
-
-    initJob(matrix_a, map, jobConf);
-  }
-
-  @Override
-  public void map(IntWritable key, VectorWritable value,
-      OutputCollector<IntWritable, VectorWritable> output, Reporter reporter)
-      throws IOException {
-
-    Vector v1 = matrix_b.getRow(key.get());
-    output.collect(key, new VectorWritable(key.get(), (DenseVector) v1
-        .add(value.getDenseVector())));
-
-  }
-
-}
+/**
+ * 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.io.IntWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.DenseVector;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.Vector;
+import org.apache.hama.io.VectorWritable;
+import org.apache.hama.mapred.DenseMap;
+import org.apache.log4j.Logger;
+
+public class Add1DLayoutMap extends DenseMap<IntWritable, VectorWritable> {
+  static final Logger LOG = Logger.getLogger(Add1DLayoutMap.class);
+  protected Matrix matrix_b;
+  public static final String MATRIX_B = "hama.addition.matrix.b";
+
+  public void configure(JobConf job) {
+    try {
+      matrix_b = new DenseMatrix(new HamaConfiguration(), job.get(MATRIX_B, ""));
+    } catch (IOException e) {
+      LOG.warn("Load matrix_b failed : " + e.getMessage());
+    }
+  }
+
+  public static void initJob(String matrix_a, String matrix_b,
+      Class<Add1DLayoutMap> map, Class<IntWritable> outputKeyClass,
+      Class<VectorWritable> outputValueClass, JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+    jobConf.set(MATRIX_B, matrix_b);
+
+    initJob(matrix_a, map, jobConf);
+  }
+
+  @Override
+  public void map(IntWritable key, VectorWritable value,
+      OutputCollector<IntWritable, VectorWritable> output, Reporter reporter)
+      throws IOException {
+
+    Vector v1 = matrix_b.getRow(key.get());
+    output.collect(key, new VectorWritable(key.get(), (DenseVector) v1
+        .add(value.getDenseVector())));
+
+  }
+
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/algebra/Mult1DLayoutMap.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/algebra/Mult1DLayoutMap.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/algebra/Mult1DLayoutMap.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/algebra/Mult1DLayoutMap.java Wed Oct 29 04:59:40 2008
@@ -1,78 +1,82 @@
-/**
- * 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 org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.mapred.JobConf;
-import org.apache.hadoop.mapred.OutputCollector;
-import org.apache.hadoop.mapred.Reporter;
-import org.apache.hama.DenseMatrix;
-import org.apache.hama.DenseVector;
-import org.apache.hama.HamaConfiguration;
-import org.apache.hama.Matrix;
-import org.apache.hama.Vector;
-import org.apache.hama.io.VectorEntry;
-import org.apache.hama.io.VectorWritable;
-import org.apache.hama.mapred.DenseMap;
-import org.apache.log4j.Logger;
-
-/**
- * 1D Block Layout version
- */
-public class Mult1DLayoutMap extends DenseMap<IntWritable, VectorWritable> {
-  static final Logger LOG = Logger.getLogger(Mult1DLayoutMap.class);
-  protected Matrix matrix_b;
-  public static final String MATRIX_B = "hama.multiplication.matrix.b";
-
-  public void configure(JobConf job) {
-    matrix_b = new DenseMatrix(new HamaConfiguration(), job.get(MATRIX_B, ""));
-  }
-
-  public static void initJob(String matrix_a, String matrix_b,
-      Class<Mult1DLayoutMap> map, Class<IntWritable> outputKeyClass,
-      Class<VectorWritable> outputValueClass, JobConf jobConf) {
-
-    jobConf.setMapOutputValueClass(outputValueClass);
-    jobConf.setMapOutputKeyClass(outputKeyClass);
-    jobConf.setMapperClass(map);
-    jobConf.set(MATRIX_B, matrix_b);
-
-    initJob(matrix_a, map, jobConf);
-  }
-
-  @Override
-  public void map(IntWritable key, VectorWritable value,
-      OutputCollector<IntWritable, VectorWritable> output, Reporter reporter)
-      throws IOException {
-
-    Iterator<VectorEntry> it = value.getDenseVector().iterator();
-    int i = 0;
-    while (it.hasNext()) {
-      Vector v = matrix_b.getRow(i);
-
-      output.collect(key, new VectorWritable(key.get(), (DenseVector) v
-          .scale(it.next().getValue())));
-      i++;
-    }
-  }
-}
+/**
+ * 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 org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.mapred.JobConf;
+import org.apache.hadoop.mapred.OutputCollector;
+import org.apache.hadoop.mapred.Reporter;
+import org.apache.hama.DenseMatrix;
+import org.apache.hama.DenseVector;
+import org.apache.hama.HamaConfiguration;
+import org.apache.hama.Matrix;
+import org.apache.hama.Vector;
+import org.apache.hama.io.VectorEntry;
+import org.apache.hama.io.VectorWritable;
+import org.apache.hama.mapred.DenseMap;
+import org.apache.log4j.Logger;
+
+/**
+ * 1D Block Layout version
+ */
+public class Mult1DLayoutMap extends DenseMap<IntWritable, VectorWritable> {
+  static final Logger LOG = Logger.getLogger(Mult1DLayoutMap.class);
+  protected Matrix matrix_b;
+  public static final String MATRIX_B = "hama.multiplication.matrix.b";
+
+  public void configure(JobConf job) {
+    try {
+      matrix_b = new DenseMatrix(new HamaConfiguration(), job.get(MATRIX_B, ""));
+    } catch (IOException e) {
+      LOG.warn("Load matrix_b failed : " + e.getMessage());
+    }
+  }
+
+  public static void initJob(String matrix_a, String matrix_b,
+      Class<Mult1DLayoutMap> map, Class<IntWritable> outputKeyClass,
+      Class<VectorWritable> outputValueClass, JobConf jobConf) {
+
+    jobConf.setMapOutputValueClass(outputValueClass);
+    jobConf.setMapOutputKeyClass(outputKeyClass);
+    jobConf.setMapperClass(map);
+    jobConf.set(MATRIX_B, matrix_b);
+
+    initJob(matrix_a, map, jobConf);
+  }
+
+  @Override
+  public void map(IntWritable key, VectorWritable value,
+      OutputCollector<IntWritable, VectorWritable> output, Reporter reporter)
+      throws IOException {
+
+    Iterator<VectorEntry> it = value.getDenseVector().iterator();
+    int i = 0;
+    while (it.hasNext()) {
+      Vector v = matrix_b.getRow(i);
+
+      output.collect(key, new VectorWritable(key.get(), (DenseVector) v
+          .scale(it.next().getValue())));
+      i++;
+    }
+  }
+}

Modified: incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java?rev=708884&r1=708883&r2=708884&view=diff
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java (original)
+++ incubator/hama/trunk/src/java/org/apache/hama/util/RandomVariable.java Wed Oct 29 04:59:40 2008
@@ -1,247 +1,255 @@
-/**
- * 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 org.apache.hama.Constants;
-
-/**
- * The RandomVaraibale Class provides static methods for generating random
- * numbers.
- */
-public class RandomVariable {
-
-  /**
-   * Generate a random number between 0 and 1.
-   * 
-   * @return a double between 0 and 1.
-   */
-  public static double rand() {
-    double x = Math.random();
-    return x;
-  }
-
-  /**
-   * Generate a random integer.
-   * 
-   * @param i0 min of the random variable.
-   * @param i1 max of the random variable.
-   * @return an int between i0 and i1.
-   */
-  public static int randInt(int i0, int i1) {
-    double x = rand();
-    int i = i0 + (int) Math.floor((i1 - i0 + 1) * x);
-    return i;
-  }
-
-  /**
-   * Generate a random matrix name.
-   * 
-   * @return random matrix name
-   */
-  public static String randMatrixName() {
-    return randString(Constants.RANDOM, 5);
-  }
-  
-  /**
-   * Generate a random aliase name.
-   * 
-   * @return random aliase name
-   */
-  public static String randAliaseName() {
-    return randString(Constants.RANDOMALIASE, 5);
-  }
-  
-  /**
-    * Generate a random string using the specified prefix and a fixed length. 
-    * @param prefix
-    *        the specified string prefix.
-    * @param length
-    *        the length of the string to be appended.
-    * @return random string.
-    */
-  public static String randString(String prefix, int length) {
-    StringBuilder result = new StringBuilder(prefix);
-    for (int i = 0; i < length; i++) {
-      char ch = (char) ((Math.random() * 26) + 97);
-      result.append(ch);
-    }
-      
-    return result.toString();
-  }
-
-  /**
-   * Generate a random number from a uniform random variable.
-   * 
-   * @param min min of the random variable.
-   * @param max max of the random variable.
-   * @return a double.
-   */
-  public static double uniform(double min, double max) {
-    double x = min + (max - min) * rand();
-    return x;
-  }
-
-  /**
-   * Generate a random number from a discrete random variable.
-   * 
-   * @param values discrete values.
-   * @param prob probability of each value.
-   * @return a double.
-   */
-  public static double dirac(double[] values, double[] prob) {
-    double[] prob_cumul = new double[values.length];
-    prob_cumul[0] = prob[0];
-    for (int i = 1; i < values.length; i++) {
-      prob_cumul[i] = prob_cumul[i - 1] + prob[i];
-    }
-    double y = rand();
-    double x = 0;
-    for (int i = 0; i < values.length - 1; i++) {
-      if ((y > prob_cumul[i]) && (y < prob_cumul[i + 1])) {
-        x = values[i];
-      }
-    }
-    return x;
-  }
-
-  /**
-   * Generate a random number from a Gaussian (Normal) random variable.
-   * 
-   * @param mu mean of the random variable.
-   * @param sigma standard deviation of the random variable.
-   * @return a double.
-   */
-  public static double normal(double mu, double sigma) {
-    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
-        * Math.sqrt(-2 * Math.log(rand()));
-    return x;
-  }
-
-  /**
-   * Generate a random number from a Chi-2 random variable.
-   * 
-   * @param n degrees of freedom of the chi2 random variable.
-   * @return a double.
-   */
-  public static double chi2(int n) {
-    double x = 0;
-    for (int i = 0; i < n; i++) {
-      double norm = normal(0, 1);
-      x += norm * norm;
-    }
-    return x;
-  }
-
-  /**
-   * Generate a random number from a LogNormal random variable.
-   * 
-   * @param mu mean of the Normal random variable.
-   * @param sigma standard deviation of the Normal random variable.
-   * @return a double.
-   */
-  public static double logNormal(double mu, double sigma) {
-    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
-        * Math.sqrt(-2 * Math.log(rand()));
-    return x;
-  }
-
-  /**
-   * Generate a random number from an exponantial random variable (Mean =
-   * 1/lambda, variance = 1/lambda^2).
-   * 
-   * @param lambda parmaeter of the exponential random variable.
-   * @return a double.
-   */
-  public static double exponential(double lambda) {
-    double x = -1 / lambda * Math.log(rand());
-    return x;
-  }
-
-  /**
-   * Generate a random number from a symetric triangular random variable.
-   * 
-   * @param min min of the random variable.
-   * @param max max of the random variable.
-   * @return a double.
-   */
-  public static double triangular(double min, double max) {
-    double x = min / 2 + (max - min) * rand() / 2 + min / 2 + (max - min)
-        * rand() / 2;
-    return x;
-  }
-
-  /**
-   * Generate a random number from a non-symetric triangular random variable.
-   * 
-   * @param min min of the random variable.
-   * @param med value of the random variable with max density.
-   * @param max max of the random variable.
-   * @return a double.
-   */
-  public static double triangular(double min, double med, double max) {
-    double y = rand();
-    double x = (y < ((med - min) / (max - min))) ? (min + Math.sqrt(y
-        * (max - min) * (med - min))) : (max - Math.sqrt((1 - y) * (max - min)
-        * (max - med)));
-    return x;
-  }
-
-  /**
-   * Generate a random number from a beta random variable.
-   * 
-   * @param a first parameter of the Beta random variable.
-   * @param b second parameter of the Beta random variable.
-   * @return a double.
-   */
-  public static double beta(double a, double b) {
-    double try_x;
-    double try_y;
-    do {
-      try_x = Math.pow(rand(), 1 / a);
-      try_y = Math.pow(rand(), 1 / b);
-    } while ((try_x + try_y) > 1);
-    return try_x / (try_x + try_y);
-  }
-
-  /**
-   * Generate a random number from a Cauchy random variable (Mean = Inf, and
-   * Variance = Inf).
-   * 
-   * @param mu median of the Weibull random variable
-   * @param sigma second parameter of the Cauchy random variable.
-   * @return a double.
-   */
-  public static double cauchy(double mu, double sigma) {
-    double x = sigma * Math.tan(Math.PI * (rand() - 0.5)) + mu;
-    return x;
-  }
-
-  /**
-   * Generate a random number from a Weibull random variable.
-   * 
-   * @param lambda first parameter of the Weibull random variable.
-   * @param c second parameter of the Weibull random variable.
-   * @return a double.
-   */
-  public static double weibull(double lambda, double c) {
-    double x = Math.pow(-Math.log(1 - rand()), 1 / c) / lambda;
-    return x;
-  }
-}
+/**
+ * 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 org.apache.hama.Constants;
+
+/**
+ * The RandomVaraibale Class provides static methods for generating random
+ * numbers.
+ */
+public class RandomVariable {
+
+  /**
+   * Generate a random number between 0 and 1.
+   * 
+   * @return a double between 0 and 1.
+   */
+  public static double rand() {
+    double x = Math.random();
+    return x;
+  }
+
+  /**
+   * Generate a random integer.
+   * 
+   * @param i0 min of the random variable.
+   * @param i1 max of the random variable.
+   * @return an int between i0 and i1.
+   */
+  public static int randInt(int i0, int i1) {
+    double x = rand();
+    int i = i0 + (int) Math.floor((i1 - i0 + 1) * x);
+    return i;
+  }
+
+  /**
+   * Generate a random matrix Path(tablename) with a default length.
+   * @return random matrix name
+   */
+  public static String randMatrixPath() {
+    return randString(Constants.RANDOM, 5);
+  }
+  
+  /**
+   * Generate a random matrix Path(tablename) with a given length.
+   * @param length the matrix path's length
+   * @return random matrix name
+   */
+  public static String randMatrixPath(int length) {
+    return randString(Constants.RANDOM, length);
+  }
+  
+  /**
+   * Generate a random aliase name.
+   * 
+   * @return random aliase name
+   */
+  public static String randAliaseName() {
+    return randString(Constants.RANDOMALIASE, 5);
+  }
+  
+  /**
+    * Generate a random string using the specified prefix and a fixed length. 
+    * @param prefix
+    *        the specified string prefix.
+    * @param length
+    *        the length of the string to be appended.
+    * @return random string.
+    */
+  public static String randString(String prefix, int length) {
+    StringBuilder result = new StringBuilder(prefix);
+    for (int i = 0; i < length; i++) {
+      char ch = (char) ((Math.random() * 26) + 97);
+      result.append(ch);
+    }
+      
+    return result.toString();
+  }
+
+  /**
+   * Generate a random number from a uniform random variable.
+   * 
+   * @param min min of the random variable.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double uniform(double min, double max) {
+    double x = min + (max - min) * rand();
+    return x;
+  }
+
+  /**
+   * Generate a random number from a discrete random variable.
+   * 
+   * @param values discrete values.
+   * @param prob probability of each value.
+   * @return a double.
+   */
+  public static double dirac(double[] values, double[] prob) {
+    double[] prob_cumul = new double[values.length];
+    prob_cumul[0] = prob[0];
+    for (int i = 1; i < values.length; i++) {
+      prob_cumul[i] = prob_cumul[i - 1] + prob[i];
+    }
+    double y = rand();
+    double x = 0;
+    for (int i = 0; i < values.length - 1; i++) {
+      if ((y > prob_cumul[i]) && (y < prob_cumul[i + 1])) {
+        x = values[i];
+      }
+    }
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Gaussian (Normal) random variable.
+   * 
+   * @param mu mean of the random variable.
+   * @param sigma standard deviation of the random variable.
+   * @return a double.
+   */
+  public static double normal(double mu, double sigma) {
+    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+        * Math.sqrt(-2 * Math.log(rand()));
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Chi-2 random variable.
+   * 
+   * @param n degrees of freedom of the chi2 random variable.
+   * @return a double.
+   */
+  public static double chi2(int n) {
+    double x = 0;
+    for (int i = 0; i < n; i++) {
+      double norm = normal(0, 1);
+      x += norm * norm;
+    }
+    return x;
+  }
+
+  /**
+   * Generate a random number from a LogNormal random variable.
+   * 
+   * @param mu mean of the Normal random variable.
+   * @param sigma standard deviation of the Normal random variable.
+   * @return a double.
+   */
+  public static double logNormal(double mu, double sigma) {
+    double x = mu + sigma * Math.cos(2 * Math.PI * rand())
+        * Math.sqrt(-2 * Math.log(rand()));
+    return x;
+  }
+
+  /**
+   * Generate a random number from an exponantial random variable (Mean =
+   * 1/lambda, variance = 1/lambda^2).
+   * 
+   * @param lambda parmaeter of the exponential random variable.
+   * @return a double.
+   */
+  public static double exponential(double lambda) {
+    double x = -1 / lambda * Math.log(rand());
+    return x;
+  }
+
+  /**
+   * Generate a random number from a symetric triangular random variable.
+   * 
+   * @param min min of the random variable.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double triangular(double min, double max) {
+    double x = min / 2 + (max - min) * rand() / 2 + min / 2 + (max - min)
+        * rand() / 2;
+    return x;
+  }
+
+  /**
+   * Generate a random number from a non-symetric triangular random variable.
+   * 
+   * @param min min of the random variable.
+   * @param med value of the random variable with max density.
+   * @param max max of the random variable.
+   * @return a double.
+   */
+  public static double triangular(double min, double med, double max) {
+    double y = rand();
+    double x = (y < ((med - min) / (max - min))) ? (min + Math.sqrt(y
+        * (max - min) * (med - min))) : (max - Math.sqrt((1 - y) * (max - min)
+        * (max - med)));
+    return x;
+  }
+
+  /**
+   * Generate a random number from a beta random variable.
+   * 
+   * @param a first parameter of the Beta random variable.
+   * @param b second parameter of the Beta random variable.
+   * @return a double.
+   */
+  public static double beta(double a, double b) {
+    double try_x;
+    double try_y;
+    do {
+      try_x = Math.pow(rand(), 1 / a);
+      try_y = Math.pow(rand(), 1 / b);
+    } while ((try_x + try_y) > 1);
+    return try_x / (try_x + try_y);
+  }
+
+  /**
+   * Generate a random number from a Cauchy random variable (Mean = Inf, and
+   * Variance = Inf).
+   * 
+   * @param mu median of the Weibull random variable
+   * @param sigma second parameter of the Cauchy random variable.
+   * @return a double.
+   */
+  public static double cauchy(double mu, double sigma) {
+    double x = sigma * Math.tan(Math.PI * (rand() - 0.5)) + mu;
+    return x;
+  }
+
+  /**
+   * Generate a random number from a Weibull random variable.
+   * 
+   * @param lambda first parameter of the Weibull random variable.
+   * @param c second parameter of the Weibull random variable.
+   * @return a double.
+   */
+  public static double weibull(double lambda, double c) {
+    double x = Math.pow(-Math.log(1 - rand()), 1 / c) / lambda;
+    return x;
+  }
+}



Mime
View raw message