Search in sources :

Example 21 with Cache

use of org.h2.util.Cache in project h2database by h2database.

the class DateTimeFunctions method getDateFormat.

private static SimpleDateFormat getDateFormat(String format, String locale, String timeZone) {
    try {
        // currently, a new instance is create for each call
        // however, could cache the last few instances
        SimpleDateFormat df;
        if (locale == null) {
            df = new SimpleDateFormat(format);
        } else {
            Locale l = new Locale(locale);
            df = new SimpleDateFormat(format, l);
        }
        if (timeZone != null) {
            df.setTimeZone(TimeZone.getTimeZone(timeZone));
        }
        return df;
    } catch (Exception e) {
        throw DbException.get(ErrorCode.PARSE_ERROR_1, e, format + "/" + locale + "/" + timeZone);
    }
}
Also used : Locale(java.util.Locale) SimpleDateFormat(java.text.SimpleDateFormat) DbException(org.h2.message.DbException)

Example 22 with Cache

use of org.h2.util.Cache in project h2database by h2database.

the class FileStore method open.

/**
 * Try to open the file.
 *
 * @param fileName the file name
 * @param readOnly whether the file should only be opened in read-only mode,
 *            even if the file is writable
 * @param encryptionKey the encryption key, or null if encryption is not
 *            used
 */
public void open(String fileName, boolean readOnly, char[] encryptionKey) {
    if (file != null) {
        return;
    }
    if (fileName != null) {
        // ensure the Cache file system is registered
        FilePathCache.INSTANCE.getScheme();
        FilePath p = FilePath.get(fileName);
        // if no explicit scheme was specified, NIO is used
        if (p instanceof FilePathDisk && !fileName.startsWith(p.getScheme() + ":")) {
            // ensure the NIO file system is registered
            FilePathNio.class.getName();
            fileName = "nio:" + fileName;
        }
    }
    this.fileName = fileName;
    FilePath f = FilePath.get(fileName);
    FilePath parent = f.getParent();
    if (parent != null && !parent.exists()) {
        throw DataUtils.newIllegalArgumentException("Directory does not exist: {0}", parent);
    }
    if (f.exists() && !f.canWrite()) {
        readOnly = true;
    }
    this.readOnly = readOnly;
    try {
        file = f.open(readOnly ? "r" : "rw");
        if (encryptionKey != null) {
            byte[] key = FilePathEncrypt.getPasswordBytes(encryptionKey);
            encryptedFile = file;
            file = new FilePathEncrypt.FileEncrypt(fileName, key, file);
        }
        try {
            if (readOnly) {
                fileLock = file.tryLock(0, Long.MAX_VALUE, true);
            } else {
                fileLock = file.tryLock();
            }
        } catch (OverlappingFileLockException e) {
            throw DataUtils.newIllegalStateException(DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName, e);
        }
        if (fileLock == null) {
            throw DataUtils.newIllegalStateException(DataUtils.ERROR_FILE_LOCKED, "The file is locked: {0}", fileName);
        }
        fileSize = file.size();
    } catch (IOException e) {
        throw DataUtils.newIllegalStateException(DataUtils.ERROR_READING_FAILED, "Could not open file {0}", fileName, e);
    }
}
Also used : FilePath(org.h2.store.fs.FilePath) FilePathEncrypt(org.h2.store.fs.FilePathEncrypt) IOException(java.io.IOException) FilePathDisk(org.h2.store.fs.FilePathDisk) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 23 with Cache

use of org.h2.util.Cache in project h2database by h2database.

the class MVStore method readPageChunkReferences.

private PageChildren readPageChunkReferences(int mapId, long pos, int parentChunk) {
    if (DataUtils.getPageType(pos) == DataUtils.PAGE_TYPE_LEAF) {
        return null;
    }
    PageChildren r;
    if (cacheChunkRef != null) {
        r = cacheChunkRef.get(pos);
    } else {
        r = null;
    }
    if (r == null) {
        // if possible, create it from the cached page
        if (cache != null) {
            Page p = cache.get(pos);
            if (p != null) {
                r = new PageChildren(p);
            }
        }
        if (r == null) {
            // page was not cached: read the data
            Chunk c = getChunk(pos);
            long filePos = c.block * BLOCK_SIZE;
            filePos += DataUtils.getPageOffset(pos);
            if (filePos < 0) {
                throw DataUtils.newIllegalStateException(DataUtils.ERROR_FILE_CORRUPT, "Negative position {0}; p={1}, c={2}", filePos, pos, c.toString());
            }
            long maxPos = (c.block + c.len) * BLOCK_SIZE;
            r = PageChildren.read(fileStore, pos, mapId, filePos, maxPos);
        }
        r.removeDuplicateChunkReferences();
        if (cacheChunkRef != null) {
            cacheChunkRef.put(pos, r, r.getMemory());
        }
    }
    if (r.children.length == 0) {
        int chunk = DataUtils.getPageChunkId(pos);
        if (chunk == parentChunk) {
            return null;
        }
    }
    return r;
}
Also used : PageChildren(org.h2.mvstore.Page.PageChildren)

Example 24 with Cache

use of org.h2.util.Cache in project h2database by h2database.

the class TestStatement method testPreparedStatement.

private void testPreparedStatement() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar(255))");
    stat.execute("insert into test values(1, 'Hello')");
    stat.execute("insert into test values(2, 'World')");
    PreparedStatement ps = conn.prepareStatement("select name from test where id in (select id from test where name REGEXP ?)");
    ps.setString(1, "Hello");
    ResultSet rs = ps.executeQuery();
    assertTrue(rs.next());
    assertEquals("Hello", rs.getString("name"));
    assertFalse(rs.next());
    ps.setString(1, "World");
    rs = ps.executeQuery();
    assertTrue(rs.next());
    assertEquals("World", rs.getString("name"));
    assertFalse(rs.next());
    // Changes the table structure
    stat.execute("create index t_id on test(name)");
    // Test the prepared statement again to check if the internal cache attributes were reset
    ps.setString(1, "Hello");
    rs = ps.executeQuery();
    assertTrue(rs.next());
    assertEquals("Hello", rs.getString("name"));
    assertFalse(rs.next());
    ps.setString(1, "World");
    rs = ps.executeQuery();
    assertTrue(rs.next());
    assertEquals("World", rs.getString("name"));
    assertFalse(rs.next());
    ps = conn.prepareStatement("insert into test values(?, ?)");
    ps.setInt(1, 3);
    ps.setString(2, "v3");
    ps.addBatch();
    ps.setInt(1, 4);
    ps.setString(2, "v4");
    ps.addBatch();
    assertTrue(Arrays.equals(new int[] { 1, 1 }, ps.executeBatch()));
    ps.setInt(1, 5);
    ps.setString(2, "v5");
    ps.addBatch();
    ps.setInt(1, 6);
    ps.setString(2, "v6");
    ps.addBatch();
    assertTrue(Arrays.equals(new long[] { 1, 1 }, ((JdbcStatementBackwardsCompat) ps).executeLargeBatch()));
    ps.setInt(1, 7);
    ps.setString(2, "v7");
    assertEquals(1, ps.executeUpdate());
    assertEquals(1, ps.getUpdateCount());
    ps.setInt(1, 8);
    ps.setString(2, "v8");
    assertEquals(1, ((JdbcPreparedStatementBackwardsCompat) ps).executeLargeUpdate());
    assertEquals(1, ((JdbcStatementBackwardsCompat) ps).getLargeUpdateCount());
    stat.execute("drop table test");
}
Also used : JdbcStatementBackwardsCompat(org.h2.jdbc.JdbcStatementBackwardsCompat) PreparedStatement(java.sql.PreparedStatement) JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 25 with Cache

use of org.h2.util.Cache in project h2database by h2database.

the class TestView method testViewAlterAndCommandCache.

/**
 * Make sure that when we change a view, that change in reflected in other
 * sessions command cache.
 */
private void testViewAlterAndCommandCache() throws SQLException {
    deleteDb("view");
    Connection conn = getConnection("view");
    Statement stat = conn.createStatement();
    stat.execute("create table t0(id int primary key)");
    stat.execute("create table t1(id int primary key)");
    stat.execute("insert into t0 values(0)");
    stat.execute("insert into t1 values(1)");
    stat.execute("create view v1 as select * from t0");
    ResultSet rs = stat.executeQuery("select * from v1");
    assertTrue(rs.next());
    assertEquals(0, rs.getInt(1));
    stat.execute("create or replace view v1 as select * from t1");
    rs = stat.executeQuery("select * from v1");
    assertTrue(rs.next());
    assertEquals(1, rs.getInt(1));
    conn.close();
    deleteDb("view");
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

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