Tweak converter for efficiency

This commit is contained in:
AlexTheCoder 2017-11-09 00:34:45 -05:00 committed by Alexander Meech
parent 2ff8e79766
commit 994e1be637

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
@ -73,6 +74,14 @@ public class Main
while (!_complete) while (!_complete)
{ {
convertGroup(_nextStart); convertGroup(_nextStart);
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} }
} }
catch (IOException e) catch (IOException e)
@ -97,13 +106,24 @@ public class Main
accounts.add(rs.getInt("accountId")); accounts.add(rs.getInt("accountId"));
} }
} }
}
catch (SQLException ex)
{
ex.printStackTrace();
_complete = true;
return;
}
try
{
if (!accounts.isEmpty()) if (!accounts.isEmpty())
{ {
for (Integer accountId : accounts) for (Integer accountId : accounts)
{ {
Map<Integer, Long> oldStats = new HashMap<>(); Map<Integer, Long> oldStats = new HashMap<>();
Map<Integer, Long> newStats = new HashMap<>(); Map<Integer, Long> newStats = new HashMap<>();
try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection())
{
try (Statement s = c.createStatement(); try (Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";") ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";")
) )
@ -123,31 +143,59 @@ public class Main
} }
} }
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<Integer, Long> oldEntry : oldStats.entrySet()) for (Entry<Integer, Long> oldEntry : oldStats.entrySet())
{ {
if (newStats.containsKey(oldEntry.getKey())) if (newStats.containsKey(oldEntry.getKey()))
{ {
if (newStats.get(oldEntry.getKey()) < oldEntry.getValue()) if (newStats.get(oldEntry.getKey()) < oldEntry.getValue())
{
try (Statement s = c.createStatement())
{ {
Long diff = oldEntry.getValue() - newStats.get(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() + ";"); updateStatement.setLong(1, diff);
} updateStatement.setInt(2, accountId);
updateStatement.setInt(3, oldEntry.getKey());
updateStatement.addBatch();
updates++;
} }
} }
else else
{ {
try (Statement s = c.createStatement()) insertStatement.setInt(1, accountId);
{ insertStatement.setInt(2, oldEntry.getKey());
s.execute("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (" + accountId + ", " + oldEntry.getKey() + ", " + oldEntry.getValue() + ");"); insertStatement.setLong(3, oldEntry.getValue());
} insertStatement.addBatch();
inserts++;
} }
} }
try (Statement s = c.createStatement()) if (updates > 0)
{ {
s.execute("DELETE FROM accountStat WHERE accountId=" + accountId + ";"); updateStatement.executeBatch();
}
if (inserts > 0)
{
insertStatement.executeBatch();
}
deleteStatement.setInt(1, accountId);
deleteStatement.execute();
}
}
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
} }
} }
completeGroup(start); completeGroup(start);