diff --git a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandListener.java b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandListener.java index 020568544..a8342dc1a 100644 --- a/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandListener.java +++ b/Plan/bukkit/src/main/java/com/djrapitops/plan/system/listeners/bukkit/CommandListener.java @@ -17,8 +17,9 @@ package com.djrapitops.plan.system.listeners.bukkit; import com.djrapitops.plan.Plan; -import com.djrapitops.plan.system.processing.Processing; -import com.djrapitops.plan.system.processing.processors.Processors; +import com.djrapitops.plan.db.access.transactions.events.CommandStoreTransaction; +import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.paths.DataGatheringSettings; @@ -41,22 +42,22 @@ public class CommandListener implements Listener { private final Plan plugin; private final PlanConfig config; - private final Processors processors; - private final Processing processing; + private final ServerInfo serverInfo; + private final DBSystem dbSystem; private final ErrorHandler errorHandler; @Inject public CommandListener( Plan plugin, PlanConfig config, - Processors processors, - Processing processing, + ServerInfo serverInfo, + DBSystem dbSystem, ErrorHandler errorHandler ) { this.plugin = plugin; this.config = config; - this.processors = processors; - this.processing = processing; + this.serverInfo = serverInfo; + this.dbSystem = dbSystem; this.errorHandler = errorHandler; } @@ -90,7 +91,7 @@ public class CommandListener implements Listener { commandName = command.getName(); } } - processing.submit(processors.commandProcessor(commandName)); + dbSystem.getDatabase().executeTransaction(new CommandStoreTransaction(serverInfo.getServerUUID(), commandName)); } private Command getBukkitCommand(String commandName) { diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/SQLDB.java b/Plan/common/src/main/java/com/djrapitops/plan/db/SQLDB.java index 29972ac23..0942ac65a 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/SQLDB.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/SQLDB.java @@ -76,7 +76,6 @@ public abstract class SQLDB extends AbstractDatabase { private final NicknamesTable nicknamesTable; private final SessionsTable sessionsTable; private final GeoInfoTable geoInfoTable; - private final CommandUseTable commandUseTable; private final TPSTable tpsTable; private final SecurityTable securityTable; private final WorldTable worldTable; @@ -114,7 +113,6 @@ public abstract class SQLDB extends AbstractDatabase { serverTable = new ServerTable(this); securityTable = new SecurityTable(this); - commandUseTable = new CommandUseTable(this); tpsTable = new TPSTable(this); usersTable = new UsersTable(this); @@ -377,11 +375,6 @@ public abstract class SQLDB extends AbstractDatabase { return nicknamesTable; } - @Deprecated - public CommandUseTable getCommandUseTable() { - return commandUseTable; - } - @Deprecated public TPSTable getTpsTable() { return tpsTable; diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java new file mode 100644 index 000000000..caa8a1c13 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/access/transactions/events/CommandStoreTransaction.java @@ -0,0 +1,67 @@ +package com.djrapitops.plan.db.access.transactions.events; + +import com.djrapitops.plan.db.access.ExecStatement; +import com.djrapitops.plan.db.access.transactions.Transaction; +import com.djrapitops.plan.db.sql.tables.CommandUseTable; +import com.djrapitops.plan.db.sql.tables.ServerTable; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.UUID; + +/** + * Transaction to update command usage information in the database. + * + * @author Rsl1122 + */ +public class CommandStoreTransaction extends Transaction { + + private final UUID serverUUID; + private final String commandName; + + public CommandStoreTransaction( + UUID serverUUID, + String commandName + ) { + this.serverUUID = serverUUID; + this.commandName = commandName; + } + + @Override + protected boolean shouldBeExecuted() { + return commandName.length() <= 20; + } + + @Override + protected void performOperations() { + if (!updateCommandUse()) { + insertCommand(); + } + } + + private boolean updateCommandUse() { + String sql = "UPDATE " + CommandUseTable.TABLE_NAME + " SET " + + CommandUseTable.TIMES_USED + "=" + CommandUseTable.TIMES_USED + "+ 1" + + " WHERE " + CommandUseTable.SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + + " AND " + CommandUseTable.COMMAND + "=?"; + + return execute(new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, serverUUID.toString()); + statement.setString(2, commandName); + } + }); + } + + private void insertCommand() { + execute(new ExecStatement(CommandUseTable.INSERT_STATEMENT) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setString(1, commandName); + statement.setInt(2, 1); + statement.setString(3, serverUUID.toString()); + } + }); + } +} \ No newline at end of file diff --git a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java index 62a64150c..9453d70d1 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/db/sql/tables/CommandUseTable.java @@ -17,26 +17,22 @@ package com.djrapitops.plan.db.sql.tables; import com.djrapitops.plan.db.DBType; -import com.djrapitops.plan.db.SQLDB; -import com.djrapitops.plan.db.access.ExecStatement; import com.djrapitops.plan.db.sql.parsing.CreateTableParser; import com.djrapitops.plan.db.sql.parsing.Sql; -import java.sql.PreparedStatement; -import java.sql.SQLException; - /** - * Table that is in charge of storing command data. - *

- * Table Name: plan_commandusages + * Table information about 'plan_commandusages'. + * + * Patches affecting this table: + * {@link com.djrapitops.plan.db.patches.Version10Patch} * * @author Rsl1122 */ -public class CommandUseTable extends Table { +public class CommandUseTable { public static final String TABLE_NAME = "plan_commandusages"; - public static final String COMMAND_ID = "id"; + public static final String ID = "id"; public static final String SERVER_ID = "server_id"; public static final String COMMAND = "command"; public static final String TIMES_USED = "times_used"; @@ -47,50 +43,17 @@ public class CommandUseTable extends Table { + SERVER_ID + ") VALUES (?, ?, " + ServerTable.STATEMENT_SELECT_SERVER_ID + ")"; - public CommandUseTable(SQLDB db) { - super(TABLE_NAME, db); + private CommandUseTable() { + /* Static information class */ } public static String createTableSQL(DBType dbType) { return CreateTableParser.create(TABLE_NAME, dbType) - .column(COMMAND_ID, Sql.INT).primaryKey() + .column(ID, Sql.INT).primaryKey() .column(COMMAND, Sql.varchar(20)).notNull() .column(TIMES_USED, Sql.INT).notNull() .column(SERVER_ID, Sql.INT).notNull() .foreignKey(SERVER_ID, ServerTable.TABLE_NAME, ServerTable.SERVER_ID) .toString(); } - - public void commandUsed(String command) { - if (command.length() > 20) { - return; - } - - String sql = "UPDATE " + TABLE_NAME + " SET " - + TIMES_USED + "=" + TIMES_USED + "+ 1" + - " WHERE " + SERVER_ID + "=" + ServerTable.STATEMENT_SELECT_SERVER_ID + - " AND " + COMMAND + "=?"; - - boolean updated = execute(new ExecStatement(sql) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, getServerUUID().toString()); - statement.setString(2, command); - } - }); - if (!updated) { - insertCommand(command); - } - } - - private void insertCommand(String command) { - execute(new ExecStatement(INSERT_STATEMENT) { - @Override - public void prepare(PreparedStatement statement) throws SQLException { - statement.setString(1, command); - statement.setInt(2, 1); - statement.setString(3, getServerUUID().toString()); - } - }); - } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java index 755b7cdb3..16a051f4d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java @@ -88,9 +88,6 @@ public interface SaveOperations { @Deprecated void registerNewUserOnThisServer(UUID uuid, long registered); - @Deprecated - void commandUsed(String commandName); - @Deprecated void insertTPSforThisServer(TPS tps); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLOps.java index 8e4c9a1a9..f1b8d537d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLOps.java @@ -19,6 +19,7 @@ package com.djrapitops.plan.system.database.databases.sql.operation; import com.djrapitops.plan.db.SQLDB; import com.djrapitops.plan.db.sql.tables.*; +@Deprecated public class SQLOps { protected final SQLDB db; @@ -29,7 +30,6 @@ public class SQLOps { protected final NicknamesTable nicknamesTable; protected final SessionsTable sessionsTable; protected final GeoInfoTable geoInfoTable; - protected final CommandUseTable commandUseTable; protected final TPSTable tpsTable; protected final SecurityTable securityTable; protected final WorldTable worldTable; @@ -47,7 +47,6 @@ public class SQLOps { nicknamesTable = db.getNicknamesTable(); sessionsTable = db.getSessionsTable(); geoInfoTable = db.getGeoInfoTable(); - commandUseTable = db.getCommandUseTable(); tpsTable = db.getTpsTable(); securityTable = db.getSecurityTable(); worldTable = db.getWorldTable(); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java index 4b5f5234b..f95a1a75d 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java @@ -166,11 +166,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { userInfoTable.registerUserInfo(uuid, registered); } - @Override - public void commandUsed(String commandName) { - commandUseTable.commandUsed(commandName); - } - @Override public void insertTPSforThisServer(TPS tps) { tpsTable.insertTPS(tps); diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/CommandProcessor.java b/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/CommandProcessor.java deleted file mode 100644 index bc26e7940..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/CommandProcessor.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.system.processing.processors; - -import com.djrapitops.plan.db.Database; -import com.djrapitops.plan.system.processing.CriticalRunnable; - -/** - * Updates Command usage amount in the database. - * - * @author Rsl1122 - */ -public class CommandProcessor implements CriticalRunnable { - - private final String command; - - private final Database database; - - CommandProcessor(String command, Database database) { - this.command = command; - this.database = database; - } - - @Override - public void run() { - database.save().commandUsed(command); - } -} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/Processors.java b/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/Processors.java index 60a27ea87..11ae0f820 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/Processors.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/system/processing/processors/Processors.java @@ -55,10 +55,6 @@ public class Processors { return new TPSInsertProcessor(tpsList, dbSystem.get().getDatabase()); } - public CommandProcessor commandProcessor(String command) { - return new CommandProcessor(command, dbSystem.get().getDatabase()); - } - public PlayerProcessors player() { return playerProcessors; } diff --git a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java index c5a6419b0..69950f07f 100644 --- a/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java +++ b/Plan/common/src/test/java/com/djrapitops/plan/db/CommonDBTest.java @@ -30,6 +30,7 @@ import com.djrapitops.plan.data.time.GMTimes; import com.djrapitops.plan.data.time.WorldTimes; import com.djrapitops.plan.db.access.Query; import com.djrapitops.plan.db.access.transactions.*; +import com.djrapitops.plan.db.access.transactions.events.CommandStoreTransaction; import com.djrapitops.plan.db.patches.Patch; import com.djrapitops.plan.db.sql.queries.AggregateQueries; import com.djrapitops.plan.db.sql.queries.LargeFetchQueries; @@ -146,7 +147,6 @@ public abstract class CommonDBTest { @Test public void testSaveCommandUse() throws DBInitException { - CommandUseTable commandUseTable = db.getCommandUseTable(); Map expected = new HashMap<>(); expected.put("plan", 1); @@ -154,45 +154,47 @@ public abstract class CommonDBTest { expected.put("pla", 7); expected.put("help", 21); - commandUseTable.commandUsed("plan"); - - for (int i = 0; i < 4; i++) { - commandUseTable.commandUsed("tp"); - } - - for (int i = 0; i < 7; i++) { - commandUseTable.commandUsed("pla"); - } - - for (int i = 0; i < 21; i++) { - commandUseTable.commandUsed("help"); - } - - for (int i = 0; i < 3; i++) { - commandUseTable.commandUsed("roiergbnougbierubieugbeigubeigubgierbgeugeg"); - } + useCommand("plan"); + useCommand("tp", 4); + useCommand("pla", 7); + useCommand("help", 21); + useCommand("roiergbnougbierubieugbeigubeigubgierbgeugeg", 3); commitTest(); Map commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID)); assertEquals(expected, commandUse); + } - for (int i = 0; i < 3; i++) { - commandUseTable.commandUsed("test"); - } - - for (int i = 0; i < 2; i++) { - commandUseTable.commandUsed("tp"); - } + @Test + public void commandUsageSavingDoesNotCreateNewEntriesForOldCommands() throws DBInitException { + Map expected = new HashMap<>(); + expected.put("plan", 1); expected.put("test", 3); expected.put("tp", 6); + expected.put("pla", 7); + expected.put("help", 21); - commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID)); + testSaveCommandUse(); + useCommand("test", 3); + useCommand("tp", 2); + + Map commandUse = db.query(AggregateQueries.commandUsageCounts(serverUUID)); assertEquals(expected, commandUse); } + private void useCommand(String commandName) { + db.executeTransaction(new CommandStoreTransaction(serverUUID, commandName)); + } + + private void useCommand(String commandName, int times) { + for (int i = 0; i < times; i++) { + useCommand(commandName); + } + } + @Test public void testTPSSaving() throws Exception { TPSTable tpsTable = db.getTpsTable(); @@ -572,13 +574,12 @@ public abstract class CommonDBTest { assertTrue(usersTable.isRegistered(playerUUID)); - CommandUseTable commandUseTable = database.getCommandUseTable(); - commandUseTable.commandUsed("plan"); - commandUseTable.commandUsed("plan"); - commandUseTable.commandUsed("tp"); - commandUseTable.commandUsed("help"); - commandUseTable.commandUsed("help"); - commandUseTable.commandUsed("help"); + useCommand("plan"); + useCommand("plan"); + useCommand("tp"); + useCommand("help"); + useCommand("help"); + useCommand("help"); List expected = new ArrayList<>(); Random r = new Random(); diff --git a/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java b/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java index 7a3f59665..d006072eb 100644 --- a/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java +++ b/Plan/sponge/src/main/java/com/djrapitops/plan/system/listeners/sponge/SpongeCommandListener.java @@ -16,8 +16,9 @@ */ package com.djrapitops.plan.system.listeners.sponge; -import com.djrapitops.plan.system.processing.Processing; -import com.djrapitops.plan.system.processing.processors.Processors; +import com.djrapitops.plan.db.access.transactions.events.CommandStoreTransaction; +import com.djrapitops.plan.system.database.DBSystem; +import com.djrapitops.plan.system.info.server.ServerInfo; import com.djrapitops.plan.system.settings.Permissions; import com.djrapitops.plan.system.settings.config.PlanConfig; import com.djrapitops.plan.system.settings.paths.DataGatheringSettings; @@ -42,20 +43,20 @@ import java.util.Optional; public class SpongeCommandListener { private final PlanConfig config; - private final Processors processors; - private final Processing processing; - private ErrorHandler errorHandler; + private final ServerInfo serverInfo; + private final DBSystem dbSystem; + private final ErrorHandler errorHandler; @Inject public SpongeCommandListener( PlanConfig config, - Processors processors, - Processing processing, + ServerInfo serverInfo, + DBSystem dbSystem, ErrorHandler errorHandler ) { this.config = config; - this.processors = processors; - this.processing = processing; + this.serverInfo = serverInfo; + this.dbSystem = dbSystem; this.errorHandler = errorHandler; } @@ -88,7 +89,7 @@ public class SpongeCommandListener { commandName = existingCommand.get().getPrimaryAlias(); } } - processing.submit(processors.commandProcessor(commandName)); + dbSystem.getDatabase().executeTransaction(new CommandStoreTransaction(serverInfo.getServerUUID(), commandName)); } } \ No newline at end of file