HBASE-13894 Avoid visitor alloc each call of ByteBufferArray get/putMultiple()
This commit is contained in:
parent
68b83f25c1
commit
ceba0659f0
|
@ -44,7 +44,7 @@ public final class ByteBufferArray {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* We allocate a number of byte buffers as the capacity. In order not to out
|
* We allocate a number of byte buffers as the capacity. In order not to out
|
||||||
* of the array bounds for the last byte(see {@link ByteBufferArray#multiple}),
|
* of the array bounds for the last byte(see {@link ByteBufferArray#multiple}),
|
||||||
* we will allocate one additional buffer with capacity 0;
|
* we will allocate one additional buffer with capacity 0;
|
||||||
* @param capacity total size of the byte buffer array
|
* @param capacity total size of the byte buffer array
|
||||||
* @param directByteBuffer true if we allocate direct buffer
|
* @param directByteBuffer true if we allocate direct buffer
|
||||||
|
@ -96,14 +96,17 @@ 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);
|
||||||
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
|
||||||
bb.get(array, arrayIdx, len);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final static Visitor GET_MULTIPLE_VISTOR = new Visitor() {
|
||||||
|
@Override
|
||||||
|
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
||||||
|
bb.get(array, arrayIdx, len);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transfers bytes from the given source array into this buffer array
|
* Transfers bytes from the given source array into this buffer array
|
||||||
* @param start start offset of this buffer array
|
* @param start start offset of this buffer array
|
||||||
|
@ -123,13 +126,16 @@ 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);
|
||||||
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
|
||||||
bb.put(array, arrayIdx, len);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final static Visitor PUT_MULTIPLE_VISITOR = new Visitor() {
|
||||||
|
@Override
|
||||||
|
public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) {
|
||||||
|
bb.put(array, arrayIdx, len);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private interface Visitor {
|
private interface Visitor {
|
||||||
/**
|
/**
|
||||||
* Visit the given byte buffer, if it is a read action, we will transfer the
|
* Visit the given byte buffer, if it is a read action, we will transfer the
|
||||||
|
@ -178,13 +184,12 @@ 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);
|
||||||
} else {
|
} else {
|
||||||
cnt = bufferSize ;
|
cnt = bufferSize;
|
||||||
bb.limit(cnt).position(0);
|
bb.limit(cnt).position(0);
|
||||||
}
|
}
|
||||||
visitor.visit(bb, array, srcIndex + arrayOffset, cnt);
|
visitor.visit(bb, array, srcIndex + arrayOffset, cnt);
|
||||||
|
|
Loading…
Reference in New Issue