use of org.h2.util.Cache in project h2database by h2database.
the class TestFileLockSerialized method testBigDatabase.
private void testBigDatabase(boolean withCache) {
boolean longRun = false;
final int howMuchRows = longRun ? 2000000 : 500000;
deleteDb("fileLockSerialized");
int cacheSizeKb = withCache ? 5000 : 0;
final CountDownLatch importFinishedLatch = new CountDownLatch(1);
final CountDownLatch select1FinishedLatch = new CountDownLatch(1);
final String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized" + ";FILE_LOCK=SERIALIZED" + ";OPEN_NEW=TRUE" + ";CACHE_SIZE=" + cacheSizeKb;
final Task importUpdateTask = new Task() {
@Override
public void call() throws Exception {
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
stat.execute("create table test(id int, id2 int)");
for (int i = 0; i < howMuchRows; i++) {
stat.execute("insert into test values(" + i + ", " + i + ")");
}
importFinishedLatch.countDown();
select1FinishedLatch.await();
stat.execute("update test set id2=999 where id=500");
conn.close();
}
};
importUpdateTask.execute();
Task selectTask = new Task() {
@Override
public void call() throws Exception {
Connection conn = getConnection(url);
Statement stat = conn.createStatement();
importFinishedLatch.await();
ResultSet rs = stat.executeQuery("select id2 from test where id=500");
assertTrue(rs.next());
assertEquals(500, rs.getInt(1));
rs.close();
select1FinishedLatch.countDown();
// wait until the other task finished
importUpdateTask.get();
// can't use the exact same query, otherwise it would use
// the query cache
rs = stat.executeQuery("select id2 from test where id=500+0");
assertTrue(rs.next());
assertEquals(999, rs.getInt(1));
rs.close();
conn.close();
}
};
selectTask.execute();
importUpdateTask.get();
selectTask.get();
deleteDb("fileLockSerialized");
}
use of org.h2.util.Cache 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.util.Cache in project h2database by h2database.
the class TestSpatial method testInPlaceUpdate.
/**
* If the user mutate the geometry of the object, the object cache must not
* be updated.
*/
private void testInPlaceUpdate() throws SQLException {
try (Connection conn = getConnection(URL)) {
ResultSet rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
assertTrue(rs.next());
// Mutate the geometry
((Geometry) rs.getObject(1)).apply(new AffineTransformation(1, 0, 1, 1, 0, 1));
rs.close();
rs = conn.createStatement().executeQuery("SELECT 'POINT(1 1)'::geometry");
assertTrue(rs.next());
// Check if the geometry is the one requested
assertEquals(1, ((Point) rs.getObject(1)).getX());
assertEquals(1, ((Point) rs.getObject(1)).getY());
rs.close();
}
}
use of org.h2.util.Cache in project h2database by h2database.
the class ModelsTest method testTableUpgrade.
private void testTableUpgrade() {
Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
// insert first, this will create version record automatically
List<SupportedTypes> original = SupportedTypes.createList();
db.insertAll(original);
// reset the dbUpgrader (clears the update check cache)
TestDbUpgrader dbUpgrader = new TestDbUpgrader();
db.setDbUpgrader(dbUpgrader);
SupportedTypes2 s2 = new SupportedTypes2();
List<SupportedTypes2> types = db.from(s2).select();
assertEquals(10, types.size());
assertEquals(1, dbUpgrader.oldVersion.get());
assertEquals(2, dbUpgrader.newVersion.get());
db.close();
}
use of org.h2.util.Cache in project h2database by h2database.
the class TestView method testSubQueryViewIndexCache.
private void testSubQueryViewIndexCache() throws SQLException {
if (config.networked) {
return;
}
Connection conn = getConnection("view");
Statement stat = conn.createStatement();
stat.execute("drop table test if exists");
stat.execute("create table test(id int primary key, " + "name varchar(25) unique, age int unique)");
// check that initial cache size is empty
Session s = (Session) ((JdbcConnection) conn).getSession();
s.clearViewIndexCache();
assertTrue(s.getViewIndexCache(true).isEmpty());
assertTrue(s.getViewIndexCache(false).isEmpty());
// create view command should not affect caches
stat.execute("create view v as select * from test");
assertTrue(s.getViewIndexCache(true).isEmpty());
assertTrue(s.getViewIndexCache(false).isEmpty());
// check view index cache
stat.executeQuery("select * from v where id > 0").next();
int size1 = s.getViewIndexCache(false).size();
assertTrue(size1 > 0);
assertTrue(s.getViewIndexCache(true).isEmpty());
stat.executeQuery("select * from v where name = 'xyz'").next();
int size2 = s.getViewIndexCache(false).size();
assertTrue(size2 > size1);
assertTrue(s.getViewIndexCache(true).isEmpty());
// check we did not add anything to view cache if we run a sub-query
stat.executeQuery("select * from (select * from test) where age = 17").next();
int size3 = s.getViewIndexCache(false).size();
assertEquals(size2, size3);
assertTrue(s.getViewIndexCache(true).isEmpty());
// check clear works
s.clearViewIndexCache();
assertTrue(s.getViewIndexCache(false).isEmpty());
assertTrue(s.getViewIndexCache(true).isEmpty());
// drop everything
stat.execute("drop view v");
stat.execute("drop table test");
conn.close();
}
Aggregations