use of org.h2.engine.Mode in project h2database by h2database.
the class AggregateDataMedian method getValue.
@Override
Value getValue(Database database, int dataType, boolean distinct) {
Value[] a = getArray();
if (a == null) {
return ValueNull.INSTANCE;
}
final CompareMode mode = database.getCompareMode();
Arrays.sort(a, new Comparator<Value>() {
@Override
public int compare(Value o1, Value o2) {
return o1.compareTo(o2, mode);
}
});
int len = a.length;
int idx = len / 2;
Value v1 = a[idx];
if ((len & 1) == 1) {
return v1.convertTo(dataType);
}
return getMedian(a[idx - 1], v1, dataType, mode);
}
use of org.h2.engine.Mode in project h2database by h2database.
the class PageStore method addMeta.
private void addMeta(Row row, Session session, boolean redo) {
int id = row.getValue(0).getInt();
int type = row.getValue(1).getInt();
int parent = row.getValue(2).getInt();
int rootPageId = row.getValue(3).getInt();
String[] options = StringUtils.arraySplit(row.getValue(4).getString(), ',', false);
String columnList = row.getValue(5).getString();
String[] columns = StringUtils.arraySplit(columnList, ',', false);
Index meta;
if (trace.isDebugEnabled()) {
trace.debug("addMeta id=" + id + " type=" + type + " root=" + rootPageId + " parent=" + parent + " columns=" + columnList);
}
if (redo && rootPageId != 0) {
// ensure the page is empty, but not used by regular data
writePage(rootPageId, createData());
allocatePage(rootPageId);
}
metaRootPageId.put(id, rootPageId);
if (type == META_TYPE_DATA_INDEX) {
CreateTableData data = new CreateTableData();
if (SysProperties.CHECK) {
if (columns == null) {
throw DbException.throwInternalError(row.toString());
}
}
for (int i = 0, len = columns.length; i < len; i++) {
Column col = new Column("C" + i, Value.INT);
data.columns.add(col);
}
data.schema = metaSchema;
data.tableName = "T" + id;
data.id = id;
data.temporary = options[2].equals("temp");
data.persistData = true;
data.persistIndexes = true;
data.create = false;
data.session = session;
RegularTable table = new RegularTable(data);
boolean binaryUnsigned = SysProperties.SORT_BINARY_UNSIGNED;
if (options.length > 3) {
binaryUnsigned = Boolean.parseBoolean(options[3]);
}
CompareMode mode = CompareMode.getInstance(options[0], Integer.parseInt(options[1]), binaryUnsigned);
table.setCompareMode(mode);
meta = table.getScanIndex(session);
} else {
Index p = metaObjects.get(parent);
if (p == null) {
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, "Table not found:" + parent + " for " + row + " meta:" + metaObjects);
}
RegularTable table = (RegularTable) p.getTable();
Column[] tableCols = table.getColumns();
int len = columns.length;
IndexColumn[] cols = new IndexColumn[len];
for (int i = 0; i < len; i++) {
String c = columns[i];
IndexColumn ic = new IndexColumn();
int idx = c.indexOf('/');
if (idx >= 0) {
String s = c.substring(idx + 1);
ic.sortType = Integer.parseInt(s);
c = c.substring(0, idx);
}
ic.column = tableCols[Integer.parseInt(c)];
cols[i] = ic;
}
IndexType indexType;
if (options[3].equals("d")) {
indexType = IndexType.createPrimaryKey(true, false);
Column[] tableColumns = table.getColumns();
for (IndexColumn indexColumn : cols) {
tableColumns[indexColumn.column.getColumnId()].setNullable(false);
}
} else {
indexType = IndexType.createNonUnique(true);
}
meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
}
PageIndex index;
if (meta instanceof MultiVersionIndex) {
index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
} else {
index = (PageIndex) meta;
}
metaObjects.put(id, index);
}
use of org.h2.engine.Mode in project h2database by h2database.
the class PageStore method addMeta.
/**
* Add the meta data of an index.
*
* @param index the index to add
* @param session the session
*/
public void addMeta(PageIndex index, Session session) {
Table table = index.getTable();
if (SysProperties.CHECK) {
if (!table.isTemporary()) {
// the Database lock before we take the PageStore lock
synchronized (database) {
synchronized (this) {
database.verifyMetaLocked(session);
}
}
}
}
synchronized (this) {
int type = index instanceof PageDataIndex ? META_TYPE_DATA_INDEX : META_TYPE_BTREE_INDEX;
IndexColumn[] columns = index.getIndexColumns();
StatementBuilder buff = new StatementBuilder();
for (IndexColumn col : columns) {
buff.appendExceptFirst(",");
int id = col.column.getColumnId();
buff.append(id);
int sortType = col.sortType;
if (sortType != 0) {
buff.append('/');
buff.append(sortType);
}
}
String columnList = buff.toString();
CompareMode mode = table.getCompareMode();
String options = mode.getName() + "," + mode.getStrength() + ",";
if (table.isTemporary()) {
options += "temp";
}
options += ",";
if (index instanceof PageDelegateIndex) {
options += "d";
}
options += "," + mode.isBinaryUnsigned();
Row row = metaTable.getTemplateRow();
row.setValue(0, ValueInt.get(index.getId()));
row.setValue(1, ValueInt.get(type));
row.setValue(2, ValueInt.get(table.getId()));
row.setValue(3, ValueInt.get(index.getRootPageId()));
row.setValue(4, ValueString.get(options));
row.setValue(5, ValueString.get(columnList));
row.setKey(index.getId() + 1);
metaIndex.add(session, row);
}
}
use of org.h2.engine.Mode in project h2database by h2database.
the class PgServerThread method initDb.
private void initDb() throws SQLException {
Statement stat = null;
try {
synchronized (server) {
// better would be: set the database to exclusive mode
boolean tableFound;
try (ResultSet rs = conn.getMetaData().getTables(null, "PG_CATALOG", "PG_VERSION", null)) {
tableFound = rs.next();
}
stat = conn.createStatement();
if (!tableFound) {
installPgCatalog(stat);
}
try (ResultSet rs = stat.executeQuery("select * from pg_catalog.pg_version")) {
if (!rs.next() || rs.getInt(1) < 2) {
// installation incomplete, or old version
installPgCatalog(stat);
} else {
// version 2 or newer: check the read version
int versionRead = rs.getInt(2);
if (versionRead > 2) {
throw DbException.throwInternalError("Incompatible PG_VERSION");
}
}
}
}
stat.execute("set search_path = PUBLIC, pg_catalog");
HashSet<Integer> typeSet = server.getTypeSet();
if (typeSet.isEmpty()) {
try (ResultSet rs = stat.executeQuery("select oid from pg_catalog.pg_type")) {
while (rs.next()) {
typeSet.add(rs.getInt(1));
}
}
}
} finally {
JdbcUtils.closeSilently(stat);
}
}
use of org.h2.engine.Mode in project h2database by h2database.
the class TestTools method testChangeFileEncryptionWithWrongPassword.
private void testChangeFileEncryptionWithWrongPassword() throws SQLException {
if (config.mvStore) {
// doesn't detect wrong passwords
return;
}
org.h2.Driver.load();
final String dir = getBaseDir();
// TODO: this doesn't seem to work in MVSTORE mode yet
String url = "jdbc:h2:" + dir + "/testChangeFileEncryption;CIPHER=AES";
DeleteDbFiles.execute(dir, "testChangeFileEncryption", true);
Connection conn = getConnection(url, "sa", "abc 123");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, DATA CLOB) " + "AS SELECT X, SPACE(3000) FROM SYSTEM_RANGE(1, 300)");
conn.close();
// try with wrong password, this used to have a bug where it kept the
// file handle open
new AssertThrows(SQLException.class) {
@Override
public void test() throws SQLException {
ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "wrong".toCharArray(), "def".toCharArray(), true);
}
};
ChangeFileEncryption.execute(dir, "testChangeFileEncryption", "AES", "abc".toCharArray(), "def".toCharArray(), true);
conn = getConnection(url, "sa", "def 123");
stat = conn.createStatement();
stat.execute("SELECT * FROM TEST");
conn.close();
String[] args = new String[] { "-dir", dir, "-db", "testChangeFileEncryption", "-quiet" };
DeleteDbFiles.main(args);
}
Aggregations