提交 138b5f4d authored 作者: Thomas Mueller's avatar Thomas Mueller

Page store: change page header format

上级 aa4c1bd1
......@@ -20,8 +20,8 @@ import org.h2.store.PageStore;
/**
* A b-tree leaf page that contains index data. Format:
* <ul>
* <li>parent page id (0 for root): int</li>
* <li>page type: byte</li>
* <li>parent page id (0 for root): int</li>
* <li>index id: varInt</li>
* <li>entry count: short</li>
* <li>list of offsets: short</li>
......@@ -68,8 +68,8 @@ public class PageBtreeLeaf extends PageBtree {
private void read() throws SQLException {
data.reset();
this.parentPageId = data.readInt();
int type = data.readByte();
this.parentPageId = data.readInt();
onlyPosition = (type & Page.FLAG_LAST) == 0;
int indexId = data.readVarInt();
if (indexId != index.getId()) {
......@@ -233,8 +233,8 @@ public class PageBtreeLeaf extends PageBtree {
}
private void writeHead() {
data.writeInt(parentPageId);
data.writeByte((byte) (Page.TYPE_BTREE_LEAF | (onlyPosition ? 0 : Page.FLAG_LAST)));
data.writeInt(parentPageId);
data.writeVarInt(index.getId());
data.writeShortInt(entryCount);
}
......
......@@ -21,8 +21,8 @@ import org.h2.util.MemoryUtils;
/**
* A b-tree node page that contains index data. Format:
* <ul>
* <li>parent page id (0 for root): int</li>
* <li>page type: byte</li>
* <li>parent page id (0 for root): int</li>
* <li>index id: varInt</li>
* <li>count of all children (-1 if not known): int</li>
* <li>entry count: short</li>
......@@ -81,8 +81,8 @@ public class PageBtreeNode extends PageBtree {
private void read() throws SQLException {
data.reset();
this.parentPageId = data.readInt();
int type = data.readByte();
this.parentPageId = data.readInt();
onlyPosition = (type & Page.FLAG_LAST) == 0;
int indexId = data.readVarInt();
if (indexId != index.getId()) {
......@@ -370,8 +370,8 @@ public class PageBtreeNode extends PageBtree {
}
private void writeHead() {
data.writeInt(parentPageId);
data.writeByte((byte) (Page.TYPE_BTREE_NODE | (onlyPosition ? 0 : Page.FLAG_LAST)));
data.writeInt(parentPageId);
data.writeVarInt(index.getId());
data.writeInt(rowCount);
data.writeShortInt(entryCount);
......
......@@ -21,8 +21,8 @@ import org.h2.store.PageStore;
/**
* A leaf page that contains data of one or multiple rows. Format:
* <ul>
* <li>parent page id (0 for root): int</li>
* <li>page type: byte</li>
* <li>parent page id (0 for root): int</li>
* <li>table id: varInt</li>
* <li>column count: varInt</li>
* <li>entry count: short</li>
......@@ -104,8 +104,8 @@ public class PageDataLeaf extends PageData {
private void read() throws SQLException {
data.reset();
this.parentPageId = data.readInt();
int type = data.readByte();
this.parentPageId = data.readInt();
int tableId = data.readVarInt();
if (tableId != index.getId()) {
throw Message.getSQLException(ErrorCode.FILE_CORRUPTED_1,
......@@ -416,7 +416,6 @@ public class PageDataLeaf extends PageData {
}
private void writeHead() {
data.writeInt(parentPageId);
int type;
if (firstOverflowPageId == 0) {
type = Page.TYPE_DATA_LEAF | Page.FLAG_LAST;
......@@ -424,6 +423,7 @@ public class PageDataLeaf extends PageData {
type = Page.TYPE_DATA_LEAF;
}
data.writeByte((byte) type);
data.writeInt(parentPageId);
data.writeVarInt(index.getId());
data.writeVarInt(columnCount);
data.writeShortInt(entryCount);
......
......@@ -21,8 +21,8 @@ import org.h2.util.MemoryUtils;
/**
* A leaf page that contains data of one or multiple rows. Format:
* <ul>
* <li>parent page id (0 for root): int</li>
* <li>page type: byte</li>
* <li>parent page id (0 for root): int</li>
* <li>table id: varInt</li>
* <li>count of all children (-1 if not known): int</li>
* <li>entry count: short</li>
......@@ -85,8 +85,8 @@ public class PageDataNode extends PageData {
private void read() throws SQLException {
data.reset();
this.parentPageId = data.readInt();
data.readByte();
this.parentPageId = data.readInt();
int indexId = data.readVarInt();
if (indexId != index.getId()) {
throw Message.getSQLException(ErrorCode.FILE_CORRUPTED_1,
......@@ -312,8 +312,8 @@ public class PageDataNode extends PageData {
}
private void writeHead() {
data.writeInt(parentPageId);
data.writeByte((byte) Page.TYPE_DATA_NODE);
data.writeInt(parentPageId);
data.writeVarInt(index.getId());
data.writeInt(rowCountStored);
data.writeShortInt(entryCount);
......
......@@ -18,8 +18,8 @@ import org.h2.store.PageStore;
/**
* Overflow data for a leaf page. Format:
* <ul>
* <li>parent page id (0 for root): int (0-3)</li>
* <li>page type: byte (4)</li>
* <li>page type: byte (0)</li>
* <li>parent page id (0 for root): int (1-4)</li>
* <li>more data: next overflow page id: int (5-8)</li>
* <li>last remaining size: short (5-6)</li>
* <li>data (9-/7-)</li>
......@@ -108,8 +108,8 @@ public class PageDataOverflow extends Page {
static PageDataOverflow create(PageStore store, int page, int type, int parentPageId, int next, Data all, int offset, int size) {
Data data = store.createData();
PageDataOverflow p = new PageDataOverflow(store, page, data);
data.writeInt(parentPageId);
data.writeByte((byte) type);
data.writeInt(parentPageId);
if (type == Page.TYPE_DATA_OVERFLOW) {
data.writeInt(next);
} else {
......@@ -129,8 +129,8 @@ public class PageDataOverflow extends Page {
*/
private void read() throws SQLException {
data.reset();
parentPageId = data.readInt();
type = data.readByte();
parentPageId = data.readInt();
if (type == (Page.TYPE_DATA_OVERFLOW | Page.FLAG_LAST)) {
size = data.readShortInt();
nextPage = 0;
......@@ -168,8 +168,8 @@ public class PageDataOverflow extends Page {
}
private void writeHead() {
data.writeInt(parentPageId);
data.writeByte((byte) type);
data.writeInt(parentPageId);
}
public void write(DataPage buff) throws SQLException {
......
......@@ -6,23 +6,20 @@
package org.h2.store;
import java.sql.SQLException;
import org.h2.constant.ErrorCode;
import org.h2.engine.Session;
import org.h2.message.Message;
import org.h2.util.BitField;
/**
* The list of free pages of a page store. The format of a free list trunk page
* is:
* <ul>
* <li>parent page id (always 0): int</li>
* <li>page type: byte</li>
* <li>data (5-)</li>
* <li>data (1-)</li>
* </ul>
*/
public class PageFreeList extends Page {
private static final int DATA_START = 5;
private static final int DATA_START = 1;
private final PageStore store;
private final BitField used = new BitField();
......@@ -45,7 +42,7 @@ public class PageFreeList extends Page {
* @param pageId the page id
* @return the page
*/
static PageFreeList read(PageStore store, Data data, int pageId) throws SQLException {
static PageFreeList read(PageStore store, Data data, int pageId) {
PageFreeList p = new PageFreeList(store, pageId);
p.data = data;
p.read();
......@@ -140,14 +137,9 @@ public class PageFreeList extends Page {
/**
* Read the page from the disk.
*/
private void read() throws SQLException {
private void read() {
data.reset();
int p = data.readInt();
int t = data.readByte();
if (p != 0) {
throw Message.getSQLException(ErrorCode.FILE_CORRUPTED_1, "pos:" + getPos() + " type:" + t + " parent:" + p
+ " expected type:" + Page.TYPE_FREE_LIST);
}
data.readByte();
for (int i = 0; i < pageCount; i += 8) {
used.setByte(i, data.readByte() & 255);
}
......@@ -160,9 +152,7 @@ public class PageFreeList extends Page {
public void write(DataPage buff) throws SQLException {
data = store.createData();
data.writeInt(0);
int type = Page.TYPE_FREE_LIST;
data.writeByte((byte) type);
data.writeByte((byte) Page.TYPE_FREE_LIST);
for (int i = 0; i < pageCount; i += 8) {
data.writeByte((byte) used.getByte(i));
}
......
......@@ -454,8 +454,8 @@ public class PageStore implements CacheWriter {
}
Data data = createData();
readPage(pageId, data);
data.readInt();
int type = data.readByte();
data.readInt();
Page p;
switch (type & ~Page.FLAG_LAST) {
case Page.TYPE_EMPTY:
......
......@@ -12,8 +12,8 @@ import org.h2.engine.Session;
/**
* A data page of a stream. The format is:
* <ul>
* <li>the trunk page id: int (0-3)</li>
* <li>page type: byte (4)</li>
* <li>page type: byte (0)</li>
* <li>the trunk page id: int (1-4)</li>
* <li>log key: int (5-8)</li>
* <li>data (9-)</li>
* </ul>
......@@ -68,8 +68,8 @@ public class PageStreamData extends Page {
*/
private void read() {
data.reset();
trunk = data.readInt();
data.readByte();
trunk = data.readInt();
logKey = data.readInt();
}
......@@ -82,8 +82,8 @@ public class PageStreamData extends Page {
*/
void initWrite() {
data = store.createData();
data.writeInt(trunk);
data.writeByte((byte) Page.TYPE_STREAM_DATA);
data.writeInt(trunk);
data.writeInt(logKey);
remaining = store.getPageSize() - data.length();
}
......
......@@ -13,8 +13,8 @@ import org.h2.engine.Session;
* A trunk page of a stream. It contains the page numbers of the stream, and the
* page number of the next trunk. The format is:
* <ul>
* <li>previous trunk page, or 0 if none: int (0-3)</li>
* <li>page type: byte (4)</li>
* <li>page type: byte (0)</li>
* <li>previous trunk page, or 0 if none: int (1-4)</li>
* <li>log key: int (5-8)</li>
* <li>next trunk page: int (9-12)</li>
* <li>number of pages: short (13-14)</li>
......@@ -84,8 +84,8 @@ public class PageStreamTrunk extends Page {
*/
private void read() {
data.reset();
parent = data.readInt();
data.readByte();
parent = data.readInt();
logKey = data.readInt();
nextTrunk = data.readInt();
pageCount = data.readShortInt();
......@@ -123,8 +123,8 @@ public class PageStreamTrunk extends Page {
public void write(DataPage buff) throws SQLException {
data = store.createData();
data.writeInt(parent);
data.writeByte((byte) Page.TYPE_STREAM_TRUNK);
data.writeInt(parent);
data.writeInt(logKey);
data.writeInt(nextTrunk);
data.writeShortInt(pageCount);
......
......@@ -859,14 +859,10 @@ public class Recover extends Tool implements DataHandler {
s = Data.create(this, pageSize);
store.seek(page * pageSize);
store.readFully(s.getBytes(), 0, pageSize);
int parentPageId = s.readInt();
int type = s.readByte();
switch (type) {
case Page.TYPE_EMPTY:
pageTypeCount[type]++;
if (parentPageId != 0) {
writer.println("-- ERROR empty page with parent: " + parentPageId);
}
emptyPages++;
continue;
}
......@@ -876,20 +872,24 @@ public class Recover extends Tool implements DataHandler {
// type 1
case Page.TYPE_DATA_LEAF: {
pageTypeCount[type]++;
int parentPageId = s.readInt();
setStorage(s.readVarInt());
int columnCount = s.readVarInt();
int entries = s.readShortInt();
writer.println("-- page " + page + ": data leaf " + (last ? "(last)" : "") + " table: " + storageId + " entries: " + entries + " columns: " + columnCount);
writer.println("-- page " + page + ": data leaf " + (last ? "(last)" : "") + " parent: " + parentPageId +
" table: " + storageId + " entries: " + entries + " columns: " + columnCount);
dumpPageDataLeaf(writer, s, last, page, columnCount, entries);
break;
}
// type 2
case Page.TYPE_DATA_NODE: {
pageTypeCount[type]++;
int parentPageId = s.readInt();
setStorage(s.readVarInt());
int rowCount = s.readInt();
int entries = s.readShortInt();
writer.println("-- page " + page + ": data node " + (last ? "(last)" : "") + " entries: " + entries + " rowCount: " + rowCount);
writer.println("-- page " + page + ": data node " + (last ? "(last)" : "") + " parent: " + parentPageId +
" entries: " + entries + " rowCount: " + rowCount);
break;
}
// type 3
......@@ -900,9 +900,11 @@ public class Recover extends Tool implements DataHandler {
// type 4
case Page.TYPE_BTREE_LEAF: {
pageTypeCount[type]++;
int parentPageId = s.readInt();
setStorage(s.readVarInt());
int entries = s.readShortInt();
writer.println("-- page " + page + ": b-tree leaf " + (last ? "(last)" : "") + " index: " + storageId + " entries: " + entries);
writer.println("-- page " + page + ": b-tree leaf " + (last ? "(last)" : "") + " parent: " + parentPageId +
" index: " + storageId + " entries: " + entries);
if (trace) {
dumpPageBtreeLeaf(writer, s, entries, !last);
}
......@@ -911,8 +913,10 @@ public class Recover extends Tool implements DataHandler {
// type 5
case Page.TYPE_BTREE_NODE:
pageTypeCount[type]++;
int parentPageId = s.readInt();
setStorage(s.readVarInt());
writer.println("-- page " + page + ": b-tree node" + (last ? "(last)" : "") + " index: " + storageId);
writer.println("-- page " + page + ": b-tree node" + (last ? "(last)" : "") + " parent: " + parentPageId +
"index: " + storageId);
if (trace) {
dumpPageBtreeNode(writer, s, !last);
}
......@@ -1107,8 +1111,8 @@ public class Recover extends Tool implements DataHandler {
store.seek((long) trunkPage * pageSize);
store.readFully(page.getBytes(), 0, pageSize);
page.reset();
page.readInt();
int t = page.readByte();
page.readInt();
if (t != Page.TYPE_STREAM_TRUNK) {
writer.println("-- eof page: " +trunkPage + " type: " + t + " expected type: " + Page.TYPE_STREAM_TRUNK);
endOfFile = true;
......@@ -1140,8 +1144,8 @@ public class Recover extends Tool implements DataHandler {
store.seek((long) nextPage * pageSize);
store.readFully(page.getBytes(), 0, pageSize);
page.reset();
int p = page.readInt();
int t = page.readByte();
int p = page.readInt();
int k = page.readInt();
if (t != Page.TYPE_STREAM_DATA) {
writer.println("-- eof page: " +nextPage+ " type: " + t + " parent: " + p +
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论