提交 cfc9bc7e authored 作者: Thomas Mueller's avatar Thomas Mueller

After truncating tables, opening large databases could become slow.

上级 d6e60a94
......@@ -363,7 +363,11 @@ public class BtreeIndex extends BaseIndex implements RecordReader {
head.setConsistent(database.getLogIndexChanges());
lastChange = System.currentTimeMillis();
flushHead(session);
int old = headPos;
headPos = head.getPos();
if (old != Index.EMPTY_HEAD) {
database.update(session, this);
}
rowCount = 0;
}
......
......@@ -20,7 +20,7 @@ import org.h2.test.TestBase;
*/
public class TestDatabaseEventListener extends TestBase implements DatabaseEventListener {
private boolean calledOpened, calledClosingDatabase, calledScan;
private boolean calledOpened, calledClosingDatabase, calledScan, calledCreateIndex;
/**
* Run just this test.
......@@ -32,12 +32,46 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
}
public void test() throws SQLException {
testIndexNotRebuilt();
testCalled();
testCloseLog0(false);
testCloseLog0(true);
deleteDb("databaseEventListener");
}
private void testIndexNotRebuilt() throws SQLException {
if (config.memory) {
return;
}
deleteDb("databaseEventListener");
String url = getURL("databaseEventListener", true);
String user = getUser(), password = getPassword();
Properties p = new Properties();
p.setProperty("user", user);
p.setProperty("password", password);
Connection conn = DriverManager.getConnection(url, p);
Statement stat = conn.createStatement();
// the old.id index head is at position 0
stat.execute("create table old(id identity) as select 1");
// the test.id index head is at position 1
stat.execute("create table test(id identity) as select 1");
conn.close();
conn = DriverManager.getConnection(url, p);
stat = conn.createStatement();
// free up space at position 0
stat.execute("drop table old");
// truncate, relocating to position 0
stat.execute("truncate table test");
stat.execute("insert into test select 1");
conn.close();
TestDatabaseEventListener l = new TestDatabaseEventListener();
p.put("DATABASE_EVENT_LISTENER_OBJECT", l);
conn = org.h2.Driver.load().connect(url, p);
conn.close();
assertTrue(!l.calledCreateIndex);
}
private void testCloseLog0(boolean shutdown) throws SQLException {
if (config.memory) {
return;
......@@ -105,6 +139,11 @@ public class TestDatabaseEventListener extends TestBase implements DatabaseEvent
if (state == DatabaseEventListener.STATE_SCAN_FILE) {
calledScan = true;
}
if (state == DatabaseEventListener.STATE_CREATE_INDEX) {
if (!name.startsWith("SYS:")) {
calledCreateIndex = true;
}
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论