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

Improved performance.

上级 6086bebb
......@@ -243,8 +243,7 @@ public class PageStore implements CacheWriter {
* @param out the output stream
* @return the new position, or -1 if there is no more data to copy
*/
public int copyDirect(int pageId, OutputStream out) throws IOException {
synchronized (database) {
public synchronized int copyDirect(int pageId, OutputStream out) throws IOException {
byte[] buffer = new byte[pageSize];
if (pageId >= pageCount) {
return -1;
......@@ -255,7 +254,6 @@ public class PageStore implements CacheWriter {
out.write(buffer, 0, pageSize);
return pageId + 1;
}
}
/**
* Open the file and read the header.
......@@ -395,13 +393,12 @@ public class PageStore implements CacheWriter {
/**
* Flush all pending changes to disk, and switch the new transaction log.
*/
public void checkpoint() {
public synchronized void checkpoint() {
trace.debug("checkpoint");
if (log == null || database.isReadOnly()) {
// the file was never fully opened
return;
}
synchronized (database) {
database.checkPowerOff();
writeIndexRowCounts();
......@@ -441,7 +438,6 @@ public class PageStore implements CacheWriter {
}
}
}
}
/**
* Shrink the file so there are no empty pages at the end.
......@@ -495,7 +491,7 @@ public class PageStore implements CacheWriter {
for (int x = lastUsed, j = 0; x > MIN_PAGE_COUNT && j < maxMove; x -= blockSize) {
for (int full = x - blockSize + 1; full <= x; full++) {
if (full > MIN_PAGE_COUNT && isUsed(full)) {
synchronized (database) {
synchronized (this) {
firstFree = getFirstFree(firstFree);
if (firstFree == -1 || firstFree >= full) {
j = maxMove;
......@@ -695,8 +691,7 @@ public class PageStore implements CacheWriter {
* @param pageId the page id
* @return the page
*/
public Page getPage(int pageId) {
synchronized (database) {
public synchronized Page getPage(int pageId) {
Page p = (Page) cache.get(pageId);
if (p != null) {
return p;
......@@ -800,7 +795,6 @@ public class PageStore implements CacheWriter {
cache.put(p);
return p;
}
}
private int getFirstUncommittedSection() {
trace.debug("getFirstUncommittedSection");
......@@ -937,9 +931,8 @@ public class PageStore implements CacheWriter {
/**
* Close the file without further writing.
*/
public void close() {
public synchronized void close() {
trace.debug("close");
synchronized (database) {
if (log != null) {
log.close();
log = null;
......@@ -953,42 +946,35 @@ public class PageStore implements CacheWriter {
}
}
}
}
public void flushLog() {
public synchronized void flushLog() {
if (file != null) {
synchronized (database) {
log.flush();
}
}
}
/**
* Flush the transaction log and sync the file.
*/
public void sync() {
public synchronized void sync() {
if (file != null) {
synchronized (database) {
log.flush();
file.sync();
}
}
}
public Trace getTrace() {
return trace;
}
public void writeBack(CacheObject obj) {
public synchronized void writeBack(CacheObject obj) {
Page record = (Page) obj;
if (trace.isDebugEnabled()) {
trace.debug("writeBack {0}", record);
}
synchronized (database) {
record.write();
record.setChanged(false);
}
}
/**
* Write an undo log entry if required.
......@@ -996,11 +982,10 @@ public class PageStore implements CacheWriter {
* @param page the page
* @param old the old data (if known) or null
*/
public void logUndo(Page page, Data old) {
public synchronized void logUndo(Page page, Data old) {
if (logMode == LOG_MODE_OFF) {
return;
}
synchronized (database) {
checkOpen();
database.checkWritingAllowed();
if (!recoveryRunning) {
......@@ -1013,15 +998,13 @@ public class PageStore implements CacheWriter {
}
}
}
}
/**
* Update a page.
*
* @param page the page
*/
public void update(Page page) {
synchronized (database) {
public synchronized void update(Page page) {
if (trace.isDebugEnabled()) {
if (!page.isChanged()) {
trace.debug("updateRecord {0}", page.toString());
......@@ -1040,7 +1023,6 @@ public class PageStore implements CacheWriter {
allocatePage(pos);
cache.update(pos, page);
}
}
private int getFreeListId(int pageId) {
return (pageId - PAGE_ID_FREE_LIST_ROOT) / freeListPagesPerList;
......@@ -1050,7 +1032,7 @@ public class PageStore implements CacheWriter {
return getFreeList(getFreeListId(pageId));
}
private PageFreeList getFreeList(int i) {
private synchronized PageFreeList getFreeList(int i) {
PageFreeList list = null;
if (i < freeLists.size()) {
list = freeLists.get(i);
......@@ -1058,7 +1040,6 @@ public class PageStore implements CacheWriter {
return list;
}
}
synchronized (database) {
int p = PAGE_ID_FREE_LIST_ROOT + i * freeListPagesPerList;
while (p >= pageCount) {
increaseFileSize();
......@@ -1076,7 +1057,6 @@ public class PageStore implements CacheWriter {
freeLists.set(i, list);
return list;
}
}
private void freePage(int pageId) {
PageFreeList list = getFreeListForPage(pageId);
......@@ -1129,9 +1109,8 @@ public class PageStore implements CacheWriter {
return pos;
}
private int allocatePage(BitField exclude, int first) {
private synchronized int allocatePage(BitField exclude, int first) {
int page;
synchronized (database) {
// TODO could remember the first possible free list page
for (int i = 0;; i++) {
PageFreeList list = getFreeList(i);
......@@ -1148,7 +1127,6 @@ public class PageStore implements CacheWriter {
}
return page;
}
}
private void increaseFileSize() {
int increment = INCREMENT_KB * 1024 / pageSize;
......@@ -1185,11 +1163,10 @@ public class PageStore implements CacheWriter {
* @param pageId the page id
* @param undo if the undo record must have been written
*/
void free(int pageId, boolean undo) {
synchronized void free(int pageId, boolean undo) {
if (trace.isDebugEnabled()) {
// trace.debug("free " + pageId + " " + undo);
}
synchronized (database) {
cache.remove(pageId);
if (SysProperties.CHECK && !recoveryRunning && undo) {
// ensure the undo entry is already written
......@@ -1209,7 +1186,6 @@ public class PageStore implements CacheWriter {
}
}
}
}
/**
* Add a page to the free list. The page is not used, therefore doesn't need
......@@ -1217,16 +1193,14 @@ public class PageStore implements CacheWriter {
*
* @param pageId the page id
*/
void freeUnused(int pageId) {
synchronized void freeUnused(int pageId) {
if (trace.isDebugEnabled()) {
trace.debug("freeUnused {0}", pageId);
}
synchronized (database) {
cache.remove(pageId);
freePage(pageId);
freed.set(pageId);
}
}
/**
* Create a data object.
......@@ -1255,14 +1229,13 @@ public class PageStore implements CacheWriter {
* @param pos the page id
* @param page the page
*/
void readPage(int pos, Data page) {
synchronized void readPage(int pos, Data page) {
if (recordPageReads) {
if (pos >= MIN_PAGE_COUNT && recordedPagesIndex.get(pos) == IntIntHashMap.NOT_FOUND) {
recordedPagesIndex.put(pos, recordedPagesList.size());
recordedPagesList.add(pos);
}
}
synchronized (database) {
if (pos < 0 || pos >= pageCount) {
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, pos + " of " + pageCount);
}
......@@ -1270,7 +1243,6 @@ public class PageStore implements CacheWriter {
file.readFully(page.getBytes(), 0, pageSize);
readCount++;
}
}
/**
* Get the page size.
......@@ -1296,7 +1268,7 @@ public class PageStore implements CacheWriter {
* @param pageId the page id
* @param data the data
*/
public void writePage(int pageId, Data data) {
public synchronized void writePage(int pageId, Data data) {
if (pageId <= 0) {
DbException.throwInternalError("write to page " + pageId);
}
......@@ -1309,23 +1281,19 @@ public class PageStore implements CacheWriter {
}
}
checksumSet(bytes, pageId);
synchronized (database) {
file.seek((long) pageId << pageSizeShift);
file.write(bytes, 0, pageSize);
writeCount++;
}
}
/**
* Remove a page from the cache.
*
* @param pageId the page id
*/
public void removeRecord(int pageId) {
synchronized (database) {
public synchronized void removeRecord(int pageId) {
cache.remove(pageId);
}
}
Database getDatabase() {
return database;
......@@ -1401,23 +1369,20 @@ public class PageStore implements CacheWriter {
* @param row the row to add
* @param add true if the row is added, false if it is removed
*/
public void logAddOrRemoveRow(Session session, int tableId, Row row, boolean add) {
public synchronized void logAddOrRemoveRow(Session session, int tableId, Row row, boolean add) {
if (logMode != LOG_MODE_OFF) {
if (!recoveryRunning) {
synchronized (database) {
log.logAddOrRemoveRow(session, tableId, row, add);
}
}
}
}
/**
* Mark a committed transaction.
*
* @param session the session
*/
public void commit(Session session) {
synchronized (database) {
public synchronized void commit(Session session) {
checkOpen();
log.commit(session.getId());
if (log.getSize() - logSizeBase > maxLogSize) {
......@@ -1425,7 +1390,6 @@ public class PageStore implements CacheWriter {
logSizeBase = log.getSize();
}
}
}
/**
* Prepare a transaction.
......@@ -1433,11 +1397,9 @@ public class PageStore implements CacheWriter {
* @param session the session
* @param transaction the name of the transaction
*/
public void prepareCommit(Session session, String transaction) {
synchronized (database) {
public synchronized void prepareCommit(Session session, String transaction) {
log.prepareCommit(session, transaction);
}
}
/**
* Check whether this is a new database.
......@@ -1822,13 +1784,11 @@ public class PageStore implements CacheWriter {
* @param session the session
* @param tableId the table id
*/
public void logTruncate(Session session, int tableId) {
synchronized (database) {
public synchronized void logTruncate(Session session, int tableId) {
if (!recoveryRunning) {
log.logTruncate(session, tableId);
}
}
}
/**
* Get the root page of an index.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论