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
1 changed files with 44 additions and 100 deletions

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=?;");
PreparedStatement update = c.prepareStatement("UPDATE accountStatsAllTime SET value=value+? WHERE accountId=? AND statId=?;");
PreparedStatement insert = c.prepareStatement("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (?, ?, ?);");
)
{ {
Map<Integer, Long> oldStats = new HashMap<>(); for (Entry<Integer[], Long> convertEntry : converting.entrySet())
Map<Integer, Long> newStats = new HashMap<>();
try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection())
{ {
try (Statement s = c.createStatement(); int accountId = convertEntry.getKey()[0];
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";") 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())
{ {
while (rs.next()) if (rs.next())
{ {
long oldValue; fetched = rs.getLong("value");
try
{
oldValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
oldValue = 0;
}
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 newValue;
try
{
newValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
newValue = 0;
}
newStats.put(rs.getInt("statId"), newValue);
}
} }
long diff = value - Math.max(fetched, 0);
try (PreparedStatement updateStatement = c.prepareStatement("UPDATE accountStatsAllTime SET value=value+? WHERE accountId=? AND statId=?;"); if (fetched == -1)
PreparedStatement insertStatement = c.prepareStatement("INSERT INTO accountStatsAllTime (accountId, statId, value) VALUES (?, ?, ?);");
//PreparedStatement deleteStatement = c.prepareStatement("DELETE FROM accountStat WHERE accountId=?;");
)
{ {
int updates = 0; insert.setInt(1, accountId);
int inserts = 0; insert.setInt(2, statId);
insert.setLong(3, value);
for (Entry<Integer, Long> oldEntry : oldStats.entrySet()) insert.execute();
{ insert.clearParameters();
if (newStats.containsKey(oldEntry.getKey())) }
{ else if (fetched >= 2 * value)
if (newStats.get(oldEntry.getKey()) < oldEntry.getValue()) {
{ update.setLong(1, -1 * value);
Long delta = oldEntry.getValue() - newStats.get(oldEntry.getKey()); update.setInt(2, accountId);
updateStatement.setLong(1, delta); update.setInt(3, statId);
updateStatement.setInt(2, accountId); update.execute();
updateStatement.setInt(3, oldEntry.getKey()); update.clearParameters();
updateStatement.addBatch(); }
updates++; else if (diff > 0)
} {
else if (oldEntry.getValue() > 0 && newStats.get(oldEntry.getKey()) >= (oldEntry.getValue() * 2)) update.setLong(1, diff);
{ update.setInt(2, accountId);
Long delta = -1 * oldEntry.getValue(); update.setInt(3, statId);
updateStatement.setLong(1, delta); update.execute();
updateStatement.setInt(2, accountId); update.clearParameters();
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);