DB Index Creation Task + test (Not used yet)

This commit is contained in:
Rsl1122 2019-01-10 14:11:26 +02:00
parent 76068ea6f8
commit c8e98e6caa
4 changed files with 106 additions and 1 deletions

View File

@ -0,0 +1,81 @@
package com.djrapitops.plan.system.database.databases.sql;
import com.djrapitops.plan.system.database.databases.DBType;
import com.djrapitops.plan.system.database.databases.sql.operation.Queries;
import com.djrapitops.plan.system.database.databases.sql.statements.Column;
import com.djrapitops.plan.system.database.databases.sql.tables.*;
import com.djrapitops.plugin.task.AbsRunnable;
import org.apache.commons.text.TextStringBuilder;
public class CreateIndexTask extends AbsRunnable {
private final SQLDB db;
public CreateIndexTask(SQLDB db) {
this.db = db;
}
@Override
public void run() {
createIndex(UsersTable.TABLE_NAME, "plan_users_uuid_index",
UsersTable.Col.UUID
);
createIndex(UserInfoTable.TABLE_NAME, "plan_user_info_uuid_index",
UserInfoTable.Col.UUID,
UserInfoTable.Col.SERVER_UUID
);
createIndex(SessionsTable.TABLE_NAME, "plan_sessions_uuid_index",
SessionsTable.Col.UUID,
SessionsTable.Col.SERVER_UUID
);
createIndex(SessionsTable.TABLE_NAME, "plan_sessions_date_index",
SessionsTable.Col.SESSION_START
);
createIndex(WorldTimesTable.TABLE_NAME, "plan_world_times_uuid_index",
WorldTimesTable.Col.UUID,
WorldTimesTable.Col.SERVER_UUID
);
createIndex(KillsTable.TABLE_NAME, "plan_kills_uuid_index",
KillsTable.Col.KILLER_UUID,
KillsTable.Col.VICTIM_UUID,
KillsTable.Col.SERVER_UUID
);
createIndex(KillsTable.TABLE_NAME, "plan_kills_date_index",
KillsTable.Col.DATE
);
createIndex(PingTable.TABLE_NAME, "plan_ping_uuid_index",
PingTable.Col.UUID,
PingTable.Col.SERVER_UUID
);
createIndex(PingTable.TABLE_NAME, "plan_ping_date_index",
PingTable.Col.DATE
);
createIndex(TPSTable.TABLE_NAME, "plan_tps_date_index",
TPSTable.Col.DATE
);
}
private void createIndex(String tableName, String indexName, Column... indexedColumns) {
if (indexedColumns.length == 0) {
throw new IllegalArgumentException("Can not create index without columns");
}
boolean isMySQL = db.getType() == DBType.MYSQL;
if (isMySQL) {
boolean indexExists = db.query(Queries.doesIndexExist(indexName, tableName));
if (indexExists) return;
}
TextStringBuilder sql = new TextStringBuilder("CREATE INDEX ");
if (!isMySQL) {
sql.append("IF NOT EXISTS ");
}
sql.append(indexName).append(" ON ").append(tableName);
sql.append(" (");
sql.appendWithSeparators(indexedColumns, ",");
sql.append(")");
db.execute(sql.toString());
}
}

View File

@ -70,4 +70,21 @@ public class Queries {
};
}
public static QueryStatement<Boolean> doesIndexExist(String indexName, String tableName) {
String sql = "SELECT COUNT(1) as IndexIsThere FROM INFORMATION_SCHEMA.STATISTICS " +
"WHERE table_schema=DATABASE() AND table_name=? AND index_name=?";
return new QueryStatement<Boolean>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, tableName);
statement.setString(2, indexName);
}
@Override
public Boolean processResults(ResultSet set) throws SQLException {
return set.next() && set.getInt("IndexIsThere") > 0;
}
};
}
}

View File

@ -44,8 +44,10 @@ import java.util.*;
*/
public class UsersTable extends UserUUIDTable {
public static final String TABLE_NAME = "plan_users";
public UsersTable(SQLDB db) {
super("plan_users", db);
super(TABLE_NAME, db);
statementSelectID = "(" + Select.from(tableName, tableName + "." + Col.ID).where(Col.UUID + "=?").toString() + " LIMIT 1)";
insertStatement = Insert.values(tableName,
Col.UUID,

View File

@ -1059,4 +1059,9 @@ public abstract class CommonDBTest {
assertFalse(settingsTable.fetchNewerConfig(savedMs, serverUUID).isPresent());
}
@Test
public void indexCreationWorksWithoutErrors() {
new CreateIndexTask(db).run();
}
}