Search in sources :

Example 1 with ThreadSettingsConfig

use of com.google.gerrit.server.config.ThreadSettingsConfig in project gerrit by GerritCodeReview.

the class MigrateAccountPatchReviewDb method run.

@Override
public int run() throws Exception {
    Injector dbInjector = createDbInjector();
    SitePaths sitePaths = new SitePaths(getSitePath());
    ThreadSettingsConfig threadSettingsConfig = dbInjector.getInstance(ThreadSettingsConfig.class);
    Config fakeCfg = new Config();
    if (!Strings.isNullOrEmpty(sourceUrl)) {
        fakeCfg.setString("accountPatchReviewDb", null, "url", sourceUrl);
    }
    JdbcAccountPatchReviewStore sourceJdbcAccountPatchReviewStore = JdbcAccountPatchReviewStore.createAccountPatchReviewStore(fakeCfg, sitePaths, threadSettingsConfig);
    Config cfg = dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class));
    String targetUrl = cfg.getString("accountPatchReviewDb", null, "url");
    if (targetUrl == null) {
        System.err.println("accountPatchReviewDb.url is null in gerrit.config");
        return 1;
    }
    System.out.println("target Url: " + targetUrl);
    JdbcAccountPatchReviewStore targetJdbcAccountPatchReviewStore = JdbcAccountPatchReviewStore.createAccountPatchReviewStore(cfg, sitePaths, threadSettingsConfig);
    targetJdbcAccountPatchReviewStore.createTableIfNotExists();
    if (!isTargetTableEmpty(targetJdbcAccountPatchReviewStore)) {
        System.err.println("target table is not empty, cannot proceed");
        return 1;
    }
    try (Connection sourceCon = sourceJdbcAccountPatchReviewStore.getConnection();
        Connection targetCon = targetJdbcAccountPatchReviewStore.getConnection();
        PreparedStatement sourceStmt = sourceCon.prepareStatement("SELECT account_id, change_id, patch_set_id, file_name " + "FROM account_patch_reviews " + "LIMIT ? " + "OFFSET ?");
        PreparedStatement targetStmt = targetCon.prepareStatement("INSERT INTO account_patch_reviews " + "(account_id, change_id, patch_set_id, file_name) VALUES " + "(?, ?, ?, ?)")) {
        targetCon.setAutoCommit(false);
        long offset = 0;
        Stopwatch sw = Stopwatch.createStarted();
        List<Row> rows = selectRows(sourceStmt, offset);
        while (!rows.isEmpty()) {
            insertRows(targetCon, targetStmt, rows);
            offset += rows.size();
            System.out.printf("%8d rows migrated\n", offset);
            rows = selectRows(sourceStmt, offset);
        }
        double t = sw.elapsed(TimeUnit.MILLISECONDS) / 1000d;
        System.out.printf("Migrated %d rows in %.01fs (%.01f/s)\n", offset, t, offset / t);
    }
    return 0;
}
Also used : GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) JdbcAccountPatchReviewStore(com.google.gerrit.server.schema.JdbcAccountPatchReviewStore) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ThreadSettingsConfig(com.google.gerrit.server.config.ThreadSettingsConfig) Config(org.eclipse.jgit.lib.Config) Connection(java.sql.Connection) Stopwatch(com.google.common.base.Stopwatch) PreparedStatement(java.sql.PreparedStatement) SitePaths(com.google.gerrit.server.config.SitePaths) Injector(com.google.inject.Injector) ThreadSettingsConfig(com.google.gerrit.server.config.ThreadSettingsConfig)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 GerritServerConfig (com.google.gerrit.server.config.GerritServerConfig)1 SitePaths (com.google.gerrit.server.config.SitePaths)1 ThreadSettingsConfig (com.google.gerrit.server.config.ThreadSettingsConfig)1 JdbcAccountPatchReviewStore (com.google.gerrit.server.schema.JdbcAccountPatchReviewStore)1 Injector (com.google.inject.Injector)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 Config (org.eclipse.jgit.lib.Config)1