You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2012/03/14 23:12:38 UTC

svn commit: r1300756 - in /lucene/dev/branches/branch_3x: ./ lucene/ lucene/core/src/java/org/apache/lucene/util/PagedBytes.java lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java

Author: mikemccand
Date: Wed Mar 14 22:12:38 2012
New Revision: 1300756

URL: http://svn.apache.org/viewvc?rev=1300756&view=rev
Log:
LUCENE-3841: restore fillSliceWithPrefix's ability to span 2 blocks

Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/   (props changed)
    lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
    lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java

Modified: lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java?rev=1300756&r1=1300755&r2=1300756&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java (original)
+++ lucene/dev/branches/branch_3x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java Wed Mar 14 22:12:38 2012
@@ -207,12 +207,22 @@ public final class PagedBytes {
       }
       assert length >= 0: "length=" + length;
       b.length = length;
-      // We always alloc a new block when writing w/ prefix;
-      // we could some day relax that and span two blocks:
-      assert blockSize - offset >= length;
-      // Within block
-      b.offset = offset;
-      b.bytes = blocks[index];
+
+      // NOTE: even though copyUsingLengthPrefix always
+      // allocs a new block if the byte[] to be added won't
+      // fit in current block, callers can write their own
+      // prefix that may span two blocks:
+      if (blockSize - offset >= length) {
+        // Within block
+        b.offset = offset;
+        b.bytes = blocks[index];
+      } else {
+        // Split
+        b.bytes = new byte[length];
+        b.offset = 0;
+        System.arraycopy(blocks[index], offset, b.bytes, 0, blockSize-offset);
+        System.arraycopy(blocks[1+index], 0, b.bytes, blockSize-offset, length-(blockSize-offset));
+      }
       return b;
     }
 

Modified: lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java?rev=1300756&r1=1300755&r2=1300756&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java (original)
+++ lucene/dev/branches/branch_3x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java Wed Mar 14 22:12:38 2012
@@ -107,4 +107,27 @@ public class TestPagedBytes extends Luce
       }
     }
   }
+
+  // LUCENE-3841: even though
+  // copyUsingLengthPrefix will never span two blocks, make
+  // sure if caller writes their own prefix followed by the
+  // bytes, it still works:
+  public void testLengthPrefixAcrossTwoBlocks() throws Exception {
+    final PagedBytes p = new PagedBytes(10);
+    final DataOutput out = p.getDataOutput();
+    final byte[] bytes1 = new byte[1000];
+    random.nextBytes(bytes1);
+    out.writeBytes(bytes1, 0, bytes1.length);
+    out.writeByte((byte) 40);
+    final byte[] bytes2 = new byte[40];
+    random.nextBytes(bytes2);
+    out.writeBytes(bytes2, 0, bytes2.length);
+
+    final PagedBytes.Reader reader = p.freeze(random.nextBoolean());
+    BytesRef answer = reader.fillSliceWithPrefix(new BytesRef(), 1000);
+    assertEquals(40, answer.length);
+    for(int i=0;i<40;i++) {
+      assertEquals(bytes2[i], answer.bytes[answer.offset + i]);
+    }
+  }
 }