提交 74d34875 authored 作者: Thomas Mueller's avatar Thomas Mueller

Page store: more internal checks

上级 00a4016d
...@@ -148,9 +148,10 @@ abstract class PageData extends Page { ...@@ -148,9 +148,10 @@ abstract class PageData extends Page {
* @param id the new page id * @param id the new page id
*/ */
void setPageId(int id) throws SQLException { void setPageId(int id) throws SQLException {
int old = getPos();
index.getPageStore().removeRecord(getPos()); index.getPageStore().removeRecord(getPos());
setPos(id); setPos(id);
remapChildren(); remapChildren(old);
} }
/** /**
...@@ -182,8 +183,10 @@ abstract class PageData extends Page { ...@@ -182,8 +183,10 @@ abstract class PageData extends Page {
/** /**
* Update the parent id of all children. * Update the parent id of all children.
*
* @param old the previous position
*/ */
abstract void remapChildren() throws SQLException; abstract void remapChildren(int old) throws SQLException;
/** /**
* Remove a row. * Remove a row.
......
...@@ -49,6 +49,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -49,6 +49,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
public PageDataIndex(TableData table, int id, IndexColumn[] columns, IndexType indexType, int headPos, Session session) throws SQLException { public PageDataIndex(TableData table, int id, IndexColumn[] columns, IndexType indexType, int headPos, Session session) throws SQLException {
initBaseIndex(table, id, table.getName() + "_TABLE_SCAN", columns, indexType); initBaseIndex(table, id, table.getName() + "_TABLE_SCAN", columns, indexType);
// trace = database.getTrace(Trace.PAGE_STORE + "_di");
// trace.setLevel(TraceSystem.DEBUG); // trace.setLevel(TraceSystem.DEBUG);
if (database.isMultiVersion()) { if (database.isMultiVersion()) {
sessionRowCount = New.hashMap(); sessionRowCount = New.hashMap();
...@@ -73,7 +74,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -73,7 +74,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
rowCount = root.getRowCount(); rowCount = root.getRowCount();
} }
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("opened " + getName() + " rows:" + rowCount); trace.debug(this + " opened rows:" + rowCount);
} }
table.setRowCount(rowCount); table.setRowCount(rowCount);
fastDuplicateKeyException = super.getDuplicateKeyException(); fastDuplicateKeyException = super.getDuplicateKeyException();
...@@ -99,9 +100,6 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -99,9 +100,6 @@ public class PageDataIndex extends PageIndex implements RowIndex {
retry = true; retry = true;
} }
} }
if (trace.isDebugEnabled()) {
trace.debug("add table:" + table.getId() + " " + row);
}
if (tableData.getContainsLargeObject()) { if (tableData.getContainsLargeObject()) {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
...@@ -149,7 +147,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -149,7 +147,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
break; break;
} }
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("split " + splitPoint); trace.debug(this + " split");
} }
long pivot = splitPoint == 0 ? row.getKey() : root.getKey(splitPoint - 1); long pivot = splitPoint == 0 ? row.getKey() : root.getKey(splitPoint - 1);
PageData page1 = root; PageData page1 = root;
...@@ -208,9 +206,6 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -208,9 +206,6 @@ public class PageDataIndex extends PageIndex implements RowIndex {
store.update(empty); store.update(empty);
return empty; return empty;
} }
if (p.index.rootPageId != rootPageId) {
throw Message.throwInternalError("Wrong index: " + p.index.getName() + ":" + p.index.rootPageId + " " + getName() + ":" + rootPageId);
}
if (parent != -1) { if (parent != -1) {
if (p.getParentPageId() != parent) { if (p.getParentPageId() != parent) {
throw Message.throwInternalError(p + " parent " + p.getParentPageId() + " expected " + parent); throw Message.throwInternalError(p + " parent " + p.getParentPageId() + " expected " + parent);
...@@ -280,9 +275,6 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -280,9 +275,6 @@ public class PageDataIndex extends PageIndex implements RowIndex {
} }
public void remove(Session session, Row row) throws SQLException { public void remove(Session session, Row row) throws SQLException {
if (trace.isDebugEnabled()) {
trace.debug("remove " + row.getKey());
}
if (tableData.getContainsLargeObject()) { if (tableData.getContainsLargeObject()) {
for (int i = 0; i < row.getColumnCount(); i++) { for (int i = 0; i < row.getColumnCount(); i++) {
Value v = row.getValue(i); Value v = row.getValue(i);
...@@ -322,7 +314,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -322,7 +314,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
public void remove(Session session) throws SQLException { public void remove(Session session) throws SQLException {
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("remove"); trace.debug(this + " remove");
} }
removeAllRows(); removeAllRows();
store.free(rootPageId, true); store.free(rootPageId, true);
...@@ -331,7 +323,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -331,7 +323,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
public void truncate(Session session) throws SQLException { public void truncate(Session session) throws SQLException {
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("truncate"); trace.debug(this + " truncate");
} }
store.logTruncate(session, tableData.getId()); store.logTruncate(session, tableData.getId());
removeAllRows(); removeAllRows();
...@@ -421,7 +413,7 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -421,7 +413,7 @@ public class PageDataIndex extends PageIndex implements RowIndex {
public void close(Session session) throws SQLException { public void close(Session session) throws SQLException {
if (trace.isDebugEnabled()) { if (trace.isDebugEnabled()) {
trace.debug("close"); trace.debug(this + " close");
} }
if (delta != null) { if (delta != null) {
delta.clear(); delta.clear();
...@@ -488,4 +480,8 @@ public class PageDataIndex extends PageIndex implements RowIndex { ...@@ -488,4 +480,8 @@ public class PageDataIndex extends PageIndex implements RowIndex {
return memorySizePerPage; return memorySizePerPage;
} }
public String toString() {
return getName();
}
} }
...@@ -368,7 +368,7 @@ public class PageDataLeaf extends PageData { ...@@ -368,7 +368,7 @@ public class PageDataLeaf extends PageData {
return this; return this;
} }
protected void remapChildren() throws SQLException { protected void remapChildren(int old) throws SQLException {
if (firstOverflowPageId == 0) { if (firstOverflowPageId == 0) {
return; return;
} }
...@@ -503,9 +503,8 @@ public class PageDataLeaf extends PageData { ...@@ -503,9 +503,8 @@ public class PageDataLeaf extends PageData {
} }
p2.entryCount = entryCount; p2.entryCount = entryCount;
p2.offsets = offsets; p2.offsets = offsets;
p2.parentPageId = parentPageId;
p2.start = start; p2.start = start;
p2.remapChildren(); p2.remapChildren(getPos());
p2.write(); p2.write();
p2.data.truncate(index.getPageStore().getPageSize()); p2.data.truncate(index.getPageStore().getPageSize());
store.update(p2); store.update(p2);
......
...@@ -190,13 +190,13 @@ public class PageDataNode extends PageData { ...@@ -190,13 +190,13 @@ public class PageDataNode extends PageData {
removeChild(splitPoint - 1); removeChild(splitPoint - 1);
childPageIds[splitPoint - 1] = lastChild; childPageIds[splitPoint - 1] = lastChild;
p2.childPageIds[0] = firstChild; p2.childPageIds[0] = firstChild;
p2.remapChildren(); p2.remapChildren(getPos());
return p2; return p2;
} }
protected void remapChildren() throws SQLException { protected void remapChildren(int old) throws SQLException {
for (int child : childPageIds) { for (int child : childPageIds) {
PageData p = index.getPage(child, -1); PageData p = index.getPage(child, old);
p.setParentPageId(getPos()); p.setParentPageId(getPos());
index.getPageStore().update(p); index.getPageStore().update(p);
} }
......
...@@ -873,7 +873,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -873,7 +873,7 @@ public class Recover extends Tool implements DataHandler {
boolean last = (type & Page.FLAG_LAST) != 0; boolean last = (type & Page.FLAG_LAST) != 0;
type &= ~Page.FLAG_LAST; type &= ~Page.FLAG_LAST;
if (!PageStore.checksumTest(s.getBytes(), page, pageSize)) { if (!PageStore.checksumTest(s.getBytes(), page, pageSize)) {
writer.println("-- ERROR: page " + page + " checksum mismatch"); writer.println("-- ERROR: page " + page + " checksum mismatch type: " + type);
} }
s.readShortInt(); s.readShortInt();
switch (type) { switch (type) {
...@@ -1166,12 +1166,12 @@ public class Recover extends Tool implements DataHandler { ...@@ -1166,12 +1166,12 @@ public class Recover extends Tool implements DataHandler {
store.seek((long) nextPage * pageSize); store.seek((long) nextPage * pageSize);
store.readFully(page.getBytes(), 0, pageSize); store.readFully(page.getBytes(), 0, pageSize);
page.reset(); page.reset();
if (!PageStore.checksumTest(page.getBytes(), nextPage, pageSize)) { int t = page.readByte();
if (t != 0 && !PageStore.checksumTest(page.getBytes(), nextPage, pageSize)) {
writer.println("-- ERROR: checksum mismatch page: " +nextPage); writer.println("-- ERROR: checksum mismatch page: " +nextPage);
endOfFile = true; endOfFile = true;
return; return;
} }
int t = page.readByte();
page.readShortInt(); page.readShortInt();
int p = page.readInt(); int p = page.readInt();
int k = page.readInt(); int k = page.readInt();
...@@ -1199,14 +1199,12 @@ public class Recover extends Tool implements DataHandler { ...@@ -1199,14 +1199,12 @@ public class Recover extends Tool implements DataHandler {
int entryCount = s.readShortInt(); int entryCount = s.readShortInt();
int[] children = new int[entryCount + 1]; int[] children = new int[entryCount + 1];
int[] offsets = new int[entryCount]; int[] offsets = new int[entryCount];
int rightmost = s.readInt(); children[entryCount] = s.readInt();
checkParent(writer, pageId, rightmost); checkParent(writer, pageId, children, entryCount);
children[entryCount] = rightmost;
int empty = Integer.MAX_VALUE; int empty = Integer.MAX_VALUE;
for (int i = 0; i < entryCount; i++) { for (int i = 0; i < entryCount; i++) {
int child = s.readInt(); children[i] = s.readInt();
checkParent(writer, pageId, child); checkParent(writer, pageId, children, i);
children[i] = child;
int off = s.readShortInt(); int off = s.readShortInt();
empty = Math.min(off, empty); empty = Math.min(off, empty);
offsets[i] = off; offsets[i] = off;
...@@ -1292,24 +1290,23 @@ public class Recover extends Tool implements DataHandler { ...@@ -1292,24 +1290,23 @@ public class Recover extends Tool implements DataHandler {
} }
} }
private void checkParent(PrintWriter writer, long pageId, long child) { private void checkParent(PrintWriter writer, long pageId, int[] children, int index) {
int child = children[index];
if (child < 0 || child >= parents.length) { if (child < 0 || child >= parents.length) {
writer.println("-- ERROR [" + pageId + "] child: " + child + " page count: " + parents.length); writer.println("-- ERROR [" + pageId + "] child[" + index + "]: " + child + " >= page count: " + parents.length);
} else if (parents[(int) child] != pageId) { } else if (parents[child] != pageId) {
writer.println("-- ERROR [" + pageId + "] expected child: " + child + " got: " + parents[(int) child]); writer.println("-- ERROR [" + pageId + "] child[" + index + "]: " + child + " parent: " + parents[child]);
} }
} }
private void dumpPageDataNode(PrintWriter writer, Data s, long pageId, int entryCount) { private void dumpPageDataNode(PrintWriter writer, Data s, long pageId, int entryCount) {
int[] children = new int[entryCount + 1]; int[] children = new int[entryCount + 1];
long[] keys = new long[entryCount]; long[] keys = new long[entryCount];
int child = s.readInt(); children[entryCount] = s.readInt();
children[entryCount] = child; checkParent(writer, pageId, children, entryCount);
checkParent(writer, pageId, child);
for (int i = 0; i < entryCount; i++) { for (int i = 0; i < entryCount; i++) {
child = s.readInt(); children[i] = s.readInt();
children[i] = child; checkParent(writer, pageId, children, i);
checkParent(writer, pageId, child);
keys[i] = s.readVarLong(); keys[i] = s.readVarLong();
} }
if (!trace) { if (!trace) {
...@@ -1347,7 +1344,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -1347,7 +1344,7 @@ public class Recover extends Tool implements DataHandler {
s.setPos(pageSize); s.setPos(pageSize);
long parent = pageId; long parent = pageId;
while (true) { while (true) {
checkParent(writer, parent, next); checkParent(writer, parent, new int[]{(int) next}, 0);
parent = next; parent = next;
store.seek(pageSize * next); store.seek(pageSize * next);
store.readFully(s2.getBytes(), 0, pageSize); store.readFully(s2.getBytes(), 0, pageSize);
......
...@@ -297,6 +297,8 @@ java org.h2.test.TestAll timer ...@@ -297,6 +297,8 @@ java org.h2.test.TestAll timer
/* /*
check freelist undo recursion
mvcc merge problem mvcc merge problem
System.setProperty("h2.optimizeInList", "true"); System.setProperty("h2.optimizeInList", "true");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论