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

new experimental page store

上级 20be2f73
...@@ -10,7 +10,12 @@ package org.h2.index; ...@@ -10,7 +10,12 @@ package org.h2.index;
/** /**
* A page. * A page.
*/ */
class Page { public class Page {
/**
* A log page.
*/
public static final int TYPE_LOG = 8;
/** /**
* An empty page. * An empty page.
......
...@@ -6,43 +6,93 @@ ...@@ -6,43 +6,93 @@
*/ */
package org.h2.store; package org.h2.store;
import java.sql.SQLException;
import org.h2.index.Page;
import org.h2.util.BitField; import org.h2.util.BitField;
/** /**
* Transaction log mechanism. * Transaction log mechanism.
* The log is split in pages, the format is:
* <ul><li>0-3: parent page id
* </li><li>4-4: page type (LOG)
* </li><li>5-8: the next page (0 for end)
* </li><li>9-: data
* </li></ul>
* The data format is:
* <ul><li>0-0: type (0: end, 1: undo)
* </li><li>1-4: page id
* </li><li>5-: data
* </li></ul>
*/ */
public class PageLog { public class PageLog {
private static final int BUFFER_SIZE = 32 * 1024;
private PageStore store; private PageStore store;
private BitField undo = new BitField(); private BitField undo = new BitField();
private byte[] ringBuffer = new byte[BUFFER_SIZE];
private int bufferPos; private int bufferPos;
private int firstPage;
private int nextPage;
private DataPageBinary data;
private DataPageBinary output;
PageLog(PageStore store) { PageLog(PageStore store, int firstPage) {
this.store = store; this.store = store;
this.firstPage = firstPage;
data = store.createDataPage();
output = store.createDataPage();
}
void open() throws SQLException {
if (firstPage == 0) {
return;
}
undo();
prepareOutput();
}
private void prepareOutput() {
output.reset();
output.writeInt(0);
output.writeByte((byte) Page.TYPE_LOG);
output.writeInt(store.allocatePage());
} }
void addUndo(int pageId) { private void undo() throws SQLException {
int next = firstPage;
while (next != 0) {
data = store.readPage(firstPage);
data.setPos(4);
}
}
void addUndo(int pageId) throws SQLException {
if (undo.get(pageId)) {
return;
}
undo.set(pageId);
data.reset();
data.writeByte((byte) 1);
data.writeInt(pageId);
DataPageBinary p = store.readPage(pageId);
data.write(p.getBytes(), 0, store.getPageSize());
write(data.getBytes(), 0, data.length());
} }
private void write(byte[] data, int offset, int length) { private void write(byte[] data, int offset, int length) {
if (bufferPos + length > BUFFER_SIZE) { if (bufferPos + length > store.getPageSize()) {
while (length > 0) { while (length > 0) {
int len = Math.min(length, BUFFER_SIZE - bufferPos); int len = Math.min(length, store.getPageSize() - bufferPos);
write(data, offset, len); write(data, offset, len);
offset += len; offset += len;
length -= len; length -= len;
} }
return; return;
} }
System.arraycopy(data, offset, ringBuffer, bufferPos, length); System.arraycopy(data, offset, output.getBytes(), bufferPos, length);
bufferPos += length; bufferPos += length;
if (bufferPos == BUFFER_SIZE) { // if (bufferPos != BUFFER_SIZE) {
// return;
// }
} }
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论