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.util.AttributeSource;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.PagedBytes;
// Simplest storage: stores fixed length byte[] per
@ -45,11 +46,10 @@ class FixedStraightBytesImpl {
private int lastDocID = -1;
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);
}
// TODO - impl bulk copy here!
@Override
public void add(int docID, BytesRef bytes) throws IOException {
@ -66,13 +66,6 @@ class FixedStraightBytesImpl {
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
protected void merge(MergeState state) throws IOException {
if (state.bits == null && state.reader instanceof Reader) {
@ -96,8 +89,9 @@ class FixedStraightBytesImpl {
}
lastDocID += maxDocs - 1;
} else
} else {
super.merge(state);
}
}
// Fills up to but not including this docID
@ -139,7 +133,8 @@ class FixedStraightBytesImpl {
@Override
public Source load() throws IOException {
return new Source(cloneData(), size, maxDoc);
return size == 1 ? new SingleByteSource(cloneData(), maxDoc) :
new StraightBytesSource(cloneData(), size, maxDoc);
}
@Override
@ -147,11 +142,58 @@ class FixedStraightBytesImpl {
datIn.close();
}
private static class Source extends BytesBaseSource {
// specialized version for single bytes
private static class SingleByteSource extends Source {
private final int maxDoc;
private final byte[] data;
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 maxDoc;
public Source(IndexInput datIn, int size, int maxDoc)
public StraightBytesSource(IndexInput datIn, int size, int maxDoc)
throws IOException {
super(datIn, null, new PagedBytes(PAGED_BYTES_BITS), size * maxDoc);
this.size = size;
@ -165,7 +207,7 @@ class FixedStraightBytesImpl {
@Override
public int getValueCount() {
throw new UnsupportedOperationException();
return maxDoc;
}
@Override

View File

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