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

MVTableEngine

上级 ba2e2de7
......@@ -23,7 +23,9 @@ import org.h2.engine.Database;
import org.h2.jdbc.JdbcConnection;
import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase;
import org.h2.tools.Recover;
import org.h2.tools.Restore;
import org.h2.tools.Script;
import org.h2.util.Task;
/**
......@@ -43,6 +45,9 @@ public class TestMVTableEngine extends TestBase {
@Override
public void test() throws Exception {
// testSpeed();
// testRecover();
testSeparateKey();
testRollback();
testRollbackAfterCrash();
testReferentialIntegrity();
testWriteDelay();
......@@ -112,6 +117,70 @@ int test;
System.out.println((System.currentTimeMillis() - time) + " " + dbName + " after");
}
private void testRecover() 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 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;
......@@ -272,6 +341,7 @@ int test;
stat = conn.createStatement();
stat.execute("create table test(id int primary key, name varchar) "
+ "engine \"org.h2.mvstore.db.MVTableEngine\"");
stat.execute("create index on test(name)");
conn.setAutoCommit(false);
stat.execute("insert into test values(1, 'Hello')");
stat.execute("insert into test values(2, 'World')");
......
......@@ -15,10 +15,10 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;
import org.h2.mvstore.MVStore;
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.TransactionMap;
import org.h2.store.fs.FileUtils;
......@@ -58,14 +58,12 @@ public class TestTransactionStore extends TestBase {
Transaction tx;
TransactionMap<String, String> m1, m2, m3;
long sp;
TreeSet<String> changed;
tx = ts.begin();
m1 = tx.openMap("m1");
m2 = tx.openMap("m2");
m3 = tx.openMap("m3");
changed = new TreeSet<String>(tx.getChangedMaps(0));
assertEquals(0, changed.size());
assertFalse(tx.getChanges(0).hasNext());
tx.commit();
tx = ts.begin();
......@@ -76,13 +74,48 @@ public class TestTransactionStore extends TestBase {
sp = tx.setSavepoint();
m2.put("1", "100");
m3.put("1", "100");
changed = new TreeSet<String>(tx.getChangedMaps(sp));
assertEquals("[m2, m3]", changed.toString());
changed = new TreeSet<String>(tx.getChangedMaps(0));
assertEquals("[m1, m2, m3]", changed.toString());
Iterator<Change> it = tx.getChanges(sp);
assertTrue(it.hasNext());
Change c;
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);
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();
s.close();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论