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

Improved performance.

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