Skip to content

Commit

Permalink
JDBC-378 Reuse ByteBuffer in JnaBlob to avoid allocation overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Jul 6, 2016
1 parent 5f9e185 commit 7791a80
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/jna-client/org/firebirdsql/gds/ng/jna/JnaBlob.java
Expand Up @@ -50,6 +50,7 @@ public class JnaBlob extends AbstractFbBlob implements FbBlob, DatabaseListener
private final IntByReference jnaHandle = new IntByReference(0);
private final ISC_STATUS[] statusVector = new ISC_STATUS[JnaDatabase.STATUS_VECTOR_SIZE];
private final FbClientLibrary clientLibrary;
private ByteBuffer byteBuffer;

public JnaBlob(JnaDatabase database, JnaTransaction transaction, BlobParameterBuffer blobParameterBuffer) {
this(database, transaction, blobParameterBuffer, NO_BLOB_ID);
Expand Down Expand Up @@ -139,12 +140,13 @@ public byte[] getSegment(int sizeRequested) throws SQLException {
}
// TODO Honour request for larger sizes by looping?
sizeRequested = Math.min(sizeRequested, getMaximumSegmentSize());
final ByteBuffer responseBuffer = ByteBuffer.allocateDirect(sizeRequested);
final ByteBuffer responseBuffer;
final ShortByReference actualLength = new ShortByReference();
synchronized (getSynchronizationObject()) {
checkDatabaseAttached();
checkTransactionActive();
checkBlobOpen();
responseBuffer = getByteBuffer(sizeRequested);

clientLibrary.isc_get_segment(statusVector, getJnaHandle(), actualLength, (short) sizeRequested,
responseBuffer);
Expand Down Expand Up @@ -216,9 +218,9 @@ public void seek(int offset, SeekMode seekMode) throws SQLException {
@Override
public byte[] getBlobInfo(byte[] requestItems, int bufferLength) throws SQLException {
try {
final ByteBuffer responseBuffer = ByteBuffer.allocateDirect(bufferLength);

final ByteBuffer responseBuffer;
synchronized (getSynchronizationObject()) {
responseBuffer = getByteBuffer(bufferLength);
checkDatabaseAttached();
clientLibrary.isc_blob_info(statusVector, getJnaHandle(),
(short) requestItems.length, requestItems,
Expand All @@ -238,20 +240,37 @@ public byte[] getBlobInfo(byte[] requestItems, int bufferLength) throws SQLExcep
@Override
protected void closeImpl() throws SQLException {
synchronized (getSynchronizationObject()) {
clientLibrary.isc_close_blob(statusVector, getJnaHandle());
processStatusVector();
try {
clientLibrary.isc_close_blob(statusVector, getJnaHandle());
processStatusVector();
} finally {
byteBuffer = null;
}
}
}

@Override
protected void cancelImpl() throws SQLException {
synchronized (getSynchronizationObject()) {
clientLibrary.isc_cancel_blob(statusVector, getJnaHandle());
processStatusVector();
try {
clientLibrary.isc_cancel_blob(statusVector, getJnaHandle());
processStatusVector();
} finally {
byteBuffer = null;
}
}
}

private void processStatusVector() throws SQLException {
getDatabase().processStatusVector(statusVector, null);
}

private ByteBuffer getByteBuffer(int requiredSize) {
if (byteBuffer == null || byteBuffer.capacity() < requiredSize) {
byteBuffer = ByteBuffer.allocateDirect(requiredSize);
} else {
byteBuffer.clear();
}
return byteBuffer;
}
}

0 comments on commit 7791a80

Please sign in to comment.