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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -100,16 +99,16 @@ public class Main
{
_startTime = System.currentTimeMillis();
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 (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())
{
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
{
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<>();
Map<Integer, Long> newStats = new HashMap<>();
try (Connection c = DBPool.getDataSource("ACCOUNT_PC").getConnection())
for (Entry<Integer[], Long> convertEntry : converting.entrySet())
{
try (Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStat WHERE accountId=" + accountId + ";")
)
int accountId = convertEntry.getKey()[0];
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;
try
{
oldValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
oldValue = 0;
}
oldStats.put(rs.getInt("statId"), oldValue);
fetched = rs.getLong("value");
}
}
try (Statement s = c.createStatement();
ResultSet rs = s.executeQuery("SELECT statId, value FROM accountStatsAllTime WHERE accountId=" + accountId + ";")
)
finally
{
while (rs.next())
{
long newValue;
try
{
newValue = rs.getLong("value");
}
catch (MySQLDataException ex)
{
newValue = 0;
}
newStats.put(rs.getInt("statId"), newValue);
}
select.clearParameters();
}
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=?;");
)
long diff = value - Math.max(fetched, 0);
if (fetched == -1)
{
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();
insert.setInt(1, accountId);
insert.setInt(2, statId);
insert.setLong(3, value);
insert.execute();
insert.clearParameters();
}
else if (fetched >= 2 * value)
{
update.setLong(1, -1 * value);
update.setInt(2, accountId);
update.setInt(3, statId);
update.execute();
update.clearParameters();
}
else if (diff > 0)
{
update.setLong(1, diff);
update.setInt(2, accountId);
update.setInt(3, statId);
update.execute();
update.clearParameters();
}
}
try
{
Thread.sleep(500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
completeGroup(start);