Batch Insert for Actions & CommandUse Tables

This commit is contained in:
Rsl1122 2017-09-02 00:23:57 +03:00
parent feeded7359
commit c1481d7935
5 changed files with 209 additions and 28 deletions

View File

@ -15,9 +15,7 @@ import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
* Table that is in charge of storing actions.
@ -41,10 +39,21 @@ public class ActionsTable extends UserIDTable {
private final String columnAdditionalInfo = "additional_info";
private final ServerTable serverTable;
private String insertStatement;
public ActionsTable(SQLDB db, boolean usingMySQL) {
super("plan_actions", db, usingMySQL);
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnServerID + ", "
+ columnActionID + ", "
+ columnDate + ", "
+ columnAdditionalInfo
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ serverTable.statementSelectServerID + ", "
+ "?, ?, ?)";
}
@Override
@ -64,17 +73,8 @@ public class ActionsTable extends UserIDTable {
public void insertAction(UUID uuid, Action action) throws SQLException {
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnUserID + ", "
+ columnServerID + ", "
+ columnActionID + ", "
+ columnDate + ", "
+ columnAdditionalInfo
+ ") VALUES ("
+ usersTable.statementSelectID + ", "
+ serverTable.statementSelectServerID + ", "
+ "?, ?, ?)"
);
statement = prepareStatement(insertStatement);
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setInt(3, action.getDoneAction().getId());
@ -103,6 +103,7 @@ public class ActionsTable extends UserIDTable {
statement = prepareStatement(Select.from(tableName, "*")
.where(columnUserID + "=" + usersTable.statementSelectID)
.toString());
statement.setFetchSize(5000);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
while (set.next()) {
@ -118,4 +119,81 @@ public class ActionsTable extends UserIDTable {
close(set, statement);
}
}
public Map<UUID, Map<UUID, List<Action>>> getAllActions() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
String usersIDColumn = usersTable + "." + usersTable.getColumnID();
String usersUUIDColumn = usersTable + "." + usersTable.getColumnUUID() + " as uuid";
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
columnActionID + ", " +
columnDate + ", " +
columnAdditionalInfo + ", " +
usersUUIDColumn + ", " +
serverUUIDColumn +
" FROM " + tableName +
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnUserID +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID
);
statement.setFetchSize(5000);
set = statement.executeQuery();
Map<UUID, Map<UUID, List<Action>>> map = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
UUID uuid = UUID.fromString(set.getString("uuid"));
Map<UUID, List<Action>> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
List<Action> actions = serverMap.getOrDefault(uuid, new ArrayList<>());
long date = set.getLong(columnDate);
Actions doneAction = Actions.getById(set.getInt(columnActionID));
String additionalInfo = set.getString(columnAdditionalInfo);
actions.add(new Action(date, doneAction, additionalInfo, -1));
serverMap.put(uuid, actions);
map.put(serverUUID, serverMap);
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void insertActions(Map<UUID, Map<UUID, List<Action>>> allActions) throws SQLException {
if (allActions.isEmpty()) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allActions.keySet()) {
// Every User
for (Map.Entry<UUID, List<Action>> entry : allActions.get(serverUUID).entrySet()) {
UUID uuid = entry.getKey();
// Every Action
List<Action> actions = entry.getValue();
for (Action action : actions) {
statement.setString(1, uuid.toString());
statement.setString(2, serverUUID.toString());
statement.setInt(3, action.getDoneAction().getId());
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.addBatch();
}
}
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -6,7 +6,6 @@ import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Select;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -27,6 +26,7 @@ public class CommandUseTable extends Table {
private final String columnServerID = "server_id";
private final ServerTable serverTable;
private String insertStatement;
/**
* @param db
@ -35,6 +35,11 @@ public class CommandUseTable extends Table {
public CommandUseTable(SQLDB db, boolean usingMySQL) {
super("plan_commandusages", db, usingMySQL);
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " ("
+ columnCommand + ", "
+ columnTimesUsed + ", "
+ columnServerID
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
}
/**
@ -72,7 +77,6 @@ public class CommandUseTable extends Table {
* @throws SQLException
*/
public Map<String, Integer> getCommandUse(UUID serverUUID) throws SQLException {
Benchmark.start("Get CommandUse");
Map<String, Integer> commandUse = new HashMap<>();
PreparedStatement statement = null;
ResultSet set = null;
@ -96,7 +100,6 @@ public class CommandUseTable extends Table {
} finally {
endTransaction(statement);
close(set, statement);
Benchmark.stop("Database", "Get CommandUse");
}
}
@ -127,11 +130,6 @@ public class CommandUseTable extends Table {
private void insertCommand(String command) throws SQLException {
PreparedStatement statement = null;
try {
String insertStatement = "INSERT INTO " + tableName + " ("
+ columnCommand + ", "
+ columnTimesUsed + ", "
+ columnServerID
+ ") VALUES (?, ?, " + serverTable.statementSelectServerID + ")";
statement = prepareStatement(insertStatement);
statement.setString(1, command);
statement.setInt(2, 1);
@ -177,4 +175,67 @@ public class CommandUseTable extends Table {
close(set, statement);
}
}
public Map<UUID, Map<String, Integer>> getAllCommandUsages() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
String serverIDColumn = serverTable + "." + serverTable.getColumnID();
String serverUUIDColumn = serverTable + "." + serverTable.getColumnUUID() + " as s_uuid";
statement = prepareStatement("SELECT " +
columnCommand + ", " +
columnTimesUsed + ", " +
serverUUIDColumn + ", " +
" FROM " + tableName +
" JOIN " + serverTable + " on " + serverIDColumn + "=" + columnServerID
);
statement.setFetchSize(5000);
set = statement.executeQuery();
Map<UUID, Map<String, Integer>> map = new HashMap<>();
while (set.next()) {
UUID serverUUID = UUID.fromString(set.getString("s_uuid"));
Map<String, Integer> serverMap = map.getOrDefault(serverUUID, new HashMap<>());
String command = set.getString(columnCommand);
int timesUsed = set.getInt(columnTimesUsed);
serverMap.put(command, timesUsed);
map.put(serverUUID, serverMap);
}
return map;
} finally {
endTransaction(statement);
close(set, statement);
}
}
public void insertCommandUsage(Map<UUID, Map<String, Integer>> allCommandUsages) throws SQLException {
if (allCommandUsages.isEmpty()) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement(insertStatement);
// Every Server
for (UUID serverUUID : allCommandUsages.keySet()) {
// Every Command
for (Map.Entry<String, Integer> entry : allCommandUsages.get(serverUUID).entrySet()) {
String command = entry.getKey();
int timesUsed = entry.getValue();
statement.setString(1, command);
statement.setInt(2, timesUsed);
statement.setString(3, serverUUID.toString());
statement.addBatch();
}
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
close(statement);
}
}
}

View File

@ -264,4 +264,8 @@ public class ServerTable extends Table {
public String getColumnID() {
return columnServerID;
}
public String getColumnUUID() {
return columnServerUUID;
}
}

View File

@ -1,8 +1,8 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseException;
import main.java.com.djrapitops.plan.database.Container;
import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
@ -134,13 +134,11 @@ public abstract class Table {
/**
* @return
*/
public boolean removeAllData() {
public void removeAllData() throws DatabaseException {
try {
execute("DELETE FROM " + tableName);
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
} catch (SQLException e) {
throw new DatabaseException("Failed to delete", e);
}
}

View File

@ -0,0 +1,40 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.database.tables.move;
import main.java.com.djrapitops.plan.api.exceptions.DBCreateTableException;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseException;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.tables.Table;
import java.sql.SQLException;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class BatchOperationTable extends Table {
public BatchOperationTable(SQLDB db, boolean usingMySQL) {
super("", db, usingMySQL);
}
@Override
public void createTable() throws DBCreateTableException {
throw new IllegalStateException("Method not supposed to be used on this table.");
}
public void clearTable(Table table) throws DatabaseException {
table.removeAllData();
}
public void copyActions(BatchOperationTable toDB) throws SQLException {
toDB.db.getActionsTable().insertActions(db.getActionsTable().getAllActions());
}
public void copyCommandUse(BatchOperationTable toDB) throws SQLException {
toDB.db.getCommandUseTable().insertCommandUsage(db.getCommandUseTable().getAllCommandUsages());
}
}