2015-03-25 23:10:59 +01:00
|
|
|
package mineplex.chestConverter;
|
|
|
|
|
2015-05-17 10:28:44 +02:00
|
|
|
import java.io.IOException;
|
2015-08-24 00:47:03 +02:00
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.DriverManager;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
2015-03-25 23:10:59 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
2015-05-13 09:00:37 +02:00
|
|
|
import java.util.ArrayList;
|
2015-03-25 23:10:59 +01:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
2015-08-24 00:47:03 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
2015-05-13 09:00:37 +02:00
|
|
|
import java.util.List;
|
2015-08-24 00:47:03 +02:00
|
|
|
import java.util.Map.Entry;
|
2015-05-17 10:28:44 +02:00
|
|
|
import java.util.logging.FileHandler;
|
|
|
|
import java.util.logging.Formatter;
|
|
|
|
import java.util.logging.LogRecord;
|
2015-03-25 23:10:59 +01:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
public class ChestConverter
|
|
|
|
{
|
|
|
|
private static ChestConverterRepository _repository = null;
|
|
|
|
private static SimpleDateFormat _dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
|
|
|
|
|
2015-05-17 10:28:44 +02:00
|
|
|
private static Logger _logger = Logger.getLogger("Converter");
|
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
private static String _connectionString = "jdbc:mysql://db.mineplex.com:3306/Account?allowMultiQueries=true";
|
|
|
|
private static String _userName = "MilitaryPolice";
|
|
|
|
private static String _password = "CUPr6Wuw2Rus$qap";
|
|
|
|
|
|
|
|
private static Connection _connection;
|
|
|
|
|
2015-03-25 23:10:59 +01:00
|
|
|
public static void main (String args[])
|
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
Class.forName("com.mysql.jdbc.Driver");
|
|
|
|
}
|
|
|
|
catch (ClassNotFoundException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
|
|
|
|
2015-05-17 10:28:44 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
FileHandler fileHandler = new FileHandler("converter.log", true);
|
|
|
|
fileHandler.setFormatter(new Formatter()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public String format(LogRecord record)
|
|
|
|
{
|
|
|
|
return record.getMessage() + "\n";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
_logger.addHandler(fileHandler);
|
|
|
|
_logger.setUseParentHandlers(false);
|
|
|
|
}
|
|
|
|
catch (SecurityException | IOException e1)
|
|
|
|
{
|
|
|
|
e1.printStackTrace();
|
|
|
|
}
|
2015-05-13 09:00:37 +02:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
int limit = 50000;
|
|
|
|
HashSet<AccountStat> accountStats = new HashSet<AccountStat>();
|
2015-03-25 23:10:59 +01:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
while (true)
|
2015-03-25 23:10:59 +01:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
accountStats.clear();
|
|
|
|
|
|
|
|
try
|
2015-05-13 09:00:37 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
Statement statement = null;
|
2015-05-13 09:00:37 +02:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_connection == null || _connection.isClosed())
|
|
|
|
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
|
|
|
|
|
|
|
statement = _connection.createStatement();
|
|
|
|
|
|
|
|
statement.execute("SELECT accountId, statId, value FROM Account.accountStats LIMIT " + limit + ";");
|
|
|
|
|
|
|
|
ResultSet resultSet = statement.getResultSet();
|
|
|
|
|
|
|
|
while (resultSet.next())
|
|
|
|
{
|
|
|
|
accountStats.add(new AccountStat(resultSet.getInt(1), resultSet.getInt(2), resultSet.getInt(3)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
exception.printStackTrace();
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
if (statement != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
statement.close();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (accountStats.size() == 0)
|
|
|
|
{
|
|
|
|
System.out.println("No accounts.");
|
|
|
|
return;
|
|
|
|
}
|
2015-05-17 10:28:44 +02:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
PreparedStatement updateStatement = null;
|
|
|
|
PreparedStatement insertStatement = null;
|
|
|
|
Statement deleteStatement = null;
|
2015-05-17 10:28:44 +02:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
try
|
2015-05-17 10:28:44 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
if (_connection == null || _connection.isClosed())
|
|
|
|
_connection = DriverManager.getConnection(_connectionString, _userName, _password);
|
|
|
|
|
|
|
|
_connection.setAutoCommit(true);
|
|
|
|
updateStatement = _connection.prepareStatement("UPDATE accountStat SET value = value + ? WHERE accountId = ? AND statId = ? AND value < ?;");
|
|
|
|
|
|
|
|
for (AccountStat stat : accountStats)
|
2015-05-17 10:28:44 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
updateStatement.setLong(1, stat.value);
|
|
|
|
updateStatement.setInt(2, stat.accountId);
|
|
|
|
updateStatement.setInt(3, stat.statId);
|
|
|
|
updateStatement.setLong(4, stat.value);
|
2015-05-17 10:28:44 +02:00
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
updateStatement.addBatch();
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
|
|
|
|
2015-08-24 00:47:03 +02:00
|
|
|
int[] rowsAffected = updateStatement.executeBatch();
|
|
|
|
_connection.setAutoCommit(false);
|
|
|
|
int i = 0;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
log("Updated rows - " + limit);
|
|
|
|
|
|
|
|
insertStatement = _connection.prepareStatement("INSERT IGNORE accountStat(accountId, statId, value) VALUES (?, ?, ?);");
|
|
|
|
|
|
|
|
for (AccountStat stat : accountStats)
|
2015-05-17 10:28:44 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
if (rowsAffected[i] < 1)
|
|
|
|
{
|
|
|
|
insertStatement.setInt(1, stat.accountId);
|
|
|
|
insertStatement.setInt(2, stat.statId);
|
|
|
|
insertStatement.setLong(3, stat.value);
|
|
|
|
|
|
|
|
insertStatement.addBatch();
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
2015-08-24 00:47:03 +02:00
|
|
|
|
|
|
|
insertStatement.executeBatch();
|
|
|
|
log("Inserted rows - " + count);
|
|
|
|
|
|
|
|
deleteStatement = _connection.createStatement();
|
|
|
|
deleteStatement.executeUpdate("DELETE FROM accountStats LIMIT " + limit + ";");
|
|
|
|
|
|
|
|
_connection.commit();
|
|
|
|
|
|
|
|
log("Deleted rows - " + limit);
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
2015-08-24 00:47:03 +02:00
|
|
|
catch (Exception exception)
|
2015-05-17 10:28:44 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
exception.printStackTrace();
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
2015-08-24 00:47:03 +02:00
|
|
|
finally
|
2015-05-17 10:28:44 +02:00
|
|
|
{
|
2015-08-24 00:47:03 +02:00
|
|
|
if (statement != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
statement.close();
|
|
|
|
}
|
|
|
|
catch (SQLException e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
2015-08-24 00:47:03 +02:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.info(e.getMessage());
|
|
|
|
}
|
2015-05-17 10:28:44 +02:00
|
|
|
}
|
2015-03-25 23:10:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void log(String message)
|
|
|
|
{
|
|
|
|
System.out.println("[" + _dateFormat.format(new Date()) + "] " + message);
|
|
|
|
}
|
|
|
|
}
|