Modify the stat converter to use Jon's new solution to hopefully speed up the process

This commit is contained in:
AlexTheCoder 2018-01-22 01:15:45 -05:00 committed by Alexander Meech
parent e23c3f6600
commit 68e64d48b8

View File

@ -10,7 +10,6 @@ import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -100,16 +99,16 @@ public class Main
{ {
_startTime = System.currentTimeMillis(); _startTime = System.currentTimeMillis();
System.out.println("[INFO] Starting " + start + " to " + (start + 9999)); System.out.println("[INFO] Starting " + start + " to " + (start + 9999));
List<Integer> accounts = new ArrayList<>(10000); Map<Integer[], Long> converting = new HashMap<>(10000);
try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection()) try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection())
{ {
try (Statement s = c.createStatement(); try (Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT DISTINCT accountId FROM accountStat LIMIT " + start + ",10000;") ResultSet rs = s.executeQuery("SELECT accountId, statId, value FROM accountStat LIMIT " + start + ",10000;")
) )
{ {
while (rs.next()) while (rs.next())
{ {
accounts.add(rs.getInt("accountId")); converting.put(new Integer[] {rs.getInt("accountId"), rs.getInt("statId")}, rs.getLong("value"));
} }
} }
} }
@ -122,113 +121,58 @@ public class Main
try try
{ {
if (!accounts.isEmpty()) if (!converting.isEmpty())
{ {
for (Integer accountId : accounts) try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection();
{ PreparedStatement select = c.prepareStatement("SELECT value FROM accountStatsAllTime WHERE accountId=? AND statId=?;");
Map<Integer, Long> oldStats = new HashMap<>(); PreparedStatement update = c.prepareStatement("UPDATE accountStatsAllTime SET value=value+? WHERE accountId=? AND statId=?;");
Map<Integer, Long> newStats = new HashMap<>(); PreparedStatement insert = c.prepareStatement("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (?, ?, ?);");
try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection())
{
try (Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";")
) )
{ {
while (rs.next()) for (Entry<Integer[], Long> convertEntry : converting.entrySet())
{ {
long oldValue; int accountId = convertEntry.getKey()[0];
try int statId = convertEntry.getKey()[1];
long value = convertEntry.getValue();
select.setInt(1, accountId);
select.setInt(2, statId);
long fetched = -1;
try (ResultSet rs = select.executeQuery())
{ {
oldValue = rs.getLong("value"); if (rs.next())
}
catch (MySQLDataException ex)
{ {
oldValue = 0; fetched = rs.getLong("value");
}
oldStats.put(rs.getInt("statId"), oldValue);
} }
} }
try (Statement s = c.createStatement(); finally
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStatsAllTime WHERE accountId=" + accountId + ";")
)
{ {
while (rs.next()) select.clearParameters();
}
long diff = value - Math.max(fetched, 0);
if (fetched == -1)
{ {
long newValue; insert.setInt(1, accountId);
try insert.setInt(2, statId);
insert.setLong(3, value);
insert.execute();
insert.clearParameters();
}
else if (fetched >= 2 * value)
{ {
newValue = rs.getLong("value"); update.setLong(1, -1 * value);
update.setInt(2, accountId);
update.setInt(3, statId);
update.execute();
update.clearParameters();
} }
catch (MySQLDataException ex) else if (diff > 0)
{ {
newValue = 0; update.setLong(1, diff);
update.setInt(2, accountId);
update.setInt(3, statId);
update.execute();
update.clearParameters();
} }
newStats.put(rs.getInt("statId"), newValue);
}
}
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())
{
if (newStats.containsKey(oldEntry.getKey()))
{
if (newStats.get(oldEntry.getKey()) < oldEntry.getValue())
{
Long delta = oldEntry.getValue() - newStats.get(oldEntry.getKey());
updateStatement.setLong(1, delta);
updateStatement.setInt(2, accountId);
updateStatement.setInt(3, oldEntry.getKey());
updateStatement.addBatch();
updates++;
}
else if (oldEntry.getValue() > 0 && newStats.get(oldEntry.getKey()) >= (oldEntry.getValue() * 2))
{
Long delta = -1 * oldEntry.getValue();
updateStatement.setLong(1, delta);
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++;
}
}
if (updates > 0)
{
updateStatement.executeBatch();
}
if (inserts > 0)
{
insertStatement.executeBatch();
}
//deleteStatement.setInt(1, accountId);
//deleteStatement.execute();
}
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
} }
} }
completeGroup(start); completeGroup(start);