You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@storm.apache.org by ka...@apache.org on 2016/01/25 00:29:12 UTC

[04/50] storm git commit: avoid abs get negative value

avoid abs get negative value


Project: http://git-wip-us.apache.org/repos/asf/storm/repo
Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/181b3b09
Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/181b3b09
Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/181b3b09

Branch: refs/heads/1.x-branch
Commit: 181b3b09a973acf99719ad60fe03f4d23c869324
Parents: 90e4f3e
Author: Xin Wang <be...@163.com>
Authored: Sat Jan 16 19:40:24 2016 +0800
Committer: vesense <be...@163.com>
Committed: Sat Jan 16 19:50:30 2016 +0800

----------------------------------------------------------------------
 .../storm/trident/partition/IndexHashGrouping.java     |  8 +++++---
 storm-core/src/jvm/org/apache/storm/utils/Utils.java   | 13 +++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/storm/blob/181b3b09/storm-core/src/jvm/org/apache/storm/trident/partition/IndexHashGrouping.java
----------------------------------------------------------------------
diff --git a/storm-core/src/jvm/org/apache/storm/trident/partition/IndexHashGrouping.java b/storm-core/src/jvm/org/apache/storm/trident/partition/IndexHashGrouping.java
index 223006f..25c3a1a 100644
--- a/storm-core/src/jvm/org/apache/storm/trident/partition/IndexHashGrouping.java
+++ b/storm-core/src/jvm/org/apache/storm/trident/partition/IndexHashGrouping.java
@@ -20,15 +20,17 @@ package org.apache.storm.trident.partition;
 import org.apache.storm.generated.GlobalStreamId;
 import org.apache.storm.grouping.CustomStreamGrouping;
 import org.apache.storm.task.WorkerTopologyContext;
+import org.apache.storm.utils.Utils;
+
 import java.util.Arrays;
 import java.util.List;
 
 public class IndexHashGrouping implements CustomStreamGrouping {
     public static int objectToIndex(Object val, int numPartitions) {
-        if(val==null) return 0;
-        else {
-            return Math.abs(val.hashCode()) % numPartitions;
+        if(val == null) {
+            return 0;
         }
+        return Utils.toPositive(val.hashCode()) % numPartitions;
     }
     
     int _index;

http://git-wip-us.apache.org/repos/asf/storm/blob/181b3b09/storm-core/src/jvm/org/apache/storm/utils/Utils.java
----------------------------------------------------------------------
diff --git a/storm-core/src/jvm/org/apache/storm/utils/Utils.java b/storm-core/src/jvm/org/apache/storm/utils/Utils.java
index 3605b7a..9d0c7c6 100644
--- a/storm-core/src/jvm/org/apache/storm/utils/Utils.java
+++ b/storm-core/src/jvm/org/apache/storm/utils/Utils.java
@@ -1369,5 +1369,18 @@ public class Utils {
         }
         return topologyInfo;
     }
+
+    /**
+     * A cheap way to deterministically convert a number to a positive value. When the input is
+     * positive, the original value is returned. When the input number is negative, the returned
+     * positive value is the original value bit AND against Integer.MAX_VALUE(0x7fffffff) which
+     * is not its absolutely value.
+     *
+     * @param number a given number
+     * @return a positive number.
+     */
+    public static int toPositive(int number) {
+        return number & Integer.MAX_VALUE;
+    }
 }