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

Formatting, code style

上级 6cdd1755
......@@ -165,27 +165,27 @@ public class TestTableEngines extends TestBase {
rs.close();
}
private void testMultiColumnTreeSetIndex() throws SQLException {
deleteDb("tableEngine");
Connection conn = getConnection("tableEngine");
Statement stat = conn.createStatement();
stat.executeUpdate("CREATE TABLE T(A INT, B VARCHAR, C BIGINT) ENGINE \"" +
TreeSetIndexTableEngine.class.getName() + "\"");
stat.executeUpdate("CREATE INDEX IDX_CBA ON T(C, B, A)");
stat.executeUpdate("CREATE INDEX IDX_BA ON T(B, A)");
List<List<Object>> dataSet = New.arrayList();
dataSet.add(Arrays.<Object>asList(1, "1", 1L));
dataSet.add(Arrays.<Object>asList(1, "0", 2L));
dataSet.add(Arrays.<Object>asList(2, "0", -1L));
dataSet.add(Arrays.<Object>asList(0, "0", 1L));
dataSet.add(Arrays.<Object>asList(0, "1", null));
dataSet.add(Arrays.<Object>asList(2, null, 0L));
PreparedStatement prep = conn.prepareStatement("INSERT INTO T VALUES(?,?,?)");
for (List<Object> row : dataSet) {
for (int i = 0; i < row.size(); i++) {
......@@ -194,13 +194,13 @@ public class TestTableEngines extends TestBase {
assertEquals(1, prep.executeUpdate());
}
prep.close();
checkPlan(stat, "select max(c) from t", "direct lookup");
checkPlan(stat, "select min(c) from t", "direct lookup");
checkPlan(stat, "select count(*) from t", "direct lookup");
checkPlan(stat, "select * from t", "scan");
checkPlan(stat, "select * from t order by c", "IDX_CBA");
checkPlan(stat, "select * from t order by c, b", "IDX_CBA");
checkPlan(stat, "select * from t order by b", "IDX_BA");
......@@ -208,7 +208,7 @@ public class TestTableEngines extends TestBase {
checkPlan(stat, "select * from t order by b, c", "scan");
checkPlan(stat, "select * from t order by a, b", "scan");
checkPlan(stat, "select * from t order by a, c, b", "scan");
checkPlan(stat, "select * from t where b > ''", "IDX_BA");
checkPlan(stat, "select * from t where a > 0 and b > ''", "IDX_BA");
checkPlan(stat, "select * from t where b < ''", "IDX_BA");
......@@ -217,9 +217,9 @@ public class TestTableEngines extends TestBase {
checkPlan(stat, "select * from t where a > 0 order by c, b", "IDX_CBA");
checkPlan(stat, "select * from t where a = 0 and c > 0", "IDX_CBA");
checkPlan(stat, "select * from t where a = 0 and b < 0", "IDX_BA");
assertEquals(6, ((Number) query(stat, "select count(*) from t").get(0).get(0)).intValue());
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by a");
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by b");
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by c");
......@@ -227,28 +227,28 @@ public class TestTableEngines extends TestBase {
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by b, a");
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by c, b, a");
checkResultsNoOrder(stat, 6, "select * from t", "select * from t order by a, c, b");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by a");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by b");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by c");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by c, a");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by b, a");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by c, b, a");
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
checkResultsNoOrder(stat, 4, "select * from t where a > 0",
"select * from t where a > 0 order by a, c, b");
checkResults(6, dataSet, stat, "select * from t order by a", null, new RowComparator(0));
checkResults(6, dataSet, stat, "select * from t order by b, c", null, new RowComparator(1, 2));
checkResults(6, dataSet, stat, "select * from t order by c, a", null, new RowComparator(2, 0));
checkResults(6, dataSet, stat, "select * from t order by b, a", null, new RowComparator(1, 0));
checkResults(6, dataSet, stat, "select * from t order by c, b, a", null, new RowComparator(2, 1, 0));
checkResults(4, dataSet, stat, "select * from t where a > 0", new RowFilter() {
@Override
protected boolean accept(List<Object> row) {
......@@ -323,11 +323,11 @@ public class TestTableEngines extends TestBase {
return "0".equals(b) && a != null && a < 2;
}
}, null);
deleteDb("tableEngine");
}
private void checkResultsNoOrder(Statement stat, int size, String qry1, String qry2)
private void checkResultsNoOrder(Statement stat, int size, String qry1, String qry2)
throws SQLException {
List<List<Object>> res1 = query(stat, qry1);
List<List<Object>> res2 = query(stat, qry2);
......@@ -346,28 +346,28 @@ public class TestTableEngines extends TestBase {
Collections.sort(res2, cmp);
assertTrue("Wrong data: \n" + res1 + "\n" + res2, res1.equals(res2));
}
private void checkResults(int size, List<List<Object>> dataSet, Statement stat, String qry, RowFilter filter,
private void checkResults(int size, List<List<Object>> dataSet, Statement stat, String qry, RowFilter filter,
RowComparator sort) throws SQLException {
List<List<Object>> res1 = query(stat, qry);
List<List<Object>> res2 = query(dataSet, filter, sort);
assertTrue("Wrong size: " + size + " \n" + res1 + "\n" + res2,
assertTrue("Wrong size: " + size + " \n" + res1 + "\n" + res2,
res1.size() == size && res2.size() == size);
assertTrue(filter != null || sort != null);
for (int i = 0; i < res1.size(); i++) {
List<Object> row1 = res1.get(i);
List<Object> row2 = res2.get(i);
assertTrue("Filter failed on row " + i + " of \n" + res1 + "\n" + res2,
filter == null || filter.accept(row1));
assertTrue("Sort failed on row " + i + " of \n" + res1 + "\n" + res2,
sort == null || sort.compare(row1, row2) == 0);
}
}
private List<List<Object>> query(List<List<Object>> dataSet, RowFilter filter, RowComparator sort) {
private static List<List<Object>> query(List<List<Object>> dataSet, RowFilter filter, RowComparator sort) {
List<List<Object>> res = New.arrayList();
if (filter == null) {
res.addAll(dataSet);
......@@ -383,8 +383,8 @@ public class TestTableEngines extends TestBase {
}
return res;
}
private List<List<Object>> query(Statement stat, String qry) throws SQLException {
private static List<List<Object>> query(Statement stat, String qry) throws SQLException {
ResultSet rs = stat.executeQuery(qry);
int cols = rs.getMetaData().getColumnCount();
List<List<Object>> list = New.arrayList();
......@@ -398,7 +398,7 @@ public class TestTableEngines extends TestBase {
rs.close();
return list;
}
private void checkPlan(Statement stat, String qry, String index) throws SQLException {
String plan = query(stat, "EXPLAIN " + qry).get(0).get(0).toString();
assertTrue("Index '" + index + "' is not used in query plan: " + plan, plan.contains(index));
......@@ -695,8 +695,9 @@ public class TestTableEngines extends TestBase {
}
}
/**
* A table engine that internally uses a tree set.
*/
public static class TreeSetIndexTableEngine implements TableEngine {
@Override
......@@ -704,37 +705,38 @@ public class TestTableEngines extends TestBase {
return new TreeSetTable(data);
}
}
/**
/**
* A table that internally uses a tree set.
*/
private static class TreeSetTable extends TableBase {
int dataMoficationId;
ArrayList<Index> indexes;
TreeSetIndex scan = new TreeSetIndex(this, "scan",
TreeSetIndex scan = new TreeSetIndex(this, "scan",
IndexColumn.wrap(getColumns()), IndexType.createScan(false)) {
@Override
public double getCost(Session session, int[] masks, TableFilter filter,
public double getCost(Session session, int[] masks, TableFilter filter,
SortOrder sortOrder) {
return getRowCount(session) + Constants.COST_ROW_OFFSET;
}
};
public TreeSetTable(CreateTableData data) {
super(data);
}
@Override
public void checkRename() {
// No-op.
}
@Override
public void unlock(Session s) {
// No-op.
}
@Override
public void truncate(Session session) {
if (indexes != null) {
......@@ -746,7 +748,7 @@ public class TestTableEngines extends TestBase {
}
dataMoficationId++;
}
@Override
public void removeRow(Session session, Row row) {
if (indexes != null) {
......@@ -758,7 +760,7 @@ public class TestTableEngines extends TestBase {
}
dataMoficationId++;
}
@Override
public void addRow(Session session, Row row) {
if (indexes != null) {
......@@ -770,17 +772,17 @@ public class TestTableEngines extends TestBase {
}
dataMoficationId++;
}
@Override
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols,
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols,
IndexType indexType, boolean create, String indexComment) {
if (indexes == null) {
indexes = New.arrayList(2);
// Scan must be always at 0.
indexes.add(scan);
indexes.add(scan);
}
Index index = new TreeSetIndex(this, indexName, cols, indexType);
for (SearchRow row : scan.set) {
for (SearchRow row : scan.set) {
index.add(session, (Row) row);
}
indexes.add(index);
......@@ -788,92 +790,93 @@ public class TestTableEngines extends TestBase {
setModified();
return index;
}
@Override
public boolean lock(Session session, boolean exclusive, boolean forceLockEvenInMvcc) {
return true;
}
@Override
public boolean isLockedExclusively() {
return false;
}
@Override
public boolean isDeterministic() {
return false;
}
@Override
public Index getUniqueIndex() {
return null;
}
@Override
public String getTableType() {
return EXTERNAL_TABLE_ENGINE;
}
@Override
public Index getScanIndex(Session session) {
return scan;
}
@Override
public long getRowCountApproximation() {
return getScanIndex(null).getRowCountApproximation();
}
@Override
public long getRowCount(Session session) {
return scan.getRowCount(session);
}
@Override
public long getMaxDataModificationId() {
return dataMoficationId;
}
@Override
public ArrayList<Index> getIndexes() {
return indexes;
}
@Override
public long getDiskSpaceUsed() {
return 0;
}
@Override
public void close(Session session) {
// No-op.
}
@Override
public void checkSupportAlter() {
// No-op.
}
@Override
public boolean canGetRowCount() {
return true;
}
@Override
public boolean canDrop() {
return true;
}
}
/**
* An index that internally uses a tree set.
*/
private static class TreeSetIndex extends BaseIndex implements Comparator<SearchRow> {
private final TreeSet<SearchRow> set = new TreeSet<SearchRow>(this);
final TreeSet<SearchRow> set = new TreeSet<SearchRow>(this);
TreeSetIndex(Table t, String name, IndexColumn[] cols, IndexType type) {
initBaseIndex(t, 0, name, cols, type);
}
@Override
public int compare(SearchRow o1, SearchRow o2) {
int res = compareRows(o1, o2);
......@@ -898,14 +901,14 @@ public class TestTableEngines extends TestBase {
set.remove(row);
}
private SearchRow mark(SearchRow row) {
private static SearchRow mark(SearchRow row) {
if (row != null) {
// Mark this row to be a search row.
row.setKey(Long.MAX_VALUE);
}
return row;
}
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
Set<SearchRow> subSet;
......@@ -981,22 +984,22 @@ public class TestTableEngines extends TestBase {
// No-op.
}
}
/**
*/
private static class IteratorCursor implements Cursor {
private Iterator<SearchRow> iter;
private Row current;
public IteratorCursor(Iterator<SearchRow> iter) {
this.iter = iter;
}
@Override
public boolean previous() {
throw DbException.getUnsupportedException("prev");
}
@Override
public boolean next() {
if (iter.hasNext()) {
......@@ -1006,19 +1009,20 @@ public class TestTableEngines extends TestBase {
current = null;
return false;
}
@Override
public SearchRow getSearchRow() {
return get();
}
@Override
public Row get() {
return current;
}
}
/**
* A comparator for rows (lists of comparable objects).
*/
private static class RowComparator implements Comparator<List<Object>> {
private int[] cols;
......@@ -1026,7 +1030,7 @@ public class TestTableEngines extends TestBase {
public RowComparator(int... cols) {
this.cols = cols;
}
@SuppressWarnings("unchecked")
@Override
public int compare(List<Object> row1, List<Object> row2) {
......@@ -1043,7 +1047,7 @@ public class TestTableEngines extends TestBase {
if (o2 == null) {
return 1;
}
int res = o1.compareTo(o2);
int res = o1.compareTo(o2);
if (res != 0) {
return res;
}
......@@ -1051,20 +1055,22 @@ public class TestTableEngines extends TestBase {
return 0;
}
}
/**
* A filter for rows (lists of objects).
*/
private abstract static class RowFilter {
abstract static class RowFilter {
protected abstract boolean accept(List<Object> row);
protected Integer getInt(List<Object> row, int col) {
return (Integer) row.get(col);
}
protected Long getLong(List<Object> row, int col) {
return (Long) row.get(col);
}
protected String getString(List<Object> row, int col) {
return (String) row.get(col);
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论