mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Always compact the DB if migrations have been applied.
This commit is contained in:
@@ -354,12 +354,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
try {
|
try {
|
||||||
if (reopen) {
|
if (reopen) {
|
||||||
Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE);
|
Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE);
|
||||||
checkSchemaVersion(txn, s, listener);
|
compact = migrateSchema(txn, s, listener) || isCompactionDue(s);
|
||||||
long lastCompacted = s.getLong(LAST_COMPACTED_KEY, 0);
|
|
||||||
long elapsed = clock.currentTimeMillis() - lastCompacted;
|
|
||||||
if (LOG.isLoggable(INFO))
|
|
||||||
LOG.info(elapsed + " ms since last compaction");
|
|
||||||
compact = elapsed > MAX_COMPACTION_INTERVAL_MS;
|
|
||||||
} else {
|
} else {
|
||||||
createTables(txn);
|
createTables(txn);
|
||||||
initialiseSettings(txn);
|
initialiseSettings(txn);
|
||||||
@@ -397,16 +392,18 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
* version used by the current code and applies any suitable migrations to
|
* version used by the current code and applies any suitable migrations to
|
||||||
* the data if necessary.
|
* the data if necessary.
|
||||||
*
|
*
|
||||||
|
* @return true if any migrations were applied, false if the schema was
|
||||||
|
* already current
|
||||||
* @throws DataTooNewException if the data uses a newer schema than the
|
* @throws DataTooNewException if the data uses a newer schema than the
|
||||||
* current code
|
* current code
|
||||||
* @throws DataTooOldException if the data uses an older schema than the
|
* @throws DataTooOldException if the data uses an older schema than the
|
||||||
* current code and cannot be migrated
|
* current code and cannot be migrated
|
||||||
*/
|
*/
|
||||||
private void checkSchemaVersion(Connection txn, Settings s,
|
private boolean migrateSchema(Connection txn, Settings s,
|
||||||
@Nullable MigrationListener listener) throws DbException {
|
@Nullable MigrationListener listener) throws DbException {
|
||||||
int dataSchemaVersion = s.getInt(SCHEMA_VERSION_KEY, -1);
|
int dataSchemaVersion = s.getInt(SCHEMA_VERSION_KEY, -1);
|
||||||
if (dataSchemaVersion == -1) throw new DbException();
|
if (dataSchemaVersion == -1) throw new DbException();
|
||||||
if (dataSchemaVersion == CODE_SCHEMA_VERSION) return;
|
if (dataSchemaVersion == CODE_SCHEMA_VERSION) return false;
|
||||||
if (CODE_SCHEMA_VERSION < dataSchemaVersion)
|
if (CODE_SCHEMA_VERSION < dataSchemaVersion)
|
||||||
throw new DataTooNewException();
|
throw new DataTooNewException();
|
||||||
// Apply any suitable migrations in order
|
// Apply any suitable migrations in order
|
||||||
@@ -425,6 +422,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
if (dataSchemaVersion != CODE_SCHEMA_VERSION)
|
if (dataSchemaVersion != CODE_SCHEMA_VERSION)
|
||||||
throw new DataTooOldException();
|
throw new DataTooOldException();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Package access for testing
|
// Package access for testing
|
||||||
@@ -432,6 +430,14 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
return Arrays.asList(new Migration38_39(), new Migration39_40());
|
return Arrays.asList(new Migration38_39(), new Migration39_40());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isCompactionDue(Settings s) {
|
||||||
|
long lastCompacted = s.getLong(LAST_COMPACTED_KEY, 0);
|
||||||
|
long elapsed = clock.currentTimeMillis() - lastCompacted;
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info(elapsed + " ms since last compaction");
|
||||||
|
return elapsed > MAX_COMPACTION_INTERVAL_MS;
|
||||||
|
}
|
||||||
|
|
||||||
private void storeSchemaVersion(Connection txn, int version)
|
private void storeSchemaVersion(Connection txn, int version)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
Settings s = new Settings();
|
Settings s = new Settings();
|
||||||
|
|||||||
Reference in New Issue
Block a user