Store a dirty flag in the database

This commit is contained in:
Sebastian Kürten
2021-04-06 12:15:38 +02:00
parent bebf3bbc39
commit db84d07c38
5 changed files with 45 additions and 0 deletions

View File

@@ -68,6 +68,13 @@ interface Database<T> {
*/
void close() throws DbException;
/**
* Returns true if the dirty flag was set while opening the database,
* indicating that the database has not been shut down properly the last
* time it was closed and some data could be lost.
*/
boolean wasDirtyOnInitialisation();
/**
* Starts a new transaction and returns an object representing it.
*/

View File

@@ -37,4 +37,10 @@ interface DatabaseConstants {
* has passed since the last compaction.
*/
long MAX_COMPACTION_INTERVAL_MS = DAYS.toMillis(30);
/**
* The {@link Settings} key under which the flag is stored indicating
* whether the database is marked as dirty.
*/
String DIRTY_KEY = "dirty";
}

View File

@@ -84,9 +84,15 @@ class H2Database extends JdbcDatabase {
@Override
public void close() throws DbException {
// H2 will close the database when the last connection closes
Connection c = null;
try {
c = createConnection();
super.closeAllConnections();
setDirty(c, false);
c.commit();
c.close();
} catch (SQLException e) {
tryToClose(c, LOG, WARNING);
throw new DbException(e);
}
}

View File

@@ -81,6 +81,8 @@ class HyperSqlDatabase extends JdbcDatabase {
try {
super.closeAllConnections();
c = createConnection();
setDirty(c, false);
c.commit();
s = c.createStatement();
s.executeQuery("SHUTDOWN");
s.close();

View File

@@ -81,6 +81,7 @@ import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERE
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
import static org.briarproject.bramble.api.sync.validation.MessageState.UNKNOWN;
import static org.briarproject.bramble.db.DatabaseConstants.DB_SETTINGS_NAMESPACE;
import static org.briarproject.bramble.db.DatabaseConstants.DIRTY_KEY;
import static org.briarproject.bramble.db.DatabaseConstants.LAST_COMPACTED_KEY;
import static org.briarproject.bramble.db.DatabaseConstants.MAX_COMPACTION_INTERVAL_MS;
import static org.briarproject.bramble.db.DatabaseConstants.SCHEMA_VERSION_KEY;
@@ -354,9 +355,14 @@ abstract class JdbcDatabase implements Database<Connection> {
@GuardedBy("connectionsLock")
private boolean closed = false;
private boolean wasDirtyOnInitialisation = false;
protected abstract Connection createConnection()
throws DbException, SQLException;
// Used exclusively during open to compact the database after schema
// migrations and after DatabaseConstants#MAX_COMPACTION_INTERVAL_MS has
// elapsed
protected abstract void compactAndClose() throws DbException;
JdbcDatabase(DatabaseTypes databaseTypes, MessageFactory messageFactory,
@@ -381,13 +387,16 @@ abstract class JdbcDatabase implements Database<Connection> {
try {
if (reopen) {
Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE);
wasDirtyOnInitialisation = isDirty(s);
compact = migrateSchema(txn, s, listener) || isCompactionDue(s);
} else {
wasDirtyOnInitialisation = false;
createTables(txn);
initialiseSettings(txn);
compact = false;
}
createIndexes(txn);
setDirty(txn, true);
commitTransaction(txn);
} catch (DbException e) {
abortTransaction(txn);
@@ -414,6 +423,11 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
@Override
public boolean wasDirtyOnInitialisation() {
return wasDirtyOnInitialisation;
}
/**
* Compares the schema version stored in the database with the schema
* version used by the current code and applies any suitable migrations to
@@ -488,6 +502,16 @@ abstract class JdbcDatabase implements Database<Connection> {
mergeSettings(txn, s, DB_SETTINGS_NAMESPACE);
}
private boolean isDirty(Settings s) {
return s.getBoolean(DIRTY_KEY, false);
}
protected void setDirty(Connection txn, boolean dirty) throws DbException {
Settings s = new Settings();
s.putBoolean(DIRTY_KEY, dirty);
mergeSettings(txn, s, DB_SETTINGS_NAMESPACE);
}
private void initialiseSettings(Connection txn) throws DbException {
Settings s = new Settings();
s.putInt(SCHEMA_VERSION_KEY, CODE_SCHEMA_VERSION);