HBASE-13894 Avoid visitor alloc each call of ByteBufferArray get/putMultiple()

This commit is contained in:
Matteo Bertozzi 2015-06-15 09:19:50 -07:00
parent 714668a40d
commit dd086bb22f
1 changed files with 19 additions and 14 deletions

View File

@ -96,13 +96,16 @@ public final class ByteBufferArray {
* @return number of bytes read * @return number of bytes read
*/ */
public int getMultiple(long start, int len, byte[] dstArray, int dstOffset) { public int getMultiple(long start, int len, byte[] dstArray, int dstOffset) {
multiple(start, len, dstArray, dstOffset, new Visitor() { multiple(start, len, dstArray, dstOffset, GET_MULTIPLE_VISTOR);
return len;
}
private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {
@Override
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) { public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
bb.get(array, arrayIdx, len); bb.get(array, arrayIdx, len);
} }
}); };
return len;
}
/** /**
* Transfers bytes from the given source array into this buffer array * Transfers bytes from the given source array into this buffer array
@ -123,12 +126,15 @@ public final class ByteBufferArray {
* read * read
*/ */
public void putMultiple(long start, int len, byte[] srcArray, int srcOffset) { public void putMultiple(long start, int len, byte[] srcArray, int srcOffset) {
multiple(start, len, srcArray, srcOffset, new Visitor() { multiple(start, len, srcArray, srcOffset, PUT_MULTIPLE_VISITOR);
}
private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {
@Override
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) { public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
bb.put(array, arrayIdx, len); bb.put(array, arrayIdx, len);
} }
}); };
}
private interface Visitor { private interface Visitor {
/** /**
@ -178,8 +184,7 @@ public final class ByteBufferArray {
if (i == startBuffer) { if (i == startBuffer) {
cnt = bufferSize - startOffset; cnt = bufferSize - startOffset;
if (cnt > len) cnt = len; if (cnt > len) cnt = len;
bb.limit(startOffset + cnt).position( bb.limit(startOffset + cnt).position(startOffset);
startOffset );
} else if (i == endBuffer) { } else if (i == endBuffer) {
cnt = endOffset; cnt = endOffset;
bb.limit(cnt).position(0); bb.limit(cnt).position(0);