incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From edwardy...@apache.org
Subject svn commit: r706241 - in /incubator/hama/trunk: CHANGES.txt src/java/org/apache/hama/io/BlockID.java src/test/org/apache/hama/io/ src/test/org/apache/hama/io/TestBlockID.java
Date Mon, 20 Oct 2008 11:37:20 GMT
Author: edwardyoon
Date: Mon Oct 20 04:37:20 2008
New Revision: 706241

URL: http://svn.apache.org/viewvc?rev=706241&view=rev
Log:
Add a writable comparable for BlockID

Added:
    incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java
    incubator/hama/trunk/src/test/org/apache/hama/io/
    incubator/hama/trunk/src/test/org/apache/hama/io/TestBlockID.java
Modified:
    incubator/hama/trunk/CHANGES.txt

Modified: incubator/hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/CHANGES.txt?rev=706241&r1=706240&r2=706241&view=diff
==============================================================================
--- incubator/hama/trunk/CHANGES.txt (original)
+++ incubator/hama/trunk/CHANGES.txt Mon Oct 20 04:37:20 2008
@@ -4,6 +4,7 @@
 
   NEW FEATURES
     
+    HAMA-83: Add a writable comparable for BlockID (edwardyoon) 
     HAMA-81: Add subVector(int i0, int i1) to Vector interface (edwardyoon)
     Hama-80: Add identity(int m, int n) which returns identity matrix (edwardyoon) 
     HAMA-62: Hama Shell Implementation (samuel via edwardyoon)

Added: incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java?rev=706241&view=auto
==============================================================================
--- incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java (added)
+++ incubator/hama/trunk/src/java/org/apache/hama/io/BlockID.java Mon Oct 20 04:37:20 2008
@@ -0,0 +1,104 @@
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.io;
+
+import java.io.DataInput;
+import java.io.DataOutput;
+import java.io.IOException;
+
+import org.apache.hadoop.io.WritableComparable;
+import org.apache.hadoop.io.WritableComparator;
+
+/** A WritableComparable for BlockIDs. */
+public class BlockID implements WritableComparable {
+  private int row;
+  private int column;
+
+  public BlockID() {
+  }
+
+  public BlockID(int row, int column) {
+    set(row, column);
+  }
+
+  public void set(int row, int column) {
+    this.row = row;
+    this.column = column;
+  }
+
+  public int getRow() {
+    return row;
+  }
+
+  public int getColumn() {
+    return column;
+  }
+
+  public void readFields(DataInput in) throws IOException {
+    row = in.readInt();
+    column = in.readInt();
+  }
+
+  public void write(DataOutput out) throws IOException {
+    out.writeInt(row);
+    out.write(column);
+  }
+
+  public String toString() {
+    return row + ", " + column;
+  }
+
+  public int compareTo(Object o) {
+    int thisRow = this.row;
+    int thatRow = ((BlockID) o).row;
+    int thisColumn = this.column;
+    int thatColumn = ((BlockID) o).column;
+
+    if (thisRow != thatRow) {
+      return (thisRow < thatRow ? -1 : 1);
+    } else {
+      return (thisColumn < thatColumn ? -1 : (thisColumn == thatColumn ? 0 : 1));
+    }
+  }
+
+  public static class Comparator extends WritableComparator {
+    protected Comparator() {
+      super(BlockID.class);
+    }
+
+    public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
+      int thisRow = readInt(b1, s1);
+      int thatRow = readInt(b2, s2);
+      int thisColumn = readInt(b1, l1);
+      int thatColumn = readInt(b2, l2);
+
+      if (thisRow != thatRow) {
+        return (thisRow < thatRow ? -1 : 1);
+      } else {
+        return (thisColumn < thatColumn ? -1 : (thisColumn == thatColumn ? 0
+            : 1));
+      }
+    }
+  }
+
+  static { // register this comparator
+    WritableComparator.define(BlockID.class, new Comparator());
+  }
+}

Added: incubator/hama/trunk/src/test/org/apache/hama/io/TestBlockID.java
URL: http://svn.apache.org/viewvc/incubator/hama/trunk/src/test/org/apache/hama/io/TestBlockID.java?rev=706241&view=auto
==============================================================================
--- incubator/hama/trunk/src/test/org/apache/hama/io/TestBlockID.java (added)
+++ incubator/hama/trunk/src/test/org/apache/hama/io/TestBlockID.java Mon Oct 20 04:37:20
2008
@@ -0,0 +1,45 @@
+/**
+ * Copyright 2007 The Apache Software Foundation
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hama.io;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TestBlockID extends TestCase {
+  final static Log LOG = LogFactory.getLog(TestBlockID.class.getName());
+
+  /**
+   * BlockID object compare
+   */
+  public void testCompare() {
+    BlockID a = new BlockID(1, 3);
+    BlockID b = new BlockID(1, 1);
+    assertEquals(a.compareTo(b), 1);
+
+    BlockID c = new BlockID(3, 1);
+    BlockID d = new BlockID(1, 1);
+    assertEquals(a.compareTo(c), -1);
+
+    assertEquals(b.compareTo(d), 0);
+  }
+
+}



Mime
View raw message