From 5362b2045d9cfd96bbbe04b0cd6749a4e98cabbd Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Thu, 19 Nov 2015 11:49:31 +0200 Subject: [PATCH 1/3] APIClient: Support Empty histogram An empty histogram can return a valid response from the API but without any buckets. This is a valid scenario and common for counters of features that are not supported yet. In those cases, the APIClient should return a zero length array. Signed-off-by: Amnon Heiman --- src/main/java/com/cloudius/urchin/api/APIClient.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/cloudius/urchin/api/APIClient.java b/src/main/java/com/cloudius/urchin/api/APIClient.java index c80a658..90fdd53 100644 --- a/src/main/java/com/cloudius/urchin/api/APIClient.java +++ b/src/main/java/com/cloudius/urchin/api/APIClient.java @@ -677,6 +677,9 @@ public class APIClient { MultivaluedMap queryParams) { JsonObject obj = getJsonObj(string, queryParams); JsonArray arr = obj.getJsonArray("buckets"); + if (arr == null) { + return new long[0]; + } long res[] = new long[arr.size()]; for (int i = 0; i< arr.size(); i++) { res[i] = arr.getJsonNumber(i).longValue(); From e0dea0c27ebd21ba2d0dd321182304c5543410ab Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Thu, 19 Nov 2015 11:52:17 +0200 Subject: [PATCH 2/3] EstimatedHistogram: Support empty histogram When creating an estimated histogram from buckets it is a valid option to get a zero size array as the buckets array. In that case the newOffsets method would get a negative value for its size, which should result in a zero length array of offsets. Signed-off-by: Amnon Heiman --- .../java/com/cloudius/urchin/utils/EstimatedHistogram.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/cloudius/urchin/utils/EstimatedHistogram.java b/src/main/java/com/cloudius/urchin/utils/EstimatedHistogram.java index 29bf21b..644e67b 100644 --- a/src/main/java/com/cloudius/urchin/utils/EstimatedHistogram.java +++ b/src/main/java/com/cloudius/urchin/utils/EstimatedHistogram.java @@ -74,6 +74,9 @@ public class EstimatedHistogram { } private static long[] newOffsets(int size) { + if (size <= 0) { + return new long[0]; + } long[] result = new long[size]; long last = 1; result[0] = last; From 695e23bd4e70388c175b26db04b45613a822d8e2 Mon Sep 17 00:00:00 2001 From: Amnon Heiman Date: Thu, 19 Nov 2015 11:55:34 +0200 Subject: [PATCH 3/3] RecentEstimatedHistogram: Support empty histogram The RecentEstimatedHistogram updates its value from the API with an array of recent values. This array can be empty, in that case the getBuckets method should just return a zero size array. Signed-off-by: Amnon Heiman --- .../com/cloudius/urchin/utils/RecentEstimatedHistogram.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/cloudius/urchin/utils/RecentEstimatedHistogram.java b/src/main/java/com/cloudius/urchin/utils/RecentEstimatedHistogram.java index 692f41e..4f43690 100644 --- a/src/main/java/com/cloudius/urchin/utils/RecentEstimatedHistogram.java +++ b/src/main/java/com/cloudius/urchin/utils/RecentEstimatedHistogram.java @@ -50,6 +50,9 @@ public class RecentEstimatedHistogram extends EstimatedHistogram { * @return a long[] containing the current histogram difference buckets */ public long[] getBuckets(long[] bucketData) { + if (bucketData.length == 0) { + return new long[0]; + } final int len = buckets.length(); long[] rv = new long[len];