Search in sources :

Example 1 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class LobRsGetterTest method suite.

/**
 * Returns a suite with all tests running with both embedded and client.
 */
public static Test suite() {
    Test suite = TestConfiguration.defaultSuite(LobRsGetterTest.class, false);
    return new CleanDatabaseTestSetup(suite) {

        protected void decorateSQL(Statement s) throws SQLException {
            Connection con = s.getConnection();
            dropTable(con, TABLE);
            // NOTE: Do not insert a lot of data into this table, many
            // of the tests iterate over all rows in the table.
            s.executeUpdate("create table " + TABLE + "(" + "id INT GENERATED ALWAYS AS IDENTITY, " + "dBlob BLOB, dClob CLOB)");
            // Insert a few rows with different characteristics:
            // multi page LOB, single page LOB, NULL
            PreparedStatement ps = con.prepareStatement("insert into " + TABLE + "(dBlob, dClob) values (?,?)");
            // 173 KB or KChars
            int mpSize = 173 * 1024;
            // 300 B or chars
            int spSize = 300;
            ps.setBinaryStream(1, new LoopingAlphabetStream(mpSize), mpSize);
            ps.setCharacterStream(2, new LoopingAlphabetReader(mpSize), mpSize);
            ps.executeUpdate();
            ps.setBinaryStream(1, new LoopingAlphabetStream(spSize), spSize);
            ps.setCharacterStream(2, new LoopingAlphabetReader(spSize), spSize);
            ps.executeUpdate();
            ps.setNull(1, Types.BLOB);
            ps.setNull(2, Types.CLOB);
            ps.executeUpdate();
            // Make sure there are three rows.
            JDBC.assertDrainResults(s.executeQuery("select * from " + TABLE), 3);
            ps.close();
            s.close();
        }
    };
}
Also used : Test(junit.framework.Test) CleanDatabaseTestSetup(org.apache.derbyTesting.junit.CleanDatabaseTestSetup) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)

Example 2 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class LobStreamsTest method testBlobWrite3Param.

/**
 * Tests the BlobOutputStream.write(byte  b[], int off, int len) method
 */
public void testBlobWrite3Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
    assertTrue("FAIL -- file not found", streamIn != null);
    PreparedStatement stmt3 = prepareStatement("SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);
    assertTrue("FAIL -- blob is NULL", (blob != null));
    int count = 0;
    byte[] buffer = new byte[1024];
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((count = streamIn.read(buffer)) != -1) {
        outstream.write(buffer, 0, count);
    }
    outstream.close();
    streamIn.close();
    PreparedStatement stmt4 = prepareStatement("UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1, blob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();
    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- blob not found", rs3.next());
    blob = rs3.getBlob(1);
    long new_length = blob.length();
    assertEquals("FAIL -- wrong blob length;", streamSize[0], new_length);
    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
    InputStream lStream = blob.getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match", compareLob2File(fStream, lStream));
    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
Also used : Blob(java.sql.Blob) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) PreparedStatement(java.sql.PreparedStatement)

Example 3 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class LobStreamsTest method testBlobWrite1Param.

/**
 * Tests the BlobOutputStream.write(int b) method
 */
public void testBlobWrite1Param() throws Exception {
    InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);
    PreparedStatement stmt3 = prepareStatement("SELECT b FROM testBlobX1 WHERE a = 1");
    ResultSet rs3 = stmt3.executeQuery();
    rs3.next();
    Blob blob = rs3.getBlob(1);
    assertTrue("FAIL -- blob is NULL", blob != null);
    int buffer;
    OutputStream outstream = blob.setBinaryStream(1L);
    while ((buffer = streamIn.read()) != -1) {
        outstream.write(buffer);
    }
    outstream.close();
    streamIn.close();
    PreparedStatement stmt4 = prepareStatement("UPDATE testBlobX1 SET b = ? WHERE a = 1");
    stmt4.setBlob(1, blob);
    stmt4.executeUpdate();
    stmt4.close();
    rs3.close();
    rs3 = stmt3.executeQuery();
    assertTrue("FAIL -- blob not found", rs3.next());
    blob = rs3.getBlob(1);
    long new_length = blob.length();
    assertEquals("FAIL -- wrong blob length", streamSize[1], new_length);
    // Check contents ...
    InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
    InputStream lStream = blob.getBinaryStream();
    assertTrue("FAIL - Blob and file contents do not match", compareLob2File(fStream, lStream));
    fStream.close();
    lStream.close();
    rs3.close();
    stmt3.close();
}
Also used : Blob(java.sql.Blob) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream) PreparedStatement(java.sql.PreparedStatement)

Example 4 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class BlobAccessTest method initializeBlobData.

/**
 * Generates test data.
 */
private static void initializeBlobData(Statement stmt) throws SQLException, UnsupportedEncodingException {
    Connection con = stmt.getConnection();
    con.setAutoCommit(false);
    if (!disableSmallBlobs) {
        println("Generating small Blobs test data.");
        // Insert small Blob data.
        try {
            stmt.executeUpdate("drop table smallBlobs");
        } catch (SQLException sqle) {
            assertSQLState("42Y55", sqle);
        }
        stmt.executeUpdate("create table smallBlobs (dBlob Blob, length int)");
        PreparedStatement smallBlobInsert = con.prepareStatement("insert into smallBlobs values (?,?)");
        // Insert 15 000 small Blobs.
        for (int BlobCounter = 1; BlobCounter < 15001; BlobCounter++) {
            byte[] content = Integer.toString(BlobCounter).getBytes("US-ASCII");
            smallBlobInsert.setBytes(1, content);
            smallBlobInsert.setInt(2, content.length);
            smallBlobInsert.executeUpdate();
            if (BlobCounter % 1000 == 0) {
                con.commit();
            }
        }
        con.commit();
    }
    if (!disableLargeBlobs) {
        println("Generating large Blobs test data.");
        // Insert large Blob data.
        try {
            stmt.executeUpdate("drop table largeBlobs");
        } catch (SQLException sqle) {
            assertSQLState("42Y55", sqle);
        }
        stmt.executeUpdate("create table largeBlobs (" + "id int unique not null, dBlob Blob, length int)");
        PreparedStatement largeBlobInsert = con.prepareStatement("insert into largeBlobs values (?,?,?)");
        // Insert some large Blobs.
        // 15 MB default
        final int size = largeBlobSizeMB * 1024 * 1024;
        for (int BlobCounter = 1; BlobCounter < 11; BlobCounter++) {
            largeBlobInsert.setInt(1, BlobCounter);
            largeBlobInsert.setBinaryStream(2, new LoopingAlphabetStream(size), size);
            largeBlobInsert.setInt(3, size);
            largeBlobInsert.executeUpdate();
        }
        con.commit();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 5 with LoopingAlphabetStream

use of org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream in project derby by apache.

the class DbTasks method insertMail.

public void insertMail(Connection conn, String thread_name) throws Exception {
    // Inserting rows to the inbox table.
    // inbox table would have random attachments - (attach table)
    // The random attachment depends on no of rows id in inbox
    InputStream streamIn = null;
    Reader streamReader = null;
    boolean saveAutoCommit = conn.getAutoCommit();
    int saveIsolation = conn.getTransactionIsolation();
    try {
        conn.setAutoCommit(false);
        PreparedStatement insertFirst = conn.prepareStatement(Statements.insertStr, Statement.RETURN_GENERATED_KEYS);
        String name = new String("ABCD");
        String l_name = new String("WXYZ");
        long total_ins_inb = 0;
        long total_ins_att = 0;
        int row_count = 0;
        int num = Rn.nextInt(10 - 1);
        if (num == 0)
            num = 1;
        for (int i = 0; i < num; i++) {
            long s_insert = System.currentTimeMillis();
            String new_name = new String(increment(name, 60));
            String new_lname = new String(decrement(l_name, 60));
            insertFirst.setString(1, new_name);
            insertFirst.setString(2, new_lname);
            insertFirst.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            name = new_name;
            l_name = new_lname;
            try {
                // to create a stream of random length between 200 bytes and 3MB
                int clobLength = Rn.nextInt(3078000 - 200 + 1) + 200;
                streamReader = new LoopingAlphabetReader(clobLength, CharAlphabet.modernLatinLowercase());
                insertFirst.setCharacterStream(4, streamReader, clobLength);
            } catch (Exception e) {
                MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "File not found Exception : " + e.getMessage());
                errorPrint(e);
                throw e;
            }
            int result = insertFirst.executeUpdate();
            if (result != 0) {
                insert_count = insert_count + 1;
            }
            streamReader.close();
            long e_insert = System.currentTimeMillis();
            total_ins_inb = total_ins_inb + (e_insert - s_insert);
            PreparedStatement insertAttach = conn.prepareStatement(Statements.insertStrAttach);
            Statement stmt1 = conn.createStatement();
            ResultSet rs = insertFirst.getGeneratedKeys();
            // 10/1 chance to have attactment
            int numa = Rn.nextInt(10 - 1);
            if (numa == 0)
                numa = 1;
            if (i == numa) {
                int attachid = 0;
                while (rs.next()) {
                    attachid = rs.getInt(1);
                }
                // insert from 1 to 5 attachments
                int num_attach = Rn.nextInt(5 - 1);
                if (num_attach == 0)
                    num_attach = 1;
                for (int j = 0; j < num_attach; j++) {
                    long a_start = System.currentTimeMillis();
                    insertAttach.setInt(1, attachid);
                    // attach_id should be automatically generated
                    try {
                        // to create a stream of random length between 0 and 5M
                        int blobLength = Rn.nextInt(5130000 - 0 + 1) + 0;
                        streamIn = new LoopingAlphabetStream(blobLength);
                        insertAttach.setBinaryStream(2, streamIn, blobLength);
                    } catch (Exception e) {
                        MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "Exception : " + e.getMessage());
                        errorPrint(e);
                        throw e;
                    }
                    int result_attach = insertAttach.executeUpdate();
                    streamIn.close();
                    if (result_attach != 0) {
                        blob_count = blob_count + 1;
                        row_count++;
                    }
                    long a_end = System.currentTimeMillis();
                    total_ins_att = total_ins_att + (a_end - a_start);
                }
            }
            id_count++;
            rs.close();
            stmt1.close();
            insertAttach.close();
        }
        log.logMsg(LogFile.INFO + thread_name + " : " + "Time taken to insert " + num + " rows to REFRESH.INBOX :" + PerfTime.readableTime(total_ins_inb));
        log.logMsg(LogFile.INFO + thread_name + " : " + "Time taken to insert " + row_count + " rows to REFRESH.ATTACH :" + PerfTime.readableTime(total_ins_att));
        insertFirst.close();
        conn.commit();
    } catch (SQLException sqe) {
        MailJdbc.logAct.logMsg(LogFile.ERROR + thread_name + " : " + "Error while inserting REFRESH.ATTACH:" + sqe.getMessage());
        sqe.printStackTrace();
        errorPrint(sqe);
        conn.rollback();
        throw sqe;
    } finally {
        conn.setTransactionIsolation(saveIsolation);
        conn.setAutoCommit(saveAutoCommit);
    }
}
Also used : SQLException(java.sql.SQLException) InputStream(java.io.InputStream) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) SQLException(java.sql.SQLException) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Aggregations

LoopingAlphabetStream (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)61 PreparedStatement (java.sql.PreparedStatement)33 InputStream (java.io.InputStream)31 ResultSet (java.sql.ResultSet)31 Statement (java.sql.Statement)20 Blob (java.sql.Blob)10 SQLException (java.sql.SQLException)9 LoopingAlphabetReader (org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader)9 PositionedStoreStream (org.apache.derby.impl.jdbc.PositionedStoreStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 OutputStream (java.io.OutputStream)5 Reader (java.io.Reader)5 Clob (java.sql.Clob)3 Timestamp (java.sql.Timestamp)3 DataInputStream (java.io.DataInputStream)2 EOFException (java.io.EOFException)2 CallableStatement (java.sql.CallableStatement)2 Date (java.sql.Date)2 Time (java.sql.Time)2