From 994e1be6373a9ea4f22bd8c62cf1693ea273cdf3 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 9 Nov 2017 00:34:45 -0500 Subject: [PATCH] Tweak converter for efficiency --- .../src/com/mineplex/statconverter/Main.java | 116 +++++++++++++----- 1 file changed, 82 insertions(+), 34 deletions(-) diff --git a/Tools/Stat Conversion/src/com/mineplex/statconverter/Main.java b/Tools/Stat Conversion/src/com/mineplex/statconverter/Main.java index 03f4cf18f..85510c1b8 100644 --- a/Tools/Stat Conversion/src/com/mineplex/statconverter/Main.java +++ b/Tools/Stat Conversion/src/com/mineplex/statconverter/Main.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -73,6 +74,14 @@ public class Main while (!_complete) { convertGroup(_nextStart); + try + { + Thread.sleep(5000); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } } } catch (IOException e) @@ -97,57 +106,96 @@ public class Main accounts.add(rs.getInt("accountId")); } } - + } + catch (SQLException ex) + { + ex.printStackTrace(); + _complete = true; + return; + } + + try + { if (!accounts.isEmpty()) { for (Integer accountId : accounts) { Map oldStats = new HashMap<>(); Map newStats = new HashMap<>(); - try (Statement s = c.createStatement(); - ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";") - ) + try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection()) { - while (rs.next()) + try (Statement s = c.createStatement(); + ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";") + ) { - oldStats.put(rs.getInt("statId"), rs.getLong("value")); - } - } - try (Statement s = c.createStatement(); - ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStatsAllTime WHERE accountId=" + accountId + ";") - ) - { - while (rs.next()) - { - newStats.put(rs.getInt("statId"), rs.getLong("value")); - } - } - - for (Entry oldEntry : oldStats.entrySet()) - { - if (newStats.containsKey(oldEntry.getKey())) - { - if (newStats.get(oldEntry.getKey()) < oldEntry.getValue()) + while (rs.next()) { - try (Statement s = c.createStatement()) + oldStats.put(rs.getInt("statId"), rs.getLong("value")); + } + } + try (Statement s = c.createStatement(); + ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStatsAllTime WHERE accountId=" + accountId + ";") + ) + { + while (rs.next()) + { + newStats.put(rs.getInt("statId"), rs.getLong("value")); + } + } + + try (PreparedStatement updateStatement = c.prepareStatement("UPDATE accountStatsAllTime SET value=value+? WHERE accountId=? AND statId=?;"); + PreparedStatement insertStatement = c.prepareStatement("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (?, ?, ?);"); + PreparedStatement deleteStatement = c.prepareStatement("DELETE FROM accountStat WHERE accountId=?;"); + ) + { + int updates = 0; + int inserts = 0; + + for (Entry oldEntry : oldStats.entrySet()) + { + if (newStats.containsKey(oldEntry.getKey())) { - Long diff = oldEntry.getValue() - newStats.get(oldEntry.getKey()); - s.execute("UPDATE accountStatsAllTime SET value=value+" + diff + " WHERE accountId=" + accountId + " AND statId=" + oldEntry.getKey() + ";"); + if (newStats.get(oldEntry.getKey()) < oldEntry.getValue()) + { + Long diff = oldEntry.getValue() - newStats.get(oldEntry.getKey()); + updateStatement.setLong(1, diff); + updateStatement.setInt(2, accountId); + updateStatement.setInt(3, oldEntry.getKey()); + updateStatement.addBatch(); + updates++; + } + } + else + { + insertStatement.setInt(1, accountId); + insertStatement.setInt(2, oldEntry.getKey()); + insertStatement.setLong(3, oldEntry.getValue()); + insertStatement.addBatch(); + inserts++; } } - } - else - { - try (Statement s = c.createStatement()) + + if (updates > 0) { - s.execute("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (" + accountId + ", " + oldEntry.getKey() + ", " + oldEntry.getValue() + ");"); + updateStatement.executeBatch(); } + + if (inserts > 0) + { + insertStatement.executeBatch(); + } + + deleteStatement.setInt(1, accountId); + deleteStatement.execute(); } } - - try (Statement s = c.createStatement()) + try { - s.execute("DELETE FROM accountStat WHERE accountId=" + accountId + ";"); + Thread.sleep(2000); + } + catch (InterruptedException e) + { + e.printStackTrace(); } } completeGroup(start);