提交 68ef8e9c authored 作者: andrei's avatar andrei

tryCommit inroduced, some class finals removed, based on code review

上级 7384fd28
......@@ -13,7 +13,7 @@ import java.util.Iterator;
* @param <K> the key type
* @param <V> the value type
*/
public final class Cursor<K, V> implements Iterator<K> {
public class Cursor<K, V> implements Iterator<K> {
private final K to;
private CursorPos cursorPos;
private CursorPos keeper;
......
......@@ -8,7 +8,7 @@ package org.h2.mvstore;
/**
* A position in a cursor
*/
public final class CursorPos {
public class CursorPos {
/**
* The current page.
......
......@@ -127,7 +127,7 @@ MVStore:
/**
* A persistent storage for maps.
*/
public final class MVStore {
public class MVStore {
/**
* Whether assertions are enabled.
......@@ -1037,6 +1037,25 @@ public final class MVStore {
onVersionChange(version);
}
/**
* Unlike regular commit this method returns immediately if there is commit
* in progress on another thread, otherwise it acts as regular commit.
*
* This method may return BEFORE this thread changes are actually persisted!
*
* @return the new version (incremented if there were changes)
*/
public long tryCommit() {
// unlike synchronization, this will also prevent re-entrance,
// which may be possible, if the meta map have changed
if (currentStoreThread.compareAndSet(null, Thread.currentThread())) {
synchronized (this) {
store();
}
}
return currentVersion;
}
/**
* Commit the changes.
* <p>
......@@ -1053,16 +1072,19 @@ public final class MVStore {
*
* @return the new version (incremented if there were changes)
*/
public long commit() {
// unlike synchronization, this will also prevent re-entrance,
// which may be possible, if the meta map have changed
if (currentStoreThread.compareAndSet(null, Thread.currentThread())) {
synchronized (this) {
public synchronized long commit() {
currentStoreThread.set(Thread.currentThread());
store();
return currentVersion;
}
private void store() {
try {
currentStoreVersion = currentVersion;
if (!closed && hasUnsavedChangesInternal()) {
currentStoreVersion = currentVersion;
if (fileStore == null) {
lastStoredVersion = currentVersion;
//noinspection NonAtomicOperationOnVolatileField
++currentVersion;
setWriteVersion(currentVersion);
metaChanged = false;
......@@ -1087,9 +1109,6 @@ public final class MVStore {
currentStoreThread.set(null);
}
}
}
return currentVersion;
}
private void storeNow() {
assert Thread.holdsLock(this);
......@@ -2283,7 +2302,7 @@ public final class MVStore {
saveNeeded = false;
// check again, because it could have been written by now
if (unsavedMemory > autoCommitMemory && autoCommitMemory > 0) {
commit();
tryCommit();
}
}
}
......@@ -2578,7 +2597,7 @@ public final class MVStore {
if (time <= lastCommitTime + autoCommitDelay) {
return;
}
commit();
tryCommit();
if (autoCompactFillRate > 0) {
// whether there were file read or write operations since
// the last time
......
......@@ -475,7 +475,7 @@ public class TransactionStore {
t.setStatus(Transaction.STATUS_CLOSED);
openTransactions.clear(t.transactionId);
if (oldStatus == Transaction.STATUS_PREPARED || store.getAutoCommitDelay() == 0) {
store.commit();
store.tryCommit();
return;
}
// to avoid having to store the transaction log,
......@@ -486,7 +486,7 @@ public class TransactionStore {
int max = store.getAutoCommitMemory();
// save at 3/4 capacity
if (unsaved * 4 > max * 3) {
store.commit();
store.tryCommit();
}
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论