Search in sources :

Example 41 with Cache

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");
}
Also used : Task(org.h2.util.Task) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 42 with Cache

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);
}
Also used : Cache(org.h2.util.Cache)

Example 43 with Cache

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();
    }
}
Also used : ValueGeometry(org.h2.value.ValueGeometry) Geometry(org.locationtech.jts.geom.Geometry) Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) AffineTransformation(org.locationtech.jts.geom.util.AffineTransformation)

Example 44 with Cache

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();
}
Also used : SupportedTypes2(org.h2.test.jaqu.SupportedTypes.SupportedTypes2) Db(org.h2.jaqu.Db)

Example 45 with Cache

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();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Session(org.h2.engine.Session)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 Connection (java.sql.Connection)11 PreparedStatement (java.sql.PreparedStatement)10 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)10 ResultSet (java.sql.ResultSet)9 SQLException (java.sql.SQLException)9 CacheException (javax.cache.CacheException)9 Statement (java.sql.Statement)7 ArrayList (java.util.ArrayList)7 IgniteException (org.apache.ignite.IgniteException)7 List (java.util.List)5 UUID (java.util.UUID)5 IgniteSystemProperties.getString (org.apache.ignite.IgniteSystemProperties.getString)5 ClusterNode (org.apache.ignite.cluster.ClusterNode)5 Prepared (org.h2.command.Prepared)5 IntArray (org.h2.util.IntArray)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 BinaryObject (org.apache.ignite.binary.BinaryObject)4