hadoop-mapreduce-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sc...@apache.org
Subject svn commit: r1076411 - in /hadoop/mapreduce/trunk: ./ src/contrib/raid/src/java/org/apache/hadoop/raid/ src/contrib/raid/src/test/org/apache/hadoop/raid/
Date Wed, 02 Mar 2011 21:48:30 GMT
Author: schen
Date: Wed Mar  2 21:48:30 2011
New Revision: 1076411

URL: http://svn.apache.org/viewvc?rev=1076411&view=rev
Log:
MAPREDUCE-2302. Add static factory methods in GaloisField. (schen)

Modified:
    hadoop/mapreduce/trunk/CHANGES.txt
    hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/GaloisField.java
    hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/ReedSolomonCode.java
    hadoop/mapreduce/trunk/src/contrib/raid/src/test/org/apache/hadoop/raid/TestGaloisField.java

Modified: hadoop/mapreduce/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/mapreduce/trunk/CHANGES.txt?rev=1076411&r1=1076410&r2=1076411&view=diff
==============================================================================
--- hadoop/mapreduce/trunk/CHANGES.txt (original)
+++ hadoop/mapreduce/trunk/CHANGES.txt Wed Mar  2 21:48:30 2011
@@ -49,6 +49,8 @@ Trunk (unreleased changes)
     MAPREDUCE-993. bin/hadoop job -events <jobid> <from-event-#> <#-of-events>
     help message is confusing. (Harsh J Chouraria via tomwhite)
 
+    MAPREDUCE-2302. Add static factory methods in GaloisField. (schen)
+
   OPTIMIZATIONS
     
     MAPREDUCE-2026. Make JobTracker.getJobCounters() and

Modified: hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/GaloisField.java
URL: http://svn.apache.org/viewvc/hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/GaloisField.java?rev=1076411&r1=1076410&r2=1076411&view=diff
==============================================================================
--- hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/GaloisField.java
(original)
+++ hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/GaloisField.java
Wed Mar  2 21:48:30 2011
@@ -18,6 +18,9 @@
 
 package org.apache.hadoop.raid;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * Implementation of Galois field arithmetics with 2^p elements.
  * The input must be unsigned integers.
@@ -34,15 +37,42 @@ public class GaloisField {
 
   // Field size 256 is good for byte based system
   private static final int DEFAULT_FIELD_SIZE = 256;
-  // primitive polynomial 1 + X + X^2 + X^4 + X^8
+  // primitive polynomial 1 + X^2 + X^3 + X^4 + X^8
   private static final int DEFAULT_PRIMITIVE_POLYNOMIAL = 285;
 
+  static private final Map<Integer, GaloisField> instances =
+    new HashMap<Integer, GaloisField>();
+
   /**
-   * An object can perform Galois field arithmetics
+   * Get the object performs Galois field arithmetics
    * @param fieldSize size of the field
    * @param primitivePolynomial a primitive polynomial corresponds to the size
    */
-  public GaloisField(int fieldSize, int primitivePolynomial) {
+  public static GaloisField getInstance(int fieldSize,
+      int primitivePolynomial) {
+    int key = ((fieldSize << 16) & 0xFFFF0000) + (primitivePolynomial & 0x0000FFFF);
+    GaloisField gf;
+    synchronized (instances) {
+      gf = instances.get(key);
+      if (gf == null) {
+        gf = new GaloisField(fieldSize, primitivePolynomial);
+        instances.put(key, gf);
+      }
+    }
+    return gf;
+  }
+
+  /**
+   * Get the object performs Galois field arithmetics with default setting
+   */
+  public static GaloisField getInstance() {
+    return getInstance(DEFAULT_FIELD_SIZE, DEFAULT_PRIMITIVE_POLYNOMIAL);
+  }
+
+  private GaloisField(int fieldSize, int primitivePolynomial) {
+    assert fieldSize > 0;
+    assert primitivePolynomial > 0;
+
     this.fieldSize = fieldSize;
     this.primitivePeriod = fieldSize - 1;
     this.primitivePolynomial = primitivePolynomial;
@@ -88,13 +118,6 @@ public class GaloisField {
   }
 
   /**
-   * An object can perform Galois field arithmetics
-   */
-  public GaloisField() {
-    this(DEFAULT_FIELD_SIZE, DEFAULT_PRIMITIVE_POLYNOMIAL);
-  }
-
-  /**
    * Return number of elements in the field
    * @return number of elements in the field
    */

Modified: hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/ReedSolomonCode.java
URL: http://svn.apache.org/viewvc/hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/ReedSolomonCode.java?rev=1076411&r1=1076410&r2=1076411&view=diff
==============================================================================
--- hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/ReedSolomonCode.java
(original)
+++ hadoop/mapreduce/trunk/src/contrib/raid/src/java/org/apache/hadoop/raid/ReedSolomonCode.java
Wed Mar  2 21:48:30 2011
@@ -25,7 +25,7 @@ public class ReedSolomonCode implements 
   private final int[] generatingPolynomial;
   private final int PRIMITIVE_ROOT = 2;
   private final int[] primitivePower;
-  private final GaloisField GF = new GaloisField();
+  private final GaloisField GF = GaloisField.getInstance();
   private int[] errSignature;
   private final int[] paritySymbolLocations;
   private final int[] dataBuff;

Modified: hadoop/mapreduce/trunk/src/contrib/raid/src/test/org/apache/hadoop/raid/TestGaloisField.java
URL: http://svn.apache.org/viewvc/hadoop/mapreduce/trunk/src/contrib/raid/src/test/org/apache/hadoop/raid/TestGaloisField.java?rev=1076411&r1=1076410&r2=1076411&view=diff
==============================================================================
--- hadoop/mapreduce/trunk/src/contrib/raid/src/test/org/apache/hadoop/raid/TestGaloisField.java
(original)
+++ hadoop/mapreduce/trunk/src/contrib/raid/src/test/org/apache/hadoop/raid/TestGaloisField.java
Wed Mar  2 21:48:30 2011
@@ -27,7 +27,7 @@ public class TestGaloisField extends Tes
 
   final int TEST_TIMES = 10000;
   final Random RAND = new Random();
-  final static GaloisField GF = new GaloisField();
+  final static GaloisField GF = GaloisField.getInstance();
 
   private int randGF() {
     return 0x000000FF & RAND.nextInt(GF.getFieldSize());
@@ -40,6 +40,18 @@ public class TestGaloisField extends Tes
     return result;
   }
 
+  public void testGetInstance() {
+    GaloisField gf1 = GaloisField.getInstance(256, 285);
+    GaloisField gf2 = GaloisField.getInstance();
+    GaloisField gf3 = GaloisField.getInstance(128, 137);
+    GaloisField gf4 = GaloisField.getInstance(128, 137);
+    GaloisField gf5 = GaloisField.getInstance(512, 529);
+    GaloisField gf6 = GaloisField.getInstance(512, 529);
+    assertTrue(gf1 == gf2);
+    assertTrue(gf3 == gf4);
+    assertTrue(gf5 == gf6);
+  }
+
   public void testDistributivity() {
     for (int i = 0; i < TEST_TIMES; i++) {
       int a = RAND.nextInt(GF.getFieldSize());



Mime
View raw message