accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject svn commit: r1376577 - in /accumulo/trunk/core/src: main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java
Date Thu, 23 Aug 2012 16:25:18 GMT
Author: kturner
Date: Thu Aug 23 16:25:18 2012
New Revision: 1376577

URL: http://svn.apache.org/viewvc?rev=1376577&view=rev
Log:
ACCUMULO-740 Fixed infinite loop in RFile

Added:
    accumulo/trunk/core/src/test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java
Modified:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java?rev=1376577&r1=1376576&r2=1376577&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java
(original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/file/rfile/RelativeKey.java
Thu Aug 23 16:25:18 2012
@@ -361,15 +361,37 @@ public class RelativeKey implements Writ
     read(in, mbseq, len);
   }
   
+  /**
+   * Determines what next array size should be by rounding up to next power of two.
+   * 
+   */
+  static int nextArraySize(int i) {
+    if (i < 0)
+      throw new IllegalArgumentException();
+    
+    if (i > (1 << 30))
+      return Integer.MAX_VALUE; // this is the next power of 2 minus one... a special case
+
+    if (i == 0) {
+      return 1;
+    }
+    
+    // round up to next power of two
+    int ret = i;
+    ret--;
+    ret |= ret >> 1;
+    ret |= ret >> 2;
+    ret |= ret >> 4;
+    ret |= ret >> 8;
+    ret |= ret >> 16;
+    ret++;
+    
+    return ret;
+  }
+
   private void read(DataInput in, MByteSequence mbseq, int len) throws IOException {
     if (mbseq.getBackingArray().length < len) {
-      int newLen = mbseq.getBackingArray().length;
-      
-      while (newLen < len) {
-        newLen = newLen * 2;
-      }
-      
-      mbseq.setArray(new byte[newLen]);
+      mbseq.setArray(new byte[nextArraySize(len)]);
     }
     
     in.readFully(mbseq.getBackingArray(), 0, len);

Added: accumulo/trunk/core/src/test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java?rev=1376577&view=auto
==============================================================================
--- accumulo/trunk/core/src/test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java
(added)
+++ accumulo/trunk/core/src/test/java/org/apache/accumulo/core/file/rfile/RelativeKeyTest.java
Thu Aug 23 16:25:18 2012
@@ -0,0 +1,47 @@
+/**
+ * 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.accumulo.core.file.rfile;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ */
+public class RelativeKeyTest extends TestCase {
+  public void test1() {
+    assertEquals(1, RelativeKey.nextArraySize(0));
+    assertEquals(1, RelativeKey.nextArraySize(1));
+    assertEquals(2, RelativeKey.nextArraySize(2));
+    assertEquals(4, RelativeKey.nextArraySize(3));
+    assertEquals(4, RelativeKey.nextArraySize(4));
+    assertEquals(8, RelativeKey.nextArraySize(5));
+    assertEquals(8, RelativeKey.nextArraySize(8));
+    assertEquals(16, RelativeKey.nextArraySize(9));
+    
+    assertEquals(1 << 16, RelativeKey.nextArraySize((1 << 16) - 1));
+    assertEquals(1 << 16, RelativeKey.nextArraySize(1 << 16));
+    assertEquals(1 << 17, RelativeKey.nextArraySize((1 << 16) + 1));
+    
+    assertEquals(1 << 30, RelativeKey.nextArraySize((1 << 30) - 1));
+
+    assertEquals(1 << 30, RelativeKey.nextArraySize(1 << 30));
+
+    assertEquals(Integer.MAX_VALUE, RelativeKey.nextArraySize(Integer.MAX_VALUE - 1));
+    assertEquals(Integer.MAX_VALUE, RelativeKey.nextArraySize(Integer.MAX_VALUE));
+  }
+  
+}



Mime
View raw message