Search in sources :

Example 56 with LoopingAlphabetStream

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

the class TriggerTests method test2UpdateAfterTriggerUpdatedLOB.

/**
 * This test creates an AFTER UPDATE trigger which in it's trigger action
 * updates a lob column from the row that just got updated. So, this test
 * does update the LOB from the triggering table inside the trigger
 * action.
 *
 * UPDATE trigger in this test is working with large data in the LOB column
 * inside the trigger action and hence it will run out of memory. For that
 * reason, the test is disabled.
 *
 * @throws SQLException
 */
public void test2UpdateAfterTriggerUpdatedLOB() throws SQLException {
    if (isDerby1482Fixed == false)
        return;
    if (testWithLargeDataInLOB)
        return;
    basicSetup();
    Statement s = createStatement();
    // The default table1 created by basicSetup does not match the
    // requirement of this test so dropping and recreating it.
    s.execute("drop table table1");
    s.execute("create table table1 (id int, status smallint, bl blob(2G), bl_null blob(2G))");
    s.execute("create trigger trigger1 after update of status on table1 referencing " + "new as n_row for each row " + "update table1 set bl_null=n_row.bl where bl_null is null");
    PreparedStatement ps = prepareStatement("insert into table1 values (?, 0, ?, null)");
    ps.setInt(1, 1);
    ps.setBinaryStream(2, new LoopingAlphabetStream(lobsize), lobsize);
    ps.executeUpdate();
    commit();
    runtest1UpdateTrigger();
}
Also used : LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 57 with LoopingAlphabetStream

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

the class BlobMemTest method testDerby5752DoubleMaterialization.

/**
 * Test that a BLOB that goes through the sorter does not get materialized
 * twice in memory. It will still be materialized as part of the sorting,
 * but the fix for DERBY-5752 prevents the creation of a second copy when
 * accessing the BLOB after the sorting.
 */
public void testDerby5752DoubleMaterialization() throws Exception {
    setAutoCommit(false);
    Statement s = createStatement();
    s.execute("create table d5752(id int, b blob)");
    int lobSize = 1000000;
    // Insert a single BLOB in the table.
    PreparedStatement insert = prepareStatement("insert into d5752 values (1,?)");
    insert.setBinaryStream(1, new LoopingAlphabetStream(lobSize), lobSize);
    insert.execute();
    closeStatement(insert);
    Blob[] blobs = new Blob[15];
    // Repeatedly sort the table and keep a reference to the BLOB.
    for (int i = 0; i < blobs.length; i++) {
        ResultSet rs = s.executeQuery("select * from d5752 order by id");
        rs.next();
        // Used to get an OutOfMemoryError here because a new copy of the
        // BLOB was created in memory.
        blobs[i] = rs.getBlob(2);
        rs.close();
    }
    // earlier (in which case we wouldn't see the OOME in the loop above).
    for (int i = 0; i < blobs.length; i++) {
        assertEquals(lobSize, blobs[i].length());
    }
}
Also used : Blob(java.sql.Blob) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 58 with LoopingAlphabetStream

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

the class StreamTest method insertBlobData.

private void insertBlobData(int blobSize) throws SQLException {
    Statement stmt = createStatement();
    stmt.executeUpdate("CREATE TABLE testLob " + " (b blob(" + blobSize + "))");
    stmt.close();
    PreparedStatement ps = prepareStatement("insert into testLob values(?)");
    InputStream stream = new LoopingAlphabetStream(blobSize);
    ps.setBinaryStream(1, stream, blobSize);
    ps.executeUpdate();
    ps.close();
}
Also used : InputStream(java.io.InputStream) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 59 with LoopingAlphabetStream

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

the class Datatypes method update_one_row.

public static synchronized void update_one_row(Connection conn, int thread_id) throws Exception {
    PreparedStatement ps2 = null;
    Statement stmt = conn.createStatement();
    ResultSet rs;
    String column = null;
    int ind = 0;
    long max = 0;
    long min = 0;
    double x;
    long id_to_update;
    InputStream streamIn = null;
    Reader streamReader = null;
    rs = stmt.executeQuery("select max(serialkey) from Datatypes");
    while (rs.next()) max = rs.getLong(1);
    rs = stmt.executeQuery("select min(serialkey) from Datatypes");
    while (rs.next()) min = rs.getLong(1);
    id_to_update = (min + 1) + (Math.abs(Rn.nextLong()) % (max - min));
    if (id_to_update == 0)
        id_to_update = 1;
    ind = Math.abs(Rn.nextInt());
    column = colnames[ind % NUMTYPES];
    try {
        conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
        ps2 = conn.prepareStatement(" update Datatypes set " + column + " = ? " + " where serialkey = " + id_to_update);
    } catch (SQLException se) {
        if (se.getNextException() == null)
            throw se;
        String m = se.getNextException().getSQLState();
        return;
    }
    String ds = null;
    String ds2 = null;
    String cs = "asdf qwerqwer 12341234 ZXCVZXCVZXCV !@#$!@#$ asdfasdf 1 q a z asdf ASDF qwerasdfzxcvasdfqwer1234asd#";
    double d = 0.0;
    float f = 0;
    BigDecimal bdec = null;
    int type = (ind % NUMTYPES);
    switch(type) {
        case TCHAR:
            ds2 = cs.substring(Math.abs(Rn.nextInt() % 100));
            ps2.setString(1, ds2);
            break;
        case TDATE:
            Date dt = new Date(1);
            dt.setTime(Math.abs(Rn.nextLong() / 150000));
            dt.setTime(Math.abs(Rn.nextLong() / 150000));
            ps2.setDate(1, dt);
            ds2 = dt.toString();
            break;
        case TDECIMAL:
            x = Math.abs(Rn.nextInt() % 18);
            if (x > 5)
                x = 5;
            d = Rn.nextDouble() * Math.pow(10, x);
            bdec = new BigDecimal(d);
            ps2.setBigDecimal(1, bdec);
            ds2 = String.valueOf(d);
            break;
        case TDECIMALNN:
            ds = String.valueOf(d);
            d = Rn.nextDouble();
            bdec = new BigDecimal(d);
            ps2.setBigDecimal(1, bdec);
            ds2 = String.valueOf(d);
            break;
        case TDOUBLE:
            d = Rn.nextDouble() * Math.pow(10, Rn.nextInt() % 300);
            ps2.setDouble(1, d);
            ds2 = String.valueOf(d);
            break;
        case TFLOAT:
            ds = String.valueOf(f);
            f = Rn.nextFloat() * (float) Math.pow(10, Rn.nextInt() % 30);
            ps2.setFloat(1, f);
            ds2 = String.valueOf(f);
            break;
        case TINT:
            int i = Rn.nextInt();
            ds2 = String.valueOf(i);
            ps2.setInt(1, i);
            break;
        case TLONGINT:
            long l = Rn.nextLong();
            ds2 = String.valueOf(l);
            ps2.setLong(1, l);
            break;
        case TNUMERICLARGE:
            ds = String.valueOf(d);
            x = Math.abs(Rn.nextInt() % 30);
            if (x > 30)
                x = 31;
            d = Rn.nextDouble() * Math.pow(10, x);
            bdec = new BigDecimal(d);
            ps2.setBigDecimal(1, bdec);
            ds2 = String.valueOf(d);
            break;
        case TREAL:
            ds = String.valueOf(f);
            f = Rn.nextFloat() * (float) Math.pow(10, Rn.nextInt() % 7);
            ps2.setFloat(1, f);
            ds2 = String.valueOf(f);
            break;
        case TSMALLINT:
            i = Rn.nextInt() % (256 * 128);
            ds = String.valueOf(i);
            short si = (short) i;
            ps2.setShort(1, si);
            ds2 = String.valueOf(si);
            break;
        case TTIME:
            Time tt = new Time(1);
            tt.setTime(Math.abs(Rn.nextInt()));
            ps2.setTime(1, tt);
            ds2 = tt.toString();
            break;
        case TTIMESTAMP:
            Timestamp ts = new Timestamp(1);
            ts.setTime(Math.abs(Rn.nextLong() / 50000));
            ps2.setTimestamp(1, ts);
            ds2 = ts.toString();
            break;
        case TVARCHAR:
            ds2 = cs.substring(Math.abs(Rn.nextInt() % 100));
            ps2.setString(1, ds2);
            break;
        case TBLOB:
            // to create a stream of random length between 0 and 100K
            int blobLength = Rn.nextInt(102400 - 0 + 1) + 0;
            streamIn = new LoopingAlphabetStream(blobLength);
            ps2.setBinaryStream(1, streamIn, blobLength);
            break;
        case TCLOB:
            // to create a stream of random length between 0 and 100K
            int clobLength = Rn.nextInt(102400 - 0 + 1) + 0;
            streamReader = new LoopingAlphabetReader(clobLength, CharAlphabet.modernLatinLowercase());
            ps2.setCharacterStream(1, streamReader, clobLength);
            break;
    }
    int rows = 0;
    boolean cleanuponly = false;
    if (cleanuponly == false) {
        try {
            rows = ps2.executeUpdate();
        } catch (SQLException se) {
            if (se.getNextException() == null)
                throw se;
            String m = se.getNextException().getSQLState();
            System.out.println(se.getNextException().getMessage() + " SQLSTATE: " + m);
        }
        if (rows < 0)
            System.out.println("t" + thread_id + " update failed.");
    }
    if (streamReader != null)
        streamReader.close();
    if (streamIn != null)
        streamIn.close();
    if (ps2 != null)
        try {
            ps2.close();
            rs.close();
        } catch (SQLException se) {
            if (se.getNextException() == null)
                throw se;
            String m = se.getNextException().getSQLState();
            System.out.println(se.getNextException().getMessage() + " SQLSTATE: " + m);
        }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) InputStream(java.io.InputStream) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) LoopingAlphabetReader(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader) ResultSet(java.sql.ResultSet) LoopingAlphabetStream(org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream)

Example 60 with LoopingAlphabetStream

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

the class SuicideOfStreamingTest method setUp.

/**
 * Create table, insert row and set debug property.
 */
public void setUp() throws Exception {
    // Create the table.
    Statement createTableSt = createStatement();
    createTableSt.execute("create table TEST_TABLE( TEST_COL blob( 65536 ))");
    createTableSt.close();
    // Insert a row.
    PreparedStatement insertLobSt = prepareStatement("insert into TEST_TABLE (TEST_COL) values (?)");
    int lobLength = 65536;
    insertLobSt.setBinaryStream(1, new LoopingAlphabetStream(lobLength), lobLength);
    insertLobSt.executeUpdate();
    insertLobSt.close();
    setSystemProperty("derby.debug.suicideOfLayerBStreaming", "true");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) 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