提交 a4ed2d4c authored 作者: Andrei Tokar's avatar Andrei Tokar

Magic value replacement with constant

assorted minor refactorings
上级 8b4bfee5
...@@ -114,7 +114,7 @@ public class IndexCursor implements Cursor { ...@@ -114,7 +114,7 @@ public class IndexCursor implements Cursor {
boolean isEnd = condition.isEnd(); boolean isEnd = condition.isEnd();
boolean isIntersects = condition.isSpatialIntersects(); boolean isIntersects = condition.isSpatialIntersects();
int columnId = column.getColumnId(); int columnId = column.getColumnId();
if (columnId >= 0) { if (columnId != SearchRow.ROWID_INDEX) {
IndexColumn idxCol = indexColumns[columnId]; IndexColumn idxCol = indexColumns[columnId];
if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) { if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) {
// if the index column is sorted the other way, we swap // if the index column is sorted the other way, we swap
...@@ -210,7 +210,7 @@ public class IndexCursor implements Cursor { ...@@ -210,7 +210,7 @@ public class IndexCursor implements Cursor {
v = ((ValueGeometry) v.convertTo(Value.GEOMETRY)). v = ((ValueGeometry) v.convertTo(Value.GEOMETRY)).
getEnvelopeUnion(vg); getEnvelopeUnion(vg);
} }
if (columnId < 0) { if (columnId == SearchRow.ROWID_INDEX) {
row.setKey(v.getLong()); row.setKey(v.getLong());
} else { } else {
row.setValue(columnId, v); row.setValue(columnId, v);
...@@ -218,14 +218,21 @@ public class IndexCursor implements Cursor { ...@@ -218,14 +218,21 @@ public class IndexCursor implements Cursor {
return row; return row;
} }
private SearchRow getSearchRow(SearchRow row, int columnId, Value v, private SearchRow getSearchRow(SearchRow row, int columnId, Value v, boolean max) {
boolean max) { Column column = columnId == SearchRow.ROWID_INDEX ?
table.getRowIdColumn() :
table.getColumn(columnId);
int vType = v.getType();
int resType = Value.getHigherOrder(column.getType(), vType);
if(vType != resType) {
v = column.convert(v, session.getDatabase().getMode());
}
if (row == null) { if (row == null) {
row = table.getTemplateRow(); row = table.getTemplateRow();
} else { } else {
v = getMax(row.getValue(columnId), v, max); v = getMax(row.getValue(columnId), v, max);
} }
if (columnId < 0) { if (columnId == SearchRow.ROWID_INDEX) {
row.setKey(v.getLong()); row.setKey(v.getLong());
} else { } else {
row.setValue(columnId, v); row.setValue(columnId, v);
...@@ -257,10 +264,7 @@ public class IndexCursor implements Cursor { ...@@ -257,10 +264,7 @@ public class IndexCursor implements Cursor {
return null; return null;
} }
} }
if (!bigger) { return (comp > 0) == bigger ? a : b;
comp = -comp;
}
return comp > 0 ? a : b;
} }
/** /**
......
...@@ -111,7 +111,7 @@ public class MVDelegateIndex extends BaseIndex implements MVIndex { ...@@ -111,7 +111,7 @@ public class MVDelegateIndex extends BaseIndex implements MVIndex {
@Override @Override
public void remove(Session session) { public void remove(Session session) {
mainIndex.setMainIndexColumn(-1); mainIndex.setMainIndexColumn(SearchRow.ROWID_INDEX);
} }
@Override @Override
......
...@@ -41,7 +41,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -41,7 +41,7 @@ public class MVPrimaryIndex extends BaseIndex {
private final String mapName; private final String mapName;
private final TransactionMap<Value, Value> dataMap; private final TransactionMap<Value, Value> dataMap;
private final AtomicLong lastKey = new AtomicLong(0); private final AtomicLong lastKey = new AtomicLong(0);
private int mainIndexColumn = -1; private int mainIndexColumn = SearchRow.ROWID_INDEX;
public MVPrimaryIndex(Database db, MVTable table, int id, public MVPrimaryIndex(Database db, MVTable table, int id,
IndexColumn[] columns, IndexType indexType) { IndexColumn[] columns, IndexType indexType) {
...@@ -90,7 +90,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -90,7 +90,7 @@ public class MVPrimaryIndex extends BaseIndex {
@Override @Override
public void add(Session session, Row row) { public void add(Session session, Row row) {
if (mainIndexColumn == -1) { if (mainIndexColumn == SearchRow.ROWID_INDEX) {
if (row.getKey() == 0) { if (row.getKey() == 0) {
row.setKey(lastKey.incrementAndGet()); row.setKey(lastKey.incrementAndGet());
} }
...@@ -239,7 +239,7 @@ public class MVPrimaryIndex extends BaseIndex { ...@@ -239,7 +239,7 @@ public class MVPrimaryIndex extends BaseIndex {
@Override @Override
public int getColumnIndex(Column col) { public int getColumnIndex(Column col) {
// can not use this index - use the delegate index instead // can not use this index - use the delegate index instead
return -1; return SearchRow.ROWID_INDEX;
} }
@Override @Override
......
...@@ -33,6 +33,7 @@ import org.h2.mvstore.db.MVTableEngine.Store; ...@@ -33,6 +33,7 @@ import org.h2.mvstore.db.MVTableEngine.Store;
import org.h2.mvstore.tx.Transaction; 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.SortOrder; 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;
...@@ -128,6 +129,9 @@ public class MVTable extends TableBase { ...@@ -128,6 +129,9 @@ public class MVTable extends TableBase {
public MVTable(CreateTableData data, MVTableEngine.Store store) { public MVTable(CreateTableData data, MVTableEngine.Store store) {
super(data); super(data);
nextAnalyze = database.getSettings().analyzeAuto; nextAnalyze = database.getSettings().analyzeAuto;
if(nextAnalyze <= 0) {
nextAnalyze = Integer.MAX_VALUE;
}
this.store = store; this.store = store;
this.transactionStore = store.getTransactionStore(); this.transactionStore = store.getTransactionStore();
this.isHidden = data.isHidden; this.isHidden = data.isHidden;
...@@ -317,15 +321,16 @@ public class MVTable extends TableBase { ...@@ -317,15 +321,16 @@ public class MVTable extends TableBase {
return true; return true;
} }
} }
if (!lockSharedSessions.containsKey(session)) { if (lockSharedSessions.putIfAbsent(session, session) == null) {
traceLock(session, exclusive, TraceLockEvent.TRACE_LOCK_OK, NO_EXTRA_INFO); traceLock(session, exclusive, TraceLockEvent.TRACE_LOCK_OK, NO_EXTRA_INFO);
session.addLock(this); session.addLock(this);
lockSharedSessions.put(session, session);
if (SysProperties.THREAD_DEADLOCK_DETECTOR) { if (SysProperties.THREAD_DEADLOCK_DETECTOR) {
if (SHARED_LOCKS.get() == null) { ArrayList<String> list = SHARED_LOCKS.get();
SHARED_LOCKS.set(new ArrayList<String>()); if (list == null) {
list = new ArrayList<>();
SHARED_LOCKS.set(list);
} }
SHARED_LOCKS.get().add(getName()); list.add(getName());
} }
} }
return true; return true;
...@@ -353,7 +358,7 @@ public class MVTable extends TableBase { ...@@ -353,7 +358,7 @@ public class MVTable extends TableBase {
} }
buff.append(t.toString()); buff.append(t.toString());
if (t instanceof MVTable) { if (t instanceof MVTable) {
if (((MVTable) t).lockExclusiveSession == s) { if (t.isLockedExclusivelyBy(s)) {
buff.append(" (exclusive)"); buff.append(" (exclusive)");
} else { } else {
buff.append(" (shared)"); buff.append(" (shared)");
...@@ -517,12 +522,12 @@ public class MVTable extends TableBase { ...@@ -517,12 +522,12 @@ public class MVTable extends TableBase {
mainIndexColumn = getMainIndexColumn(indexType, cols); mainIndexColumn = getMainIndexColumn(indexType, cols);
if (database.isStarting()) { if (database.isStarting()) {
if (transactionStore.hasMap("index." + indexId)) { if (transactionStore.hasMap("index." + indexId)) {
mainIndexColumn = -1; mainIndexColumn = SearchRow.ROWID_INDEX;
} }
} else if (primaryIndex.getRowCountMax() != 0) { } else if (primaryIndex.getRowCountMax() != 0) {
mainIndexColumn = -1; mainIndexColumn = SearchRow.ROWID_INDEX;
} }
if (mainIndexColumn != -1) { if (mainIndexColumn != SearchRow.ROWID_INDEX) {
primaryIndex.setMainIndexColumn(mainIndexColumn); primaryIndex.setMainIndexColumn(mainIndexColumn);
index = new MVDelegateIndex(this, indexId, indexName, primaryIndex, index = new MVDelegateIndex(this, indexId, indexName, primaryIndex,
indexType); indexType);
...@@ -656,15 +661,15 @@ public class MVTable extends TableBase { ...@@ -656,15 +661,15 @@ public class MVTable extends TableBase {
} }
private int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) { private int getMainIndexColumn(IndexType indexType, IndexColumn[] cols) {
if (primaryIndex.getMainIndexColumn() != -1) { if (primaryIndex.getMainIndexColumn() != SearchRow.ROWID_INDEX) {
return -1; return SearchRow.ROWID_INDEX;
} }
if (!indexType.isPrimaryKey() || cols.length != 1) { if (!indexType.isPrimaryKey() || cols.length != 1) {
return -1; return SearchRow.ROWID_INDEX;
} }
IndexColumn first = cols[0]; IndexColumn first = cols[0];
if (first.sortType != SortOrder.ASCENDING) { if (first.sortType != SortOrder.ASCENDING) {
return -1; return SearchRow.ROWID_INDEX;
} }
switch (first.column.getType()) { switch (first.column.getType()) {
case Value.BYTE: case Value.BYTE:
...@@ -673,7 +678,7 @@ public class MVTable extends TableBase { ...@@ -673,7 +678,7 @@ public class MVTable extends TableBase {
case Value.LONG: case Value.LONG:
break; break;
default: default:
return -1; return SearchRow.ROWID_INDEX;
} }
return first.column.getColumnId(); return first.column.getColumnId();
} }
...@@ -687,10 +692,10 @@ public class MVTable extends TableBase { ...@@ -687,10 +692,10 @@ public class MVTable extends TableBase {
list.clear(); list.clear();
} }
private static void sortRows(ArrayList<Row> list, final Index index) { private static void sortRows(ArrayList<? extends SearchRow> list, final Index index) {
Collections.sort(list, new Comparator<Row>() { Collections.sort(list, new Comparator<SearchRow>() {
@Override @Override
public int compare(Row r1, Row r2) { public int compare(SearchRow r1, SearchRow r2) {
return index.compareRows(r1, r2); return index.compareRows(r1, r2);
} }
}); });
...@@ -754,13 +759,12 @@ public class MVTable extends TableBase { ...@@ -754,13 +759,12 @@ public class MVTable extends TableBase {
private void analyzeIfRequired(Session session) { private void analyzeIfRequired(Session session) {
synchronized (this) { synchronized (this) {
if (nextAnalyze == 0 || nextAnalyze > changesSinceAnalyze++) { if (++changesSinceAnalyze <= nextAnalyze) {
return; return;
} }
changesSinceAnalyze = 0; changesSinceAnalyze = 0;
int n = 2 * nextAnalyze; if (nextAnalyze <= Integer.MAX_VALUE / 2) {
if (n > 0) { nextAnalyze *= 2;
nextAnalyze = n;
} }
} }
session.markTableForAnalyze(this); session.markTableForAnalyze(this);
...@@ -885,7 +889,7 @@ public class MVTable extends TableBase { ...@@ -885,7 +889,7 @@ public class MVTable extends TableBase {
public Column getRowIdColumn() { public Column getRowIdColumn() {
if (rowIdColumn == null) { if (rowIdColumn == null) {
rowIdColumn = new Column(Column.ROWID, Value.LONG); rowIdColumn = new Column(Column.ROWID, Value.LONG);
rowIdColumn.setTable(this, -1); rowIdColumn.setTable(this, SearchRow.ROWID_INDEX);
} }
return rowIdColumn; return rowIdColumn;
} }
......
...@@ -73,7 +73,7 @@ public class RowImpl implements Row { ...@@ -73,7 +73,7 @@ public class RowImpl implements Row {
@Override @Override
public Value getValue(int i) { public Value getValue(int i) {
return i == -1 ? ValueLong.get(key) : data[i]; return i == SearchRow.ROWID_INDEX ? ValueLong.get(key) : data[i];
} }
/** /**
...@@ -93,7 +93,7 @@ public class RowImpl implements Row { ...@@ -93,7 +93,7 @@ public class RowImpl implements Row {
@Override @Override
public void setValue(int i, Value v) { public void setValue(int i, Value v) {
if (i == -1) { if (i == SearchRow.ROWID_INDEX) {
this.key = v.getLong(); this.key = v.getLong();
} else { } else {
data[i] = v; data[i] = v;
......
...@@ -13,6 +13,8 @@ import org.h2.value.Value; ...@@ -13,6 +13,8 @@ import org.h2.value.Value;
*/ */
public interface SearchRow { public interface SearchRow {
int ROWID_INDEX = -1;
/** /**
* An empty array of SearchRow objects. * An empty array of SearchRow objects.
*/ */
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论