brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [2/3] brooklyn-server git commit: Avoid overflow by hardcoding character sets and choosing 4 characters at a time
Date Mon, 30 May 2016 14:12:43 GMT
Avoid overflow by hardcoding character sets and choosing 4 characters at a time


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/115edffe
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/115edffe
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/115edffe

Branch: refs/heads/master
Commit: 115edffe04949fccb77d2cb4270623c133b7941a
Parents: 47eb5f3
Author: Andrew Donald Kennedy <andrew.kennedy@cloudsoftcorp.com>
Authored: Wed May 25 18:10:12 2016 +0100
Committer: Andrew Donald Kennedy <andrew.kennedy@cloudsoftcorp.com>
Committed: Fri May 27 19:52:24 2016 +0100

----------------------------------------------------------------------
 .../core/objs/AbstractBrooklynObject.java       |  2 +-
 .../apache/brooklyn/util/text/Identifiers.java  | 21 +++++++++++++++-----
 2 files changed, 17 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/115edffe/core/src/main/java/org/apache/brooklyn/core/objs/AbstractBrooklynObject.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/objs/AbstractBrooklynObject.java
b/core/src/main/java/org/apache/brooklyn/core/objs/AbstractBrooklynObject.java
index 947f7c9..7b77628 100644
--- a/core/src/main/java/org/apache/brooklyn/core/objs/AbstractBrooklynObject.java
+++ b/core/src/main/java/org/apache/brooklyn/core/objs/AbstractBrooklynObject.java
@@ -49,7 +49,7 @@ public abstract class AbstractBrooklynObject implements BrooklynObjectInternal
{
     private boolean hasWarnedOfNoManagementContextWhenPersistRequested;
 
     @SetFromFlag("id")
-    private String id = Identifiers.makeRandomId(10, Identifiers.LOWER_CASE_ALPHA, Identifiers.LOWER_CASE_ALPHA
+ Identifiers.NUMERIC);
+    private String id = Identifiers.makeRandomLowercaseId(10);
 
     private String catalogItemId;
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/115edffe/utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java b/utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java
index edc2bd3..d025f5c 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/text/Identifiers.java
@@ -91,21 +91,29 @@ public class Identifiers {
      * <b>NOTE</b> This version is 30-50% faster than the old double-based one,
      * which computed a random every 3 turns -- takes about 600 ns to do id
      * of len 10, compared to 10000 ns for old version [on 1.6ghz machine]
+     * <p>
+     * <b>TODO</b> The integer value passed to {@link Randonm#nextInt(int)}
+     * will overflow if the length of the character sets passed in is more
+     * than 128. It is possible to mitigate this by truncating the strings,
+     * or calculating the maximum number of characters per invocation of
+     * {@code nextInt()} by taking the logarithm of {@link Integer#MAX_INT}
+     * using the length of the character set as the base. Currently this
+     * method is private to prevent overly long arguments.
      */
-    public static String makeRandomId(int l, String validStartChars, String validNonStartChars)
{
+    private static String makeRandomId(int l, String validStartChars, String validNonStartChars)
{
         if (l <= 0) return "";
         char[] id = new char[l];
         int s = validStartChars.length();
         int n = validNonStartChars.length();
-        int d = random.nextInt(s * n * n * n * n);
-        int i = 0;    
+        int d = random.nextInt(s * n * n * n);
+        int i = 0;
         id[i] = validStartChars.charAt(d % s);
         d /= s;
         if (++i < l) do {
             id[i] = validNonStartChars.charAt(d % n);
             if (++i >= l) break;
-            if (i % 5 == 0) {
-                d = random.nextInt(n * n * n * n * n);
+            if (i % 4 == 0) {
+                d = random.nextInt(n * n * n *n);
             } else {
                 d /= n;
             }
@@ -115,6 +123,9 @@ public class Identifiers {
     public static String makeRandomId(int l) {
         return makeRandomId(l, ID_VALID_START_CHARS, ID_VALID_NONSTART_CHARS);
     }
+    public static String makeRandomLowercaseId(int l) {
+        return makeRandomId(l, LOWER_CASE_ALPHA, LOWER_CASE_ALPHA + NUMERIC);
+    }
 
     /**
      *


Mime
View raw message