mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 14:19:53 +01:00
Cleaned up database initialisation.
This commit is contained in:
@@ -26,4 +26,18 @@ abstract class StringMap extends Hashtable<String, String> {
|
|||||||
public void putBoolean(String key, boolean value) {
|
public void putBoolean(String key, boolean value) {
|
||||||
put(key, String.valueOf(value));
|
put(key, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getInt(String key, int defaultValue) {
|
||||||
|
String s = get(key);
|
||||||
|
if (s == null) return defaultValue;
|
||||||
|
try {
|
||||||
|
return Integer.valueOf(s);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putInt(String key, int value) {
|
||||||
|
put(key, String.valueOf(value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import java.util.Map;
|
|||||||
public interface DatabaseComponent {
|
public interface DatabaseComponent {
|
||||||
|
|
||||||
/** Opens the database and returns true if the database already existed. */
|
/** Opens the database and returns true if the database already existed. */
|
||||||
boolean open() throws DbException, IOException;
|
boolean open() throws DbException;
|
||||||
|
|
||||||
/** Waits for any open transactions to finish and closes the database. */
|
/** Waits for any open transactions to finish and closes the database. */
|
||||||
void close() throws DbException, IOException;
|
void close() throws DbException, IOException;
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ interface Database<T> {
|
|||||||
* <p>
|
* <p>
|
||||||
* Locking: write.
|
* Locking: write.
|
||||||
*/
|
*/
|
||||||
boolean open() throws DbException, IOException;
|
boolean open() throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prevents new transactions from starting, waits for all current
|
* Prevents new transactions from starting, waits for all current
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
|||||||
this.shutdown = shutdown;
|
this.shutdown = shutdown;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean open() throws DbException, IOException {
|
public boolean open() throws DbException {
|
||||||
Runnable shutdownHook = new Runnable() {
|
Runnable shutdownHook = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
lock.writeLock().lock();
|
lock.writeLock().lock();
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import org.briarproject.api.system.Clock;
|
|||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
@@ -18,7 +17,7 @@ import javax.inject.Inject;
|
|||||||
/** Contains all the H2-specific code for the database. */
|
/** Contains all the H2-specific code for the database. */
|
||||||
class H2Database extends JdbcDatabase {
|
class H2Database extends JdbcDatabase {
|
||||||
|
|
||||||
private static final String HASH_TYPE = "BINARY(48)";
|
private static final String HASH_TYPE = "BINARY(32)";
|
||||||
private static final String BINARY_TYPE = "BINARY";
|
private static final String BINARY_TYPE = "BINARY";
|
||||||
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
|
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
|
||||||
private static final String SECRET_TYPE = "BINARY(32)";
|
private static final String SECRET_TYPE = "BINARY(32)";
|
||||||
@@ -30,13 +29,14 @@ class H2Database extends JdbcDatabase {
|
|||||||
H2Database(DatabaseConfig config, Clock clock) {
|
H2Database(DatabaseConfig config, Clock clock) {
|
||||||
super(HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE, clock);
|
super(HASH_TYPE, BINARY_TYPE, COUNTER_TYPE, SECRET_TYPE, clock);
|
||||||
this.config = config;
|
this.config = config;
|
||||||
String path = new File(config.getDatabaseDirectory(), "db").getAbsolutePath();
|
File dir = config.getDatabaseDirectory();
|
||||||
|
String path = new File(dir, "db").getAbsolutePath();
|
||||||
// FIXME: Remove WRITE_DELAY=0 after implementing BTPv2?
|
// FIXME: Remove WRITE_DELAY=0 after implementing BTPv2?
|
||||||
url = "jdbc:h2:split:" + path + ";CIPHER=AES;MULTI_THREADED=1"
|
url = "jdbc:h2:split:" + path + ";CIPHER=AES;MULTI_THREADED=1"
|
||||||
+ ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false";
|
+ ";WRITE_DELAY=0;DB_CLOSE_ON_EXIT=false";
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean open() throws DbException, IOException {
|
public boolean open() throws DbException {
|
||||||
boolean reopen = config.databaseExists();
|
boolean reopen = config.databaseExists();
|
||||||
if (!reopen) config.getDatabaseDirectory().mkdirs();
|
if (!reopen) config.getDatabaseDirectory().mkdirs();
|
||||||
super.open("org.h2.Driver", reopen);
|
super.open("org.h2.Driver", reopen);
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import org.briarproject.api.transport.IncomingKeys;
|
|||||||
import org.briarproject.api.transport.OutgoingKeys;
|
import org.briarproject.api.transport.OutgoingKeys;
|
||||||
import org.briarproject.api.transport.TransportKeys;
|
import org.briarproject.api.transport.TransportKeys;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -271,8 +270,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void open(String driverClass, boolean reopen) throws DbException,
|
protected void open(String driverClass, boolean reopen) throws DbException {
|
||||||
IOException {
|
|
||||||
// Load the JDBC driver
|
// Load the JDBC driver
|
||||||
try {
|
try {
|
||||||
Class.forName(driverClass);
|
Class.forName(driverClass);
|
||||||
@@ -286,10 +284,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
if (!checkSchemaVersion(txn)) throw new DbException();
|
if (!checkSchemaVersion(txn)) throw new DbException();
|
||||||
} else {
|
} else {
|
||||||
createTables(txn);
|
createTables(txn);
|
||||||
Settings s = new Settings();
|
storeSchemaVersion(txn);
|
||||||
s.put("schemaVersion", String.valueOf(SCHEMA_VERSION));
|
|
||||||
s.put("minSchemaVersion", String.valueOf(MIN_SCHEMA_VERSION));
|
|
||||||
mergeSettings(txn, s, "db");
|
|
||||||
}
|
}
|
||||||
commitTransaction(txn);
|
commitTransaction(txn);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
@@ -299,16 +294,19 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkSchemaVersion(Connection txn) throws DbException {
|
private boolean checkSchemaVersion(Connection txn) throws DbException {
|
||||||
try {
|
Settings s = getSettings(txn, "db");
|
||||||
Settings s = getSettings(txn, "db");
|
int schemaVersion = s.getInt("schemaVersion", -1);
|
||||||
int schemaVersion = Integer.valueOf(s.get("schemaVersion"));
|
if (schemaVersion == SCHEMA_VERSION) return true;
|
||||||
if (schemaVersion == SCHEMA_VERSION) return true;
|
if (schemaVersion < MIN_SCHEMA_VERSION) return false;
|
||||||
if (schemaVersion < MIN_SCHEMA_VERSION) return false;
|
int minSchemaVersion = s.getInt("minSchemaVersion", -1);
|
||||||
int minSchemaVersion = Integer.valueOf(s.get("minSchemaVersion"));
|
return SCHEMA_VERSION >= minSchemaVersion;
|
||||||
return SCHEMA_VERSION >= minSchemaVersion;
|
}
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new DbException(e);
|
private void storeSchemaVersion(Connection txn) throws DbException {
|
||||||
}
|
Settings s = new Settings();
|
||||||
|
s.putInt("schemaVersion", SCHEMA_VERSION);
|
||||||
|
s.putInt("minSchemaVersion", MIN_SCHEMA_VERSION);
|
||||||
|
mergeSettings(txn, s, "db");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void tryToClose(ResultSet rs) {
|
private void tryToClose(ResultSet rs) {
|
||||||
@@ -2352,7 +2350,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setContactStatus(Connection txn, ContactId c, StorageStatus s)
|
public void setContactStatus(Connection txn, ContactId c, StorageStatus s)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
String sql = "UPDATE contacts SET status = ? WHERE contactId = ?";
|
String sql = "UPDATE contacts SET status = ? WHERE contactId = ?";
|
||||||
@@ -2387,7 +2385,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setMessageValidity(Connection txn, MessageId m, boolean valid)
|
public void setMessageValidity(Connection txn, MessageId m, boolean valid)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
try {
|
try {
|
||||||
String sql = "UPDATE messages SET valid = ? WHERE messageId = ?";
|
String sql = "UPDATE messages SET valid = ? WHERE messageId = ?";
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package org.briarproject.lifecycle;
|
package org.briarproject.lifecycle;
|
||||||
|
|
||||||
import static java.util.logging.Level.INFO;
|
import org.briarproject.api.db.DatabaseComponent;
|
||||||
import static java.util.logging.Level.WARNING;
|
import org.briarproject.api.db.DbException;
|
||||||
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING;
|
import org.briarproject.api.event.EventBus;
|
||||||
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.DB_ERROR;
|
import org.briarproject.api.event.ShutdownEvent;
|
||||||
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.SERVICE_ERROR;
|
import org.briarproject.api.lifecycle.LifecycleManager;
|
||||||
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
|
import org.briarproject.api.lifecycle.Service;
|
||||||
|
import org.briarproject.api.system.Clock;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@@ -17,13 +18,12 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import org.briarproject.api.db.DatabaseComponent;
|
import static java.util.logging.Level.INFO;
|
||||||
import org.briarproject.api.db.DbException;
|
import static java.util.logging.Level.WARNING;
|
||||||
import org.briarproject.api.event.EventBus;
|
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.ALREADY_RUNNING;
|
||||||
import org.briarproject.api.event.ShutdownEvent;
|
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.DB_ERROR;
|
||||||
import org.briarproject.api.lifecycle.LifecycleManager;
|
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.SERVICE_ERROR;
|
||||||
import org.briarproject.api.lifecycle.Service;
|
import static org.briarproject.api.lifecycle.LifecycleManager.StartResult.SUCCESS;
|
||||||
import org.briarproject.api.system.Clock;
|
|
||||||
|
|
||||||
class LifecycleManagerImpl implements LifecycleManager {
|
class LifecycleManagerImpl implements LifecycleManager {
|
||||||
|
|
||||||
@@ -98,9 +98,6 @@ class LifecycleManagerImpl implements LifecycleManager {
|
|||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
return DB_ERROR;
|
return DB_ERROR;
|
||||||
} catch (IOException e) {
|
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
|
||||||
return DB_ERROR;
|
|
||||||
} finally {
|
} finally {
|
||||||
startStopSemaphore.release();
|
startStopSemaphore.release();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user