Unverified 提交 cdaa59b4 authored 作者: Noel Grandin's avatar Noel Grandin 提交者: GitHub

Merge pull request #1210 from katzyn/pagestore

Remove MVCC logic from PageStore
...@@ -36,7 +36,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -36,7 +36,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
protected int[] columnIds; protected int[] columnIds;
protected Table table; protected Table table;
protected IndexType indexType; protected IndexType indexType;
protected boolean isMultiVersion;
/** /**
* Initialize the base index. * Initialize the base index.
...@@ -350,11 +349,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -350,11 +349,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
long k1 = rowData.getKey(); long k1 = rowData.getKey();
long k2 = compare.getKey(); long k2 = compare.getKey();
if (k1 == k2) { if (k1 == k2) {
if (isMultiVersion) {
int v1 = rowData.getVersion();
int v2 = compare.getVersion();
return Integer.compare(v2, v1);
}
return 0; return 0;
} }
return k1 > k2 ? 1 : -1; return k1 > k2 ? 1 : -1;
...@@ -457,10 +451,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index { ...@@ -457,10 +451,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
// nothing to do // nothing to do
} }
void setMultiVersion(boolean multiVersion) {
this.isMultiVersion = multiVersion;
}
@Override @Override
public Row getRow(Session session, long key) { public Row getRow(Session session, long key) {
throw DbException.getUnsupportedException(toString()); throw DbException.getUnsupportedException(toString());
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.index;
import org.h2.engine.Session;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
/**
* The cursor implementation for the multi-version index.
*/
public class MultiVersionCursor implements Cursor {
private final MultiVersionIndex index;
private final Session session;
private final Cursor baseCursor, deltaCursor;
private final Object sync;
private SearchRow baseRow;
private Row deltaRow;
private boolean onBase;
private boolean end;
private boolean needNewDelta, needNewBase;
private boolean reverse;
MultiVersionCursor(Session session, MultiVersionIndex index, Cursor base,
Cursor delta, Object sync) {
this.session = session;
this.index = index;
this.baseCursor = base;
this.deltaCursor = delta;
this.sync = sync;
needNewDelta = true;
needNewBase = true;
}
/**
* Load the current row.
*/
void loadCurrent() {
synchronized (sync) {
baseRow = baseCursor.getSearchRow();
deltaRow = deltaCursor.get();
needNewDelta = false;
needNewBase = false;
}
}
private void loadNext(boolean base) {
synchronized (sync) {
if (base) {
if (step(baseCursor)) {
baseRow = baseCursor.getSearchRow();
} else {
baseRow = null;
}
} else {
if (step(deltaCursor)) {
deltaRow = deltaCursor.get();
} else {
deltaRow = null;
}
}
}
}
private boolean step(Cursor cursor) {
return reverse ? cursor.previous() : cursor.next();
}
@Override
public Row get() {
synchronized (sync) {
if (end) {
return null;
}
return onBase ? baseCursor.get() : deltaCursor.get();
}
}
@Override
public SearchRow getSearchRow() {
synchronized (sync) {
if (end) {
return null;
}
return onBase ? baseCursor.getSearchRow() : deltaCursor.getSearchRow();
}
}
@Override
public boolean next() {
synchronized (sync) {
if (SysProperties.CHECK && end) {
DbException.throwInternalError();
}
while (true) {
if (needNewDelta) {
loadNext(false);
needNewDelta = false;
}
if (needNewBase) {
loadNext(true);
needNewBase = false;
}
if (deltaRow == null) {
if (baseRow == null) {
end = true;
return false;
}
onBase = true;
needNewBase = true;
return true;
}
int sessionId = deltaRow.getSessionId();
boolean isThisSession = sessionId == session.getId();
boolean isDeleted = deltaRow.isDeleted();
if (isThisSession && isDeleted) {
needNewDelta = true;
continue;
}
if (baseRow == null) {
if (isDeleted) {
if (isThisSession) {
end = true;
return false;
}
// the row was deleted by another session: return it
onBase = false;
needNewDelta = true;
return true;
}
DbException.throwInternalError();
}
int compare = index.compareRows(deltaRow, baseRow);
if (compare == 0) {
// can't use compareKeys because the
// version would be compared as well
long k1 = deltaRow.getKey();
long k2 = baseRow.getKey();
compare = Long.compare(k1, k2);
}
if (compare == 0) {
if (isDeleted) {
if (isThisSession) {
DbException.throwInternalError();
}
// another session updated the row
} else {
if (isThisSession) {
onBase = false;
needNewBase = true;
needNewDelta = true;
return true;
}
// another session inserted the row: ignore
needNewBase = true;
needNewDelta = true;
continue;
}
}
if (compare > 0) {
onBase = true;
needNewBase = true;
return true;
}
onBase = false;
needNewDelta = true;
return true;
}
}
}
@Override
public boolean previous() {
reverse = true;
try {
return next();
} finally {
reverse = false;
}
}
}
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.index;
import java.util.ArrayList;
import org.h2.api.ErrorCode;
import org.h2.command.dml.AllColumnsForPlan;
import org.h2.engine.Database;
import org.h2.engine.DbObject;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
import org.h2.result.SortOrder;
import org.h2.schema.Schema;
import org.h2.table.Column;
import org.h2.table.IndexColumn;
import org.h2.table.RegularTable;
import org.h2.table.Table;
import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* A multi-version index is a combination of a regular index,
* and a in-memory tree index that contains uncommitted changes.
* Uncommitted changes can include new rows, and deleted rows.
*/
public class MultiVersionIndex implements Index {
private final Index base;
private final TreeIndex delta;
private final RegularTable table;
private final Object sync;
private final Column firstColumn;
public MultiVersionIndex(Index base, RegularTable table) {
this.base = base;
this.table = table;
IndexType deltaIndexType = IndexType.createNonUnique(false);
if (base instanceof SpatialIndex) {
throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1,
"MVCC & spatial index");
}
this.delta = new TreeIndex(table, -1, "DELTA", base.getIndexColumns(),
deltaIndexType);
delta.setMultiVersion(true);
this.sync = base.getDatabase();
this.firstColumn = base.getColumns()[0];
}
@Override
public void add(Session session, Row row) {
synchronized (sync) {
base.add(session, row);
if (removeIfExists(session, row)) {
// for example rolling back a delete operation
} else if (row.getSessionId() != 0) {
// don't insert rows that are added when creating an index
delta.add(session, row);
}
}
}
@Override
public void close(Session session) {
synchronized (sync) {
base.close(session);
}
}
@Override
public boolean isFindUsingFullTableScan() {
return base.isFindUsingFullTableScan();
}
@Override
public Cursor find(TableFilter filter, SearchRow first, SearchRow last) {
synchronized (sync) {
Cursor baseCursor = base.find(filter, first, last);
Cursor deltaCursor = delta.find(filter, first, last);
return new MultiVersionCursor(filter.getSession(), this,
baseCursor, deltaCursor, sync);
}
}
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
synchronized (sync) {
Cursor baseCursor = base.find(session, first, last);
Cursor deltaCursor = delta.find(session, first, last);
return new MultiVersionCursor(session, this, baseCursor, deltaCursor, sync);
}
}
@Override
public Cursor findNext(Session session, SearchRow first, SearchRow last) {
throw DbException.throwInternalError(toString());
}
@Override
public boolean canFindNext() {
// TODO possible, but more complicated
return false;
}
@Override
public boolean canGetFirstOrLast() {
return base.canGetFirstOrLast() && delta.canGetFirstOrLast();
}
@Override
public Cursor findFirstOrLast(Session session, boolean first) {
if (first) {
// TODO optimization: this loops through NULL elements
Cursor cursor = find(session, null, null);
while (cursor.next()) {
SearchRow row = cursor.getSearchRow();
Value v = row.getValue(firstColumn.getColumnId());
if (v != ValueNull.INSTANCE) {
return cursor;
}
}
return cursor;
}
Cursor baseCursor = base.findFirstOrLast(session, false);
Cursor deltaCursor = delta.findFirstOrLast(session, false);
MultiVersionCursor cursor = new MultiVersionCursor(session, this,
baseCursor, deltaCursor, sync);
cursor.loadCurrent();
// TODO optimization: this loops through NULL elements
while (cursor.previous()) {
SearchRow row = cursor.getSearchRow();
if (row == null) {
break;
}
Value v = row.getValue(firstColumn.getColumnId());
if (v != ValueNull.INSTANCE) {
return cursor;
}
}
return cursor;
}
@Override
public double getCost(Session session, int[] masks,
TableFilter[] filters, int filter, SortOrder sortOrder,
AllColumnsForPlan allColumnsSet) {
return base.getCost(session, masks, filters, filter, sortOrder, allColumnsSet);
}
@Override
public boolean needRebuild() {
return base.needRebuild();
}
/**
* Check if there is an uncommitted row with the given key
* within a different session.
*
* @param session the original session
* @param row the row (only the key is checked)
* @return true if there is an uncommitted row
*/
public boolean isUncommittedFromOtherSession(Session session, Row row) {
Cursor c = delta.find(session, row, row);
while (c.next()) {
Row r = c.get();
return r.getSessionId() != session.getId();
}
return false;
}
private boolean removeIfExists(Session session, Row row) {
// maybe it was inserted by the same session just before
Cursor c = delta.find(session, row, row);
while (c.next()) {
Row r = c.get();
if (r.getKey() == row.getKey() && r.getVersion() == row.getVersion()) {
if (r != row && table.getScanIndex(session).compareRows(r, row) != 0) {
row.setVersion(r.getVersion() + 1);
} else {
delta.remove(session, r);
return true;
}
}
}
return false;
}
@Override
public void remove(Session session, Row row) {
synchronized (sync) {
base.remove(session, row);
if (removeIfExists(session, row)) {
// added and deleted in the same transaction: no change
} else {
delta.add(session, row);
}
}
}
@Override
public void remove(Session session) {
synchronized (sync) {
base.remove(session);
}
}
@Override
public void truncate(Session session) {
synchronized (sync) {
delta.truncate(session);
base.truncate(session);
}
}
@Override
public void commit(int operation, Row row) {
synchronized (sync) {
removeIfExists(null, row);
}
}
@Override
public int compareRows(SearchRow rowData, SearchRow compare) {
return base.compareRows(rowData, compare);
}
@Override
public int getColumnIndex(Column col) {
return base.getColumnIndex(col);
}
@Override
public boolean isFirstColumn(Column column) {
return base.isFirstColumn(column);
}
@Override
public Column[] getColumns() {
return base.getColumns();
}
@Override
public IndexColumn[] getIndexColumns() {
return base.getIndexColumns();
}
@Override
public String getCreateSQL() {
return base.getCreateSQL();
}
@Override
public String getCreateSQLForCopy(Table forTable, String quotedName) {
return base.getCreateSQLForCopy(forTable, quotedName);
}
@Override
public String getDropSQL() {
return base.getDropSQL();
}
@Override
public IndexType getIndexType() {
return base.getIndexType();
}
@Override
public String getPlanSQL() {
return base.getPlanSQL();
}
@Override
public long getRowCount(Session session) {
return base.getRowCount(session);
}
@Override
public Table getTable() {
return base.getTable();
}
@Override
public int getType() {
return base.getType();
}
@Override
public void removeChildrenAndResources(Session session) {
synchronized (sync) {
table.removeIndex(this);
remove(session);
}
}
@Override
public String getSQL() {
return base.getSQL();
}
@Override
public Schema getSchema() {
return base.getSchema();
}
@Override
public void checkRename() {
base.checkRename();
}
@Override
public ArrayList<DbObject> getChildren() {
return base.getChildren();
}
@Override
public String getComment() {
return base.getComment();
}
@Override
public Database getDatabase() {
return base.getDatabase();
}
@Override
public int getId() {
return base.getId();
}
@Override
public String getName() {
return base.getName();
}
@Override
public boolean isTemporary() {
return base.isTemporary();
}
@Override
public void rename(String newName) {
base.rename(newName);
}
@Override
public void setComment(String comment) {
base.setComment(comment);
}
@Override
public void setTemporary(boolean temporary) {
base.setTemporary(temporary);
}
@Override
public long getRowCountApproximation() {
return base.getRowCountApproximation();
}
@Override
public long getDiskSpaceUsed() {
return base.getDiskSpaceUsed();
}
public Index getBaseIndex() {
return base;
}
@Override
public Row getRow(Session session, long key) {
return base.getRow(session, key);
}
@Override
public boolean isHidden() {
return base.isHidden();
}
@Override
public boolean isRowIdIndex() {
return base.isRowIdIndex() && delta.isRowIdIndex();
}
@Override
public boolean canScan() {
return base.canScan();
}
@Override
public void setSortedInsertMode(boolean sortedInsertMode) {
base.setSortedInsertMode(sortedInsertMode);
delta.setSortedInsertMode(sortedInsertMode);
}
@Override
public IndexLookupBatch createLookupBatch(TableFilter[] filters, int filter) {
// Lookup batching is not supported.
return null;
}
}
...@@ -131,11 +131,9 @@ abstract class PageData extends Page { ...@@ -131,11 +131,9 @@ abstract class PageData extends Page {
* @param session the session * @param session the session
* @param minKey the smallest key * @param minKey the smallest key
* @param maxKey the largest key * @param maxKey the largest key
* @param multiVersion if the delta should be used
* @return the cursor * @return the cursor
*/ */
abstract Cursor find(Session session, long minKey, long maxKey, abstract Cursor find(Session session, long minKey, long maxKey);
boolean multiVersion);
/** /**
* Get the key at this position. * Get the key at this position.
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
*/ */
package org.h2.index; package org.h2.index;
import java.util.Iterator;
import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
...@@ -20,20 +18,11 @@ class PageDataCursor implements Cursor { ...@@ -20,20 +18,11 @@ class PageDataCursor implements Cursor {
private int idx; private int idx;
private final long maxKey; private final long maxKey;
private Row row; private Row row;
private final boolean multiVersion;
private final Session session;
private Iterator<Row> delta;
PageDataCursor(Session session, PageDataLeaf current, int idx, long maxKey, PageDataCursor(PageDataLeaf current, int idx, long maxKey) {
boolean multiVersion) {
this.current = current; this.current = current;
this.idx = idx; this.idx = idx;
this.maxKey = maxKey; this.maxKey = maxKey;
this.multiVersion = multiVersion;
this.session = session;
if (multiVersion) {
delta = current.index.getDelta();
}
} }
@Override @Override
...@@ -48,30 +37,7 @@ class PageDataCursor implements Cursor { ...@@ -48,30 +37,7 @@ class PageDataCursor implements Cursor {
@Override @Override
public boolean next() { public boolean next() {
if (!multiVersion) {
nextRow();
return checkMax();
}
while (true) {
if (delta != null) {
if (!delta.hasNext()) {
delta = null;
row = null;
continue;
}
row = delta.next();
if (!row.isDeleted() || row.getSessionId() == session.getId()) {
continue;
}
} else {
nextRow(); nextRow();
if (row != null && row.getSessionId() != 0 &&
row.getSessionId() != session.getId()) {
continue;
}
}
break;
}
return checkMax(); return checkMax();
} }
......
...@@ -5,16 +5,11 @@ ...@@ -5,16 +5,11 @@
*/ */
package org.h2.index; package org.h2.index;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.dml.AllColumnsForPlan; import org.h2.command.dml.AllColumnsForPlan;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.engine.SysProperties; import org.h2.engine.SysProperties;
import org.h2.engine.UndoLogRecord;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
...@@ -40,9 +35,6 @@ public class PageDataIndex extends PageIndex { ...@@ -40,9 +35,6 @@ public class PageDataIndex extends PageIndex {
private final RegularTable tableData; private final RegularTable tableData;
private long lastKey; private long lastKey;
private long rowCount; private long rowCount;
private HashSet<Row> delta;
private int rowCountDiff;
private final HashMap<Integer, Integer> sessionRowCount;
private int mainIndexColumn = -1; private int mainIndexColumn = -1;
private DbException fastDuplicateKeyException; private DbException fastDuplicateKeyException;
...@@ -53,21 +45,12 @@ public class PageDataIndex extends PageIndex { ...@@ -53,21 +45,12 @@ public class PageDataIndex extends PageIndex {
private int memoryPerPage; private int memoryPerPage;
private int memoryCount; private int memoryCount;
private final boolean multiVersion;
public PageDataIndex(RegularTable table, int id, IndexColumn[] columns, public PageDataIndex(RegularTable table, int id, IndexColumn[] columns,
IndexType indexType, boolean create, Session session) { IndexType indexType, boolean create, Session session) {
initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType); initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType);
this.multiVersion = database.isMVStore();
// trace = database.getTrace(Trace.PAGE_STORE + "_di"); // trace = database.getTrace(Trace.PAGE_STORE + "_di");
// trace.setLevel(TraceSystem.DEBUG); // trace.setLevel(TraceSystem.DEBUG);
if (multiVersion) {
sessionRowCount = new HashMap<>();
isMultiVersion = true;
} else {
sessionRowCount = null;
}
tableData = table; tableData = table;
this.store = database.getPageStore(); this.store = database.getPageStore();
store.addIndex(this); store.addIndex(this);
...@@ -190,16 +173,6 @@ public class PageDataIndex extends PageIndex { ...@@ -190,16 +173,6 @@ public class PageDataIndex extends PageIndex {
root = newRoot; root = newRoot;
} }
row.setDeleted(false); row.setDeleted(false);
if (multiVersion) {
if (delta == null) {
delta = new HashSet<>();
}
boolean wasDeleted = delta.remove(row);
if (!wasDeleted) {
delta.add(row);
}
incrementRowCount(session.getId(), 1);
}
invalidateRowCount(); invalidateRowCount();
rowCount++; rowCount++;
store.logAddOrRemoveRow(session, tableData.getId(), row, true); store.logAddOrRemoveRow(session, tableData.getId(), row, true);
...@@ -279,7 +252,7 @@ public class PageDataIndex extends PageIndex { ...@@ -279,7 +252,7 @@ public class PageDataIndex extends PageIndex {
long from = first == null ? Long.MIN_VALUE : first.getKey(); long from = first == null ? Long.MIN_VALUE : first.getKey();
long to = last == null ? Long.MAX_VALUE : last.getKey(); long to = last == null ? Long.MAX_VALUE : last.getKey();
PageData root = getPage(rootPageId, 0); PageData root = getPage(rootPageId, 0);
return root.find(session, from, to, isMultiVersion); return root.find(session, from, to);
} }
...@@ -289,12 +262,11 @@ public class PageDataIndex extends PageIndex { ...@@ -289,12 +262,11 @@ public class PageDataIndex extends PageIndex {
* @param session the session * @param session the session
* @param first the key of the first row * @param first the key of the first row
* @param last the key of the last row * @param last the key of the last row
* @param multiVersion if mvcc should be used
* @return the cursor * @return the cursor
*/ */
Cursor find(Session session, long first, long last, boolean multiVersion) { Cursor find(Session session, long first, long last) {
PageData root = getPage(rootPageId, 0); PageData root = getPage(rootPageId, 0);
return root.find(session, first, last, multiVersion); return root.find(session, first, last);
} }
@Override @Override
...@@ -350,18 +322,6 @@ public class PageDataIndex extends PageIndex { ...@@ -350,18 +322,6 @@ public class PageDataIndex extends PageIndex {
store.incrementChangeCount(); store.incrementChangeCount();
} }
} }
if (multiVersion) {
// if storage is null, the delete flag is not yet set
row.setDeleted(true);
if (delta == null) {
delta = new HashSet<>();
}
boolean wasAdded = delta.remove(row);
if (!wasAdded) {
delta.add(row);
}
incrementRowCount(session.getId(), -1);
}
store.logAddOrRemoveRow(session, tableData.getId(), row, false); store.logAddOrRemoveRow(session, tableData.getId(), row, false);
} }
...@@ -387,9 +347,6 @@ public class PageDataIndex extends PageIndex { ...@@ -387,9 +347,6 @@ public class PageDataIndex extends PageIndex {
session.commit(false); session.commit(false);
database.getLobStorage().removeAllForTable(table.getId()); database.getLobStorage().removeAllForTable(table.getId());
} }
if (multiVersion) {
sessionRowCount.clear();
}
tableData.setRowCount(0); tableData.setRowCount(0);
} }
...@@ -439,13 +396,6 @@ public class PageDataIndex extends PageIndex { ...@@ -439,13 +396,6 @@ public class PageDataIndex extends PageIndex {
@Override @Override
public long getRowCount(Session session) { public long getRowCount(Session session) {
if (multiVersion) {
Integer i = sessionRowCount.get(session.getId());
long count = i == null ? 0 : i.intValue();
count += rowCount;
count -= rowCountDiff;
return count;
}
return rowCount; return rowCount;
} }
...@@ -476,46 +426,11 @@ public class PageDataIndex extends PageIndex { ...@@ -476,46 +426,11 @@ public class PageDataIndex extends PageIndex {
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("{0} close", this); trace.debug("{0} close", this);
} }
if (delta != null) {
delta.clear();
}
rowCountDiff = 0;
if (sessionRowCount != null) {
sessionRowCount.clear();
}
// can not close the index because it might get used afterwards, // can not close the index because it might get used afterwards,
// for example after running recovery // for example after running recovery
writeRowCount(); writeRowCount();
} }
Iterator<Row> getDelta() {
if (delta == null) {
return Collections.emptyIterator();
}
return delta.iterator();
}
private void incrementRowCount(int sessionId, int count) {
if (multiVersion) {
Integer id = sessionId;
Integer c = sessionRowCount.get(id);
int current = c == null ? 0 : c.intValue();
sessionRowCount.put(id, current + count);
rowCountDiff += count;
}
}
@Override
public void commit(int operation, Row row) {
if (multiVersion) {
if (delta != null) {
delta.remove(row);
}
incrementRowCount(row.getSessionId(),
operation == UndoLogRecord.DELETE ? 1 : -1);
}
}
/** /**
* The root page has changed. * The root page has changed.
* *
......
...@@ -315,9 +315,9 @@ public class PageDataLeaf extends PageData { ...@@ -315,9 +315,9 @@ public class PageDataLeaf extends PageData {
} }
@Override @Override
Cursor find(Session session, long minKey, long maxKey, boolean multiVersion) { Cursor find(Session session, long minKey, long maxKey) {
int x = find(minKey); int x = find(minKey);
return new PageDataCursor(session, this, x, maxKey, multiVersion); return new PageDataCursor(this, x, maxKey);
} }
/** /**
......
...@@ -160,11 +160,10 @@ public class PageDataNode extends PageData { ...@@ -160,11 +160,10 @@ public class PageDataNode extends PageData {
} }
@Override @Override
Cursor find(Session session, long minKey, long maxKey, boolean multiVersion) { Cursor find(Session session, long minKey, long maxKey) {
int x = find(minKey); int x = find(minKey);
int child = childPageIds[x]; int child = childPageIds[x];
return index.getPage(child, getPos()).find(session, minKey, maxKey, return index.getPage(child, getPos()).find(session, minKey, maxKey);
multiVersion);
} }
@Override @Override
......
...@@ -67,17 +67,17 @@ public class PageDelegateIndex extends PageIndex { ...@@ -67,17 +67,17 @@ public class PageDelegateIndex extends PageIndex {
// ifNull is MIN_VALUE as well, because the column is never NULL // ifNull is MIN_VALUE as well, because the column is never NULL
// so avoid returning all rows (returning one row is OK) // so avoid returning all rows (returning one row is OK)
long max = mainIndex.getKey(last, Long.MAX_VALUE, Long.MIN_VALUE); long max = mainIndex.getKey(last, Long.MAX_VALUE, Long.MIN_VALUE);
return mainIndex.find(session, min, max, false); return mainIndex.find(session, min, max);
} }
@Override @Override
public Cursor findFirstOrLast(Session session, boolean first) { public Cursor findFirstOrLast(Session session, boolean first) {
Cursor cursor; Cursor cursor;
if (first) { if (first) {
cursor = mainIndex.find(session, Long.MIN_VALUE, Long.MAX_VALUE, false); cursor = mainIndex.find(session, Long.MIN_VALUE, Long.MAX_VALUE);
} else { } else {
long x = mainIndex.getLastKey(); long x = mainIndex.getLastKey();
cursor = mainIndex.find(session, x, x, false); cursor = mainIndex.find(session, x, x);
} }
cursor.next(); cursor.next();
return cursor; return cursor;
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
*/ */
package org.h2.index; package org.h2.index;
import java.util.Iterator;
import org.h2.engine.Session;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
...@@ -17,17 +15,9 @@ import org.h2.result.SearchRow; ...@@ -17,17 +15,9 @@ import org.h2.result.SearchRow;
public class ScanCursor implements Cursor { public class ScanCursor implements Cursor {
private final ScanIndex scan; private final ScanIndex scan;
private Row row; private Row row;
private final Session session;
private final boolean multiVersion;
private Iterator<Row> delta;
ScanCursor(Session session, ScanIndex scan, boolean multiVersion) { ScanCursor(ScanIndex scan) {
this.session = session;
this.scan = scan; this.scan = scan;
this.multiVersion = multiVersion;
if (multiVersion) {
delta = scan.getDelta();
}
row = null; row = null;
} }
...@@ -43,29 +33,6 @@ public class ScanCursor implements Cursor { ...@@ -43,29 +33,6 @@ public class ScanCursor implements Cursor {
@Override @Override
public boolean next() { public boolean next() {
if (multiVersion) {
while (true) {
if (delta != null) {
if (!delta.hasNext()) {
delta = null;
row = null;
continue;
}
row = delta.next();
if (!row.isDeleted() || row.getSessionId() == session.getId()) {
continue;
}
} else {
row = scan.getNextRow(row);
if (row != null && row.getSessionId() != 0 &&
row.getSessionId() != session.getId()) {
continue;
}
}
break;
}
return row != null;
}
row = scan.getNextRow(row); row = scan.getNextRow(row);
return row != null; return row != null;
} }
......
...@@ -6,16 +6,11 @@ ...@@ -6,16 +6,11 @@
package org.h2.index; package org.h2.index;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import org.h2.api.ErrorCode; import org.h2.api.ErrorCode;
import org.h2.command.dml.AllColumnsForPlan; import org.h2.command.dml.AllColumnsForPlan;
import org.h2.engine.Constants; import org.h2.engine.Constants;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.engine.UndoLogRecord;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.result.Row; import org.h2.result.Row;
import org.h2.result.SearchRow; import org.h2.result.SearchRow;
...@@ -36,19 +31,11 @@ public class ScanIndex extends BaseIndex { ...@@ -36,19 +31,11 @@ public class ScanIndex extends BaseIndex {
private long firstFree = -1; private long firstFree = -1;
private ArrayList<Row> rows = Utils.newSmallArrayList(); private ArrayList<Row> rows = Utils.newSmallArrayList();
private final RegularTable tableData; private final RegularTable tableData;
private int rowCountDiff;
private final HashMap<Integer, Integer> sessionRowCount;
private HashSet<Row> delta;
private long rowCount; private long rowCount;
public ScanIndex(RegularTable table, int id, IndexColumn[] columns, public ScanIndex(RegularTable table, int id, IndexColumn[] columns,
IndexType indexType) { IndexType indexType) {
initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType); initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType);
if (database.isMVStore()) {
sessionRowCount = new HashMap<>();
} else {
sessionRowCount = null;
}
tableData = table; tableData = table;
} }
...@@ -66,10 +53,6 @@ public class ScanIndex extends BaseIndex { ...@@ -66,10 +53,6 @@ public class ScanIndex extends BaseIndex {
} }
tableData.setRowCount(0); tableData.setRowCount(0);
rowCount = 0; rowCount = 0;
rowCountDiff = 0;
if (database.isMVStore()) {
sessionRowCount.clear();
}
} }
@Override @Override
...@@ -102,44 +85,13 @@ public class ScanIndex extends BaseIndex { ...@@ -102,44 +85,13 @@ public class ScanIndex extends BaseIndex {
rows.set((int) key, row); rows.set((int) key, row);
} }
row.setDeleted(false); row.setDeleted(false);
if (database.isMVStore()) {
if (delta == null) {
delta = new HashSet<>();
}
boolean wasDeleted = delta.remove(row);
if (!wasDeleted) {
delta.add(row);
}
incrementRowCount(session.getId(), 1);
}
rowCount++; rowCount++;
} }
@Override
public void commit(int operation, Row row) {
if (database.isMVStore()) {
if (delta != null) {
delta.remove(row);
}
incrementRowCount(row.getSessionId(),
operation == UndoLogRecord.DELETE ? 1 : -1);
}
}
private void incrementRowCount(int sessionId, int count) {
if (database.isMVStore()) {
Integer id = sessionId;
Integer c = sessionRowCount.get(id);
int current = c == null ? 0 : c.intValue();
sessionRowCount.put(id, current + count);
rowCountDiff += count;
}
}
@Override @Override
public void remove(Session session, Row row) { public void remove(Session session, Row row) {
// in-memory // in-memory
if (!database.isMVStore() && rowCount == 1) { if (rowCount == 1) {
rows = Utils.newSmallArrayList(); rows = Utils.newSmallArrayList();
firstFree = -1; firstFree = -1;
} else { } else {
...@@ -153,24 +105,12 @@ public class ScanIndex extends BaseIndex { ...@@ -153,24 +105,12 @@ public class ScanIndex extends BaseIndex {
rows.set((int) key, free); rows.set((int) key, free);
firstFree = key; firstFree = key;
} }
if (database.isMVStore()) {
// if storage is null, the delete flag is not yet set
row.setDeleted(true);
if (delta == null) {
delta = new HashSet<>();
}
boolean wasAdded = delta.remove(row);
if (!wasAdded) {
delta.add(row);
}
incrementRowCount(session.getId(), -1);
}
rowCount--; rowCount--;
} }
@Override @Override
public Cursor find(Session session, SearchRow first, SearchRow last) { public Cursor find(Session session, SearchRow first, SearchRow last) {
return new ScanCursor(session, this, database.isMVStore()); return new ScanCursor(this);
} }
@Override @Override
...@@ -182,13 +122,6 @@ public class ScanIndex extends BaseIndex { ...@@ -182,13 +122,6 @@ public class ScanIndex extends BaseIndex {
@Override @Override
public long getRowCount(Session session) { public long getRowCount(Session session) {
if (database.isMVStore()) {
Integer i = sessionRowCount.get(session.getId());
long count = i == null ? 0 : i.intValue();
count += rowCount;
count -= rowCountDiff;
return count;
}
return rowCount; return rowCount;
} }
...@@ -248,13 +181,6 @@ public class ScanIndex extends BaseIndex { ...@@ -248,13 +181,6 @@ public class ScanIndex extends BaseIndex {
throw DbException.getUnsupportedException("SCAN"); throw DbException.getUnsupportedException("SCAN");
} }
Iterator<Row> getDelta() {
if (delta == null) {
return Collections.emptyIterator();
}
return delta.iterator();
}
@Override @Override
public long getRowCountApproximation() { public long getRowCountApproximation() {
return rowCount; return rowCount;
......
...@@ -24,7 +24,6 @@ import org.h2.engine.SysProperties; ...@@ -24,7 +24,6 @@ import org.h2.engine.SysProperties;
import org.h2.index.Cursor; import org.h2.index.Cursor;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.IndexType; import org.h2.index.IndexType;
import org.h2.index.MultiVersionIndex;
import org.h2.index.PageBtreeIndex; import org.h2.index.PageBtreeIndex;
import org.h2.index.PageBtreeLeaf; import org.h2.index.PageBtreeLeaf;
import org.h2.index.PageBtreeNode; import org.h2.index.PageBtreeNode;
...@@ -1731,13 +1730,7 @@ public class PageStore implements CacheWriter { ...@@ -1731,13 +1730,7 @@ public class PageStore implements CacheWriter {
} }
meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null); meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
} }
PageIndex index; metaObjects.put(id, (PageIndex) meta);
if (meta instanceof MultiVersionIndex) {
index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
} else {
index = (PageIndex) meta;
}
metaObjects.put(id, index);
} }
/** /**
......
...@@ -42,7 +42,6 @@ import org.h2.expression.ValueExpression; ...@@ -42,7 +42,6 @@ import org.h2.expression.ValueExpression;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.IndexType; import org.h2.index.IndexType;
import org.h2.index.MetaIndex; import org.h2.index.MetaIndex;
import org.h2.index.MultiVersionIndex;
import org.h2.jdbc.JdbcSQLException; import org.h2.jdbc.JdbcSQLException;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.mvstore.FileStore; import org.h2.mvstore.FileStore;
...@@ -935,13 +934,7 @@ public class MetaTable extends Table { ...@@ -935,13 +934,7 @@ public class MetaTable extends Table {
} }
} }
IndexColumn[] cols = index.getIndexColumns(); IndexColumn[] cols = index.getIndexColumns();
String indexClass; String indexClass = index.getClass().getName();
if (index instanceof MultiVersionIndex) {
indexClass = ((MultiVersionIndex) index).
getBaseIndex().getClass().getName();
} else {
indexClass = index.getClass().getName();
}
for (int k = 0; k < cols.length; k++) { for (int k = 0; k < cols.length; k++) {
IndexColumn idxCol = cols[k]; IndexColumn idxCol = cols[k];
Column column = idxCol.column; Column column = idxCol.column;
......
...@@ -26,7 +26,6 @@ import org.h2.index.Cursor; ...@@ -26,7 +26,6 @@ import org.h2.index.Cursor;
import org.h2.index.HashIndex; import org.h2.index.HashIndex;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.index.IndexType; import org.h2.index.IndexType;
import org.h2.index.MultiVersionIndex;
import org.h2.index.NonUniqueHashIndex; import org.h2.index.NonUniqueHashIndex;
import org.h2.index.PageBtreeIndex; import org.h2.index.PageBtreeIndex;
import org.h2.index.PageDataIndex; import org.h2.index.PageDataIndex;
...@@ -116,9 +115,6 @@ public class RegularTable extends TableBase { ...@@ -116,9 +115,6 @@ public class RegularTable extends TableBase {
@Override @Override
public void addRow(Session session, Row row) { public void addRow(Session session, Row row) {
lastModificationId = database.getNextModificationDataId(); lastModificationId = database.getNextModificationDataId();
if (database.isMVStore()) {
row.setSessionId(session.getId());
}
int i = 0; int i = 0;
try { try {
for (int size = indexes.size(); i < size; i++) { for (int size = indexes.size(); i < size; i++) {
...@@ -141,19 +137,7 @@ public class RegularTable extends TableBase { ...@@ -141,19 +137,7 @@ public class RegularTable extends TableBase {
trace.error(e2, "could not undo operation"); trace.error(e2, "could not undo operation");
throw e2; throw e2;
} }
DbException de = DbException.convert(e); throw DbException.convert(e);
if (de.getErrorCode() == ErrorCode.DUPLICATE_KEY_1) {
for (Index index : indexes) {
if (index.getIndexType().isUnique() && index instanceof MultiVersionIndex) {
MultiVersionIndex mv = (MultiVersionIndex) index;
if (mv.isUncommittedFromOtherSession(session, row)) {
throw DbException.get(
ErrorCode.CONCURRENT_UPDATE_1, index.getName());
}
}
}
}
throw de;
} }
analyzeIfRequired(session); analyzeIfRequired(session);
} }
...@@ -167,7 +151,7 @@ public class RegularTable extends TableBase { ...@@ -167,7 +151,7 @@ public class RegularTable extends TableBase {
} }
private void checkRowCount(Session session, Index index, int offset) { private void checkRowCount(Session session, Index index, int offset) {
if (SysProperties.CHECK && !database.isMVStore()) { if (SysProperties.CHECK) {
if (!(index instanceof PageDelegateIndex)) { if (!(index instanceof PageDelegateIndex)) {
long rc = index.getRowCount(session); long rc = index.getRowCount(session);
if (rc != rowCount + offset) { if (rc != rowCount + offset) {
...@@ -259,9 +243,6 @@ public class RegularTable extends TableBase { ...@@ -259,9 +243,6 @@ public class RegularTable extends TableBase {
index = new TreeIndex(this, indexId, indexName, cols, indexType); index = new TreeIndex(this, indexId, indexName, cols, indexType);
} }
} }
if (database.isMVStore()) {
index = new MultiVersionIndex(index, this);
}
if (index.needRebuild() && rowCount > 0) { if (index.needRebuild() && rowCount > 0) {
try { try {
Index scan = getScanIndex(session); Index scan = getScanIndex(session);
...@@ -366,26 +347,11 @@ public class RegularTable extends TableBase { ...@@ -366,26 +347,11 @@ public class RegularTable extends TableBase {
@Override @Override
public long getRowCount(Session session) { public long getRowCount(Session session) {
if (database.isMVStore()) {
return getScanIndex(session).getRowCount(session);
}
return rowCount; return rowCount;
} }
@Override @Override
public void removeRow(Session session, Row row) { public void removeRow(Session session, Row row) {
if (database.isMVStore()) {
if (row.isDeleted()) {
throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, getName());
}
int old = row.getSessionId();
int newId = session.getId();
if (old == 0) {
row.setSessionId(newId);
} else if (old != newId) {
throw DbException.get(ErrorCode.CONCURRENT_UPDATE_1, getName());
}
}
lastModificationId = database.getNextModificationDataId(); lastModificationId = database.getNextModificationDataId();
int i = indexes.size() - 1; int i = indexes.size() - 1;
try { try {
...@@ -444,17 +410,6 @@ public class RegularTable extends TableBase { ...@@ -444,17 +410,6 @@ public class RegularTable extends TableBase {
if (lockMode == Constants.LOCK_MODE_OFF) { if (lockMode == Constants.LOCK_MODE_OFF) {
return lockExclusiveSession != null; return lockExclusiveSession != null;
} }
if (!forceLockEvenInMvcc && database.isMVStore()) {
// MVCC: update, delete, and insert use a shared lock.
// Select doesn't lock except when using FOR UPDATE
if (exclusive) {
exclusive = false;
} else {
if (lockExclusiveSession == null) {
return false;
}
}
}
if (lockExclusiveSession == session) { if (lockExclusiveSession == session) {
return true; return true;
} }
...@@ -550,7 +505,7 @@ public class RegularTable extends TableBase { ...@@ -550,7 +505,7 @@ public class RegularTable extends TableBase {
} else { } else {
if (lockExclusiveSession == null) { if (lockExclusiveSession == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) { if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
if (!database.isMultiThreaded() && !database.isMVStore()) { if (!database.isMultiThreaded()) {
// READ_COMMITTED: a read lock is acquired, // READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation // but released immediately after the operation
// is complete. // is complete.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论