szetszwo commented on code in PR #10919:
URL: https://github.com/apache/ozone/pull/10919#discussion_r3699709691


##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/CompositeKey.java:
##########
@@ -18,53 +18,94 @@
 package org.apache.hadoop.hdds.utils;
 
 import java.util.Arrays;
+import java.util.Objects;
+import org.apache.ratis.util.Preconditions;
 
 /**
  * This is a utility to combine multiple objects as a key that can be used in
  * hash map access. The advantage of this is that it is cheap in comparison
  * to other methods like string concatenation.
- *
- * For example, if a composition of volume, bucket and key is needed to
- * access a hash map, the natural method is:
- * <pre> {@code
- * String key = "/" + volume + "/" + bucket + "/" + key.
- * map.put(key, value);
- * }</pre>
- * This is costly because it creates (and stores) a new buffer.
- *
- * In comparison, the following achieve the same logic without creating any new
- * buffer.
- * <pre> {@code
- * Object key = combineKeys(volume, bucket, key).
- * map.put(key, value);
- * }</pre>
- *
  */
-public final class CompositeKey {
-  private final int hashCode;
-  private final Object[] components;
-
-  CompositeKey(Object[] components) {
-    this.components = components;
-    this.hashCode = Arrays.hashCode(components);
+public abstract class CompositeKey {
+  /** The same as {@link Arrays#hashCode(Object[])} for one loop step. */
+  static int hash(int result, Object next) {
+    return 31 * result + next.hashCode();
   }
 
-  @Override
-  public int hashCode() {
-    return hashCode;
+  private static final class TwoComponents extends CompositeKey {
+    private final int hashCode;
+    private final Object first;
+    private final Object second;
+
+    private TwoComponents(Object first, Object second) {
+      this.hashCode = hash(hash(1, first), second);

Review Comment:
   @spacemonkd , It would be easier to understand if you could quote also line 
42 and 43.
   
   Or, if you showed below instead.
   ```diff
   +++ 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/CompositeKey.java
   @@ -38,9 +38,9 @@ private static final class TwoComponents extends 
CompositeKey {
        private final Object second;
   
        private TwoComponents(Object first, Object second) {
   -      this.hashCode = hash(hash(1, first), second);
          this.first = Objects.requireNonNull(first, "first == null");
          this.second = Objects.requireNonNull(second, "second == null");
   +      this.hashCode = hash(hash(1, first), second);
        }
   ```



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to