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 {
protected int[] columnIds;
protected Table table;
protected IndexType indexType;
protected boolean isMultiVersion;
/**
* Initialize the base index.
......@@ -350,11 +349,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
long k1 = rowData.getKey();
long k2 = compare.getKey();
if (k1 == k2) {
if (isMultiVersion) {
int v1 = rowData.getVersion();
int v2 = compare.getVersion();
return Integer.compare(v2, v1);
}
return 0;
}
return k1 > k2 ? 1 : -1;
......@@ -457,10 +451,6 @@ public abstract class BaseIndex extends SchemaObjectBase implements Index {
// nothing to do
}
void setMultiVersion(boolean multiVersion) {
this.isMultiVersion = multiVersion;
}
@Override
public Row getRow(Session session, long key) {
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;
}
}
}
......@@ -131,11 +131,9 @@ abstract class PageData extends Page {
* @param session the session
* @param minKey the smallest key
* @param maxKey the largest key
* @param multiVersion if the delta should be used
* @return the cursor
*/
abstract Cursor find(Session session, long minKey, long maxKey,
boolean multiVersion);
abstract Cursor find(Session session, long minKey, long maxKey);
/**
* Get the key at this position.
......
......@@ -5,8 +5,6 @@
*/
package org.h2.index;
import java.util.Iterator;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
......@@ -20,20 +18,11 @@ class PageDataCursor implements Cursor {
private int idx;
private final long maxKey;
private Row row;
private final boolean multiVersion;
private final Session session;
private Iterator<Row> delta;
PageDataCursor(Session session, PageDataLeaf current, int idx, long maxKey,
boolean multiVersion) {
PageDataCursor(PageDataLeaf current, int idx, long maxKey) {
this.current = current;
this.idx = idx;
this.maxKey = maxKey;
this.multiVersion = multiVersion;
this.session = session;
if (multiVersion) {
delta = current.index.getDelta();
}
}
@Override
......@@ -48,30 +37,7 @@ class PageDataCursor implements Cursor {
@Override
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();
if (row != null && row.getSessionId() != 0 &&
row.getSessionId() != session.getId()) {
continue;
}
}
break;
}
return checkMax();
}
......
......@@ -5,16 +5,11 @@
*/
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.command.dml.AllColumnsForPlan;
import org.h2.engine.Constants;
import org.h2.engine.Session;
import org.h2.engine.SysProperties;
import org.h2.engine.UndoLogRecord;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
......@@ -40,9 +35,6 @@ public class PageDataIndex extends PageIndex {
private final RegularTable tableData;
private long lastKey;
private long rowCount;
private HashSet<Row> delta;
private int rowCountDiff;
private final HashMap<Integer, Integer> sessionRowCount;
private int mainIndexColumn = -1;
private DbException fastDuplicateKeyException;
......@@ -53,21 +45,12 @@ public class PageDataIndex extends PageIndex {
private int memoryPerPage;
private int memoryCount;
private final boolean multiVersion;
public PageDataIndex(RegularTable table, int id, IndexColumn[] columns,
IndexType indexType, boolean create, Session session) {
initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType);
this.multiVersion = database.isMVStore();
// trace = database.getTrace(Trace.PAGE_STORE + "_di");
// trace.setLevel(TraceSystem.DEBUG);
if (multiVersion) {
sessionRowCount = new HashMap<>();
isMultiVersion = true;
} else {
sessionRowCount = null;
}
tableData = table;
this.store = database.getPageStore();
store.addIndex(this);
......@@ -190,16 +173,6 @@ public class PageDataIndex extends PageIndex {
root = newRoot;
}
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();
rowCount++;
store.logAddOrRemoveRow(session, tableData.getId(), row, true);
......@@ -279,7 +252,7 @@ public class PageDataIndex extends PageIndex {
long from = first == null ? Long.MIN_VALUE : first.getKey();
long to = last == null ? Long.MAX_VALUE : last.getKey();
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 {
* @param session the session
* @param first the key of the first row
* @param last the key of the last row
* @param multiVersion if mvcc should be used
* @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);
return root.find(session, first, last, multiVersion);
return root.find(session, first, last);
}
@Override
......@@ -350,18 +322,6 @@ public class PageDataIndex extends PageIndex {
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);
}
......@@ -387,9 +347,6 @@ public class PageDataIndex extends PageIndex {
session.commit(false);
database.getLobStorage().removeAllForTable(table.getId());
}
if (multiVersion) {
sessionRowCount.clear();
}
tableData.setRowCount(0);
}
......@@ -439,13 +396,6 @@ public class PageDataIndex extends PageIndex {
@Override
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;
}
......@@ -476,46 +426,11 @@ public class PageDataIndex extends PageIndex {
if (trace.isDebugEnabled()) {
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,
// for example after running recovery
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.
*
......
......@@ -315,9 +315,9 @@ public class PageDataLeaf extends PageData {
}
@Override
Cursor find(Session session, long minKey, long maxKey, boolean multiVersion) {
Cursor find(Session session, long minKey, long maxKey) {
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 {
}
@Override
Cursor find(Session session, long minKey, long maxKey, boolean multiVersion) {
Cursor find(Session session, long minKey, long maxKey) {
int x = find(minKey);
int child = childPageIds[x];
return index.getPage(child, getPos()).find(session, minKey, maxKey,
multiVersion);
return index.getPage(child, getPos()).find(session, minKey, maxKey);
}
@Override
......
......@@ -67,17 +67,17 @@ public class PageDelegateIndex extends PageIndex {
// ifNull is MIN_VALUE as well, because the column is never NULL
// so avoid returning all rows (returning one row is OK)
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
public Cursor findFirstOrLast(Session session, boolean first) {
Cursor cursor;
if (first) {
cursor = mainIndex.find(session, Long.MIN_VALUE, Long.MAX_VALUE, false);
cursor = mainIndex.find(session, Long.MIN_VALUE, Long.MAX_VALUE);
} else {
long x = mainIndex.getLastKey();
cursor = mainIndex.find(session, x, x, false);
cursor = mainIndex.find(session, x, x);
}
cursor.next();
return cursor;
......
......@@ -5,8 +5,6 @@
*/
package org.h2.index;
import java.util.Iterator;
import org.h2.engine.Session;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
......@@ -17,17 +15,9 @@ import org.h2.result.SearchRow;
public class ScanCursor implements Cursor {
private final ScanIndex scan;
private Row row;
private final Session session;
private final boolean multiVersion;
private Iterator<Row> delta;
ScanCursor(Session session, ScanIndex scan, boolean multiVersion) {
this.session = session;
ScanCursor(ScanIndex scan) {
this.scan = scan;
this.multiVersion = multiVersion;
if (multiVersion) {
delta = scan.getDelta();
}
row = null;
}
......@@ -43,29 +33,6 @@ public class ScanCursor implements Cursor {
@Override
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);
return row != null;
}
......
......@@ -6,16 +6,11 @@
package org.h2.index;
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.command.dml.AllColumnsForPlan;
import org.h2.engine.Constants;
import org.h2.engine.Session;
import org.h2.engine.UndoLogRecord;
import org.h2.message.DbException;
import org.h2.result.Row;
import org.h2.result.SearchRow;
......@@ -36,19 +31,11 @@ public class ScanIndex extends BaseIndex {
private long firstFree = -1;
private ArrayList<Row> rows = Utils.newSmallArrayList();
private final RegularTable tableData;
private int rowCountDiff;
private final HashMap<Integer, Integer> sessionRowCount;
private HashSet<Row> delta;
private long rowCount;
public ScanIndex(RegularTable table, int id, IndexColumn[] columns,
IndexType indexType) {
initBaseIndex(table, id, table.getName() + "_DATA", columns, indexType);
if (database.isMVStore()) {
sessionRowCount = new HashMap<>();
} else {
sessionRowCount = null;
}
tableData = table;
}
......@@ -66,10 +53,6 @@ public class ScanIndex extends BaseIndex {
}
tableData.setRowCount(0);
rowCount = 0;
rowCountDiff = 0;
if (database.isMVStore()) {
sessionRowCount.clear();
}
}
@Override
......@@ -102,44 +85,13 @@ public class ScanIndex extends BaseIndex {
rows.set((int) key, row);
}
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++;
}
@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
public void remove(Session session, Row row) {
// in-memory
if (!database.isMVStore() && rowCount == 1) {
if (rowCount == 1) {
rows = Utils.newSmallArrayList();
firstFree = -1;
} else {
......@@ -153,24 +105,12 @@ public class ScanIndex extends BaseIndex {
rows.set((int) key, free);
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--;
}
@Override
public Cursor find(Session session, SearchRow first, SearchRow last) {
return new ScanCursor(session, this, database.isMVStore());
return new ScanCursor(this);
}
@Override
......@@ -182,13 +122,6 @@ public class ScanIndex extends BaseIndex {
@Override
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;
}
......@@ -248,13 +181,6 @@ public class ScanIndex extends BaseIndex {
throw DbException.getUnsupportedException("SCAN");
}
Iterator<Row> getDelta() {
if (delta == null) {
return Collections.emptyIterator();
}
return delta.iterator();
}
@Override
public long getRowCountApproximation() {
return rowCount;
......
......@@ -24,7 +24,6 @@ import org.h2.engine.SysProperties;
import org.h2.index.Cursor;
import org.h2.index.Index;
import org.h2.index.IndexType;
import org.h2.index.MultiVersionIndex;
import org.h2.index.PageBtreeIndex;
import org.h2.index.PageBtreeLeaf;
import org.h2.index.PageBtreeNode;
......@@ -1731,13 +1730,7 @@ public class PageStore implements CacheWriter {
}
meta = table.addIndex(session, "I" + id, id, cols, indexType, false, null);
}
PageIndex index;
if (meta instanceof MultiVersionIndex) {
index = (PageIndex) ((MultiVersionIndex) meta).getBaseIndex();
} else {
index = (PageIndex) meta;
}
metaObjects.put(id, index);
metaObjects.put(id, (PageIndex) meta);
}
/**
......
......@@ -42,7 +42,6 @@ import org.h2.expression.ValueExpression;
import org.h2.index.Index;
import org.h2.index.IndexType;
import org.h2.index.MetaIndex;
import org.h2.index.MultiVersionIndex;
import org.h2.jdbc.JdbcSQLException;
import org.h2.message.DbException;
import org.h2.mvstore.FileStore;
......@@ -935,13 +934,7 @@ public class MetaTable extends Table {
}
}
IndexColumn[] cols = index.getIndexColumns();
String indexClass;
if (index instanceof MultiVersionIndex) {
indexClass = ((MultiVersionIndex) index).
getBaseIndex().getClass().getName();
} else {
indexClass = index.getClass().getName();
}
String indexClass = index.getClass().getName();
for (int k = 0; k < cols.length; k++) {
IndexColumn idxCol = cols[k];
Column column = idxCol.column;
......
......@@ -26,7 +26,6 @@ import org.h2.index.Cursor;
import org.h2.index.HashIndex;
import org.h2.index.Index;
import org.h2.index.IndexType;
import org.h2.index.MultiVersionIndex;
import org.h2.index.NonUniqueHashIndex;
import org.h2.index.PageBtreeIndex;
import org.h2.index.PageDataIndex;
......@@ -116,9 +115,6 @@ public class RegularTable extends TableBase {
@Override
public void addRow(Session session, Row row) {
lastModificationId = database.getNextModificationDataId();
if (database.isMVStore()) {
row.setSessionId(session.getId());
}
int i = 0;
try {
for (int size = indexes.size(); i < size; i++) {
......@@ -141,19 +137,7 @@ public class RegularTable extends TableBase {
trace.error(e2, "could not undo operation");
throw e2;
}
DbException de = 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;
throw DbException.convert(e);
}
analyzeIfRequired(session);
}
......@@ -167,7 +151,7 @@ public class RegularTable extends TableBase {
}
private void checkRowCount(Session session, Index index, int offset) {
if (SysProperties.CHECK && !database.isMVStore()) {
if (SysProperties.CHECK) {
if (!(index instanceof PageDelegateIndex)) {
long rc = index.getRowCount(session);
if (rc != rowCount + offset) {
......@@ -259,9 +243,6 @@ public class RegularTable extends TableBase {
index = new TreeIndex(this, indexId, indexName, cols, indexType);
}
}
if (database.isMVStore()) {
index = new MultiVersionIndex(index, this);
}
if (index.needRebuild() && rowCount > 0) {
try {
Index scan = getScanIndex(session);
......@@ -366,26 +347,11 @@ public class RegularTable extends TableBase {
@Override
public long getRowCount(Session session) {
if (database.isMVStore()) {
return getScanIndex(session).getRowCount(session);
}
return rowCount;
}
@Override
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();
int i = indexes.size() - 1;
try {
......@@ -444,17 +410,6 @@ public class RegularTable extends TableBase {
if (lockMode == Constants.LOCK_MODE_OFF) {
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) {
return true;
}
......@@ -550,7 +505,7 @@ public class RegularTable extends TableBase {
} else {
if (lockExclusiveSession == null) {
if (lockMode == Constants.LOCK_MODE_READ_COMMITTED) {
if (!database.isMultiThreaded() && !database.isMVStore()) {
if (!database.isMultiThreaded()) {
// READ_COMMITTED: a read lock is acquired,
// but released immediately after the operation
// is complete.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论