Search in sources :

Example 21 with FbWireDatabase

use of org.firebirdsql.gds.ng.wire.FbWireDatabase in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testBlobSeek_streamed.

/**
 * Tests absolute seek on a stream blob.
 */
@Test
public void testBlobSeek_streamed() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    // Use sufficiently large value so that multiple segments are used
    final int requiredSize = 200;
    populateStreamBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            // NOTE: What matters is if the blob on the server is stream or segment
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            final int offset = requiredSize / 2;
            blob.seek(offset, FbBlob.SeekMode.ABSOLUTE);
            byte[] segment = blob.getSegment(100);
            byte[] expected = Arrays.copyOfRange(baseContent, offset, offset + 100);
            blob.close();
            statement.close();
            assertEquals("Unexpected length read from blob", 100, segment.length);
            assertArrayEquals("Unexpected segment content", expected, segment);
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 22 with FbWireDatabase

use of org.firebirdsql.gds.ng.wire.FbWireDatabase in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testReopen.

/**
 * Tests reopen is allowed.
 */
@Test
public void testReopen() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            // Reopen
            blob.open();
            bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            statement.close();
            byte[] result = bos.toByteArray();
            assertEquals("Unexpected length read from blob", requiredSize, result.length);
            assertTrue("Unexpected blob content", validateBlobContent(result, baseContent, requiredSize));
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 23 with FbWireDatabase

use of org.firebirdsql.gds.ng.wire.FbWireDatabase in project jaybird by FirebirdSQL.

the class TestV10InputBlob method testBlobRetrieval.

/**
 * Tests retrieval of a blob (what goes in is what comes out).
 */
@Test
public void testBlobRetrieval() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    // Use sufficiently large value so that multiple segments are used
    final int requiredSize = 4 * Short.MAX_VALUE;
    populateBlob(testId, baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        try {
            long blobId = getBlobId(testId, db);
            final FbBlob blob = db.createBlobForInput(transaction, null, blobId);
            blob.open();
            ByteArrayOutputStream bos = new ByteArrayOutputStream(requiredSize);
            while (!blob.isEof()) {
                bos.write(blob.getSegment(blob.getMaximumSegmentSize()));
            }
            blob.close();
            statement.close();
            byte[] result = bos.toByteArray();
            assertEquals("Unexpected length read from blob", requiredSize, result.length);
            assertTrue("Unexpected blob content", validateBlobContent(result, baseContent, requiredSize));
        } finally {
            if (transaction != null)
                transaction.commit();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Example 24 with FbWireDatabase

use of org.firebirdsql.gds.ng.wire.FbWireDatabase in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testBlobStorage_Stream.

/**
 * Tests storage of a stream blob (what goes in is what comes out).
 */
@Test
public void testBlobStorage_Stream() throws Exception {
    final int testId = 1;
    final byte[] baseContent = generateBaseContent();
    // Use sufficiently large value so that multiple segments are used
    final int requiredSize = 4 * Short.MAX_VALUE;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        final BlobParameterBuffer blobParameterBuffer = db.createBlobParameterBuffer();
        blobParameterBuffer.addArgument(BlobParameterBuffer.TYPE, BlobParameterBuffer.TYPE_STREAM);
        writeBlob(testId, testBytes, db, blobParameterBuffer);
    }
    assertTrue("Unexpected blob content", validateBlob(testId, baseContent, requiredSize));
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) BlobParameterBuffer(org.firebirdsql.gds.BlobParameterBuffer) Test(org.junit.Test)

Example 25 with FbWireDatabase

use of org.firebirdsql.gds.ng.wire.FbWireDatabase in project jaybird by FirebirdSQL.

the class TestV10OutputBlob method testReopen.

/**
 * Test reopen is not allowed.
 */
@Test
public void testReopen() throws Exception {
    expectedException.expect(SQLNonTransientException.class);
    expectedException.expect(allOf(errorCodeEquals(ISCConstants.isc_segstr_no_op), fbMessageStartsWith(ISCConstants.isc_segstr_no_op)));
    final byte[] baseContent = generateBaseContent();
    final int requiredSize = 256;
    final byte[] testBytes = generateBlobContent(baseContent, requiredSize);
    try (FbWireDatabase db = createDatabaseConnection()) {
        final FbTransaction transaction = getTransaction(db);
        try {
            final FbBlob blob = db.createBlobForOutput(transaction, null);
            blob.open();
            int bytesWritten = 0;
            while (bytesWritten < testBytes.length) {
                // TODO the interface for writing blobs should be simpler
                byte[] buffer = new byte[Math.min(blob.getMaximumSegmentSize(), testBytes.length - bytesWritten)];
                System.arraycopy(testBytes, bytesWritten, buffer, 0, buffer.length);
                blob.putSegment(buffer);
                bytesWritten += buffer.length;
            }
            blob.close();
            // Reopen
            blob.open();
        } finally {
            transaction.commit();
        }
    }
}
Also used : FbWireDatabase(org.firebirdsql.gds.ng.wire.FbWireDatabase) FbTransaction(org.firebirdsql.gds.ng.FbTransaction) FbBlob(org.firebirdsql.gds.ng.FbBlob) Test(org.junit.Test)

Aggregations

FbWireDatabase (org.firebirdsql.gds.ng.wire.FbWireDatabase)27 Test (org.junit.Test)23 WireDatabaseConnection (org.firebirdsql.gds.ng.wire.WireDatabaseConnection)13 FbBlob (org.firebirdsql.gds.ng.FbBlob)11 AbstractFbWireDatabase (org.firebirdsql.gds.ng.wire.AbstractFbWireDatabase)11 FBManager (org.firebirdsql.management.FBManager)9 FbTransaction (org.firebirdsql.gds.ng.FbTransaction)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 SQLException (java.sql.SQLException)2 IOException (java.io.IOException)1 SQLNonTransientException (java.sql.SQLNonTransientException)1 BlobParameterBuffer (org.firebirdsql.gds.BlobParameterBuffer)1 DatatypeCoder (org.firebirdsql.gds.ng.DatatypeCoder)1 FbExceptionBuilder (org.firebirdsql.gds.ng.FbExceptionBuilder)1 FbStatement (org.firebirdsql.gds.ng.FbStatement)1 StatementState (org.firebirdsql.gds.ng.StatementState)1 FieldValue (org.firebirdsql.gds.ng.fields.FieldValue)1 SimpleStatementListener (org.firebirdsql.gds.ng.wire.SimpleStatementListener)1 Ignore (org.junit.Ignore)1