提交 6c5d1ec8 authored 作者: Thomas Mueller's avatar Thomas Mueller

When using temporary table, the database didn't shrink sometimes when closing.

上级 dadfe1d7
......@@ -55,7 +55,7 @@ public class InDoubtTransaction {
/**
* Change the state of this transaction.
* This will also update the log file.
* This will also update the transaction log.
*
* @param state the new state
*/
......
......@@ -24,7 +24,9 @@ public class PageInputStream extends InputStream {
private PageStreamTrunk.Iterator trunkIterator;
private int dataPage;
private PageStreamTrunk trunk;
private int trunkIndex;
private PageStreamData data;
private int dataPos;
private boolean endOfFile;
private int remaining;
private byte[] buffer = new byte[1];
......@@ -73,8 +75,9 @@ public class PageInputStream extends InputStream {
return -1;
}
int l = Math.min(remaining, len);
data.read(buff, off, l);
data.read(dataPos, buff, off, l);
remaining -= l;
dataPos += l;
return l;
} catch (DbException e) {
throw new EOFException();
......@@ -89,15 +92,15 @@ public class PageInputStream extends InputStream {
while (true) {
if (trunk == null) {
trunk = trunkIterator.next();
trunkIndex = 0;
logKey++;
if (trunk == null || trunk.getLogKey() != logKey) {
endOfFile = true;
return;
}
trunk.resetIndex();
}
if (trunk != null) {
next = trunk.getNextPageData();
next = trunk.getPageData(trunkIndex++);
if (next == -1) {
trunk = null;
} else if (dataPage == -1 || dataPage == next) {
......@@ -118,8 +121,8 @@ public class PageInputStream extends InputStream {
endOfFile = true;
return;
}
data.initRead();
remaining = data.getRemaining();
dataPos = data.getReadStart();
remaining = store.getPageSize() - dataPos;
}
/**
......@@ -141,9 +144,8 @@ public class PageInputStream extends InputStream {
break;
}
pages.set(t.getPos());
t.resetIndex();
while (true) {
int n = t.getNextPageData();
for (int i = 0;; i++) {
int n = t.getPageData(i);
if (n == -1) {
break;
}
......
......@@ -31,7 +31,7 @@ import org.h2.value.ValueNull;
* <li>type (0: no-op, 1: undo, 2: commit, ...)</li>
* <li>data</li>
* </ul>
* The log file is split into sections.
* The transaction log is split into sections.
* A checkpoint starts a new section.
*/
public class PageLog {
......@@ -533,7 +533,7 @@ public class PageLog {
}
// store it on a separate log page
int pageSize = store.getPageSize();
flushOut();
pageOut.flush();
pageOut.fillPage();
Data buffer = getBuffer();
buffer.writeByte((byte) PREPARE_COMMIT);
......@@ -636,7 +636,7 @@ public class PageLog {
undo = new BitSet();
logSectionId++;
logPos = 0;
flushOut();
pageOut.flush();
pageOut.fillPage();
int currentDataPage = pageOut.getCurrentDataPageId();
logSectionPageMap.put(logSectionId, currentDataPage);
......@@ -684,15 +684,14 @@ public class PageLog {
Page p = store.getPage(trunkPage);
PageStreamTrunk t = (PageStreamTrunk) p;
logKey = t.getLogKey();
t.resetIndex();
if (t.contains(firstDataPageToKeep)) {
return t.getPos();
}
trunkPage = t.getNextTrunk();
IntArray list = new IntArray();
list.add(t.getPos());
while (true) {
int next = t.getNextPageData();
for (int i = 0;; i++) {
int next = t.getPageData(i);
if (next == -1) {
break;
}
......@@ -719,8 +718,8 @@ public class PageLog {
* Check if the session committed after than the given position.
*
* @param sessionId the session id
* @param logId the log file id
* @param pos the position in the log file
* @param logId the log id
* @param pos the position in the log
* @return true if it is committed
*/
private boolean isSessionCommitted(int sessionId, int logId, int pos) {
......@@ -735,8 +734,8 @@ public class PageLog {
* Set the last commit record for a session.
*
* @param sessionId the session id
* @param logId the log file id
* @param pos the position in the log file
* @param logId the log id
* @param pos the position in the log
*/
private void setLastCommitForSession(int sessionId, int logId, int pos) {
SessionState state = getOrAddSessionState(sessionId);
......
......@@ -24,6 +24,7 @@ public class PageOutputStream {
private int trunkNext;
private IntArray reservedPages = new IntArray();
private PageStreamTrunk trunk;
private int trunkIndex;
private PageStreamData data;
private int reserved;
private int remaining;
......@@ -79,7 +80,7 @@ public class PageOutputStream {
}
private void initNextData() {
int nextData = trunk == null ? -1 : trunk.getNextPageData();
int nextData = trunk == null ? -1 : trunk.getPageData(trunkIndex++);
if (nextData == -1) {
int parent = trunkPageId;
if (trunkNext != 0) {
......@@ -93,10 +94,11 @@ public class PageOutputStream {
trunkNext = reservedPages.get(len);
logKey++;
trunk = PageStreamTrunk.create(store, parent, trunkPageId, trunkNext, logKey, pageIds);
trunkIndex = 0;
pageCount++;
trunk.write();
reservedPages.removeRange(0, len + 1);
nextData = trunk.getNextPageData();
nextData = trunk.getPageData(trunkIndex++);
}
data = PageStreamData.create(store, nextData, trunk.getPos(), logKey);
pageCount++;
......
......@@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.zip.CRC32;
import org.h2.command.ddl.CreateTableData;
import org.h2.constant.ErrorCode;
......@@ -323,7 +324,7 @@ public class PageStore implements CacheWriter {
}
/**
* Flush all pending changes to disk, and re-open the log file.
* Flush all pending changes to disk, and switch the new transaction log.
*/
public void checkpoint() {
trace.debug("checkpoint");
......@@ -564,8 +565,6 @@ public class PageStore implements CacheWriter {
}
private void readStaticHeader() {
long length = file.length();
database.notifyFileSize(length);
file.seek(FileStore.HEADER_LENGTH);
Data page = Data.create(database, new byte[PAGE_SIZE_MIN - FileStore.HEADER_LENGTH]);
file.readFully(page.getBytes(), 0, PAGE_SIZE_MIN - FileStore.HEADER_LENGTH);
......@@ -1056,11 +1055,13 @@ public class PageStore implements CacheWriter {
}
PageDataIndex systemTable = (PageDataIndex) metaObjects.get(0);
isNew = systemTable == null;
for (Index openIndex : metaObjects.values()) {
for (Iterator<PageIndex> it = metaObjects.values().iterator(); it.hasNext();) {
Index openIndex = it.next();
if (openIndex.getTable().isTemporary()) {
openIndex.truncate(systemSession);
openIndex.remove(systemSession);
removeMetaIndex(openIndex, systemSession);
it.remove();
} else {
openIndex.close(systemSession);
}
......@@ -1414,9 +1415,9 @@ public class PageStore implements CacheWriter {
}
/**
* Set the maximum log file size in megabytes.
* Set the maximum transaction log size in megabytes.
*
* @param maxSize the new maximum log file size
* @param maxSize the new maximum log size
*/
public void setMaxLogSize(long maxSize) {
this.maxLogSize = maxSize;
......@@ -1586,20 +1587,4 @@ public class PageStore implements CacheWriter {
return changeCount;
}
int getLogFirstTrunkPage() {
return logFirstTrunkPage;
}
int getLogKey() {
return logKey;
}
public PageLog getLog() {
return log;
}
int getLogFirstDataPage() {
return logFirstDataPage;
}
}
......@@ -118,12 +118,13 @@ public class PageStreamData extends Page {
/**
* Read the next bytes from the buffer.
*
* @param startPos the position in the data page
* @param buff the target buffer
* @param off the offset in the target buffer
* @param len the number of bytes to read
*/
void read(byte[] buff, int off, int len) {
data.read(buff, off, len);
void read(int startPos, byte[] buff, int off, int len) {
System.arraycopy(data.getBytes(), startPos, buff, off, len);
}
/**
......@@ -144,14 +145,6 @@ public class PageStreamData extends Page {
return store.getPageSize() >> 2;
}
/**
* Reset the index.
*/
void initRead() {
data.setPos(DATA_START);
remaining = store.getPageSize() - DATA_START;
}
public void moveTo(Session session, int newPos) {
// not required
}
......@@ -168,4 +161,8 @@ public class PageStreamData extends Page {
return true;
}
public int getReadStart() {
return DATA_START;
}
}
\ No newline at end of file
......@@ -42,7 +42,6 @@ public class PageStreamTrunk extends Page {
private int[] pageIds;
private int pageCount;
private Data data;
private int index;
private PageStreamTrunk(PageStore store, int parent, int pageId, int next, int logKey, int[] pageIds) {
setPos(pageId);
......@@ -107,13 +106,12 @@ public class PageStreamTrunk extends Page {
}
/**
* Reset the read/write index.
* Get the data page id at the given position.
*
* @param index the index (0, 1, ...)
* @return the value, or -1 if the index is too large
*/
void resetIndex() {
index = 0;
}
int getNextPageData() {
int getPageData(int index) {
if (index >= pageIds.length) {
return -1;
}
......
......@@ -19,7 +19,7 @@ class SessionState {
public int sessionId;
/**
* The last log file id where a commit for this session is found.
* The last log id where a commit for this session is found.
*/
public int lastCommitLog;
......@@ -36,8 +36,8 @@ class SessionState {
/**
* Check if this session state is already committed at this point.
*
* @param logId the log file id
* @param pos the position in the log file
* @param logId the log id
* @param pos the position in the log
* @return true if it is committed
*/
public boolean isCommitted(int logId, int pos) {
......
......@@ -14,8 +14,8 @@ import org.h2.message.Trace;
import org.h2.message.TraceSystem;
/**
* The writer thread is responsible to flush the transaction log file from time
* to time.
* The writer thread is responsible to flush the transaction transaction log
* from time to time.
*/
public class WriterThread implements Runnable {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论