Java Code Examples for java.sql.Blob#truncate()

The following examples show how to use java.sql.Blob#truncate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlobRegressionTest.java    From Komondor with GNU General Public License v3.0 5 votes vote down vote up
public void testBug34677() throws Exception {
    createTable("testBug34677", "(field1 BLOB)");
    this.stmt.executeUpdate("INSERT INTO testBug34677 VALUES ('abc')");

    this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug34677");
    this.rs.next();
    Blob blob = this.rs.getBlob(1);
    blob.truncate(0L);
    assertEquals(0, blob.length());
    assertEquals(-1, blob.getBinaryStream().read());

}
 
Example 2
Source File: BlobRegressionTest.java    From r-course with MIT License 5 votes vote down vote up
public void testBug34677() throws Exception {
    createTable("testBug34677", "(field1 BLOB)");
    this.stmt.executeUpdate("INSERT INTO testBug34677 VALUES ('abc')");

    this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug34677");
    this.rs.next();
    Blob blob = this.rs.getBlob(1);
    blob.truncate(0L);
    assertEquals(0, blob.length());
    assertEquals(-1, blob.getBinaryStream().read());

}
 
Example 3
Source File: JDBCSequentialFileFactoryDriver.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Persists data to this files associated database mapping.
 *
 * @param file
 * @param data
 * @return
 * @throws SQLException
 */
public int writeToFile(JDBCSequentialFile file, byte[] data, boolean append) throws SQLException {
   synchronized (connection) {
      connection.setAutoCommit(false);
      appendToLargeObject.setLong(1, file.getId());

      int bytesWritten = 0;
      try (ResultSet rs = appendToLargeObject.executeQuery()) {
         if (rs.next()) {
            Blob blob = rs.getBlob(1);
            if (blob == null) {
               blob = connection.createBlob();
            }
            if (append) {
               bytesWritten = blob.setBytes(blob.length() + 1, data);
            } else {
               blob.truncate(0);
               bytesWritten = blob.setBytes(1, data);
            }
            rs.updateBlob(1, blob);
            rs.updateRow();
         }
         connection.commit();
         return bytesWritten;
      } catch (SQLException e) {
         connection.rollback();
         throw e;
      }
   }
}
 
Example 4
Source File: BlobRegressionTest.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public void testBug34677() throws Exception {
    createTable("testBug34677", "(field1 BLOB)");
    this.stmt.executeUpdate("INSERT INTO testBug34677 VALUES ('abc')");

    this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug34677");
    this.rs.next();
    Blob blob = this.rs.getBlob(1);
    blob.truncate(0L);
    assertEquals(0, blob.length());
    assertEquals(-1, blob.getBinaryStream().read());

}
 
Example 5
Source File: BlobSetMethodsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    pstmt.setInt (1,1);
    Blob blob = con.createBlob();
    //add 1024 bytes
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
    blob.setBytes (1, data);
    assertEquals (BUFFER_SIZE, blob.length());
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(
            "select data from blobtest where id = 1");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE, blob.length());
    //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (UPDATE_SIZE, data1);
    byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //update it at the end
    blob.setBytes (BUFFER_SIZE + 1, data1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //insert the blob and test again
    pstmt.setInt (1, 2);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    rs = stmt.executeQuery("select data from blobtest where " +
            "id = 2");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);

    //test truncate on small size blob
    blob = con.createBlob();
    data = new byte [100];
    for (int i = 0; i < 100; i++) {
        data [i] = (byte) i;
    }
    blob.setBytes (1, data);
    assertEquals (blob.length(), 100);
    blob.truncate (50);
    assertEquals (blob.length(), 50);
    blob.setBytes (1, data);
    assertEquals ("set failed", blob.length(), 100);
    blob.truncate (50);
    assertEquals ("truncation failed", blob.length(), 50);
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 6
Source File: BlobSetMethodsTest.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    Blob blob = con.createBlob();
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
 //now add more than 4k so file get in use
    for (int i = 0; i < 5; i++)
        blob.setBytes (i * BUFFER_SIZE + 1, data);
    assertEquals (BUFFER_SIZE * 5 , blob.length());
                //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (BUFFER_SIZE + 1, data1);
    blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    //insert it into table
    pstmt.setInt (1, 3);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate ();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                            "id = 3");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //test truncate
    blob.truncate (BUFFER_SIZE);
    assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 7
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    pstmt.setInt (1,1);
    Blob blob = con.createBlob();
    //add 1024 bytes
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
    blob.setBytes (1, data);
    assertEquals (BUFFER_SIZE, blob.length());
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(
            "select data from blobtest where id = 1");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE, blob.length());
    //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (UPDATE_SIZE, data1);
    byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //update it at the end
    blob.setBytes (BUFFER_SIZE + 1, data1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //insert the blob and test again
    pstmt.setInt (1, 2);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    rs = stmt.executeQuery("select data from blobtest where " +
            "id = 2");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);

    //test truncate on small size blob
    blob = con.createBlob();
    data = new byte [100];
    for (int i = 0; i < 100; i++) {
        data [i] = (byte) i;
    }
    blob.setBytes (1, data);
    assertEquals (blob.length(), 100);
    blob.truncate (50);
    assertEquals (blob.length(), 50);
    blob.setBytes (1, data);
    assertEquals ("set failed", blob.length(), 100);
    blob.truncate (50);
    assertEquals ("truncation failed", blob.length(), 50);
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 8
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    Blob blob = con.createBlob();
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
 //now add more than 4k so file get in use
    for (int i = 0; i < 5; i++)
        blob.setBytes (i * BUFFER_SIZE + 1, data);
    assertEquals (BUFFER_SIZE * 5 , blob.length());
                //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (BUFFER_SIZE + 1, data1);
    blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    //insert it into table
    pstmt.setInt (1, 3);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate ();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                            "id = 3");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //test truncate
    blob.truncate (BUFFER_SIZE);
    assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 9
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)"+ getSuffix());
  stmt.close();
  
  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  pstmt.setInt (1,1);
  Blob blob = con.createBlob();
  //add 1024 bytes
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  blob.setBytes (1, data);
  assertEquals (BUFFER_SIZE, blob.length());
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE, blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (UPDATE_SIZE, data1);
  byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
 
  //update it at the end
  blob.setBytes (BUFFER_SIZE + 1, data1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //insert the blob and test again
  pstmt.setInt (1, 2);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  rs = stmt.executeQuery("select data from blobtest where " +
                         "id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //test truncate on small size blob
  blob = con.createBlob();
  data = new byte [100];
  for (int i = 0; i < 100; i++) {
    data [i] = (byte) i;
  }
  blob.setBytes (1, data);
  assertEquals (blob.length(), 100);
  blob.truncate (50);
  assertEquals (blob.length(), 50);
  blob.setBytes (1, data);
  assertEquals ("set failed", blob.length(), 100);
  blob.truncate (50);
  assertEquals ("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
  
  stmt = con.createStatement();
  stmt.execute ("drop table blobtest");
  this.waitTillAllClear();
  stmt.close();
}
 
Example 10
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)" + getSuffix());
  stmt.close();

  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  //now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes (i * BUFFER_SIZE + 1, data);
  assertEquals (BUFFER_SIZE * 5 , blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (BUFFER_SIZE + 1, data1);
  blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  //insert it into table
  pstmt.setInt (1, 3);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate ();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                                   "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  //test truncate
  blob.truncate (BUFFER_SIZE);
  assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}
 
Example 11
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  pstmt.setInt(1, 1);
  Blob blob = con.createBlob();
  // add 1024 bytes
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  blob.setBytes(1, data);
  assertEquals(BUFFER_SIZE, blob.length());
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(UPDATE_SIZE, data1);
  byte[] data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // update it at the end
  blob.setBytes(BUFFER_SIZE + 1, data1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // insert the blob and test again
  pstmt.setInt(1, 2);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();
  rs = stmt.executeQuery("select data from blobtest where id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);

  // test truncate on small size blob
  blob = con.createBlob();
  data = new byte[100];
  for (int i = 0; i < 100; i++) {
    data[i] = (byte)i;
  }
  blob.setBytes(1, data);
  assertEquals(blob.length(), 100);
  blob.truncate(50);
  assertEquals(blob.length(), 50);
  blob.setBytes(1, data);
  assertEquals("set failed", blob.length(), 100);
  blob.truncate(50);
  assertEquals("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();

  stmt = con.createStatement();
  stmt.execute("drop table blobtest");
  stmt.close();
}
 
Example 12
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  // now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes(i * BUFFER_SIZE + 1, data);
  assertEquals(BUFFER_SIZE * 5, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(BUFFER_SIZE + 1, data1);
  blob.setBytes(BUFFER_SIZE * 5 + 1, data1);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  // insert it into table
  pstmt.setInt(1, 3);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where "
      + "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  byte[] data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // test truncate
  blob.truncate(BUFFER_SIZE);
  assertEquals("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}
 
Example 13
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    pstmt.setInt (1,1);
    Blob blob = con.createBlob();
    //add 1024 bytes
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
    blob.setBytes (1, data);
    assertEquals (BUFFER_SIZE, blob.length());
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(
            "select data from blobtest where id = 1");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE, blob.length());
    //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (UPDATE_SIZE, data1);
    byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //update it at the end
    blob.setBytes (BUFFER_SIZE + 1, data1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //insert the blob and test again
    pstmt.setInt (1, 2);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate();
    rs = stmt.executeQuery("select data from blobtest where " +
            "id = 2");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
    data2 = blob.getBytes (100, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);

    //test truncate on small size blob
    blob = con.createBlob();
    data = new byte [100];
    for (int i = 0; i < 100; i++) {
        data [i] = (byte) i;
    }
    blob.setBytes (1, data);
    assertEquals (blob.length(), 100);
    blob.truncate (50);
    assertEquals (blob.length(), 50);
    blob.setBytes (1, data);
    assertEquals ("set failed", blob.length(), 100);
    blob.truncate (50);
    assertEquals ("truncation failed", blob.length(), 50);
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 14
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob () throws SQLException {
    Connection con = getConnection();
    con.setAutoCommit (false);
    PreparedStatement pstmt = con.prepareStatement("insert into " +
            "blobtest (id, data) values (?,?)");
    Blob blob = con.createBlob();
    byte [] data = new byte [BUFFER_SIZE];
    for (int i = 0; i < BUFFER_SIZE; i++) {
        data [i] = (byte) (i % 255);
    }
 //now add more than 4k so file get in use
    for (int i = 0; i < 5; i++)
        blob.setBytes (i * BUFFER_SIZE + 1, data);
    assertEquals (BUFFER_SIZE * 5 , blob.length());
                //update blob in the middle
    byte [] data1 = new byte [UPDATE_SIZE];
    for (int i = 0; i < UPDATE_SIZE; i++)
        data1 [i] = 120;//just any value
    blob.setBytes (BUFFER_SIZE + 1, data1);
    blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    //insert it into table
    pstmt.setInt (1, 3);
    pstmt.setBlob (2, blob);
    pstmt.executeUpdate ();
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                            "id = 3");
    assertEquals(true, rs.next());
    blob = rs.getBlob (1);
    byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
    assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
    for (int i = 0; i < UPDATE_SIZE; i++)
        assertEquals (data1 [i], data2 [i]);
    //test truncate
    blob.truncate (BUFFER_SIZE);
    assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
    rs.close();
    con.commit();
    stmt.close();
    pstmt.close();
}
 
Example 15
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob () throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)"+ getSuffix());
  stmt.close();
  
  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  pstmt.setInt (1,1);
  Blob blob = con.createBlob();
  //add 1024 bytes
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  blob.setBytes (1, data);
  assertEquals (BUFFER_SIZE, blob.length());
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE, blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (UPDATE_SIZE, data1);
  byte [] data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
 
  //update it at the end
  blob.setBytes (BUFFER_SIZE + 1, data1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //insert the blob and test again
  pstmt.setInt (1, 2);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate();
  
  rs = stmt.executeQuery("select data from blobtest where " +
                         "id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  assertEquals (BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes (100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  
  //test truncate on small size blob
  blob = con.createBlob();
  data = new byte [100];
  for (int i = 0; i < 100; i++) {
    data [i] = (byte) i;
  }
  blob.setBytes (1, data);
  assertEquals (blob.length(), 100);
  blob.truncate (50);
  assertEquals (blob.length(), 50);
  blob.setBytes (1, data);
  assertEquals ("set failed", blob.length(), 100);
  blob.truncate (50);
  assertEquals ("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
  
  stmt = con.createStatement();
  stmt.execute ("drop table blobtest");
  this.waitTillAllClear();
  stmt.close();
}
 
Example 16
Source File: BlobSetMethodsTest.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws SQLException {
  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute ("create table blobtest (id integer, data Blob)" + getSuffix());
  stmt.close();

  con.setAutoCommit (false);
  PreparedStatement pstmt = con.prepareStatement("insert into " +
                                                 "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte [] data = new byte [BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data [i] = (byte) (i % 255);
  }
  //now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes (i * BUFFER_SIZE + 1, data);
  assertEquals (BUFFER_SIZE * 5 , blob.length());
  //update blob in the middle
  byte [] data1 = new byte [UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1 [i] = 120;//just any value
  blob.setBytes (BUFFER_SIZE + 1, data1);
  blob.setBytes (BUFFER_SIZE * 5 + 1, data1);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  //insert it into table
  pstmt.setInt (1, 3);
  pstmt.setBlob (2, blob);
  pstmt.executeUpdate ();
  
  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where " +
                                   "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob (1);
  byte [] data2 = blob.getBytes (BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals (5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  data2 = blob.getBytes (5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals (data1 [i], data2 [i]);
  //test truncate
  blob.truncate (BUFFER_SIZE);
  assertEquals ("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}
 
Example 17
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * tests set bytes method of blob in memory only mode (less than 4k)
 */
public void testSetBytesSmallBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  pstmt.setInt(1, 1);
  Blob blob = con.createBlob();
  // add 1024 bytes
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  blob.setBytes(1, data);
  assertEquals(BUFFER_SIZE, blob.length());
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where id = 1");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(UPDATE_SIZE, data1);
  byte[] data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // update it at the end
  blob.setBytes(BUFFER_SIZE + 1, data1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // insert the blob and test again
  pstmt.setInt(1, 2);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();
  rs = stmt.executeQuery("select data from blobtest where id = 2");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  assertEquals(BUFFER_SIZE + UPDATE_SIZE, blob.length());
  data2 = blob.getBytes(100, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);

  // test truncate on small size blob
  blob = con.createBlob();
  data = new byte[100];
  for (int i = 0; i < 100; i++) {
    data[i] = (byte)i;
  }
  blob.setBytes(1, data);
  assertEquals(blob.length(), 100);
  blob.truncate(50);
  assertEquals(blob.length(), 50);
  blob.setBytes(1, data);
  assertEquals("set failed", blob.length(), 100);
  blob.truncate(50);
  assertEquals("truncation failed", blob.length(), 50);
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();

  stmt = con.createStatement();
  stmt.execute("drop table blobtest");
  stmt.close();
}
 
Example 18
Source File: BlobTestsDUnit.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Tests large blob (more than 4k) to ensure LOBStreamControl uses file.
 */
public void testSetBytesLargeBlob() throws Exception {
  startVMs(1, 4);

  Connection con = TestUtil.getConnection();
  Statement stmt = con.createStatement();
  stmt.execute("create table blobtest (id integer, data Blob)");
  stmt.close();

  con.setAutoCommit(false);
  PreparedStatement pstmt = con.prepareStatement("insert into "
      + "blobtest (id, data) values (?,?)");
  Blob blob = con.createBlob();
  byte[] data = new byte[BUFFER_SIZE];
  for (int i = 0; i < BUFFER_SIZE; i++) {
    data[i] = (byte)(i % 255);
  }
  // now add more than 4k so file get in use
  for (int i = 0; i < 5; i++)
    blob.setBytes(i * BUFFER_SIZE + 1, data);
  assertEquals(BUFFER_SIZE * 5, blob.length());
  // update blob in the middle
  byte[] data1 = new byte[UPDATE_SIZE];
  for (int i = 0; i < UPDATE_SIZE; i++)
    data1[i] = 120;// just any value
  blob.setBytes(BUFFER_SIZE + 1, data1);
  blob.setBytes(BUFFER_SIZE * 5 + 1, data1);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  // insert it into table
  pstmt.setInt(1, 3);
  pstmt.setBlob(2, blob);
  pstmt.executeUpdate();

  stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("select data from blobtest where "
      + "id = 3");
  assertEquals(true, rs.next());
  blob = rs.getBlob(1);
  byte[] data2 = blob.getBytes(BUFFER_SIZE + 1, UPDATE_SIZE);
  assertEquals(5 * BUFFER_SIZE + UPDATE_SIZE, blob.length());
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  data2 = blob.getBytes(5 * BUFFER_SIZE + 1, UPDATE_SIZE);
  for (int i = 0; i < UPDATE_SIZE; i++)
    assertEquals(data1[i], data2[i]);
  // test truncate
  blob.truncate(BUFFER_SIZE);
  assertEquals("truncate failed", BUFFER_SIZE, blob.length());
  rs.close();
  con.commit();
  stmt.close();
  pstmt.close();
}