LUCENE-3224: fix silly bugs; add assertions in ByteArrayDataInput

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1138104 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2011-06-21 17:28:13 +00:00
parent a2f08bfb9b
commit 6b41b73894
6 changed files with 64 additions and 11 deletions

View File

@ -312,13 +312,13 @@ public class BlockTermsReader extends FieldsProducer {
private int blocksSinceSeek;
private byte[] termSuffixes;
private ByteArrayDataInput termSuffixesReader = new ByteArrayDataInput(null);
private ByteArrayDataInput termSuffixesReader = new ByteArrayDataInput();
/* Common prefix used for all terms in this block. */
private int termBlockPrefix;
private byte[] docFreqBytes;
private final ByteArrayDataInput freqReader = new ByteArrayDataInput(null);
private final ByteArrayDataInput freqReader = new ByteArrayDataInput();
private int metaDataUpto;
public SegmentTermsEnum() throws IOException {

View File

@ -533,7 +533,7 @@ public class MemoryCodec extends Codec {
private final static class FSTTermsEnum extends TermsEnum {
private final FieldInfo field;
private final BytesRefFSTEnum<BytesRef> fstEnum;
private final ByteArrayDataInput buffer = new ByteArrayDataInput(null);
private final ByteArrayDataInput buffer = new ByteArrayDataInput();
private boolean didDecode;
private int docFreq;

View File

@ -110,7 +110,7 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {
final PulsingTermState termState = (PulsingTermState) _termState;
if (termState.inlinedBytes == null) {
termState.inlinedBytes = new byte[128];
termState.inlinedBytesReader = new ByteArrayDataInput(null);
termState.inlinedBytesReader = new ByteArrayDataInput();
}
int len = termsIn.readVInt();
if (termState.inlinedBytes.length < len) {
@ -222,7 +222,7 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {
}
private static class PulsingDocsEnum extends DocsEnum {
private final ByteArrayDataInput postings = new ByteArrayDataInput(null);
private final ByteArrayDataInput postings = new ByteArrayDataInput();
private final boolean omitTF;
private final boolean storePayloads;
private Bits skipDocs;
@ -320,7 +320,7 @@ public class PulsingPostingsReaderImpl extends PostingsReaderBase {
}
private static class PulsingDocsAndPositionsEnum extends DocsAndPositionsEnum {
private final ByteArrayDataInput postings = new ByteArrayDataInput(null);
private final ByteArrayDataInput postings = new ByteArrayDataInput();
private final boolean storePayloads;
private Bits skipDocs;

View File

@ -156,7 +156,7 @@ public class StandardPostingsReader extends PostingsReaderBase {
//System.out.println("SPR.readTermsBlock termsIn.fp=" + termsIn.getFilePointer());
if (termState.bytes == null) {
termState.bytes = new byte[ArrayUtil.oversize(len, 1)];
termState.bytesReader = new ByteArrayDataInput(null);
termState.bytesReader = new ByteArrayDataInput();
} else if (termState.bytes.length < len) {
termState.bytes = new byte[ArrayUtil.oversize(len, 1)];
}

View File

@ -17,6 +17,8 @@ package org.apache.lucene.store;
* limitations under the License.
*/
import org.apache.lucene.util.BytesRef;
/** @lucene.experimental */
public final class ByteArrayDataInput extends DataInput {
@ -25,9 +27,16 @@ public final class ByteArrayDataInput extends DataInput {
private int pos;
private int limit;
// TODO: allow BytesRef (slice) too
public ByteArrayDataInput(byte[] bytes) {
this.bytes = bytes;
reset(bytes);
}
public ByteArrayDataInput(byte[] bytes, int offset, int len) {
reset(bytes, offset, len);
}
public ByteArrayDataInput() {
reset(BytesRef.EMPTY_BYTES);
}
public void reset(byte[] bytes) {
@ -41,7 +50,7 @@ public final class ByteArrayDataInput extends DataInput {
public void reset(byte[] bytes, int offset, int len) {
this.bytes = bytes;
pos = offset;
limit = len;
limit = offset + len;
}
public boolean eof() {
@ -59,12 +68,14 @@ public final class ByteArrayDataInput extends DataInput {
@Override
public int readInt() {
assert pos+4 <= limit;
return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)
| ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF);
}
@Override
public long readLong() {
assert pos+8 <= limit;
final int i1 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) |
((bytes[pos++] & 0xff) << 8) | (bytes[pos++] & 0xff);
final int i2 = ((bytes[pos++] & 0xff) << 24) | ((bytes[pos++] & 0xff) << 16) |
@ -74,9 +85,11 @@ public final class ByteArrayDataInput extends DataInput {
@Override
public int readVInt() {
checkBounds();
byte b = bytes[pos++];
int i = b & 0x7F;
for (int shift = 7; (b & 0x80) != 0; shift += 7) {
checkBounds();
b = bytes[pos++];
i |= (b & 0x7F) << shift;
}
@ -85,9 +98,11 @@ public final class ByteArrayDataInput extends DataInput {
@Override
public long readVLong() {
checkBounds();
byte b = bytes[pos++];
long i = b & 0x7F;
for (int shift = 7; (b & 0x80) != 0; shift += 7) {
checkBounds();
b = bytes[pos++];
i |= (b & 0x7FL) << shift;
}
@ -97,7 +112,7 @@ public final class ByteArrayDataInput extends DataInput {
// NOTE: AIOOBE not EOF if you read too much
@Override
public byte readByte() {
assert pos < limit;
checkBounds();
return bytes[pos++];
}
@ -108,4 +123,9 @@ public final class ByteArrayDataInput extends DataInput {
System.arraycopy(bytes, pos, b, offset, len);
pos += len;
}
private boolean checkBounds() {
assert pos < limit;
return true;
}
}

View File

@ -0,0 +1,33 @@
package org.apache.lucene.store;
/**
* 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.
*/
import org.apache.lucene.util.LuceneTestCase;
public class TestByteArrayDataInput extends LuceneTestCase {
public void testBasic() throws Exception {
byte[] bytes = new byte[] {1, 65};
ByteArrayDataInput in = new ByteArrayDataInput(bytes);
assertEquals("A", in.readString());
bytes = new byte[] {1, 1, 65};
in.reset(bytes, 1, 2);
assertEquals("A", in.readString());
}
}