use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestCache method testCache.
private void testCache() {
out = "";
Cache c = CacheLRU.getCache(this, "LRU", 16);
for (int i = 0; i < 20; i++) {
c.put(new Obj(i));
}
assertEquals("flush 0 flush 1 flush 2 flush 3 ", out);
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestTransactionStore method testCompareWithPostgreSQL.
private void testCompareWithPostgreSQL() throws Exception {
ArrayList<Statement> statements = New.arrayList();
ArrayList<Transaction> transactions = New.arrayList();
ArrayList<TransactionMap<Integer, String>> maps = New.arrayList();
int connectionCount = 3, opCount = 1000, rowCount = 10;
try {
Class.forName("org.postgresql.Driver");
for (int i = 0; i < connectionCount; i++) {
Connection conn = DriverManager.getConnection("jdbc:postgresql:test?loggerLevel=OFF", "sa", "sa");
statements.add(conn.createStatement());
}
} catch (Exception e) {
// database not installed - ok
return;
}
statements.get(0).execute("drop table if exists test cascade");
statements.get(0).execute("create table test(id int primary key, name varchar(255))");
MVStore s = MVStore.open(null);
TransactionStore ts = new TransactionStore(s);
ts.init();
for (int i = 0; i < connectionCount; i++) {
Statement stat = statements.get(i);
// 100 ms to avoid blocking (the test is single threaded)
stat.execute("set statement_timeout to 100");
Connection c = stat.getConnection();
c.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
c.setAutoCommit(false);
Transaction transaction = ts.begin();
transactions.add(transaction);
TransactionMap<Integer, String> map;
map = transaction.openMap("test");
maps.add(map);
}
StringBuilder buff = new StringBuilder();
Random r = new Random(1);
try {
for (int i = 0; i < opCount; i++) {
int connIndex = r.nextInt(connectionCount);
Statement stat = statements.get(connIndex);
Transaction transaction = transactions.get(connIndex);
TransactionMap<Integer, String> map = maps.get(connIndex);
if (transaction == null) {
transaction = ts.begin();
map = transaction.openMap("test");
transactions.set(connIndex, transaction);
maps.set(connIndex, map);
// read all data, to get a snapshot
ResultSet rs = stat.executeQuery("select * from test order by id");
buff.append(i).append(": [" + connIndex + "]=");
int size = 0;
while (rs.next()) {
buff.append(' ');
int k = rs.getInt(1);
String v = rs.getString(2);
buff.append(k).append(':').append(v);
assertEquals(v, map.get(k));
size++;
}
buff.append('\n');
if (size != map.sizeAsLong()) {
assertEquals(size, map.sizeAsLong());
}
}
int x = r.nextInt(rowCount);
int y = r.nextInt(rowCount);
buff.append(i).append(": [" + connIndex + "]: ");
ResultSet rs = null;
switch(r.nextInt(7)) {
case 0:
buff.append("commit");
stat.getConnection().commit();
transaction.commit();
transactions.set(connIndex, null);
break;
case 1:
buff.append("rollback");
stat.getConnection().rollback();
transaction.rollback();
transactions.set(connIndex, null);
break;
case 2:
// insert or update
String old = map.get(x);
if (old == null) {
buff.append("insert " + x + "=" + y);
if (map.tryPut(x, "" + y)) {
stat.execute("insert into test values(" + x + ", '" + y + "')");
} else {
buff.append(" -> row was locked");
// the statement would time out in PostgreSQL
// TODO test sometimes if timeout occurs
}
} else {
buff.append("update " + x + "=" + y + " (old:" + old + ")");
if (map.tryPut(x, "" + y)) {
int c = stat.executeUpdate("update test set name = '" + y + "' where id = " + x);
assertEquals(1, c);
} else {
buff.append(" -> row was locked");
// the statement would time out in PostgreSQL
// TODO test sometimes if timeout occurs
}
}
break;
case 3:
buff.append("delete " + x);
try {
int c = stat.executeUpdate("delete from test where id = " + x);
if (c == 1) {
map.remove(x);
} else {
assertNull(map.get(x));
}
} catch (SQLException e) {
assertNotNull(map.get(x));
assertFalse(map.tryRemove(x));
// PostgreSQL needs to rollback
buff.append(" -> rollback");
stat.getConnection().rollback();
transaction.rollback();
transactions.set(connIndex, null);
}
break;
case 4:
case 5:
case 6:
rs = stat.executeQuery("select * from test where id = " + x);
String expected = rs.next() ? rs.getString(2) : null;
buff.append("select " + x + "=" + expected);
assertEquals("i:" + i, expected, map.get(x));
break;
}
buff.append('\n');
}
} catch (Exception e) {
e.printStackTrace();
fail(buff.toString());
}
for (Statement stat : statements) {
stat.getConnection().close();
}
ts.close();
s.close();
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestConcurrent method copyFileSlowly.
private static void copyFileSlowly(FileChannel file, long length, OutputStream out) throws Exception {
file.position(0);
InputStream in = new BufferedInputStream(new FileChannelInputStream(file, false));
for (int j = 0; j < length; j++) {
int x = in.read();
if (x < 0) {
break;
}
out.write(x);
}
in.close();
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestMVRTree method render.
private static void render(MVRTreeMap<String> r, String fileName) {
int width = 1000, height = 500;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) img.getGraphics();
g2d.setBackground(Color.WHITE);
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);
SpatialKey b = new SpatialKey(0, Float.MAX_VALUE, Float.MIN_VALUE, Float.MAX_VALUE, Float.MIN_VALUE);
for (SpatialKey x : r.keySet()) {
b.setMin(0, Math.min(b.min(0), x.min(0)));
b.setMin(1, Math.min(b.min(1), x.min(1)));
b.setMax(0, Math.max(b.max(0), x.max(0)));
b.setMax(1, Math.max(b.max(1), x.max(1)));
}
// System.out.println(b);
for (SpatialKey x : r.keySet()) {
int[] rect = scale(b, x, width, height);
g2d.drawRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]);
String s = r.get(x);
g2d.drawChars(s.toCharArray(), 0, s.length(), rect[0], rect[1] - 4);
}
g2d.setColor(Color.red);
ArrayList<SpatialKey> list = New.arrayList();
r.addNodeKeys(list, r.getRoot());
for (SpatialKey x : list) {
int[] rect = scale(b, x, width, height);
g2d.drawRect(rect[0], rect[1], rect[2] - rect[0], rect[3] - rect[1]);
}
ImageWriter out = ImageIO.getImageWritersByFormatName("png").next();
try {
out.setOutput(new FileImageOutputStream(new File(fileName)));
out.write(img);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.h2.dev.util.BinaryArithmeticStream.Out in project h2database by h2database.
the class TestCallableStatement method testPrepare.
private void testPrepare(Connection conn) throws Exception {
Statement stat = conn.createStatement();
CallableStatement call;
ResultSet rs;
stat.execute("CREATE TABLE TEST(ID INT, NAME VARCHAR)");
call = conn.prepareCall("INSERT INTO TEST VALUES(?, ?)");
call.setInt(1, 1);
call.setString(2, "Hello");
call.execute();
call = conn.prepareCall("SELECT * FROM TEST", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
rs = call.executeQuery();
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2));
assertFalse(rs.next());
call = conn.prepareCall("SELECT * FROM TEST", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.HOLD_CURSORS_OVER_COMMIT);
rs = call.executeQuery();
rs.next();
assertEquals(1, rs.getInt(1));
assertEquals("Hello", rs.getString(2));
assertFalse(rs.next());
stat.execute("CREATE ALIAS testCall FOR \"" + getClass().getName() + ".testCall\"");
call = conn.prepareCall("{CALL testCall(?, ?, ?, ?)}");
call.setInt("A", 50);
call.setString("B", "abc");
long t = System.currentTimeMillis();
call.setTimestamp("C", new Timestamp(t));
call.setTimestamp("D", Timestamp.valueOf("2001-02-03 10:20:30.0"));
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter("B", Types.VARCHAR);
call.executeUpdate();
try {
call.getTimestamp("C");
fail("not registered out parameter accessible");
} catch (SQLException e) {
// expected exception
}
call.registerOutParameter(3, Types.TIMESTAMP);
call.registerOutParameter(4, Types.TIMESTAMP);
call.executeUpdate();
assertEquals(t + 1, call.getTimestamp(3).getTime());
assertEquals(t + 1, call.getTimestamp("C").getTime());
assertEquals("2001-02-03 10:20:30.0", call.getTimestamp(4).toString());
assertEquals("2001-02-03 10:20:30.0", call.getTimestamp("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("2001-02-03T10:20:30", call.getObject(4, LocalDateTimeUtils.LOCAL_DATE_TIME).toString());
assertEquals("2001-02-03T10:20:30", call.getObject("D", LocalDateTimeUtils.LOCAL_DATE_TIME).toString());
}
assertEquals("10:20:30", call.getTime(4).toString());
assertEquals("10:20:30", call.getTime("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("10:20:30", call.getObject(4, LocalDateTimeUtils.LOCAL_TIME).toString());
assertEquals("10:20:30", call.getObject("D", LocalDateTimeUtils.LOCAL_TIME).toString());
}
assertEquals("2001-02-03", call.getDate(4).toString());
assertEquals("2001-02-03", call.getDate("D").toString());
if (LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("2001-02-03", call.getObject(4, LocalDateTimeUtils.LOCAL_DATE).toString());
assertEquals("2001-02-03", call.getObject("D", LocalDateTimeUtils.LOCAL_DATE).toString());
}
assertEquals(100, call.getInt(1));
assertEquals(100, call.getInt("A"));
assertEquals(100, call.getLong(1));
assertEquals(100, call.getLong("A"));
assertEquals("100", call.getBigDecimal(1).toString());
assertEquals("100", call.getBigDecimal("A").toString());
assertEquals(100, call.getFloat(1));
assertEquals(100, call.getFloat("A"));
assertEquals(100, call.getDouble(1));
assertEquals(100, call.getDouble("A"));
assertEquals(100, call.getByte(1));
assertEquals(100, call.getByte("A"));
assertEquals(100, call.getShort(1));
assertEquals(100, call.getShort("A"));
assertTrue(call.getBoolean(1));
assertTrue(call.getBoolean("A"));
assertEquals("ABC", call.getString(2));
Reader r = call.getCharacterStream(2);
assertEquals("ABC", IOUtils.readStringAndClose(r, -1));
r = call.getNCharacterStream(2);
assertEquals("ABC", IOUtils.readStringAndClose(r, -1));
assertEquals("ABC", call.getString("B"));
assertEquals("ABC", call.getNString(2));
assertEquals("ABC", call.getNString("B"));
assertEquals("ABC", call.getClob(2).getSubString(1, 3));
assertEquals("ABC", call.getClob("B").getSubString(1, 3));
assertEquals("ABC", call.getNClob(2).getSubString(1, 3));
assertEquals("ABC", call.getNClob("B").getSubString(1, 3));
try {
call.getString(100);
fail("incorrect parameter index value");
} catch (SQLException e) {
// expected exception
}
try {
call.getString(0);
fail("incorrect parameter index value");
} catch (SQLException e) {
// expected exception
}
try {
call.getBoolean("X");
fail("incorrect parameter name value");
} catch (SQLException e) {
// expected exception
}
call.setCharacterStream("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setCharacterStream("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setCharacterStream("B", new StringReader("xyz-"), 3L);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz".getBytes(StandardCharsets.UTF_8)));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setAsciiStream("B", new ByteArrayInputStream("xyz-".getBytes(StandardCharsets.UTF_8)), 3L);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setClob("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setClob("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNClob("B", new StringReader("xyz"));
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNClob("B", new StringReader("xyz-"), 3);
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setString("B", "xyz");
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
call.setNString("B", "xyz");
call.executeUpdate();
assertEquals("XYZ", call.getString("B"));
// test for exceptions after closing
call.close();
assertThrows(ErrorCode.OBJECT_CLOSED, call).executeUpdate();
assertThrows(ErrorCode.OBJECT_CLOSED, call).registerOutParameter(1, Types.INTEGER);
assertThrows(ErrorCode.OBJECT_CLOSED, call).getString("X");
}
Aggregations