lucene 4: optimize read/write BytesRef handling

This commit is contained in:
Shay Banon 2012-11-06 16:26:18 +01:00
parent c8cf72d657
commit 2b58c2dfff
4 changed files with 31 additions and 4 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.common.io.stream;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
@ -69,6 +70,16 @@ public class BytesStreamInput extends StreamInput {
return bytes;
}
@Override
public BytesRef readBytesRef(int length) throws IOException {
if (unsafe) {
return super.readBytesRef(length);
}
BytesRef bytes = new BytesRef(buf, pos, length);
pos += length;
return bytes;
}
@Override
public long skip(long n) throws IOException {
if (pos + n > count) {

View File

@ -87,10 +87,16 @@ public abstract class StreamInput extends InputStream {
public BytesRef readBytesRef() throws IOException {
int length = readVInt();
int offset = readVInt();
return readBytesRef(length);
}
public BytesRef readBytesRef(int length) throws IOException {
if (length == 0) {
return new BytesRef();
}
byte[] bytes = new byte[length];
readBytes(bytes, offset, length);
return new BytesRef(bytes, offset, length);
readBytes(bytes, 0, length);
return new BytesRef(bytes, 0, length);
}
public void readFully(byte[] b) throws IOException {

View File

@ -113,7 +113,6 @@ public abstract class StreamOutput extends OutputStream {
return;
}
writeVInt(bytes.length);
writeVInt(bytes.offset);
write(bytes.bytes, bytes.offset, bytes.length);
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.transport.netty;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.bytes.ChannelBufferBytesReference;
import org.elasticsearch.common.io.stream.StreamInput;
@ -57,6 +58,16 @@ public class ChannelBufferStreamInput extends StreamInput {
return ref;
}
@Override
public BytesRef readBytesRef(int length) throws IOException {
if (!buffer.hasArray()) {
return super.readBytesRef(length);
}
BytesRef bytesRef = new BytesRef(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length);
buffer.skipBytes(length);
return bytesRef;
}
@Override
public int available() throws IOException {
return endIndex - buffer.readerIndex();