提交 fd46653c authored 作者: Thomas Mueller's avatar Thomas Mueller

New experimental feature SHUTDOWN DEFRAG.

上级 dc492ea5
...@@ -538,13 +538,13 @@ public class PageDataLeaf extends PageData { ...@@ -538,13 +538,13 @@ public class PageDataLeaf extends PageData {
p2.writeData(); p2.writeData();
p2.data.truncate(index.getPageStore().getPageSize()); p2.data.truncate(index.getPageStore().getPageSize());
store.update(p2); store.update(p2);
store.free(getPos());
if (parentPageId == ROOT) { if (parentPageId == ROOT) {
index.setRootPageId(session, newPos); index.setRootPageId(session, newPos);
} else { } else {
PageDataNode p = (PageDataNode) store.getPage(parentPageId); PageDataNode p = (PageDataNode) store.getPage(parentPageId);
p.moveChild(getPos(), newPos); p.moveChild(getPos(), newPos);
} }
store.free(getPos());
} }
/** /**
......
...@@ -10,7 +10,6 @@ import java.lang.reflect.Array; ...@@ -10,7 +10,6 @@ import java.lang.reflect.Array;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.util.CacheObject; import org.h2.util.CacheObject;
/** /**
* A page. Format: * A page. Format:
* <ul><li>0-3: parent page id (0 for root) * <ul><li>0-3: parent page id (0 for root)
...@@ -253,4 +252,13 @@ public abstract class Page extends CacheObject { ...@@ -253,4 +252,13 @@ public abstract class Page extends CacheObject {
} }
} }
/**
* If this page can be moved. Transaction log and free-list pages can not.
*
* @return true if moving is allowed
*/
public boolean canMove() {
return true;
}
} }
...@@ -222,4 +222,8 @@ public class PageFreeList extends Page { ...@@ -222,4 +222,8 @@ public class PageFreeList extends Page {
return false; return false;
} }
public boolean canMove() {
return false;
}
} }
...@@ -520,24 +520,34 @@ public class PageStore implements CacheWriter { ...@@ -520,24 +520,34 @@ public class PageStore implements CacheWriter {
} }
} }
recordPageReads = false; recordPageReads = false;
int target = MIN_PAGE_COUNT - 1;
for (int i = 0; i < recordedPagesList.size(); i++) { for (int i = 0; i < recordedPagesList.size(); i++) {
writeBack(); writeBack();
cache.clear(); int source = recordedPagesList.get(i);
int a = MIN_PAGE_COUNT + i; Page pageSource = getPage(source);
int b = recordedPagesList.get(i); if (!pageSource.canMove()) {
if (a == b) { continue;
}
while (true) {
Page pageTarget = getPage(++target);
if (pageTarget == null || pageTarget.canMove()) {
break;
}
}
if (target == source) {
continue; continue;
} }
int temp = getFirstFree(); int temp = getFirstFree();
if (temp == -1) { if (temp == -1) {
DbException.throwInternalError("no free page for defrag"); DbException.throwInternalError("no free page for defrag");
} }
swap(a, b, temp); cache.clear();
int index = recordedPagesList.indexOf(a); swap(source, target, temp);
int index = recordedPagesList.indexOf(target);
if (index >= 0) { if (index >= 0) {
recordedPagesList.set(index, b); recordedPagesList.set(index, source);
} }
recordedPagesList.set(i, a); recordedPagesList.set(i, target);
} }
recordedPagesList = null; recordedPagesList = null;
recordedPages = null; recordedPages = null;
...@@ -606,17 +616,23 @@ public class PageStore implements CacheWriter { ...@@ -606,17 +616,23 @@ public class PageStore implements CacheWriter {
Page pageA = null; Page pageA = null;
if (isUsed(a)) { if (isUsed(a)) {
pageA = getPage(a); pageA = getPage(a);
pageA.moveTo(systemSession, free); if (pageA != null) {
pageA.moveTo(systemSession, free);
}
free(a); free(a);
} }
if (isUsed(b)) { if (isUsed(b)) {
Page pageB = getPage(b); Page pageB = getPage(b);
pageB.moveTo(systemSession, a); if (pageB != null) {
pageB.moveTo(systemSession, a);
}
free(b); free(b);
} }
if (pageA != null) { if (pageA != null) {
f = getPage(free); f = getPage(free);
f.moveTo(systemSession, b); if (f != null) {
f.moveTo(systemSession, b);
}
free(free); free(free);
} }
} }
......
...@@ -165,4 +165,8 @@ public class PageStreamData extends Page { ...@@ -165,4 +165,8 @@ public class PageStreamData extends Page {
return DATA_START; return DATA_START;
} }
public boolean canMove() {
return false;
}
} }
\ No newline at end of file
...@@ -287,4 +287,8 @@ public class PageStreamTrunk extends Page { ...@@ -287,4 +287,8 @@ public class PageStreamTrunk extends Page {
return "page[" + getPos() + "] stream trunk key:" + logKey + " next:" + nextTrunk; return "page[" + getPos() + "] stream trunk key:" + logKey + " next:" + nextTrunk;
} }
public boolean canMove() {
return false;
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论