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);
}
}
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);
}
}
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;
}
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");
}
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");
}
Aggregations