Refactor DB index creation task into a transaction

This commit is contained in:
Rsl1122 2019-01-26 11:58:03 +02:00
parent 300dc69e33
commit 73c9f71166
3 changed files with 24 additions and 21 deletions

View File

@ -23,11 +23,11 @@ import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.QueryStatement; import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.access.transactions.CleanTransaction; import com.djrapitops.plan.db.access.transactions.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.CreateTablesTransaction; import com.djrapitops.plan.db.access.transactions.CreateTablesTransaction;
import com.djrapitops.plan.db.access.transactions.Transaction; import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.patches.*; import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.db.tasks.CreateIndexTask;
import com.djrapitops.plan.db.tasks.PatchTask; import com.djrapitops.plan.db.tasks.PatchTask;
import com.djrapitops.plan.system.database.databases.operation.*; import com.djrapitops.plan.system.database.databases.operation.*;
import com.djrapitops.plan.system.database.databases.sql.operation.*; import com.djrapitops.plan.system.database.databases.sql.operation.*;
@ -233,8 +233,12 @@ public abstract class SQLDB extends AbstractDatabase {
private void registerIndexCreationTask() { private void registerIndexCreationTask() {
try { try {
runnableFactory.create("Database Index Creation", new CreateIndexTask(this)) runnableFactory.create("Database Index Creation", new AbsRunnable() {
.runTaskLaterAsynchronously(TimeAmount.toTicks(1, TimeUnit.MINUTES)); @Override
public void run() {
executeTransaction(new CreateIndexTransaction(getType()));
}
}).runTaskLaterAsynchronously(TimeAmount.toTicks(1, TimeUnit.MINUTES));
} catch (Exception ignore) { } catch (Exception ignore) {
// Task failed to register because plugin is being disabled // Task failed to register because plugin is being disabled
} }

View File

@ -14,25 +14,28 @@
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>. * along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/ */
package com.djrapitops.plan.db.tasks; package com.djrapitops.plan.db.access.transactions;
import com.djrapitops.plan.db.DBType; import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.sql.queries.schema.MySQLSchemaQueries; import com.djrapitops.plan.db.sql.queries.schema.MySQLSchemaQueries;
import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plugin.task.AbsRunnable;
import org.apache.commons.text.TextStringBuilder; import org.apache.commons.text.TextStringBuilder;
public class CreateIndexTask extends AbsRunnable { /**
* Transaction that creates the database index if it has not yet been created.
*
* @author Rsl1122
*/
public class CreateIndexTransaction extends Transaction {
private final SQLDB db; private final DBType dbType;
public CreateIndexTask(SQLDB db) { public CreateIndexTransaction(DBType dbType) {
this.db = db; this.dbType = dbType;
} }
@Override @Override
public void run() { protected void performOperations() {
createIndex(UsersTable.TABLE_NAME, "plan_users_uuid_index", createIndex(UsersTable.TABLE_NAME, "plan_users_uuid_index",
UsersTable.USER_UUID UsersTable.USER_UUID
); );
@ -76,9 +79,9 @@ public class CreateIndexTask extends AbsRunnable {
throw new IllegalArgumentException("Can not create index without columns"); throw new IllegalArgumentException("Can not create index without columns");
} }
boolean isMySQL = db.getType() == DBType.MYSQL; boolean isMySQL = dbType == DBType.MYSQL;
if (isMySQL) { if (isMySQL) {
boolean indexExists = db.query(MySQLSchemaQueries.doesIndexExist(indexName, tableName)); boolean indexExists = query(MySQLSchemaQueries.doesIndexExist(indexName, tableName));
if (indexExists) return; if (indexExists) return;
} }
@ -92,6 +95,6 @@ public class CreateIndexTask extends AbsRunnable {
sql.appendWithSeparators(indexedColumns, ","); sql.appendWithSeparators(indexedColumns, ",");
sql.append(")"); sql.append(")");
db.execute(sql.toString()); execute(sql.toString());
} }
} }

View File

@ -29,16 +29,12 @@ import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes; import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.BackupCopyTransaction; import com.djrapitops.plan.db.access.transactions.*;
import com.djrapitops.plan.db.access.transactions.RemoveEverythingTransaction;
import com.djrapitops.plan.db.access.transactions.RemovePlayerTransaction;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.patches.Patch; import com.djrapitops.plan.db.patches.Patch;
import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; import com.djrapitops.plan.db.sql.queries.LargeFetchQueries;
import com.djrapitops.plan.db.sql.queries.LargeStoreQueries; import com.djrapitops.plan.db.sql.queries.LargeStoreQueries;
import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries; import com.djrapitops.plan.db.sql.queries.OptionalFetchQueries;
import com.djrapitops.plan.db.sql.tables.*; import com.djrapitops.plan.db.sql.tables.*;
import com.djrapitops.plan.db.tasks.CreateIndexTask;
import com.djrapitops.plan.system.PlanSystem; import com.djrapitops.plan.system.PlanSystem;
import com.djrapitops.plan.system.database.DBSystem; import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.Server; import com.djrapitops.plan.system.info.server.Server;
@ -1063,7 +1059,7 @@ public abstract class CommonDBTest {
@Test @Test
public void indexCreationWorksWithoutErrors() { public void indexCreationWorksWithoutErrors() {
new CreateIndexTask(db).run(); db.executeTransaction(new CreateIndexTransaction(db.getType()));
} }
} }