LUCENE-3196: Optimize FixedStraightBytes for bytes size == 1

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1135293 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-06-13 21:34:21 +00:00
parent a58aa65924
commit fd891d7ef8
2 changed files with 61 additions and 19 deletions

View File

@ -26,6 +26,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.AttributeSource; import org.apache.lucene.util.AttributeSource;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.PagedBytes; import org.apache.lucene.util.PagedBytes;
// Simplest storage: stores fixed length byte[] per // Simplest storage: stores fixed length byte[] per
@ -45,11 +46,10 @@ class FixedStraightBytesImpl {
private int lastDocID = -1; private int lastDocID = -1;
private byte[] oneRecord; private byte[] oneRecord;
protected Writer(Directory dir, String id) throws IOException { public Writer(Directory dir, String id) throws IOException {
super(dir, id, CODEC_NAME, VERSION_CURRENT, false, null, null); super(dir, id, CODEC_NAME, VERSION_CURRENT, false, null, null);
} }
// TODO - impl bulk copy here!
@Override @Override
public void add(int docID, BytesRef bytes) throws IOException { public void add(int docID, BytesRef bytes) throws IOException {
@ -66,13 +66,6 @@ class FixedStraightBytesImpl {
datOut.writeBytes(bytes.bytes, bytes.offset, bytes.length); datOut.writeBytes(bytes.bytes, bytes.offset, bytes.length);
} }
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.index.values.Writer#merge(org.apache.lucene.index.values
* .Writer.MergeState)
*/
@Override @Override
protected void merge(MergeState state) throws IOException { protected void merge(MergeState state) throws IOException {
if (state.bits == null && state.reader instanceof Reader) { if (state.bits == null && state.reader instanceof Reader) {
@ -96,8 +89,9 @@ class FixedStraightBytesImpl {
} }
lastDocID += maxDocs - 1; lastDocID += maxDocs - 1;
} else } else {
super.merge(state); super.merge(state);
}
} }
// Fills up to but not including this docID // Fills up to but not including this docID
@ -126,7 +120,7 @@ class FixedStraightBytesImpl {
return oneRecord == null ? 0 : oneRecord.length; return oneRecord == null ? 0 : oneRecord.length;
} }
} }
public static class Reader extends BytesReaderBase { public static class Reader extends BytesReaderBase {
private final int size; private final int size;
private final int maxDoc; private final int maxDoc;
@ -139,19 +133,67 @@ class FixedStraightBytesImpl {
@Override @Override
public Source load() throws IOException { public Source load() throws IOException {
return new Source(cloneData(), size, maxDoc); return size == 1 ? new SingleByteSource(cloneData(), maxDoc) :
new StraightBytesSource(cloneData(), size, maxDoc);
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
datIn.close(); datIn.close();
} }
// specialized version for single bytes
private static class SingleByteSource extends Source {
private final int maxDoc;
private final byte[] data;
private static class Source extends BytesBaseSource { public SingleByteSource(IndexInput datIn, int maxDoc) throws IOException {
this.maxDoc = maxDoc;
try {
data = new byte[maxDoc];
datIn.readBytes(data, 0, data.length, false);
} finally {
IOUtils.closeSafely(false, datIn);
}
}
@Override
public BytesRef getBytes(int docID, BytesRef bytesRef) {
bytesRef.length = 1;
bytesRef.bytes = data;
bytesRef.offset = docID;
return bytesRef;
}
@Override
public ValueType type() {
return ValueType.BYTES_FIXED_STRAIGHT;
}
@Override
public ValuesEnum getEnum(AttributeSource attrSource) throws IOException {
return new SourceEnum(attrSource, type(), this, maxDoc) {
@Override
public int advance(int target) throws IOException {
if (target >= numDocs) {
return pos = NO_MORE_DOCS;
}
bytesRef.length = 1;
bytesRef.bytes = data;
bytesRef.offset = target;
return pos = target;
}
};
}
}
private static class StraightBytesSource extends BytesBaseSource {
private final int size; private final int size;
private final int maxDoc; private final int maxDoc;
public Source(IndexInput datIn, int size, int maxDoc) public StraightBytesSource(IndexInput datIn, int size, int maxDoc)
throws IOException { throws IOException {
super(datIn, null, new PagedBytes(PAGED_BYTES_BITS), size * maxDoc); super(datIn, null, new PagedBytes(PAGED_BYTES_BITS), size * maxDoc);
this.size = size; this.size = size;
@ -162,10 +204,10 @@ class FixedStraightBytesImpl {
public BytesRef getBytes(int docID, BytesRef bytesRef) { public BytesRef getBytes(int docID, BytesRef bytesRef) {
return data.fillSlice(bytesRef, docID * size, size); return data.fillSlice(bytesRef, docID * size, size);
} }
@Override @Override
public int getValueCount() { public int getValueCount() {
throw new UnsupportedOperationException(); return maxDoc;
} }
@Override @Override

View File

@ -342,7 +342,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {
for (ValueType byteIndexValue : byteVariantList) { for (ValueType byteIndexValue : byteVariantList) {
List<Closeable> closeables = new ArrayList<Closeable>(); List<Closeable> closeables = new ArrayList<Closeable>();
int bytesSize = 7 + random.nextInt(128); int bytesSize = 1 + random.nextInt(128);
OpenBitSet deleted = indexValues(w, numValues, byteIndexValue, OpenBitSet deleted = indexValues(w, numValues, byteIndexValue,
byteVariantList, withDeletions, bytesSize); byteVariantList, withDeletions, bytesSize);
final IndexReader r = IndexReader.open(w, withDeletions); final IndexReader r = IndexReader.open(w, withDeletions);
@ -493,7 +493,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {
Index.NO }; Index.NO };
private OpenBitSet indexValues(IndexWriter w, int numValues, ValueType value, private OpenBitSet indexValues(IndexWriter w, int numValues, ValueType value,
List<ValueType> valueVarList, boolean withDeletions, int multOfSeven) List<ValueType> valueVarList, boolean withDeletions, int bytesSize)
throws CorruptIndexException, IOException { throws CorruptIndexException, IOException {
final boolean isNumeric = NUMERICS.contains(value); final boolean isNumeric = NUMERICS.contains(value);
OpenBitSet deleted = new OpenBitSet(numValues); OpenBitSet deleted = new OpenBitSet(numValues);
@ -507,7 +507,7 @@ public class TestDocValuesIndexing extends LuceneTestCase {
final BytesRef bytesRef = new BytesRef(); final BytesRef bytesRef = new BytesRef();
final String idBase = value.name() + "_"; final String idBase = value.name() + "_";
final byte[] b = new byte[multOfSeven]; final byte[] b = new byte[bytesSize];
if (bytesRef != null) { if (bytesRef != null) {
bytesRef.bytes = b; bytesRef.bytes = b;
bytesRef.length = b.length; bytesRef.length = b.length;