提交 2d4f153e authored 作者: Noel Grandin's avatar Noel Grandin

use enum for file lock method

上级 469a69ea
......@@ -44,6 +44,7 @@ import org.h2.schema.Sequence;
import org.h2.schema.TriggerObject;
import org.h2.store.DataHandler;
import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.store.FileStore;
import org.h2.store.InDoubtTransaction;
import org.h2.store.LobStorageBackend;
......@@ -139,7 +140,7 @@ public class Database implements DataHandler {
private boolean starting;
private TraceSystem traceSystem;
private Trace trace;
private final int fileLockMethod;
private final FileLockMethod fileLockMethod;
private Role publicRole;
private final AtomicLong modificationDataId = new AtomicLong();
private final AtomicLong modificationMetaId = new AtomicLong();
......@@ -235,14 +236,14 @@ public class Database implements DataHandler {
}
if (dbSettings.mvStore && lockMethodName == null) {
if (autoServerMode) {
fileLockMethod = FileLock.LOCK_FILE;
fileLockMethod = FileLockMethod.FILE;
} else {
fileLockMethod = FileLock.LOCK_FS;
fileLockMethod = FileLockMethod.FS;
}
} else {
fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
}
if (dbSettings.mvStore && fileLockMethod == FileLock.LOCK_SERIALIZED) {
if (dbSettings.mvStore && fileLockMethod == FileLockMethod.SERIALIZED) {
throw DbException.getUnsupportedException(
"MV_STORE combined with FILE_LOCK=SERIALIZED");
}
......@@ -407,7 +408,7 @@ public class Database implements DataHandler {
*/
private synchronized boolean reconnectModified(boolean pending) {
if (readOnly || lock == null ||
fileLockMethod != FileLock.LOCK_SERIALIZED) {
fileLockMethod != FileLockMethod.SERIALIZED) {
return true;
}
try {
......@@ -516,7 +517,7 @@ public class Database implements DataHandler {
}
if (lock != null) {
stopServer();
if (fileLockMethod != FileLock.LOCK_SERIALIZED) {
if (fileLockMethod != FileLockMethod.SERIALIZED) {
// allow testing shutdown
lock.unlock();
}
......@@ -646,9 +647,9 @@ public class Database implements DataHandler {
trace.info("opening {0} (build {1})", databaseName, Constants.BUILD_ID);
if (autoServerMode) {
if (readOnly ||
fileLockMethod == FileLock.LOCK_NO ||
fileLockMethod == FileLock.LOCK_SERIALIZED ||
fileLockMethod == FileLock.LOCK_FS ||
fileLockMethod == FileLockMethod.NO ||
fileLockMethod == FileLockMethod.SERIALIZED ||
fileLockMethod == FileLockMethod.FS ||
!persistent) {
throw DbException.getUnsupportedException(
"autoServerMode && (readOnly || " +
......@@ -665,8 +666,8 @@ public class Database implements DataHandler {
"Lock file exists: " + lockFileName);
}
}
if (!readOnly && fileLockMethod != FileLock.LOCK_NO) {
if (fileLockMethod != FileLock.LOCK_FS) {
if (!readOnly && fileLockMethod != FileLockMethod.NO) {
if (fileLockMethod != FileLockMethod.FS) {
lock = new FileLock(traceSystem, lockFileName, Constants.LOCK_SLEEP);
lock.lock(fileLockMethod);
if (autoServerMode) {
......@@ -1260,7 +1261,7 @@ public class Database implements DataHandler {
return;
}
throwLastBackgroundException();
if (fileLockMethod == FileLock.LOCK_SERIALIZED &&
if (fileLockMethod == FileLockMethod.SERIALIZED &&
!reconnectChangePending) {
// another connection may have written something - don't write
try {
......@@ -1437,8 +1438,8 @@ public class Database implements DataHandler {
}
closeFiles();
if (persistent && lock == null &&
fileLockMethod != FileLock.LOCK_NO &&
fileLockMethod != FileLock.LOCK_FS) {
fileLockMethod != FileLockMethod.NO &&
fileLockMethod != FileLockMethod.FS) {
// everything already closed (maybe in checkPowerOff)
// don't delete temp files in this case because
// the database could be open now (even from within another process)
......@@ -1456,7 +1457,7 @@ public class Database implements DataHandler {
lobSession = null;
}
if (lock != null) {
if (fileLockMethod == FileLock.LOCK_SERIALIZED) {
if (fileLockMethod == FileLockMethod.SERIALIZED) {
// wait before deleting the .lock file,
// otherwise other connections can not detect that
if (lock.load().containsKey("changePending")) {
......@@ -1985,7 +1986,7 @@ public class Database implements DataHandler {
if (readOnly) {
throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY);
}
if (fileLockMethod == FileLock.LOCK_SERIALIZED) {
if (fileLockMethod == FileLockMethod.SERIALIZED) {
if (!reconnectChangePending) {
throw DbException.get(ErrorCode.DATABASE_IS_READ_ONLY);
}
......@@ -2542,7 +2543,7 @@ public class Database implements DataHandler {
if (pageSize != Constants.DEFAULT_PAGE_SIZE) {
pageStore.setPageSize(pageSize);
}
if (!readOnly && fileLockMethod == FileLock.LOCK_FS) {
if (!readOnly && fileLockMethod == FileLockMethod.FS) {
pageStore.setLockFile(true);
}
pageStore.setLogMode(logMode);
......@@ -2578,7 +2579,7 @@ public class Database implements DataHandler {
* @return true if reconnecting is required
*/
public boolean isReconnectNeeded() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) {
if (fileLockMethod != FileLockMethod.SERIALIZED) {
return false;
}
if (reconnectChangePending) {
......@@ -2632,7 +2633,7 @@ public class Database implements DataHandler {
* the .lock.db file.
*/
public void checkpointIfRequired() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED ||
if (fileLockMethod != FileLockMethod.SERIALIZED ||
readOnly || !reconnectChangePending || closing) {
return;
}
......@@ -2661,7 +2662,7 @@ public class Database implements DataHandler {
}
public boolean isFileLockSerialized() {
return fileLockMethod == FileLock.LOCK_SERIALIZED;
return fileLockMethod == FileLockMethod.SERIALIZED;
}
private void flushSequences() {
......@@ -2695,7 +2696,7 @@ public class Database implements DataHandler {
* false if another connection was faster
*/
public boolean beforeWriting() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) {
if (fileLockMethod != FileLockMethod.SERIALIZED) {
return true;
}
while (checkpointRunning) {
......@@ -2724,7 +2725,7 @@ public class Database implements DataHandler {
* This method is called after updates are finished.
*/
public void afterWriting() {
if (fileLockMethod != FileLock.LOCK_SERIALIZED) {
if (fileLockMethod != FileLockMethod.SERIALIZED) {
return;
}
synchronized (reconnectSync) {
......
......@@ -7,7 +7,6 @@ package org.h2.engine;
import java.util.HashMap;
import java.util.Objects;
import org.h2.api.ErrorCode;
import org.h2.command.CommandInterface;
import org.h2.command.Parser;
......@@ -15,6 +14,7 @@ import org.h2.command.dml.SetTypes;
import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.util.MathUtils;
import org.h2.util.ThreadDeadlockDetector;
import org.h2.util.Utils;
......@@ -141,8 +141,8 @@ public class Engine implements SessionFactory {
try {
ConnectionInfo backup = null;
String lockMethodName = ci.getProperty("FILE_LOCK", null);
int fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
if (fileLockMethod == FileLock.LOCK_SERIALIZED) {
FileLockMethod fileLockMethod = FileLock.getFileLockMethod(lockMethodName);
if (fileLockMethod == FileLockMethod.SERIALIZED) {
// In serialized mode, database instance sharing is not possible
ci.setProperty("OPEN_NEW", "TRUE");
try {
......
......@@ -41,7 +41,7 @@ public class FileLister {
FileLock lock = new FileLock(new TraceSystem(null), fileName,
Constants.LOCK_SLEEP);
try {
lock.lock(FileLock.LOCK_FILE);
lock.lock(FileLockMethod.FILE);
lock.unlock();
} catch (DbException e) {
throw DbException.get(
......
......@@ -35,33 +35,6 @@ import org.h2.value.Transfer;
*/
public class FileLock implements Runnable {
/**
* This locking method means no locking is used at all.
*/
public static final int LOCK_NO = 0;
/**
* This locking method means the cooperative file locking protocol should be
* used.
*/
public static final int LOCK_FILE = 1;
/**
* This locking method means a socket is created on the given machine.
*/
public static final int LOCK_SOCKET = 2;
/**
* This locking method means multiple writers are allowed, and they
* synchronize themselves.
*/
public static final int LOCK_SERIALIZED = 3;
/**
* Use the file system to lock the file; don't use a separate lock file.
*/
public static final int LOCK_FS = 4;
private static final String MAGIC = "FileLock";
private static final String FILE = "file";
private static final String SOCKET = "socket";
......@@ -125,22 +98,22 @@ public class FileLock implements Runnable {
* @param fileLockMethod the file locking method to use
* @throws DbException if locking was not successful
*/
public synchronized void lock(int fileLockMethod) {
public synchronized void lock(FileLockMethod fileLockMethod) {
checkServer();
if (locked) {
DbException.throwInternalError("already locked");
}
switch (fileLockMethod) {
case LOCK_FILE:
case FILE:
lockFile();
break;
case LOCK_SOCKET:
case SOCKET:
lockSocket();
break;
case LOCK_SERIALIZED:
case SERIALIZED:
lockSerialized();
break;
case LOCK_FS:
case FS:
break;
}
locked = true;
......@@ -480,17 +453,17 @@ public class FileLock implements Runnable {
* @return the method type
* @throws DbException if the method name is unknown
*/
public static int getFileLockMethod(String method) {
public static FileLockMethod getFileLockMethod(String method) {
if (method == null || method.equalsIgnoreCase("FILE")) {
return FileLock.LOCK_FILE;
return FileLockMethod.FILE;
} else if (method.equalsIgnoreCase("NO")) {
return FileLock.LOCK_NO;
return FileLockMethod.NO;
} else if (method.equalsIgnoreCase("SOCKET")) {
return FileLock.LOCK_SOCKET;
return FileLockMethod.SOCKET;
} else if (method.equalsIgnoreCase("SERIALIZED")) {
return FileLock.LOCK_SERIALIZED;
return FileLockMethod.SERIALIZED;
} else if (method.equalsIgnoreCase("FS")) {
return FileLock.LOCK_FS;
return FileLockMethod.FS;
} else {
throw DbException.get(
ErrorCode.UNSUPPORTED_LOCK_METHOD_1, method);
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.store;
public enum FileLockMethod {
/**
* This locking method means no locking is used at all.
*/
NO,
/**
* This locking method means the cooperative file locking protocol should be
* used.
*/
FILE,
/**
* This locking method means a socket is created on the given machine.
*/
SOCKET,
/**
* This locking method means multiple writers are allowed, and they
* synchronize themselves.
*/
SERIALIZED,
/**
* Use the file system to lock the file; don't use a separate lock file.
*/
FS;
}
......@@ -7,11 +7,11 @@ package org.h2.test.unit;
import java.io.File;
import java.sql.Connection;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.message.TraceSystem;
import org.h2.store.FileLock;
import org.h2.store.FileLockMethod;
import org.h2.test.TestBase;
/**
......@@ -77,7 +77,7 @@ public class TestFileLock extends TestBase implements Runnable {
f.setLastModified(System.currentTimeMillis() + 10000);
FileLock lock = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP);
lock.lock(FileLock.LOCK_FILE);
lock.lock(FileLockMethod.FILE);
lock.unlock();
}
......@@ -86,14 +86,14 @@ public class TestFileLock extends TestBase implements Runnable {
Constants.LOCK_SLEEP);
FileLock lock2 = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP);
lock1.lock(FileLock.LOCK_FILE);
lock1.lock(FileLockMethod.FILE);
createClassProxy(FileLock.class);
assertThrows(ErrorCode.DATABASE_ALREADY_OPEN_1, lock2).lock(
FileLock.LOCK_FILE);
FileLockMethod.FILE);
lock1.unlock();
lock2 = new FileLock(new TraceSystem(null), getFile(),
Constants.LOCK_SLEEP);
lock2.lock(FileLock.LOCK_FILE);
lock2.lock(FileLockMethod.FILE);
lock2.unlock();
}
......@@ -123,8 +123,8 @@ public class TestFileLock extends TestBase implements Runnable {
while (!stop) {
lock = new FileLock(new TraceSystem(null), getFile(), 100);
try {
lock.lock(allowSockets ? FileLock.LOCK_SOCKET
: FileLock.LOCK_FILE);
lock.lock(allowSockets ? FileLockMethod.SOCKET
: FileLockMethod.FILE);
base.trace(lock + " locked");
locks++;
if (locks > 1) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论