提交 3793f779 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVTableEngine

上级 ba2e2de7
...@@ -23,7 +23,9 @@ import org.h2.engine.Database; ...@@ -23,7 +23,9 @@ import org.h2.engine.Database;
import org.h2.jdbc.JdbcConnection; import org.h2.jdbc.JdbcConnection;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.tools.Recover;
import org.h2.tools.Restore; import org.h2.tools.Restore;
import org.h2.tools.Script;
import org.h2.util.Task; import org.h2.util.Task;
/** /**
...@@ -43,6 +45,9 @@ public class TestMVTableEngine extends TestBase { ...@@ -43,6 +45,9 @@ public class TestMVTableEngine extends TestBase {
@Override @Override
public void test() throws Exception { public void test() throws Exception {
// testSpeed(); // testSpeed();
// testRecover();
testSeparateKey();
testRollback();
testRollbackAfterCrash(); testRollbackAfterCrash();
testReferentialIntegrity(); testReferentialIntegrity();
testWriteDelay(); testWriteDelay();
...@@ -112,14 +117,78 @@ int test; ...@@ -112,14 +117,78 @@ int test;
System.out.println((System.currentTimeMillis() - time) + " " + dbName + " after"); System.out.println((System.currentTimeMillis() - time) + " " + dbName + " after");
} }
private void testRollbackAfterCrash() throws Exception { private void testRecover() throws Exception {
FileUtils.deleteRecursive(getBaseDir(), true); FileUtils.deleteRecursive(getBaseDir(), true);
Connection conn; Connection conn;
Statement stat; Statement stat;
String url = "mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine";
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("create table test(id int primary key)");
stat.execute("insert into test values(1)");
conn.close();
Recover.execute(getBaseDir(), "mvstore");
FileUtils.deleteRecursive(getBaseDir(), true);
Script.execute(url, getUser(), getPassword(), System.out);
conn = getConnection(url);
stat = conn.createStatement();
ResultSet rs;
rs = stat.executeQuery("select * from test");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
conn.close();
}
private void testRollback() throws Exception {
FileUtils.deleteRecursive(getBaseDir(), true);
Connection conn;
Statement stat;
String url = "mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine";
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("create table test(id identity)");
conn.setAutoCommit(false);
stat.execute("insert into test values(1)");
stat.execute("delete from test");
conn.rollback();
conn.close();
}
private void testSeparateKey() throws Exception {
FileUtils.deleteRecursive(getBaseDir(), true);
Connection conn;
Statement stat;
String url = "mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine";
conn = getConnection(url);
stat = conn.createStatement();
stat.execute("create table a(id int)");
stat.execute("insert into a values(1)");
stat.execute("insert into a values(1)");
stat.execute("create table test(id int not null) as select 100");
stat.execute("create primary key on test(id)");
ResultSet rs = stat.executeQuery("select * from test where id = 100");
assertTrue(rs.next());
conn.close();
conn = getConnection(url);
stat = conn.createStatement();
rs = stat.executeQuery("select * from test where id = 100");
assertTrue(rs.next());
conn.close();
}
private void testRollbackAfterCrash() throws Exception {
FileUtils.deleteRecursive(getBaseDir(), true);
Connection conn;
Statement stat;
String url = "mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine"; String url = "mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine";
String url2 = "mvstore2;default_table_engine=org.h2.mvstore.db.MVTableEngine"; String url2 = "mvstore2;default_table_engine=org.h2.mvstore.db.MVTableEngine";
conn = getConnection(url); conn = getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int)"); stat.execute("create table test(id int)");
...@@ -128,10 +197,10 @@ int test; ...@@ -128,10 +197,10 @@ int test;
conn.setAutoCommit(false); conn.setAutoCommit(false);
stat.execute("insert into test values(1)"); stat.execute("insert into test values(1)");
stat.execute("shutdown immediately"); stat.execute("shutdown immediately");
conn = getConnection(url); conn = getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select row_count_estimate " + ResultSet rs = stat.executeQuery("select row_count_estimate " +
"from information_schema.tables where table_name='TEST'"); "from information_schema.tables where table_name='TEST'");
rs.next(); rs.next();
assertEquals(1, rs.getLong(1)); assertEquals(1, rs.getLong(1));
...@@ -143,14 +212,14 @@ int test; ...@@ -143,14 +212,14 @@ int test;
stat.execute("delete from test"); stat.execute("delete from test");
stat.execute("checkpoint"); stat.execute("checkpoint");
stat.execute("shutdown immediately"); stat.execute("shutdown immediately");
conn = getConnection(url); conn = getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
rs = stat.executeQuery("select * from test"); rs = stat.executeQuery("select * from test");
assertTrue(rs.next()); assertTrue(rs.next());
stat.execute("drop all objects delete files"); stat.execute("drop all objects delete files");
conn.close(); conn.close();
conn = getConnection(url); conn = getConnection(url);
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar)"); stat.execute("create table test(id int primary key, name varchar)");
...@@ -165,35 +234,35 @@ int test; ...@@ -165,35 +234,35 @@ int test;
conn2 = getConnection(url2); conn2 = getConnection(url2);
conn.close(); conn.close();
conn2.close(); conn2.close();
} }
private void testReferentialIntegrity() throws Exception { private void testReferentialIntegrity() throws Exception {
FileUtils.deleteRecursive(getBaseDir(), true); FileUtils.deleteRecursive(getBaseDir(), true);
Connection conn; Connection conn;
Statement stat; Statement stat;
conn = getConnection("mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine"); conn = getConnection("mvstore;default_table_engine=org.h2.mvstore.db.MVTableEngine");
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int, parent int " + stat.execute("create table test(id int, parent int " +
"references test(id) on delete cascade)"); "references test(id) on delete cascade)");
stat.execute("insert into test values(0, 0)"); stat.execute("insert into test values(0, 0)");
stat.execute("delete from test"); stat.execute("delete from test");
stat.execute("drop table test"); stat.execute("drop table test");
stat.execute("create table parent(id int, name varchar)"); stat.execute("create table parent(id int, name varchar)");
stat.execute("create table child(id int, parentid int, " + stat.execute("create table child(id int, parentid int, " +
"foreign key(parentid) references parent(id))"); "foreign key(parentid) references parent(id))");
stat.execute("insert into parent values(1, 'mary'), (2, 'john')"); stat.execute("insert into parent values(1, 'mary'), (2, 'john')");
stat.execute("insert into child values(10, 1), (11, 1), (20, 2), (21, 2)"); stat.execute("insert into child values(10, 1), (11, 1), (20, 2), (21, 2)");
stat.execute("update parent set name = 'marc' where id = 1"); stat.execute("update parent set name = 'marc' where id = 1");
stat.execute("merge into parent key(id) values(1, 'marcy')"); stat.execute("merge into parent key(id) values(1, 'marcy')");
stat.execute("drop table parent, child"); stat.execute("drop table parent, child");
stat.execute("create table test(id identity, parent bigint, " + stat.execute("create table test(id identity, parent bigint, " +
"foreign key(parent) references(id))"); "foreign key(parent) references(id))");
stat.execute("insert into test values(0, 0), (1, NULL), " + stat.execute("insert into test values(0, 0), (1, NULL), " +
"(2, 1), (3, 3), (4, 3)"); "(2, 1), (3, 3), (4, 3)");
stat.execute("drop table test"); stat.execute("drop table test");
...@@ -224,9 +293,9 @@ int test; ...@@ -224,9 +293,9 @@ int test;
} }
stat.execute("drop table child, parent"); stat.execute("drop table child, parent");
stat.execute("create table test(id identity, parent bigint, " + stat.execute("create table test(id identity, parent bigint, " +
"foreign key(parent) references(id))"); "foreign key(parent) references(id))");
stat.execute("insert into test values(0, 0), (1, NULL), " + stat.execute("insert into test values(0, 0), (1, NULL), " +
"(2, 1), (3, 3), (4, 3)"); "(2, 1), (3, 3), (4, 3)");
stat.execute("drop table test"); stat.execute("drop table test");
...@@ -272,6 +341,7 @@ int test; ...@@ -272,6 +341,7 @@ int test;
stat = conn.createStatement(); stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar) " stat.execute("create table test(id int primary key, name varchar) "
+ "engine \"org.h2.mvstore.db.MVTableEngine\""); + "engine \"org.h2.mvstore.db.MVTableEngine\"");
stat.execute("create index on test(name)");
conn.setAutoCommit(false); conn.setAutoCommit(false);
stat.execute("insert into test values(1, 'Hello')"); stat.execute("insert into test values(1, 'Hello')");
stat.execute("insert into test values(2, 'World')"); stat.execute("insert into test values(2, 'World')");
......
...@@ -15,10 +15,10 @@ import java.util.ArrayList; ...@@ -15,10 +15,10 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.TreeSet;
import org.h2.mvstore.MVStore; import org.h2.mvstore.MVStore;
import org.h2.mvstore.db.TransactionStore; import org.h2.mvstore.db.TransactionStore;
import org.h2.mvstore.db.TransactionStore.Change;
import org.h2.mvstore.db.TransactionStore.Transaction; import org.h2.mvstore.db.TransactionStore.Transaction;
import org.h2.mvstore.db.TransactionStore.TransactionMap; import org.h2.mvstore.db.TransactionStore.TransactionMap;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
...@@ -58,14 +58,12 @@ public class TestTransactionStore extends TestBase { ...@@ -58,14 +58,12 @@ public class TestTransactionStore extends TestBase {
Transaction tx; Transaction tx;
TransactionMap<String, String> m1, m2, m3; TransactionMap<String, String> m1, m2, m3;
long sp; long sp;
TreeSet<String> changed;
tx = ts.begin(); tx = ts.begin();
m1 = tx.openMap("m1"); m1 = tx.openMap("m1");
m2 = tx.openMap("m2"); m2 = tx.openMap("m2");
m3 = tx.openMap("m3"); m3 = tx.openMap("m3");
changed = new TreeSet<String>(tx.getChangedMaps(0)); assertFalse(tx.getChanges(0).hasNext());
assertEquals(0, changed.size());
tx.commit(); tx.commit();
tx = ts.begin(); tx = ts.begin();
...@@ -76,13 +74,48 @@ public class TestTransactionStore extends TestBase { ...@@ -76,13 +74,48 @@ public class TestTransactionStore extends TestBase {
sp = tx.setSavepoint(); sp = tx.setSavepoint();
m2.put("1", "100"); m2.put("1", "100");
m3.put("1", "100"); m3.put("1", "100");
changed = new TreeSet<String>(tx.getChangedMaps(sp)); Iterator<Change> it = tx.getChanges(sp);
assertEquals("[m2, m3]", changed.toString()); assertTrue(it.hasNext());
changed = new TreeSet<String>(tx.getChangedMaps(0)); Change c;
assertEquals("[m1, m2, m3]", changed.toString()); c = it.next();
assertEquals("m3", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m2", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
it = tx.getChanges(0);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m3", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m2", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m1", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
tx.rollbackToSavepoint(sp); tx.rollbackToSavepoint(sp);
changed = new TreeSet<String>(tx.getChangedMaps(0));
assertEquals("[m1]", changed.toString()); it = tx.getChanges(0);
assertTrue(it.hasNext());
c = it.next();
assertEquals("m1", c.mapName);
assertEquals("1", c.key.toString());
assertNull(c.value);
assertFalse(it.hasNext());
tx.commit(); tx.commit();
s.close(); s.close();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论