LUCENE-3870: allow prefixes across block boundaries in PagedBytes.Reader

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1301423 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2012-03-16 10:39:58 +00:00
parent e24e7856f0
commit f779f83a95
2 changed files with 45 additions and 6 deletions

View File

@ -193,17 +193,27 @@ public final class PagedBytes {
* @lucene.internal * @lucene.internal
**/ **/
public BytesRef fillSliceWithPrefix(BytesRef b, long start) { public BytesRef fillSliceWithPrefix(BytesRef b, long start) {
final int index = (int) (start >> blockBits); int index = (int) (start >> blockBits);
int offset = (int) (start & blockMask); int offset = (int) (start & blockMask);
final byte[] block = blocks[index]; byte[] block = blocks[index];
final int length; final int length;
assert offset <= block.length-1;
if ((block[offset] & 128) == 0) { if ((block[offset] & 128) == 0) {
length = block[offset]; length = block[offset];
offset = offset+1; offset = offset+1;
} else { } else {
length = ((block[offset] & 0x7f) << 8) | (block[1+offset] & 0xff); if (offset==block.length-1) {
offset = offset+2; final byte[] nextBlock = blocks[++index];
assert length > 0; length = ((block[offset] & 0x7f) << 8) | (nextBlock[0] & 0xff);
offset = 1;
block = nextBlock;
assert length > 0;
} else {
assert offset < block.length-1;
length = ((block[offset] & 0x7f) << 8) | (block[1+offset] & 0xff);
offset = offset+2;
assert length > 0;
}
} }
assert length >= 0: "length=" + length; assert length >= 0: "length=" + length;
b.length = length; b.length = length;

View File

@ -470,7 +470,6 @@ public class TestDocValuesIndexing extends LuceneTestCase {
} }
break; break;
case VAR_INTS: case VAR_INTS:
System.out.println(source.hasArray());
assertFalse(source.hasArray()); assertFalse(source.hasArray());
break; break;
case FLOAT_32: case FLOAT_32:
@ -931,4 +930,34 @@ public class TestDocValuesIndexing extends LuceneTestCase {
r.close(); r.close();
dir.close(); dir.close();
} }
// LUCENE-3870
public void testLengthPrefixAcrossTwoPages() throws Exception {
Directory d = newDirectory();
IndexWriter w = new IndexWriter(d, new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
Document doc = new Document();
byte[] bytes = new byte[32764];
BytesRef b = new BytesRef();
b.bytes = bytes;
b.length = bytes.length;
doc.add(new DocValuesField("field", b, DocValues.Type.BYTES_VAR_DEREF));
w.addDocument(doc);
bytes[0] = 1;
w.addDocument(doc);
DirectoryReader r = w.getReader();
Source s = r.getSequentialSubReaders()[0].docValues("field").getSource();
BytesRef bytes1 = s.getBytes(0, new BytesRef());
assertEquals(bytes.length, bytes1.length);
bytes[0] = 0;
assertEquals(b, bytes1);
bytes1 = s.getBytes(1, new BytesRef());
assertEquals(bytes.length, bytes1.length);
bytes[0] = 1;
assertEquals(b, bytes1);
r.close();
w.close();
d.close();
}
} }