mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-01-27 02:21:30 +01:00
Refactored CommandUseTable#getAllCommandUsages to a query
This commit is contained in:
parent
8e77b37188
commit
6bab6009a1
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.db.sql.queries.batch;
|
||||
|
||||
import com.djrapitops.plan.db.access.Query;
|
||||
import com.djrapitops.plan.db.access.QueryAllStatement;
|
||||
import com.djrapitops.plan.db.sql.tables.CommandUseTable;
|
||||
import com.djrapitops.plan.db.sql.tables.ServerTable;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Static method class for queries that use large amount of memory.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class LargeFetchQueries {
|
||||
|
||||
private LargeFetchQueries() {
|
||||
/* Static method class */
|
||||
}
|
||||
|
||||
/**
|
||||
* Query database for all command usage data.
|
||||
*
|
||||
* @return Multi map: Server UUID - (Command name - Usage count)
|
||||
*/
|
||||
public static Query<Map<UUID, Map<String, Integer>>> fetchAllCommandUsageData() {
|
||||
String serverIDColumn = ServerTable.TABLE_NAME + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = ServerTable.TABLE_NAME + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
CommandUseTable.Col.COMMAND + ", " +
|
||||
CommandUseTable.Col.TIMES_USED + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + CommandUseTable.TABLE_NAME +
|
||||
" INNER JOIN " + ServerTable.TABLE_NAME + " on " + serverIDColumn + "=" + CommandUseTable.Col.SERVER_ID;
|
||||
|
||||
return new QueryAllStatement<Map<UUID, Map<String, Integer>>>(sql, 10000) {
|
||||
@Override
|
||||
public Map<UUID, Map<String, Integer>> processResults(ResultSet set) throws SQLException {
|
||||
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(CommandUseTable.Col.COMMAND.get());
|
||||
int timesUsed = set.getInt(CommandUseTable.Col.TIMES_USED.get());
|
||||
|
||||
serverMap.put(command, timesUsed);
|
||||
map.put(serverUUID, serverMap);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,6 @@ package com.djrapitops.plan.db.sql.tables;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.access.ExecStatement;
|
||||
import com.djrapitops.plan.db.access.QueryAllStatement;
|
||||
import com.djrapitops.plan.db.access.QueryStatement;
|
||||
import com.djrapitops.plan.db.sql.parsing.Column;
|
||||
import com.djrapitops.plan.db.sql.parsing.Select;
|
||||
@ -45,8 +44,10 @@ import java.util.UUID;
|
||||
*/
|
||||
public class CommandUseTable extends Table {
|
||||
|
||||
public static final String TABLE_NAME = "plan_commandusages";
|
||||
|
||||
public CommandUseTable(SQLDB db) {
|
||||
super("plan_commandusages", db);
|
||||
super(TABLE_NAME, db);
|
||||
serverTable = db.getServerTable();
|
||||
insertStatement = "INSERT INTO " + tableName + " ("
|
||||
+ Col.COMMAND + ", "
|
||||
@ -183,36 +184,6 @@ public class CommandUseTable extends Table {
|
||||
});
|
||||
}
|
||||
|
||||
public Map<UUID, Map<String, Integer>> getAllCommandUsages() {
|
||||
String serverIDColumn = serverTable + "." + ServerTable.Col.SERVER_ID;
|
||||
String serverUUIDColumn = serverTable + "." + ServerTable.Col.SERVER_UUID + " as s_uuid";
|
||||
String sql = "SELECT " +
|
||||
Col.COMMAND + ", " +
|
||||
Col.TIMES_USED + ", " +
|
||||
serverUUIDColumn +
|
||||
" FROM " + tableName +
|
||||
" INNER JOIN " + serverTable + " on " + serverIDColumn + "=" + Col.SERVER_ID;
|
||||
|
||||
return query(new QueryAllStatement<Map<UUID, Map<String, Integer>>>(sql, 10000) {
|
||||
@Override
|
||||
public Map<UUID, Map<String, Integer>> processResults(ResultSet set) throws SQLException {
|
||||
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(Col.COMMAND.get());
|
||||
int timesUsed = set.getInt(Col.TIMES_USED.get());
|
||||
|
||||
serverMap.put(command, timesUsed);
|
||||
map.put(serverUUID, serverMap);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public enum Col implements Column {
|
||||
COMMAND_ID("id"),
|
||||
SERVER_ID("server_id"),
|
||||
|
@ -18,6 +18,7 @@ package com.djrapitops.plan.db.sql.tables.move;
|
||||
|
||||
import com.djrapitops.plan.data.container.UserInfo;
|
||||
import com.djrapitops.plan.db.SQLDB;
|
||||
import com.djrapitops.plan.db.sql.queries.batch.LargeFetchQueries;
|
||||
import com.djrapitops.plan.db.sql.tables.ServerTable;
|
||||
import com.djrapitops.plan.db.sql.tables.Table;
|
||||
import com.djrapitops.plan.db.sql.tables.UsersTable;
|
||||
@ -107,7 +108,7 @@ public class BatchOperationTable extends Table {
|
||||
if (toDB.equals(this)) {
|
||||
return;
|
||||
}
|
||||
toDB.db.getCommandUseTable().insertCommandUsage(db.getCommandUseTable().getAllCommandUsages());
|
||||
toDB.db.getCommandUseTable().insertCommandUsage(db.query(LargeFetchQueries.fetchAllCommandUsageData()));
|
||||
}
|
||||
|
||||
public void copyIPsAndGeolocs(BatchOperationTable toDB) {
|
||||
|
Loading…
Reference in New Issue
Block a user