use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestMVTableEngine method testBlob.
private void testBlob() throws SQLException, IOException {
if (config.memory) {
return;
}
deleteDb(getTestName());
String dbName = getTestName() + ";MV_STORE=TRUE";
Connection conn;
Statement stat;
conn = getConnection(dbName);
stat = conn.createStatement();
stat.execute("create table test(id int, name blob)");
PreparedStatement prep = conn.prepareStatement("insert into test values(1, ?)");
prep.setBinaryStream(1, new ByteArrayInputStream(new byte[129]));
prep.execute();
conn.close();
conn = getConnection(dbName);
stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select * from test");
while (rs.next()) {
InputStream in = rs.getBinaryStream(2);
int len = 0;
while (in.read() >= 0) {
len++;
}
assertEquals(129, len);
}
conn.close();
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestTransactionStore method testStopWhileCommitting.
private void testStopWhileCommitting() throws Exception {
String fileName = getBaseDir() + "/testStopWhileCommitting.h3";
FileUtils.delete(fileName);
Random r = new Random(0);
for (int i = 0; i < 10; ) {
MVStore s;
TransactionStore ts;
Transaction tx;
TransactionMap<Integer, String> m;
s = MVStore.open(fileName);
ts = new TransactionStore(s);
ts.init();
tx = ts.begin();
s.setReuseSpace(false);
m = tx.openMap("test");
final String value = "x" + i;
for (int j = 0; j < 1000; j++) {
m.put(j, value);
}
final AtomicInteger state = new AtomicInteger();
final MVStore store = s;
final MVMap<Integer, String> other = s.openMap("other");
Task task = new Task() {
@Override
public void call() throws Exception {
for (int i = 0; !stop; i++) {
state.set(i);
other.put(i, value);
store.commit();
}
}
};
task.execute();
// wait for the task to start
while (state.get() < 1) {
Thread.yield();
}
// commit while writing in the task
tx.commit();
// wait for the task to stop
task.get();
store.close();
s = MVStore.open(fileName);
// roll back a bit, until we have some undo log entries
assertTrue(s.hasMap("undoLog"));
for (int back = 0; back < 100; back++) {
int minus = r.nextInt(10);
s.rollbackTo(Math.max(0, s.getCurrentVersion() - minus));
MVMap<?, ?> undo = s.openMap("undoLog");
if (undo.size() > 0) {
break;
}
}
// re-open the store, because we have opened
// the undoLog map with the wrong data type
s.close();
s = MVStore.open(fileName);
ts = new TransactionStore(s);
List<Transaction> list = ts.getOpenTransactions();
if (list.size() != 0) {
tx = list.get(0);
if (tx.getStatus() == Transaction.STATUS_COMMITTING) {
i++;
}
}
s.close();
FileUtils.delete(fileName);
assertFalse(FileUtils.exists(fileName));
}
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestStreamStore method testTreeStructure.
private void testTreeStructure() throws IOException {
final AtomicInteger reads = new AtomicInteger();
Map<Long, byte[]> map = new HashMap<Long, byte[]>() {
private static final long serialVersionUID = 1L;
@Override
public byte[] get(Object k) {
reads.incrementAndGet();
return super.get(k);
}
};
StreamStore store = new StreamStore(map);
store.setMinBlockSize(10);
store.setMaxBlockSize(100);
byte[] id = store.put(new ByteArrayInputStream(new byte[10000]));
InputStream in = store.get(id);
assertEquals(0, in.read(new byte[0]));
assertEquals(0, in.read());
assertEquals(3, reads.get());
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestStreamStore method testFormat.
private void testFormat() throws IOException {
Map<Long, byte[]> map = new HashMap<>();
StreamStore store = new StreamStore(map);
store.setMinBlockSize(10);
store.setMaxBlockSize(20);
store.setNextKey(123);
byte[] id;
id = store.put(new ByteArrayInputStream(new byte[200]));
assertEquals(200, store.length(id));
assertEquals("02c8018801", StringUtils.convertBytesToHex(id));
id = store.put(new ByteArrayInputStream(new byte[0]));
assertEquals("", StringUtils.convertBytesToHex(id));
id = store.put(new ByteArrayInputStream(new byte[1]));
assertEquals("000100", StringUtils.convertBytesToHex(id));
id = store.put(new ByteArrayInputStream(new byte[3]));
assertEquals("0003000000", StringUtils.convertBytesToHex(id));
id = store.put(new ByteArrayInputStream(new byte[10]));
assertEquals("010a8901", StringUtils.convertBytesToHex(id));
byte[] combined = StringUtils.convertHexToBytes("0001aa0002bbcc");
assertEquals(3, store.length(combined));
InputStream in = store.get(combined);
assertEquals(1, in.skip(1));
assertEquals(0xbb, in.read());
assertEquals(1, in.skip(1));
}
use of org.h2.dev.util.BinaryArithmeticStream.In in project h2database by h2database.
the class TestCrashAPI method getConnection.
private Connection getConnection(int seed, boolean delete) throws SQLException {
openCount++;
if (delete) {
deleteDb();
}
// can not use FILE_LOCK=NO, otherwise something could be written into
// the database in the finalize method
String add = ";MAX_QUERY_TIMEOUT=10000";
// int testing;
// if(openCount >= 32) {
// int test;
// Runtime.getRuntime().halt(0);
// System.exit(1);
// }
// System.out.println("now open " + openCount);
// add += ";TRACE_LEVEL_FILE=3";
// config.logMode = 2;
// }
String dbName = "crashApi" + seed;
String url = getURL(DIR + "/" + dbName, true) + add;
// int test;
// url += ";DB_CLOSE_ON_EXIT=FALSE";
// int test;
// url += ";TRACE_LEVEL_FILE=3";
Connection conn = null;
String fileName = "temp/backup/db-" + uniqueId++ + ".zip";
Backup.execute(fileName, getBaseDir() + "/" + DIR, dbName, true);
// close databases earlier
System.gc();
try {
conn = DriverManager.getConnection(url, "sa", getPassword(""));
// delete the backup if opening was successful
FileUtils.delete(fileName);
} catch (SQLException e) {
if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
// delete if the password changed
FileUtils.delete(fileName);
}
throw e;
}
int len = random.getInt(50);
int first = random.getInt(statements.size() - len);
int end = first + len;
Statement stat = conn.createStatement();
stat.execute("SET LOCK_TIMEOUT 10");
stat.execute("SET WRITE_DELAY 0");
if (random.nextBoolean()) {
if (random.nextBoolean()) {
double g = random.nextGaussian();
int size = (int) Math.abs(10000 * g * g);
stat.execute("SET CACHE_SIZE " + size);
} else {
stat.execute("SET CACHE_SIZE 0");
}
}
stat.execute("SCRIPT NOPASSWORDS NOSETTINGS");
for (int i = first; i < end && i < statements.size() && !stopped; i++) {
try {
stat.execute("SELECT * FROM TEST WHERE ID=1");
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
try {
stat.execute("SELECT * FROM TEST WHERE ID=1 OR ID=1");
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
String sql = statements.get(i);
try {
// if(openCount == 32) {
// int test;
// System.out.println("stop!");
// }
stat.execute(sql);
} catch (Throwable t) {
printIfBad(seed, -i, -1, t);
}
}
if (random.nextBoolean()) {
try {
conn.commit();
} catch (Throwable t) {
printIfBad(seed, 0, -1, t);
}
}
return conn;
}
Aggregations