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