提交 b9283ea4 authored 作者: Thomas Mueller Graf's avatar Thomas Mueller Graf

Formatting / Javadocs

上级 2134953d
...@@ -54,13 +54,15 @@ class Optimizer { ...@@ -54,13 +54,15 @@ class Optimizer {
} }
/** /**
* @return {@code true} If join reordering is enabled (it can be disabled by hint). * Whether join reordering is enabled (it can be disabled by hint).
*
* @return {@code true} if yes
*/ */
private static boolean isJoinReorderingEnabled() { private static boolean isJoinReorderingEnabled() {
OptimizerHints hints = OptimizerHints.get(); OptimizerHints hints = OptimizerHints.get();
return hints == null || hints.joinReorderEnabled; return hints == null || hints.getJoinReorderEnabled();
} }
/** /**
* How many filter to calculate using brute force. The remaining filters are * How many filter to calculate using brute force. The remaining filters are
* selected using a greedy algorithm which has a runtime of (1 + 2 + ... + * selected using a greedy algorithm which has a runtime of (1 + 2 + ... +
......
...@@ -6,23 +6,26 @@ ...@@ -6,23 +6,26 @@
package org.h2.command.dml; package org.h2.command.dml;
/** /**
* Thread local hints for H2 query optimizer. All the ongoing queries in the current thread * Thread local hints for H2 query optimizer. All the ongoing queries in the
* will run with respect to these hints, so if they are needed only for a single * current thread will run with respect to these hints, so if they are needed
* operation it is preferable to setup and drop them in try-finally block. * only for a single operation it is preferable to setup and drop them in
* * try-finally block.
*
* Currently works only in embedded mode. * Currently works only in embedded mode.
* *
* @author Sergi Vladykin * @author Sergi Vladykin
*/ */
public class OptimizerHints { public class OptimizerHints {
private static final ThreadLocal<OptimizerHints> HINTS = new ThreadLocal<OptimizerHints>();
boolean joinReorderEnabled = true; private static final ThreadLocal<OptimizerHints> HINTS =
new ThreadLocal<OptimizerHints>();
private boolean joinReorderEnabled = true;
/** /**
* Set thread local hints or {@code null} to drop any existing hints. * Set thread local hints or {@code null} to drop any existing hints.
* *
* @param hints * @param hints the hints
*/ */
public static void set(OptimizerHints hints) { public static void set(OptimizerHints hints) {
if (hints != null) { if (hints != null) {
...@@ -31,21 +34,27 @@ public class OptimizerHints { ...@@ -31,21 +34,27 @@ public class OptimizerHints {
HINTS.remove(); HINTS.remove();
} }
} }
/** /**
* @return Current thread local hints or {@code null} if none. * Get the current thread local hints or {@code null} if none.
*
* @return the hints
*/ */
public static OptimizerHints get() { public static OptimizerHints get() {
return HINTS.get(); return HINTS.get();
} }
/** /**
* Set whether reordering of tables (or anything else in the {@code FROM} clause) is enabled. * Set whether reordering of tables (or anything else in the {@code FROM}
* By default is {@code true}. * clause) is enabled. By default is {@code true}.
* *
* @param joinReorderEnabled Flag value. * @param joinReorderEnabled Flag value.
*/ */
public void setJoinReorderEnabled(boolean joinReorderEnabled) { public void setJoinReorderEnabled(boolean joinReorderEnabled) {
this.joinReorderEnabled = joinReorderEnabled; this.joinReorderEnabled = joinReorderEnabled;
} }
public boolean getJoinReorderEnabled() {
return joinReorderEnabled;
}
} }
...@@ -13,8 +13,8 @@ import org.h2.command.dml.OptimizerHints; ...@@ -13,8 +13,8 @@ import org.h2.command.dml.OptimizerHints;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
* Test for optimizer hints. * Test for optimizer hints.
* *
* @author Sergi Vladykin * @author Sergi Vladykin
*/ */
public class TestOptimizerHints extends TestBase { public class TestOptimizerHints extends TestBase {
...@@ -36,34 +36,34 @@ public class TestOptimizerHints extends TestBase { ...@@ -36,34 +36,34 @@ public class TestOptimizerHints extends TestBase {
deleteDb("testOptimizerHints"); deleteDb("testOptimizerHints");
Connection conn = getConnection("testOptimizerHints"); Connection conn = getConnection("testOptimizerHints");
Statement s = conn.createStatement(); Statement s = conn.createStatement();
s.execute("create table t1(id int)"); s.execute("create table t1(id int)");
s.execute("create table t2(id int, ref_id int)"); s.execute("create table t2(id int, ref_id int)");
s.execute("insert into t1 values(1),(2),(3)"); s.execute("insert into t1 values(1),(2),(3)");
s.execute("insert into t2 values(1,2),(2,3),(3,4),(4,6),(5,1),(6,4)"); s.execute("insert into t2 values(1,2),(2,3),(3,4),(4,6),(5,1),(6,4)");
s.execute("create unique index idx1_id on t1(id)"); s.execute("create unique index idx1_id on t1(id)");
s.execute("create index idx2_id on t2(id)"); s.execute("create index idx2_id on t2(id)");
s.execute("create index idx2_ref_id on t2(ref_id)"); s.execute("create index idx2_ref_id on t2(ref_id)");
enableJoinReordering(false); enableJoinReordering(false);
try { try {
String plan; String plan;
plan = plan(s, "select * from t1, t2 where t1.id = t2.ref_id"); plan = plan(s, "select * from t1, t2 where t1.id = t2.ref_id");
assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T2")); assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T2"));
plan = plan(s, "select * from t2, t1 where t1.id = t2.ref_id"); plan = plan(s, "select * from t2, t1 where t1.id = t2.ref_id");
assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1")); assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1"));
plan = plan(s, "select * from t2, t1 where t1.id = 1"); plan = plan(s, "select * from t2, t1 where t1.id = 1");
assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1")); assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1"));
plan = plan(s, "select * from t2, t1 where t1.id = t2.ref_id and t2.id = 1"); plan = plan(s, "select * from t2, t1 where t1.id = t2.ref_id and t2.id = 1");
assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1")); assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T1"));
plan = plan(s, "select * from t1, t2 where t1.id = t2.ref_id and t2.id = 1"); plan = plan(s, "select * from t1, t2 where t1.id = t2.ref_id and t2.id = 1");
assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T2")); assertTrue(plan, plan.contains("INNER JOIN PUBLIC.T2"));
} finally { } finally {
...@@ -80,12 +80,12 @@ public class TestOptimizerHints extends TestBase { ...@@ -80,12 +80,12 @@ public class TestOptimizerHints extends TestBase {
hints.setJoinReorderEnabled(enable); hints.setJoinReorderEnabled(enable);
OptimizerHints.set(hints); OptimizerHints.set(hints);
} }
/** /**
* @param s Statement. * @param s Statement.
* @param query Query. * @param query Query.
* @return Plan. * @return Plan.
* @throws SQLException If failed. * @throws SQLException If failed.
*/ */
private String plan(Statement s, String query) throws SQLException { private String plan(Statement s, String query) throws SQLException {
ResultSet rs = s.executeQuery("explain " + query); ResultSet rs = s.executeQuery("explain " + query);
......
...@@ -49,7 +49,8 @@ public class TestMvcc4 extends TestBase { ...@@ -49,7 +49,8 @@ public class TestMvcc4 extends TestBase {
+ "entity_id VARCHAR(100) NOT NULL PRIMARY KEY, " + "entity_id VARCHAR(100) NOT NULL PRIMARY KEY, "
+ "lastUpdated TIMESTAMP NOT NULL)"); + "lastUpdated TIMESTAMP NOT NULL)");
PreparedStatement ps = setup.prepareStatement("INSERT INTO test (entity_id, lastUpdated) VALUES (?, ?)"); PreparedStatement ps = setup.prepareStatement(
"INSERT INTO test (entity_id, lastUpdated) VALUES (?, ?)");
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
String id = "" + i; String id = "" + i;
ps.setString(1, id); ps.setString(1, id);
...@@ -72,7 +73,8 @@ public class TestMvcc4 extends TestBase { ...@@ -72,7 +73,8 @@ public class TestMvcc4 extends TestBase {
Connection c2 = getConnection("mvcc4"); Connection c2 = getConnection("mvcc4");
c2.setAutoCommit(false); c2.setAutoCommit(false);
PreparedStatement ps = c2.prepareStatement("SELECT * FROM test WHERE entity_id = ? FOR UPDATE"); PreparedStatement ps = c2.prepareStatement(
"SELECT * FROM test WHERE entity_id = ? FOR UPDATE");
ps.setString(1, "1"); ps.setString(1, "1");
ps.executeQuery().next(); ps.executeQuery().next();
...@@ -93,32 +95,32 @@ public class TestMvcc4 extends TestBase { ...@@ -93,32 +95,32 @@ public class TestMvcc4 extends TestBase {
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
{ // Execute an update. This should initially fail, and enter the waiting
//Execute an update. This should initially fail, and enter the waiting for lock case. // for lock case.
PreparedStatement ps = c1.prepareStatement("UPDATE test SET lastUpdated = ?"); PreparedStatement ps = c1.prepareStatement("UPDATE test SET lastUpdated = ?");
ps.setTimestamp(1, new Timestamp(System.currentTimeMillis())); ps.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
ps.executeUpdate(); ps.executeUpdate();
}
c1.commit(); c1.commit();
c1.close(); c1.close();
Connection verify = getConnection("mvcc4"); Connection verify = getConnection("mvcc4");
{
verify.setAutoCommit(false); verify.setAutoCommit(false);
PreparedStatement ps = verify.prepareStatement("SELECT COUNT(*) FROM test"); ps = verify.prepareStatement("SELECT COUNT(*) FROM test");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
assertTrue(rs.next()); assertTrue(rs.next());
assertTrue(rs.getInt(1) == 2); assertTrue(rs.getInt(1) == 2);
verify.commit(); verify.commit();
verify.close(); verify.close();
}
setup.close(); setup.close();
} }
private static void waitForThreadToBlockOnDB(Thread t) { private static void waitForThreadToBlockOnDB(Thread t) {
while (true) { while (true) {
// TODO must not use getAllStackTraces, as the method names are
// implementation details
Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces(); Map<Thread, StackTraceElement[]> threadMap = Thread.getAllStackTraces();
StackTraceElement[] elements = threadMap.get(t); StackTraceElement[] elements = threadMap.get(t);
if (elements != null if (elements != null
......
...@@ -777,4 +777,4 @@ bradmesserle dan incorporated keegan industries tagtraum cyr israels rafel ...@@ -777,4 +777,4 @@ bradmesserle dan incorporated keegan industries tagtraum cyr israels rafel
dance schedule hitting reverted youngest footers inliner deadlocked reorder nger dance schedule hitting reverted youngest footers inliner deadlocked reorder nger
nullid syspublic sysibmts sysibminternal syscat sysfun sysstat systools sysibmadm nullid syspublic sysibmts sysibminternal syscat sysfun sysstat systools sysibmadm
sysproc jcc expecting gpg showed unreferenced activating cvf stephane lacoin sysproc jcc expecting gpg showed unreferenced activating cvf stephane lacoin
centrale umr ecole nantes sticc lab centrale umr ecole nantes sticc lab reordering preferable
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论