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

MVStore: move background exception listener to the builder

上级 1dfdeea0
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
package org.h2.mvstore.db; package org.h2.mvstore.db;
import java.beans.ExceptionListener;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -38,52 +39,61 @@ public class MVTableEngine implements TableEngine { ...@@ -38,52 +39,61 @@ public class MVTableEngine implements TableEngine {
* @param db the database * @param db the database
* @return the store * @return the store
*/ */
public static Store init(Database db) { public static Store init(final Database db) {
Store store = db.getMvStore(); Store store = db.getMvStore();
if (store == null) { if (store != null) {
byte[] key = db.getFilePasswordHash(); return store;
String dbPath = db.getDatabasePath(); }
MVStore.Builder builder = new MVStore.Builder(); byte[] key = db.getFilePasswordHash();
if (dbPath == null) { String dbPath = db.getDatabasePath();
store = new Store(db, builder.open()); MVStore.Builder builder = new MVStore.Builder();
if (dbPath == null) {
store = new Store(db, builder.open());
} else {
String fileName = dbPath + Constants.SUFFIX_MV_FILE;
builder.fileName(fileName);
if (db.isReadOnly()) {
builder.readOnly();
} else { } else {
String fileName = dbPath + Constants.SUFFIX_MV_FILE; // possibly create the directory
builder.fileName(fileName); boolean exists = FileUtils.exists(fileName);
if (db.isReadOnly()) { if (exists && !FileUtils.canWrite(fileName)) {
builder.readOnly(); // read only
} else { } else {
// possibly create the directory String dir = FileUtils.getParent(fileName);
boolean exists = FileUtils.exists(fileName); FileUtils.createDirectories(dir);
if (exists && !FileUtils.canWrite(fileName)) {
// read only
} else {
String dir = FileUtils.getParent(fileName);
FileUtils.createDirectories(dir);
}
} }
if (key != null) { }
char[] password = new char[key.length]; if (key != null) {
for (int i = 0; i < key.length; i++) { char[] password = new char[key.length];
password[i] = (char) key[i]; for (int i = 0; i < key.length; i++) {
} password[i] = (char) key[i];
builder.encryptionKey(password);
} }
try { builder.encryptionKey(password);
store = new Store(db, builder.open()); }
} catch (IllegalStateException e) { builder.backgroundExceptionListener(new ExceptionListener() {
int errorCode = DataUtils.getErrorCode(e.getMessage());
if (errorCode == DataUtils.ERROR_FILE_CORRUPT) { @Override
if (key != null) { public void exceptionThrown(Exception e) {
throw DbException.get(ErrorCode.FILE_ENCRYPTION_ERROR_1, fileName); db.setBackgroundException(DbException.convert(e));
} }
} else if (errorCode == DataUtils.ERROR_FILE_LOCKED) {
throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, fileName); });
try {
store = new Store(db, builder.open());
} catch (IllegalStateException e) {
int errorCode = DataUtils.getErrorCode(e.getMessage());
if (errorCode == DataUtils.ERROR_FILE_CORRUPT) {
if (key != null) {
throw DbException.get(ErrorCode.FILE_ENCRYPTION_ERROR_1, fileName);
} }
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, fileName); } else if (errorCode == DataUtils.ERROR_FILE_LOCKED) {
throw DbException.get(ErrorCode.DATABASE_ALREADY_OPEN_1, fileName);
} }
throw DbException.get(ErrorCode.FILE_CORRUPTED_1, fileName);
} }
db.setMvStore(store);
} }
db.setMvStore(store);
return store; return store;
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论