提交 47559bd1 authored 作者: Thomas Mueller's avatar Thomas Mueller

MVTableEngine improvements

上级 d893c6bd
...@@ -856,6 +856,9 @@ public class Session extends SessionWithState { ...@@ -856,6 +856,9 @@ public class Session extends SessionWithState {
* @return true if yes * @return true if yes
*/ */
public boolean containsUncommitted() { public boolean containsUncommitted() {
if (database.getMvStore() != null) {
return transaction != null;
}
return firstUncommittedLog != Session.LOG_WRITTEN; return firstUncommittedLog != Session.LOG_WRITTEN;
} }
......
...@@ -19,7 +19,7 @@ import org.h2.result.SearchRow; ...@@ -19,7 +19,7 @@ import org.h2.result.SearchRow;
import org.h2.result.SortOrder; import org.h2.result.SortOrder;
import org.h2.table.Column; import org.h2.table.Column;
import org.h2.table.IndexColumn; import org.h2.table.IndexColumn;
import org.h2.table.RegularTable; import org.h2.table.Table;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueGeometry; import org.h2.value.ValueGeometry;
...@@ -41,7 +41,6 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -41,7 +41,6 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
private final MVRTreeMap<Long> treeMap; private final MVRTreeMap<Long> treeMap;
private final MVStore store; private final MVStore store;
private final RegularTable tableData;
private boolean closed; private boolean closed;
private boolean needRebuild; private boolean needRebuild;
private boolean persistent; private boolean persistent;
...@@ -58,7 +57,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -58,7 +57,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
* @param create whether to create a new index * @param create whether to create a new index
* @param session the session. * @param session the session.
*/ */
public SpatialTreeIndex(RegularTable table, int id, String indexName, public SpatialTreeIndex(Table table, int id, String indexName,
IndexColumn[] columns, IndexType indexType, boolean persistent, IndexColumn[] columns, IndexType indexType, boolean persistent,
boolean create, Session session) { boolean create, Session session) {
if (indexType.isUnique()) { if (indexType.isUnique()) {
...@@ -82,7 +81,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -82,7 +81,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
initBaseIndex(table, id, indexName, columns, indexType); initBaseIndex(table, id, indexName, columns, indexType);
this.needRebuild = create; this.needRebuild = create;
this.persistent = persistent; this.persistent = persistent;
tableData = table; this.table = table;
if (!database.isStarting()) { if (!database.isStarting()) {
if (columns[0].column.getType() != Value.GEOMETRY) { if (columns[0].column.getType() != Value.GEOMETRY) {
throw DbException.getUnsupportedException("spatial index on non-geometry column, " throw DbException.getUnsupportedException("spatial index on non-geometry column, "
...@@ -158,7 +157,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -158,7 +157,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
} }
private Cursor find(Session session) { private Cursor find(Session session) {
return new SpatialCursor(treeMap.keySet().iterator(), tableData, session); return new SpatialCursor(treeMap.keySet().iterator(), table, session);
} }
@Override @Override
...@@ -166,7 +165,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -166,7 +165,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
if (intersection == null) { if (intersection == null) {
return find(filter.getSession()); return find(filter.getSession());
} }
return new SpatialCursor(treeMap.findIntersectingKeys(getEnvelope(intersection)), tableData, filter.getSession()); return new SpatialCursor(treeMap.findIntersectingKeys(getEnvelope(intersection)), table, filter.getSession());
} }
@Override @Override
...@@ -189,7 +188,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -189,7 +188,7 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
@Override @Override
public double getCost(Session session, int[] masks, TableFilter filter, SortOrder sortOrder) { public double getCost(Session session, int[] masks, TableFilter filter, SortOrder sortOrder) {
return getCostRangeIndex(masks, tableData.getRowCountApproximation(), filter, sortOrder); return getCostRangeIndex(masks, table.getRowCountApproximation(), filter, sortOrder);
} }
@Override @Override
...@@ -253,18 +252,18 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex { ...@@ -253,18 +252,18 @@ public class SpatialTreeIndex extends BaseIndex implements SpatialIndex {
private final Iterator<SpatialKey> it; private final Iterator<SpatialKey> it;
private SpatialKey current; private SpatialKey current;
private final RegularTable tableData; private final Table table;
private Session session; private Session session;
public SpatialCursor(Iterator<SpatialKey> it, RegularTable tableData, Session session) { public SpatialCursor(Iterator<SpatialKey> it, Table table, Session session) {
this.it = it; this.it = it;
this.tableData = tableData; this.table = table;
this.session = session; this.session = session;
} }
@Override @Override
public Row get() { public Row get() {
return tableData.getRow(session, current.getId()); return table.getRow(session, current.getId());
} }
@Override @Override
......
...@@ -26,6 +26,7 @@ import org.h2.index.Cursor; ...@@ -26,6 +26,7 @@ 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.MultiVersionIndex;
import org.h2.index.SpatialTreeIndex;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.message.Trace; import org.h2.message.Trace;
import org.h2.mvstore.db.TransactionStore.Transaction; import org.h2.mvstore.db.TransactionStore.Transaction;
...@@ -354,13 +355,7 @@ public class MVTable extends TableBase { ...@@ -354,13 +355,7 @@ public class MVTable extends TableBase {
// ignore // ignore
} }
/** @Override
* Get the given row.
*
* @param session the session
* @param key the primary key
* @return the row
*/
public Row getRow(Session session, long key) { public Row getRow(Session session, long key) {
return primaryIndex.getRow(session, key); return primaryIndex.getRow(session, key);
} }
...@@ -401,6 +396,9 @@ public class MVTable extends TableBase { ...@@ -401,6 +396,9 @@ public class MVTable extends TableBase {
primaryIndex.setMainIndexColumn(mainIndexColumn); primaryIndex.setMainIndexColumn(mainIndexColumn);
index = new MVDelegateIndex(this, indexId, index = new MVDelegateIndex(this, indexId,
indexName, primaryIndex, indexType); indexName, primaryIndex, indexType);
} else if (indexType.isSpatial()) {
index = new SpatialTreeIndex(this, indexId, indexName, cols,
indexType, true, create, session);
} else { } else {
index = new MVSecondaryIndex(session.getDatabase(), index = new MVSecondaryIndex(session.getDatabase(),
this, indexId, this, indexId,
......
...@@ -269,7 +269,7 @@ public class MVTableEngine implements TableEngine { ...@@ -269,7 +269,7 @@ public class MVTableEngine implements TableEngine {
* @param maxCompactTime the maximum time in milliseconds to compact * @param maxCompactTime the maximum time in milliseconds to compact
*/ */
public void close(long maxCompactTime) { public void close(long maxCompactTime) {
if (!store.isClosed()) { if (!store.isClosed() && store.getFileStore() != null) {
if (!store.getFileStore().isReadOnly()) { if (!store.getFileStore().isReadOnly()) {
store.store(); store.store();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
......
...@@ -101,13 +101,7 @@ public class RegularTable extends TableBase { ...@@ -101,13 +101,7 @@ public class RegularTable extends TableBase {
} }
} }
/** @Override
* Read the given row.
*
* @param session the session
* @param key unique key
* @return the row
*/
public Row getRow(Session session, long key) { public Row getRow(Session session, long key) {
return scanIndex.getRow(session, key); return scanIndex.getRow(session, key);
} }
......
...@@ -166,6 +166,17 @@ public abstract class Table extends SchemaObjectBase { ...@@ -166,6 +166,17 @@ public abstract class Table extends SchemaObjectBase {
public abstract Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType, public abstract Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType,
boolean create, String indexComment); boolean create, String indexComment);
/**
* Get the given row.
*
* @param session the session
* @param key the primary key
* @return the row
*/
public Row getRow(Session session, long key) {
return null;
}
/** /**
* Remove a row from the table and all indexes. * Remove a row from the table and all indexes.
* *
......
...@@ -1247,10 +1247,12 @@ public abstract class TestBase { ...@@ -1247,10 +1247,12 @@ public abstract class TestBase {
ArrayList<String> list2 = new ArrayList<String>(); ArrayList<String> list2 = new ArrayList<String>();
while (rs1.next()) { while (rs1.next()) {
String s1 = rs1.getString(1); String s1 = rs1.getString(1);
s1 = removeRowCount(s1);
if (!rs2.next()) { if (!rs2.next()) {
fail("expected: " + s1); fail("expected: " + s1);
} }
String s2 = rs2.getString(1); String s2 = rs2.getString(1);
s2 = removeRowCount(s2);
if (!s1.equals(s2)) { if (!s1.equals(s2)) {
list1.add(s1); list1.add(s1);
list2.add(s2); list2.add(s2);
...@@ -1258,13 +1260,21 @@ public abstract class TestBase { ...@@ -1258,13 +1260,21 @@ public abstract class TestBase {
} }
for (String s : list1) { for (String s : list1) {
if (!list2.remove(s)) { if (!list2.remove(s)) {
fail("only found in first: " + s); fail("only found in first: " + s + " remaining: " + list2);
} }
} }
assertEquals(0, list2.size()); assertEquals("remaining: " + list2, 0, list2.size());
assertFalse(rs2.next()); assertFalse(rs2.next());
} }
private static String removeRowCount(String scriptLine) {
int index = scriptLine.indexOf("+/-");
if (index >= 0) {
scriptLine = scriptLine.substring(index);
}
return scriptLine;
}
/** /**
* Create a new object of the calling class. * Create a new object of the calling class.
* *
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论