提交 847cf627 authored 作者: Thomas Mueller's avatar Thomas Mueller

The information schema tables are only initialized when needed.

上级 8308f5c8
...@@ -34,7 +34,7 @@ public class Analyze extends DefineCommand { ...@@ -34,7 +34,7 @@ public class Analyze extends DefineCommand {
Database db = session.getDatabase(); Database db = session.getDatabase();
session.getUser().checkAdmin(); session.getUser().checkAdmin();
// TODO do we need to lock the table? // TODO do we need to lock the table?
for (Table table : db.getAllTablesAndViews()) { for (Table table : db.getAllTablesAndViews(false)) {
if (!(table instanceof TableData)) { if (!(table instanceof TableData)) {
continue; continue;
} }
......
...@@ -50,7 +50,7 @@ public class DropDatabase extends DefineCommand { ...@@ -50,7 +50,7 @@ public class DropDatabase extends DefineCommand {
db.removeDatabaseObject(session, schema); db.removeDatabaseObject(session, schema);
} }
} }
ObjectArray<Table> tables = db.getAllTablesAndViews(); ObjectArray<Table> tables = db.getAllTablesAndViews(false);
for (Table t : tables) { for (Table t : tables) {
if (t.getName() != null && Table.VIEW.equals(t.getTableType())) { if (t.getName() != null && Table.VIEW.equals(t.getTableType())) {
db.removeSchemaObject(session, t); db.removeSchemaObject(session, t);
......
...@@ -171,7 +171,7 @@ public class ScriptCommand extends ScriptBase { ...@@ -171,7 +171,7 @@ public class ScriptCommand extends ScriptBase {
} }
add(agg.getCreateSQL(), false); add(agg.getCreateSQL(), false);
} }
ObjectArray<Table> tables = db.getAllTablesAndViews(); ObjectArray<Table> tables = db.getAllTablesAndViews(false);
// sort by id, so that views are after tables and views on views // sort by id, so that views are after tables and views on views
// after the base views // after the base views
tables.sort(new Comparator<Table>() { tables.sort(new Comparator<Table>() {
......
...@@ -45,7 +45,7 @@ import org.h2.store.PageStore; ...@@ -45,7 +45,7 @@ import org.h2.store.PageStore;
import org.h2.store.RecordReader; import org.h2.store.RecordReader;
import org.h2.store.Storage; import org.h2.store.Storage;
import org.h2.store.WriterThread; import org.h2.store.WriterThread;
import org.h2.store.fs.FileSystem; import org.h2.store.fs.FileSystemMemory;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.MetaTable; import org.h2.table.MetaTable;
...@@ -175,6 +175,7 @@ public class Database implements DataHandler { ...@@ -175,6 +175,7 @@ public class Database implements DataHandler {
private int cacheSize; private int cacheSize;
private boolean compactFully; private boolean compactFully;
private SourceCompiler compiler; private SourceCompiler compiler;
private boolean metaTablesInitialized;
public Database(String name, ConnectionInfo ci, String cipher) throws SQLException { public Database(String name, ConnectionInfo ci, String cipher) throws SQLException {
this.compareMode = CompareMode.getInstance(null, 0); this.compareMode = CompareMode.getInstance(null, 0);
...@@ -693,10 +694,6 @@ public class Database implements DataHandler { ...@@ -693,10 +694,6 @@ public class Database implements DataHandler {
metaIdIndex = meta.addIndex(systemSession, "SYS_ID", 0, pkCols, IndexType.createPrimaryKey( metaIdIndex = meta.addIndex(systemSession, "SYS_ID", 0, pkCols, IndexType.createPrimaryKey(
false, false), Index.EMPTY_HEAD, null); false, false), Index.EMPTY_HEAD, null);
objectIds.set(0); objectIds.set(0);
// there could be views on system tables, so they must be added first
for (int i = 0; i < MetaTable.getMetaTableTypeCount(); i++) {
addMetaData(i);
}
starting = true; starting = true;
Cursor cursor = metaIdIndex.find(systemSession, null, null); Cursor cursor = metaIdIndex.find(systemSession, null, null);
// first, create all function aliases and sequences because // first, create all function aliases and sequences because
...@@ -758,7 +755,7 @@ public class Database implements DataHandler { ...@@ -758,7 +755,7 @@ public class Database implements DataHandler {
boolean recompileSuccessful; boolean recompileSuccessful;
do { do {
recompileSuccessful = false; recompileSuccessful = false;
for (Table obj : getAllTablesAndViews()) { for (Table obj : getAllTablesAndViews(false)) {
if (obj instanceof TableView) { if (obj instanceof TableView) {
TableView view = (TableView) obj; TableView view = (TableView) obj;
if (view.isInvalid()) { if (view.isInvalid()) {
...@@ -777,7 +774,7 @@ public class Database implements DataHandler { ...@@ -777,7 +774,7 @@ public class Database implements DataHandler {
// when opening a database, views are initialized before indexes, // when opening a database, views are initialized before indexes,
// so they may not have the optimal plan yet // so they may not have the optimal plan yet
// this is not a problem, it is just nice to see the newest plan // this is not a problem, it is just nice to see the newest plan
for (Table obj : getAllTablesAndViews()) { for (Table obj : getAllTablesAndViews(false)) {
if (obj instanceof TableView) { if (obj instanceof TableView) {
TableView view = (TableView) obj; TableView view = (TableView) obj;
if (!view.isInvalid()) { if (!view.isInvalid()) {
...@@ -855,9 +852,19 @@ public class Database implements DataHandler { ...@@ -855,9 +852,19 @@ public class Database implements DataHandler {
return storage; return storage;
} }
private void addMetaData(int type) throws SQLException { private void initMetaTables() {
MetaTable m = new MetaTable(infoSchema, -1 - type, type); if (metaTablesInitialized) {
infoSchema.add(m); return;
}
for (int type = 0; type < MetaTable.getMetaTableTypeCount(); type++) {
try {
MetaTable m = new MetaTable(infoSchema, -1 - type, type);
infoSchema.add(m);
} catch (SQLException e) {
throw Message.convertToInternal(e);
}
}
metaTablesInitialized = true;
} }
private synchronized void addMeta(Session session, DbObject obj) throws SQLException { private synchronized void addMeta(Session session, DbObject obj) throws SQLException {
...@@ -1034,7 +1041,11 @@ public class Database implements DataHandler { ...@@ -1034,7 +1041,11 @@ public class Database implements DataHandler {
* @return the schema or null * @return the schema or null
*/ */
public Schema findSchema(String schemaName) { public Schema findSchema(String schemaName) {
return schemas.get(schemaName); Schema schema = schemas.get(schemaName);
if (schema == infoSchema) {
initMetaTables();
}
return schema;
} }
/** /**
...@@ -1201,7 +1212,7 @@ public class Database implements DataHandler { ...@@ -1201,7 +1212,7 @@ public class Database implements DataHandler {
try { try {
if (systemSession != null) { if (systemSession != null) {
if (powerOffCount != -1) { if (powerOffCount != -1) {
for (Table table : getAllTablesAndViews()) { for (Table table : getAllTablesAndViews(false)) {
table.close(systemSession); table.close(systemSession);
} }
for (SchemaObject obj : getAllSchemaObjects(DbObject.SEQUENCE)) { for (SchemaObject obj : getAllSchemaObjects(DbObject.SEQUENCE)) {
...@@ -1426,6 +1437,9 @@ public class Database implements DataHandler { ...@@ -1426,6 +1437,9 @@ public class Database implements DataHandler {
* @return all objects of that type * @return all objects of that type
*/ */
public ObjectArray<SchemaObject> getAllSchemaObjects(int type) { public ObjectArray<SchemaObject> getAllSchemaObjects(int type) {
if (type == DbObject.TABLE_OR_VIEW) {
initMetaTables();
}
ObjectArray<SchemaObject> list = ObjectArray.newInstance(); ObjectArray<SchemaObject> list = ObjectArray.newInstance();
for (Schema schema : schemas.values()) { for (Schema schema : schemas.values()) {
list.addAll(schema.getAll(type)); list.addAll(schema.getAll(type));
...@@ -1438,7 +1452,10 @@ public class Database implements DataHandler { ...@@ -1438,7 +1452,10 @@ public class Database implements DataHandler {
* *
* @return all objects of that type * @return all objects of that type
*/ */
public ObjectArray<Table> getAllTablesAndViews() { public ObjectArray<Table> getAllTablesAndViews(boolean includeMeta) {
if (includeMeta) {
initMetaTables();
}
ObjectArray<Table> list = ObjectArray.newInstance(); ObjectArray<Table> list = ObjectArray.newInstance();
for (Schema schema : schemas.values()) { for (Schema schema : schemas.values()) {
list.addAll(schema.getAllTablesAndViews()); list.addAll(schema.getAllTablesAndViews());
...@@ -1447,6 +1464,7 @@ public class Database implements DataHandler { ...@@ -1447,6 +1464,7 @@ public class Database implements DataHandler {
} }
public ObjectArray<Schema> getAllSchemas() { public ObjectArray<Schema> getAllSchemas() {
initMetaTables();
return ObjectArray.newInstance(schemas.values()); return ObjectArray.newInstance(schemas.values());
} }
...@@ -1603,7 +1621,7 @@ public class Database implements DataHandler { ...@@ -1603,7 +1621,7 @@ public class Database implements DataHandler {
boolean inTempDir = readOnly; boolean inTempDir = readOnly;
String name = databaseName; String name = databaseName;
if (!persistent) { if (!persistent) {
name = FileSystem.PREFIX_MEMORY + name; name = FileSystemMemory.PREFIX + name;
} }
return FileUtils.createTempFile(name, Constants.SUFFIX_TEMP_FILE, true, inTempDir); return FileUtils.createTempFile(name, Constants.SUFFIX_TEMP_FILE, true, inTempDir);
} catch (IOException e) { } catch (IOException e) {
...@@ -1719,7 +1737,7 @@ public class Database implements DataHandler { ...@@ -1719,7 +1737,7 @@ public class Database implements DataHandler {
default: default:
} }
HashSet<DbObject> set = New.hashSet(); HashSet<DbObject> set = New.hashSet();
for (Table t : getAllTablesAndViews()) { for (Table t : getAllTablesAndViews(false)) {
if (except == t) { if (except == t) {
continue; continue;
} }
...@@ -1735,7 +1753,7 @@ public class Database implements DataHandler { ...@@ -1735,7 +1753,7 @@ public class Database implements DataHandler {
private String getFirstInvalidTable(Session session) { private String getFirstInvalidTable(Session session) {
String conflict = null; String conflict = null;
try { try {
for (Table t : getAllTablesAndViews()) { for (Table t : getAllTablesAndViews(false)) {
conflict = t.getSQL(); conflict = t.getSQL();
session.prepare(t.getCreateSQL()); session.prepare(t.getCreateSQL());
} }
...@@ -2327,7 +2345,7 @@ public class Database implements DataHandler { ...@@ -2327,7 +2345,7 @@ public class Database implements DataHandler {
* @return the table or null if no table is defined * @return the table or null if no table is defined
*/ */
public Table getFirstUserTable() { public Table getFirstUserTable() {
for (Table table : getAllTablesAndViews()) { for (Table table : getAllTablesAndViews(false)) {
if (table.getCreateSQL() != null) { if (table.getCreateSQL() != null) {
return table; return table;
} }
......
...@@ -64,7 +64,8 @@ public class User extends RightOwner { ...@@ -64,7 +64,8 @@ public class User extends RightOwner {
*/ */
public void setUserPasswordHash(byte[] userPasswordHash) { public void setUserPasswordHash(byte[] userPasswordHash) {
if (userPasswordHash != null) { if (userPasswordHash != null) {
salt = RandomUtils.getSecureBytes(Constants.SALT_LEN); salt = new byte[Constants.SALT_LEN];
RandomUtils.nextBytes(salt);
SHA256 sha = new SHA256(); SHA256 sha = new SHA256();
this.passwordHash = sha.getHashWithSalt(userPasswordHash, salt); this.passwordHash = sha.getHashWithSalt(userPasswordHash, salt);
} }
......
...@@ -565,7 +565,7 @@ public class MetaTable extends Table { ...@@ -565,7 +565,7 @@ public class MetaTable extends Table {
} }
private ObjectArray<Table> getAllTables(Session session) { private ObjectArray<Table> getAllTables(Session session) {
ObjectArray<Table> tables = database.getAllTablesAndViews(); ObjectArray<Table> tables = database.getAllTablesAndViews(true);
ObjectArray<Table> tempTables = session.getLocalTempTables(); ObjectArray<Table> tempTables = session.getLocalTempTables();
tables.addAll(tempTables); tables.addAll(tempTables);
return tables; return tables;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论