Skip to content

Apache Lucene open-source search software

SOLR-1104: Fix some rounding errors in LukeRequestHandler's histogram

Chris M. Hostetterc9ed885d3b6a
diff --git a/CHANGES.txt b/CHANGES.txt
index 7acae06daf8..1d6f58bc706 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -316,7 +316,8 @@ Bug Fixes
35. SOLR-1072: absolute paths used in sharedLib attribute were
incorrectly treated as relative paths. (hossman)
-
+36. SOLR-1104: Fix some rounding errors in LukeRequestHandler's histogram (hossman)
+
Other Changes
----------------------
1. Upgraded to Lucene 2.4.0 (yonik)
diff --git a/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java b/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java
index 17bdc0c2cae..ba498e6d504 100644
--- a/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java
+++ b/src/java/org/apache/solr/handler/admin/LukeRequestHandler.java
@@ -506,7 +506,7 @@ public class LukeRequestHandler extends RequestHandlerBase
///////////////////////////////////////////////////////////////////////////////////////
- private static class TermHistogram
+ static class TermHistogram
{
int maxBucket = -1;
public Map<Integer,Integer> hist = new HashMap<Integer, Integer>();
@@ -514,8 +514,7 @@ public class LukeRequestHandler extends RequestHandlerBase
private static final double LOG2 = Math.log( 2 );
public static int getPowerOfTwoBucket( int num )
{
- int exp = (int)Math.ceil( (Math.log( num ) / LOG2 ) );
- return (int) Math.pow( 2, exp );
+ return Math.max(1, Integer.highestOneBit(num-1) << 1);
}
public void add( int df )
diff --git a/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java b/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java
new file mode 100644
index 00000000000..a121932ef04
--- /dev/null
+++ b/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java
@@ -0,0 +1,52 @@
+/**
+ * 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.solr.handler.admin;
+
+import junit.framework.TestCase;
+
+import org.apache.solr.common.util.SimpleOrderedMap;
+
+/**
+ * :TODO: currently only tests some of the utilities in the LukeRequestHandler
+ */
+public class LukeRequestHandlerTest extends TestCase {
+
+ /** tests some simple edge cases */
+ public void testHistogramPowerOfTwoBucket() {
+ assertHistoBucket(1, 1);
+ assertHistoBucket(2, 2);
+ assertHistoBucket(4, 3);
+ assertHistoBucket(4, 4);
+ assertHistoBucket(8, 5);
+ assertHistoBucket(8, 6);
+ assertHistoBucket(8, 7);
+ assertHistoBucket(8, 8);
+ assertHistoBucket(16, 9);
+
+ final int MAX_VALID = ((Integer.MAX_VALUE/2)+1)/2;
+
+ assertHistoBucket(MAX_VALID, MAX_VALID-1 );
+ assertHistoBucket(MAX_VALID, MAX_VALID );
+ assertHistoBucket(MAX_VALID*2, MAX_VALID+1 );
+
+ }
+ private void assertHistoBucket(int expected, int in) {
+ assertEquals("histobucket: " + in, expected,
+ LukeRequestHandler.TermHistogram.getPowerOfTwoBucket( in ));
+ }
+}