LUCENE-2816: MMapDirectory speedups

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1050737 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2010-12-19 00:15:28 +00:00
parent aae5cff6dd
commit b5726a4267
1 changed files with 74 additions and 20 deletions

View File

@ -242,7 +242,34 @@ public class MMapDirectory extends FSDirectory {
throw new IOException("read past EOF");
}
}
@Override
public short readShort() throws IOException {
try {
return buffer.getShort();
} catch (BufferUnderflowException e) {
throw new IOException("read past EOF");
}
}
@Override
public int readInt() throws IOException {
try {
return buffer.getInt();
} catch (BufferUnderflowException e) {
throw new IOException("read past EOF");
}
}
@Override
public long readLong() throws IOException {
try {
return buffer.getLong();
} catch (BufferUnderflowException e) {
throw new IOException("read past EOF");
}
}
@Override
public long getFilePointer() {
return buffer.position();
@ -294,7 +321,6 @@ public class MMapDirectory extends FSDirectory {
private final int maxBufSize;
private ByteBuffer curBuf; // redundant for speed: buffers[curBufIndex]
private int curAvail; // redundant for speed: (bufSizes[curBufIndex] - curBuf.position())
private boolean isClone = false;
@ -333,37 +359,66 @@ public class MMapDirectory extends FSDirectory {
@Override
public byte readByte() throws IOException {
// Performance might be improved by reading ahead into an array of
// e.g. 128 bytes and readByte() from there.
if (curAvail == 0) {
try {
return curBuf.get();
} catch (BufferUnderflowException e) {
curBufIndex++;
if (curBufIndex >= buffers.length)
throw new IOException("read past EOF");
curBuf = buffers[curBufIndex];
curBuf.position(0);
curAvail = bufSizes[curBufIndex];
return curBuf.get();
}
curAvail--;
return curBuf.get();
}
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
while (len > curAvail) {
curBuf.get(b, offset, curAvail);
len -= curAvail;
offset += curAvail;
curBufIndex++;
if (curBufIndex >= buffers.length)
throw new IOException("read past EOF");
curBuf = buffers[curBufIndex];
curBuf.position(0);
curAvail = bufSizes[curBufIndex];
try {
curBuf.get(b, offset, len);
} catch (BufferUnderflowException e) {
int curAvail = curBuf.remaining();
while (len > curAvail) {
curBuf.get(b, offset, curAvail);
len -= curAvail;
offset += curAvail;
curBufIndex++;
if (curBufIndex >= buffers.length)
throw new IOException("read past EOF");
curBuf = buffers[curBufIndex];
curBuf.position(0);
curAvail = curBuf.remaining();
}
curBuf.get(b, offset, len);
}
curBuf.get(b, offset, len);
curAvail -= len;
}
@Override
public short readShort() throws IOException {
try {
return curBuf.getShort();
} catch (BufferUnderflowException e) {
return super.readShort();
}
}
@Override
public int readInt() throws IOException {
try {
return curBuf.getInt();
} catch (BufferUnderflowException e) {
return super.readInt();
}
}
@Override
public long readLong() throws IOException {
try {
return curBuf.getLong();
} catch (BufferUnderflowException e) {
return super.readLong();
}
}
@Override
public long getFilePointer() {
return ((long) curBufIndex * maxBufSize) + curBuf.position();
@ -375,7 +430,6 @@ public class MMapDirectory extends FSDirectory {
curBuf = buffers[curBufIndex];
int bufOffset = (int) (pos - ((long) curBufIndex * maxBufSize));
curBuf.position(bufOffset);
curAvail = bufSizes[curBufIndex] - bufOffset;
}
@Override