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 2011/06/21 19:28:14 UTC
svn commit: r1138104 - in /lucene/dev/trunk/lucene/src:
java/org/apache/lucene/index/codecs/
java/org/apache/lucene/index/codecs/memory/
java/org/apache/lucene/index/codecs/pulsing/
java/org/apache/lucene/index/codecs/standard/ java/org/apache/lucene/s...
Author: mikemccand
Date: Tue Jun 21 17:28:13 2011
New Revision: 1138104
URL: http://svn.apache.org/viewvc?rev=1138104&view=rev
Log:
LUCENE-3224: fix silly bugs; add assertions in ByteArrayDataInput
Added:
lucene/dev/trunk/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java (with props)
Modified:
lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java
lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java
lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java
lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java
lucene/dev/trunk/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java
Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java?rev=1138104&r1=1138103&r2=1138104&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/BlockTermsReader.java Tue Jun 21 17:28:13 2011
@@ -312,13 +312,13 @@ public class BlockTermsReader extends Fi
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 {
Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java?rev=1138104&r1=1138103&r2=1138104&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/memory/MemoryCodec.java Tue Jun 21 17:28:13 2011
@@ -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;
Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java?rev=1138104&r1=1138103&r2=1138104&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/pulsing/PulsingPostingsReaderImpl.java Tue Jun 21 17:28:13 2011
@@ -110,7 +110,7 @@ public class PulsingPostingsReaderImpl e
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 e
}
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 e
}
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;
Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java?rev=1138104&r1=1138103&r2=1138104&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/index/codecs/standard/StandardPostingsReader.java Tue Jun 21 17:28:13 2011
@@ -156,7 +156,7 @@ public class StandardPostingsReader exte
//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)];
}
Modified: lucene/dev/trunk/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java?rev=1138104&r1=1138103&r2=1138104&view=diff
==============================================================================
--- lucene/dev/trunk/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java (original)
+++ lucene/dev/trunk/lucene/src/java/org/apache/lucene/store/ByteArrayDataInput.java Tue Jun 21 17:28:13 2011
@@ -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 ex
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 ex
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 ex
@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 ex
@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 ex
@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 ex
// 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 ex
System.arraycopy(bytes, pos, b, offset, len);
pos += len;
}
+
+ private boolean checkBounds() {
+ assert pos < limit;
+ return true;
+ }
}
Added: lucene/dev/trunk/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java?rev=1138104&view=auto
==============================================================================
--- lucene/dev/trunk/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java (added)
+++ lucene/dev/trunk/lucene/src/test/org/apache/lucene/store/TestByteArrayDataInput.java Tue Jun 21 17:28:13 2011
@@ -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());
+ }
+}