Refactored UserInfoTable#insertUserInfo to an executable

This commit is contained in:
Rsl1122 2019-01-25 11:38:24 +02:00
parent da4901b37a
commit d0d645d09a
4 changed files with 47 additions and 37 deletions

View File

@ -94,7 +94,7 @@ public class BackupCopyTransaction extends RemoveEverythingTransaction {
} }
private void copyUserInfo() { private void copyUserInfo() {
db.getUserInfoTable().insertUserInfo(sourceDB.query(LargeFetchQueries.fetchPerServerUserInformation())); copy(LargeStoreQueries::storePerServerUserInformation, LargeFetchQueries.fetchPerServerUserInformation());
} }
private void copyWorlds() { private void copyWorlds() {

View File

@ -19,6 +19,7 @@ package com.djrapitops.plan.db.sql.queries;
import com.djrapitops.plan.data.WebUser; import com.djrapitops.plan.data.WebUser;
import com.djrapitops.plan.data.container.GeoInfo; import com.djrapitops.plan.data.container.GeoInfo;
import com.djrapitops.plan.data.container.TPS; import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.container.UserInfo;
import com.djrapitops.plan.data.store.objects.Nickname; import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.ExecBatchStatement; import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.Executable; import com.djrapitops.plan.db.access.Executable;
@ -242,4 +243,35 @@ public class LargeStoreQueries {
} }
}; };
} }
/**
* Execute a big batch of Per server UserInfo insert statements.
*
* @param ofServers Map: Server UUID - List of user information
* @return Executable, use inside a {@link com.djrapitops.plan.db.access.transactions.Transaction}
*/
public static Executable storePerServerUserInformation(Map<UUID, List<UserInfo>> ofServers) {
if (Verify.isEmpty(ofServers)) {
return Executable.empty();
}
return new ExecBatchStatement(UserInfoTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every Server
for (Map.Entry<UUID, List<UserInfo>> entry : ofServers.entrySet()) {
UUID serverUUID = entry.getKey();
// Every User
for (UserInfo user : entry.getValue()) {
statement.setString(1, user.getUuid().toString());
statement.setLong(2, user.getRegistered());
statement.setString(3, serverUUID.toString());
statement.setBoolean(4, user.isBanned());
statement.setBoolean(5, user.isOperator());
statement.addBatch();
}
}
}
};
}
} }

View File

@ -29,7 +29,6 @@ import com.djrapitops.plan.db.sql.parsing.Select;
import com.djrapitops.plan.db.sql.parsing.Sql; import com.djrapitops.plan.db.sql.parsing.Sql;
import com.djrapitops.plan.db.sql.parsing.Update; import com.djrapitops.plan.db.sql.parsing.Update;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
import com.djrapitops.plugin.utilities.Verify;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -58,20 +57,19 @@ public class UserInfoTable extends Table {
public static final String OP = "opped"; public static final String OP = "opped";
public static final String BANNED = "banned"; public static final String BANNED = "banned";
private final String insertStatement; public static final String INSERT_STATEMENT = "INSERT INTO " + TABLE_NAME + " (" +
USER_UUID + ", " +
REGISTERED + ", " +
SERVER_UUID + ", " +
BANNED + ", " +
OP +
") VALUES (?, ?, ?, ?, ?)";
private final UsersTable usersTable; private final UsersTable usersTable;
public UserInfoTable(SQLDB db) { public UserInfoTable(SQLDB db) {
super(TABLE_NAME, db); super(TABLE_NAME, db);
usersTable = db.getUsersTable(); usersTable = db.getUsersTable();
insertStatement = "INSERT INTO " + tableName + " (" +
USER_UUID + ", " +
REGISTERED + ", " +
SERVER_UUID + ", " +
BANNED + ", " +
OP +
") VALUES (?, ?, ?, ?, ?)";
} }
public static String createTableSQL(DBType dbType) { public static String createTableSQL(DBType dbType) {
@ -86,7 +84,7 @@ public class UserInfoTable extends Table {
} }
public void registerUserInfo(UUID uuid, long registered) { public void registerUserInfo(UUID uuid, long registered) {
execute(new ExecStatement(insertStatement) { execute(new ExecStatement(INSERT_STATEMENT) {
@Override @Override
public void prepare(PreparedStatement statement) throws SQLException { public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
@ -240,31 +238,6 @@ public class UserInfoTable extends Table {
return getServerUserInfo(getServerUUID()); return getServerUserInfo(getServerUUID());
} }
public void insertUserInfo(Map<UUID, List<UserInfo>> allUserInfos) {
if (Verify.isEmpty(allUserInfos)) {
return;
}
executeBatch(new ExecStatement(insertStatement) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
// Every Server
for (Map.Entry<UUID, List<UserInfo>> entry : allUserInfos.entrySet()) {
UUID serverUUID = entry.getKey();
// Every User
for (UserInfo user : entry.getValue()) {
statement.setString(1, user.getUuid().toString());
statement.setLong(2, user.getRegistered());
statement.setString(3, serverUUID.toString());
statement.setBoolean(4, user.isBanned());
statement.setBoolean(5, user.isOperator());
statement.addBatch();
}
}
}
});
}
public int getServerUserCount(UUID serverUUID) { public int getServerUserCount(UUID serverUUID) {
String sql = "SELECT " + String sql = "SELECT " +
" COUNT(" + REGISTERED + ") as c" + " COUNT(" + REGISTERED + ") as c" +

View File

@ -78,7 +78,12 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
@Override @Override
public void insertUserInfo(Map<UUID, List<UserInfo>> ofServers) { public void insertUserInfo(Map<UUID, List<UserInfo>> ofServers) {
userInfoTable.insertUserInfo(ofServers); db.executeTransaction(new Transaction() {
@Override
protected void performOperations() {
execute(LargeStoreQueries.storePerServerUserInformation(ofServers));
}
});
} }
@Override @Override