Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Performance] Large Object's Input Stream (BlobInputStream) takes a lot of time due to read one by one in application level #2375

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ public synchronized void reset() throws IOException {
}
}

/**
* Buffered read on Application Level.
*/
@Override
public int read(byte[] buf, int off, int len) throws IOException {

if (bpos != 0) {
return super.read(buf,off,len);
}

LargeObject lo = getLo();
try {

if (limit > 0 && apos >= limit) {
return -1;
}

if (limit > 0 && limit - apos < len) {
len = (int) (limit - apos);
}

int result = lo.read(buf, off, len);
apos += result;

return result == 0 ? -1 : result;
} catch (SQLException se) {
throw new IOException(se.toString());
}
}

/**
* Tests if this input stream supports the <code>mark</code> and <code>reset</code> methods. The
* <code>markSupported</code> method of <code>InputStream</code> returns <code>false</code>.
Expand Down
57 changes: 57 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/BlobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.postgresql.test.TestUtil;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -262,6 +263,62 @@ public void testLargeLargeObject() throws Exception {
assertEquals(length, lob.length());
}

@Test
public void testLargeObjectBufferedRead() throws Exception {
String fileName = "/test-file.xml";
long loid = uploadFile("/test-file.xml", LOOP);
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();

InputStream fis = getClass().getResourceAsStream(fileName);

InputStream lois = lom.open(loid).getInputStream();

for (byte[] fileBuffer = new byte[10], loBuffer = new byte[10]; (fis.read(fileBuffer)) != -1;) {
lois.read(loBuffer);
Assert.assertArrayEquals(fileBuffer, loBuffer);
}

fis.close();
lois.close();
}

@Test
public void testLargeObjectBufferedReadWithLimit() throws Exception {
long loid = uploadFile("/test-file.xml", LOOP);
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
int limit = 35;
InputStream lois = lom.open(loid).getInputStream(limit);
byte[] loBuffer = new byte[10];
int totalReadBytes = 0;

for (int countBytes; (countBytes = lois.read(loBuffer)) != -1;) {
totalReadBytes += countBytes;
}

Assert.assertEquals(limit, totalReadBytes);
lois.close();
}

@Test
public void testLargeObjectBufferedReadPlusSingleWithLimit() throws Exception {
long loid = uploadFile("/test-file.xml", LOOP);
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
int limit = 35;
InputStream lois = lom.open(loid).getInputStream(limit);
byte[] loBuffer = new byte[10];
int totalReadBytes = 0;

for (int countBytes; (countBytes = lois.read(loBuffer)) != -1;) {
totalReadBytes += countBytes;
if (lois.read() != -1) {
++totalReadBytes;
}
}

Assert.assertEquals(limit, totalReadBytes);
lois.close();
}

/*
* Helper - uploads a file into a blob using old style methods. We use this because it always
* works, and we can use it as a base to test the new methods.
Expand Down