manojpec commented on a change in pull request #3884:
URL: https://github.com/apache/hudi/pull/3884#discussion_r739498221



##########
File path: 
hudi-common/src/main/java/org/apache/hudi/common/util/hash/HashID.java
##########
@@ -0,0 +1,132 @@
+/*
+ * 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.hudi.common.util.hash;
+
+import net.jpountz.xxhash.XXHash32;
+import net.jpountz.xxhash.XXHash64;
+import net.jpountz.xxhash.XXHashFactory;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hudi.exception.HoodieIOException;
+
+import java.io.Serializable;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * A stateless Hash class which generates ID for the desired bit count.
+ */
+public class HashID implements Serializable {
+
+  private static final String MD5_ALGORITHM_NAME = "MD5";
+  private static final int HASH_SEED = 0xdabadaba;
+
+  /**
+   * Represents HashID size in bits.
+   */
+  public enum Size {
+    BITS_32(32),
+    BITS_64(64),
+    BITS_128(128);
+
+    private final int bits;
+
+    Size(int bitCount) {
+      this.bits = bitCount;
+    }
+
+    /**
+     * Get this Hash size in bytes.
+     *
+     * @return Bytes needed to represent this size
+     */
+    public int byteSize() {
+      return (((this.bits - 1) / Byte.SIZE) + 1);
+    }
+
+    /**
+     * Get this Hash size in bits.
+     *
+     * @return bits needed to represent the size
+     */
+    public int bits() {
+      return this.bits;
+    }
+
+    @Override
+    public String toString() {
+      return "HashSize{" + bits + "}";
+    }
+  }
+
+  /**
+   * Get the hash value for a string message and for the desired @{@link Size}.
+   *
+   * @param message - String message to get the hash value for
+   * @param bits    - @{@link Size} of the hash value
+   * @return Hash value for the message as byte array
+   */
+  public static byte[] hash(final String message, final Size bits) {
+    return hash(message.getBytes(StandardCharsets.UTF_8), bits);
+  }
+
+  /**
+   * Get the hash value for a byte array and for the desired @{@link Size}.
+   *
+   * @param messageBytes - Byte array message to get the hash value for
+   * @param bits         - @{@link Size} of the hash value
+   * @return Hash value for the message as byte array
+   */
+  public static byte[] hash(final byte[] messageBytes, final Size bits) {
+    switch (bits) {
+      case BITS_32:
+      case BITS_64:
+        return getXXHash(messageBytes, bits);
+      case BITS_128:
+        return getMD5Hash(messageBytes);
+      default:
+        throw new IllegalArgumentException("Unexpected Hash size bits: " + 
bits);
+    }
+  }
+
+  private static byte[] getXXHash(final byte[] message, final Size bits) {
+    XXHashFactory factory = XXHashFactory.fastestInstance();

Review comment:
       I would like HashID to be a stateless class only expose static methods. 
Theres actually no state need to be maintained as this is pretty light weight 
instantiation. The seed is the only thing which makes the hash generation 
deterministic across time and platforms. I can do static instantiation of 
factory if this is really needed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to