From 76121eb87160837e579a728c0aa9676099e82f96 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Tue, 16 Oct 2018 11:17:17 +0100 Subject: [PATCH] Always compact the DB if migrations have been applied. --- .../briarproject/bramble/db/JdbcDatabase.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java index 307da37d7..c6e2f7bc9 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java @@ -354,12 +354,7 @@ abstract class JdbcDatabase implements Database { try { if (reopen) { Settings s = getSettings(txn, DB_SETTINGS_NAMESPACE); - checkSchemaVersion(txn, s, listener); - 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; + compact = migrateSchema(txn, s, listener) || isCompactionDue(s); } else { createTables(txn); initialiseSettings(txn); @@ -397,16 +392,18 @@ abstract class JdbcDatabase implements Database { * version used by the current code and applies any suitable migrations to * 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 * current code * @throws DataTooOldException if the data uses an older schema than the * 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 { int dataSchemaVersion = s.getInt(SCHEMA_VERSION_KEY, -1); if (dataSchemaVersion == -1) throw new DbException(); - if (dataSchemaVersion == CODE_SCHEMA_VERSION) return; + if (dataSchemaVersion == CODE_SCHEMA_VERSION) return false; if (CODE_SCHEMA_VERSION < dataSchemaVersion) throw new DataTooNewException(); // Apply any suitable migrations in order @@ -425,6 +422,7 @@ abstract class JdbcDatabase implements Database { } if (dataSchemaVersion != CODE_SCHEMA_VERSION) throw new DataTooOldException(); + return true; } // Package access for testing @@ -432,6 +430,14 @@ abstract class JdbcDatabase implements Database { 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) throws DbException { Settings s = new Settings();