提交 00603e4b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Move getMainIndexColumn() to TableBase for reuse

上级 12d530ed
...@@ -35,7 +35,6 @@ import org.h2.mvstore.tx.Transaction; ...@@ -35,7 +35,6 @@ import org.h2.mvstore.tx.Transaction;
import org.h2.mvstore.tx.TransactionStore; import org.h2.mvstore.tx.TransactionStore;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
import org.h2.result.SortOrder;
import org.h2.schema.SchemaObject; import org.h2.schema.SchemaObject;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
...@@ -504,8 +503,8 @@ public class MVTable extends TableBase { ...@@ -504,8 +503,8 @@ public class MVTable extends TableBase {
database.lockMeta(session); database.lockMeta(session);
} }
MVIndex index; MVIndex index;
int mainIndexColumn; int mainIndexColumn = primaryIndex.getMainIndexColumn() != SearchRow.ROWID_INDEX
mainIndexColumn = getMainIndexColumn(indexType, cols); ? SearchRow.ROWID_INDEX : getMainIndexColumn(indexType, cols);
if (database.isStarting()) { if (database.isStarting()) {
if (transactionStore.hasMap("index." + indexId)) { if (transactionStore.hasMap("index." + indexId)) {
mainIndexColumn = SearchRow.ROWID_INDEX; mainIndexColumn = SearchRow.ROWID_INDEX;
...@@ -646,29 +645,6 @@ public class MVTable extends TableBase { ...@@ -646,29 +645,6 @@ public class MVTable extends TableBase {
} }
} }
private int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) {
if (primaryIndex.getMainIndexColumn() != SearchRow.ROWID_INDEX) {
return SearchRow.ROWID_INDEX;
}
if (!indexType.isPrimaryKey() || cols.length != 1) {
return SearchRow.ROWID_INDEX;
}
IndexColumn first = cols[0];
if (first.sortType != SortOrder.ASCENDING) {
return SearchRow.ROWID_INDEX;
}
switch (first.column.getType()) {
case Value.BYTE:
case Value.SHORT:
case Value.INT:
case Value.LONG:
break;
default:
return SearchRow.ROWID_INDEX;
}
return first.column.getColumnId();
}
private static void addRowsToIndex(Session session, ArrayList<Row> list, private static void addRowsToIndex(Session session, ArrayList<Row> list,
Index index) { Index index) {
sortRows(list, index); sortRows(list, index);
......
...@@ -36,7 +36,6 @@ import org.h2.index.TreeIndex; ...@@ -36,7 +36,6 @@ import org.h2.index.TreeIndex;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SortOrder;
import org.h2.schema.SchemaObject; import org.h2.schema.SchemaObject;
import org.h2.util.MathUtils; import org.h2.util.MathUtils;
import org.h2.util.Utils; import org.h2.util.Utils;
...@@ -199,7 +198,8 @@ public class RegularTable extends TableBase { ...@@ -199,7 +198,8 @@ public class RegularTable extends TableBase {
if (database.isStarting() && if (database.isStarting() &&
database.getPageStore().getRootPageId(indexId) != 0) { database.getPageStore().getRootPageId(indexId) != 0) {
mainIndexColumn = -1; mainIndexColumn = -1;
} else if (!database.isStarting() && mainIndex.getRowCount(session) != 0) { } else if (!database.isStarting() && mainIndex.getRowCount(session) != 0
|| mainIndex.getMainIndexColumn() != -1) {
mainIndexColumn = -1; mainIndexColumn = -1;
} else { } else {
mainIndexColumn = getMainIndexColumn(indexType, cols); mainIndexColumn = getMainIndexColumn(indexType, cols);
...@@ -289,29 +289,6 @@ public class RegularTable extends TableBase { ...@@ -289,29 +289,6 @@ public class RegularTable extends TableBase {
return index; return index;
} }
private int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) {
if (mainIndex.getMainIndexColumn() != -1) {
return -1;
}
if (!indexType.isPrimaryKey() || cols.length != 1) {
return -1;
}
IndexColumn first = cols[0];
if (first.sortType != SortOrder.ASCENDING) {
return -1;
}
switch (first.column.getType()) {
case Value.BYTE:
case Value.SHORT:
case Value.INT:
case Value.LONG:
break;
default:
return -1;
}
return first.column.getColumnId();
}
@Override @Override
public boolean canGetRowCount() { public boolean canGetRowCount() {
return true; return true;
......
...@@ -10,9 +10,13 @@ import java.util.List; ...@@ -10,9 +10,13 @@ import java.util.List;
import org.h2.command.ddl.CreateTableData; import org.h2.command.ddl.CreateTableData;
import org.h2.engine.Database; import org.h2.engine.Database;
import org.h2.engine.DbSettings; import org.h2.engine.DbSettings;
import org.h2.index.IndexType;
import org.h2.mvstore.db.MVTableEngine; import org.h2.mvstore.db.MVTableEngine;
import org.h2.result.SearchRow;
import org.h2.result.SortOrder;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.Value;
/** /**
* The base class of a regular table, or a user defined table. * The base class of a regular table, or a user defined table.
...@@ -31,6 +35,33 @@ public abstract class TableBase extends Table { ...@@ -31,6 +35,33 @@ public abstract class TableBase extends Table {
private final boolean globalTemporary; private final boolean globalTemporary;
/**
* Returns main index column if index is an primary key index and has only
* one column with _ROWID_ compatible data type.
*
* @param indexType type of an index
* @param cols columns of the index
* @return main index column or {@link SearchRow#ROWID_INDEX}
*/
public static int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) {
if (!indexType.isPrimaryKey() || cols.length != 1) {
return SearchRow.ROWID_INDEX;
}
IndexColumn first = cols[0];
if (first.sortType != SortOrder.ASCENDING) {
return SearchRow.ROWID_INDEX;
}
switch (first.column.getType()) {
case Value.BYTE:
case Value.SHORT:
case Value.INT:
case Value.LONG:
return first.column.getColumnId();
default:
return SearchRow.ROWID_INDEX;
}
}
public TableBase(CreateTableData data) { public TableBase(CreateTableData data) {
super(data.schema, data.id, data.tableName, super(data.schema, data.id, data.tableName,
data.persistIndexes, data.persistData); data.persistIndexes, data.persistData);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论