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