Refactored NicknamesTable#saveUserName to a transaction:

- Removed NameProcessor
This commit is contained in:
Rsl1122 2019-02-04 23:02:14 +02:00
parent cce5688f80
commit 71a5592fd1
12 changed files with 161 additions and 144 deletions

View File

@ -16,8 +16,11 @@
*/
package com.djrapitops.plan.system.listeners.bukkit;
import com.djrapitops.plan.system.processing.Processing;
import com.djrapitops.plan.system.processing.processors.player.PlayerProcessors;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import org.bukkit.entity.Player;
@ -36,18 +39,21 @@ import java.util.UUID;
*/
public class ChatListener implements Listener {
private final PlayerProcessors processorFactory;
private final Processing processing;
private final ServerInfo serverInfo;
private final DBSystem dbSystem;
private final NicknameCache nicknameCache;
private final ErrorHandler errorHandler;
@Inject
public ChatListener(
PlayerProcessors processorFactory,
Processing processing,
ServerInfo serverInfo,
DBSystem dbSystem,
NicknameCache nicknameCache,
ErrorHandler errorHandler
) {
this.processorFactory = processorFactory;
this.processing = processing;
this.serverInfo = serverInfo;
this.dbSystem = dbSystem;
this.nicknameCache = nicknameCache;
this.errorHandler = errorHandler;
}
@ -65,9 +71,17 @@ public class ChatListener implements Listener {
}
private void actOnChatEvent(AsyncPlayerChatEvent event) {
Player p = event.getPlayer();
UUID uuid = p.getUniqueId();
String displayName = p.getDisplayName();
processing.submit(processorFactory.nameProcessor(uuid, displayName));
long time = System.currentTimeMillis();
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
String displayName = player.getDisplayName();
if (displayName.equals(nicknameCache.getDisplayName(uuid))) {
return;
}
dbSystem.getDatabase().executeTransaction(
new NicknameStoreTransaction(uuid, new Nickname(displayName, time, serverInfo.getServerUUID()))
);
}
}

View File

@ -17,8 +17,10 @@
package com.djrapitops.plan.system.listeners.bukkit;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.events.GeoInfoStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerServerRegisterTransaction;
import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransaction;
import com.djrapitops.plan.system.cache.GeolocationCache;
@ -165,8 +167,15 @@ public class PlayerOnlineListener implements Listener {
database.executeTransaction(new PlayerServerRegisterTransaction(uuid, player::getFirstPlayed, playerName, serverUUID));
processing.submitCritical(() -> sessionCache.cacheSession(uuid, new Session(uuid, serverUUID, time, world, gm)));
processing.submitNonCritical(processors.player().nameProcessor(uuid, displayName));
if (!displayName.equals(nicknameCache.getDisplayName(uuid))) {
database.executeTransaction(
new NicknameStoreTransaction(uuid, new Nickname(displayName, time, serverUUID))
);
}
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(uuid));
}
@EventHandler(priority = EventPriority.MONITOR)

View File

@ -21,6 +21,7 @@ import com.djrapitops.plan.data.container.Ping;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.container.TPS;
import com.djrapitops.plan.data.store.keys.SessionKeys;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.data.time.GMTimes;
import com.djrapitops.plan.db.access.ExecBatchStatement;
import com.djrapitops.plan.db.access.ExecStatement;
@ -287,4 +288,44 @@ public class DataStoreQueries {
}
};
}
/**
* Store nickname information of a player on a server.
*
* @param playerUUID UUID of the player.
* @param nickname Nickname information.
* @return Executable, use inside a {@link com.djrapitops.plan.db.access.transactions.Transaction}
*/
public static Executable storePlayerNickname(UUID playerUUID, Nickname nickname) {
return connection -> {
if (!updatePlayerNickname(playerUUID, nickname).execute(connection)) {
insertPlayerNickname(playerUUID, nickname).execute(connection);
}
return false;
};
}
private static Executable updatePlayerNickname(UUID playerUUID, Nickname nickname) {
return new ExecStatement(NicknamesTable.UPDATE_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, nickname.getDate());
statement.setString(2, nickname.getName());
statement.setString(3, playerUUID.toString());
statement.setString(4, nickname.getServerUUID().toString());
}
};
}
private static Executable insertPlayerNickname(UUID playerUUID, Nickname nickname) {
return new ExecStatement(NicknamesTable.INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, playerUUID.toString());
statement.setString(2, nickname.getServerUUID().toString());
statement.setString(3, nickname.getName());
statement.setLong(4, nickname.getDate());
}
};
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.access.transactions.events;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.queries.DataStoreQueries;
import com.djrapitops.plan.db.access.transactions.Transaction;
import java.util.UUID;
/**
* Transaction to store player's nickname information in the database.
*
* @author Rsl1122
*/
public class NicknameStoreTransaction extends Transaction {
private final UUID playerUUID;
private final Nickname nickname;
public NicknameStoreTransaction(UUID playerUUID, Nickname nickname) {
this.playerUUID = playerUUID;
this.nickname = nickname;
}
@Override
protected void performOperations() {
execute(DataStoreQueries.storePlayerNickname(playerUUID, nickname));
}
}

View File

@ -19,7 +19,6 @@ package com.djrapitops.plan.db.sql.tables;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.DBType;
import com.djrapitops.plan.db.SQLDB;
import com.djrapitops.plan.db.access.ExecStatement;
import com.djrapitops.plan.db.access.QueryStatement;
import com.djrapitops.plan.db.patches.NicknameLastSeenPatch;
import com.djrapitops.plan.db.patches.NicknamesOptimizationPatch;
@ -82,39 +81,6 @@ public class NicknamesTable extends Table {
.toString();
}
public void saveUserName(UUID uuid, Nickname name) {
List<Nickname> saved = getNicknameInformation(uuid);
if (saved.contains(name)) {
updateNickname(uuid, name);
} else {
insertNickname(uuid, name);
}
}
private void updateNickname(UUID uuid, Nickname name) {
execute(new ExecStatement(UPDATE_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, name.getDate());
statement.setString(2, name.getName());
statement.setString(3, uuid.toString());
statement.setString(4, getServerUUID().toString());
}
});
}
private void insertNickname(UUID uuid, Nickname name) {
execute(new ExecStatement(INSERT_STATEMENT) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
statement.setString(2, getServerUUID().toString());
statement.setString(3, name.getName());
statement.setLong(4, name.getDate());
}
});
}
public List<Nickname> getNicknameInformation(UUID uuid) {
String sql = "SELECT " +
NICKNAME + ", " +

View File

@ -75,9 +75,6 @@ public interface SaveOperations {
@Deprecated
void playerWasKicked(UUID uuid);
@Deprecated
void playerDisplayName(UUID uuid, Nickname nickname);
@Deprecated
void session(UUID uuid, Session session);

View File

@ -141,11 +141,6 @@ public class SQLSaveOps extends SQLOps implements SaveOperations {
usersTable.kicked(uuid);
}
@Override
public void playerDisplayName(UUID uuid, Nickname nickname) {
nicknamesTable.saveUserName(uuid, nickname);
}
@Override
public void session(UUID uuid, Session session) {
db.executeTransaction(new Transaction() {

View File

@ -1,63 +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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.processing.processors.player;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.processing.CriticalRunnable;
import java.util.UUID;
/**
* Processor for updating name in the database if the player has changed it.
*
* @author Rsl1122
*/
public class NameProcessor implements CriticalRunnable {
private final UUID uuid;
private final Nickname nickname;
private final Database database;
private final NicknameCache nicknameCache;
NameProcessor(
UUID uuid, Nickname nickname,
Database database,
NicknameCache nicknameCache
) {
this.uuid = uuid;
this.nickname = nickname;
this.database = database;
this.nicknameCache = nicknameCache;
}
@Override
public void run() {
String cachedDisplayName = nicknameCache.getDisplayName(uuid);
boolean sameAsCached = nickname.getName().equals(cachedDisplayName);
if (sameAsCached) {
return;
}
nicknameCache.updateDisplayName(uuid, nickname.getName());
database.save().playerDisplayName(uuid, nickname);
}
}

View File

@ -16,7 +16,6 @@
*/
package com.djrapitops.plan.system.processing.processors.player;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.cache.SessionCache;
import com.djrapitops.plan.system.database.DBSystem;
@ -65,9 +64,4 @@ public class PlayerProcessors {
public KickProcessor kickProcessor(UUID uuid) {
return new KickProcessor(uuid, dbSystem.get().getDatabase());
}
public NameProcessor nameProcessor(UUID uuid, String displayName) {
Nickname nickname = new Nickname(displayName, System.currentTimeMillis(), serverInfo.get().getServerUUID());
return new NameProcessor(uuid, nickname, dbSystem.get().getDatabase(), nicknameCache.get());
}
}

View File

@ -255,8 +255,8 @@ public abstract class CommonDBTest {
NicknamesTable nickTable = db.getNicknamesTable();
Nickname expected = new Nickname("TestNickname", System.currentTimeMillis(), serverUUID);
nickTable.saveUserName(playerUUID, expected);
nickTable.saveUserName(playerUUID, expected);
db.executeTransaction(new NicknameStoreTransaction(playerUUID, expected));
db.executeTransaction(new NicknameStoreTransaction(playerUUID, expected));
commitTest();
List<Nickname> nicknames = nickTable.getNicknameInformation(playerUUID);
@ -479,7 +479,7 @@ public abstract class CommonDBTest {
session.setPlayerKills(createKills());
execute(DataStoreQueries.storeSession(session));
nicknamesTable.saveUserName(playerUUID, new Nickname("TestNick", System.currentTimeMillis(), serverUUID));
db.executeTransaction(new NicknameStoreTransaction(playerUUID, new Nickname("TestNick", System.currentTimeMillis(), serverUUID)));
saveGeoInfo(playerUUID, new GeoInfo("1.2.3.4", "TestLoc", 223456789L, "3"));
assertTrue(db.query(PlayerFetchQueries.isPlayerRegistered(playerUUID)));
@ -517,9 +517,6 @@ public abstract class CommonDBTest {
}
private void saveAllData() throws NoSuchAlgorithmException {
NicknamesTable nicknamesTable = db.getNicknamesTable();
TPSTable tpsTable = db.getTpsTable();
saveUserOne();
saveUserTwo();
@ -531,7 +528,9 @@ public abstract class CommonDBTest {
session.setPlayerKills(createKills());
execute(DataStoreQueries.storeSession(session));
nicknamesTable.saveUserName(playerUUID, new Nickname("TestNick", System.currentTimeMillis(), serverUUID));
db.executeTransaction(
new NicknameStoreTransaction(playerUUID, new Nickname("TestNick", System.currentTimeMillis(), serverUUID))
);
saveGeoInfo(playerUUID, new GeoInfo("1.2.3.4", "TestLoc", 223456789L,
new SHA256Hash("1.2.3.4").create()));
@ -940,8 +939,8 @@ public abstract class CommonDBTest {
db.executeTransaction(new PlayerRegisterTransaction(playerUUID, () -> 1L, "Not random"));
String nickname = "2" + RandomData.randomString(10);
db.getNicknamesTable().saveUserName(uuid, new Nickname(nickname, System.currentTimeMillis(), serverUUID));
db.getNicknamesTable().saveUserName(playerUUID, new Nickname("No nick", System.currentTimeMillis(), serverUUID));
db.executeTransaction(new NicknameStoreTransaction(uuid, new Nickname(nickname, System.currentTimeMillis(), serverUUID)));
db.executeTransaction(new NicknameStoreTransaction(playerUUID, new Nickname("No nick", System.currentTimeMillis(), serverUUID)));
String search = "2";

View File

@ -16,8 +16,11 @@
*/
package com.djrapitops.plan.system.listeners.sponge;
import com.djrapitops.plan.system.processing.Processing;
import com.djrapitops.plan.system.processing.processors.player.PlayerProcessors;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.system.cache.NicknameCache;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import org.spongepowered.api.entity.living.player.Player;
@ -36,18 +39,21 @@ import java.util.UUID;
*/
public class SpongeChatListener {
private final PlayerProcessors processorFactory;
private final Processing processing;
private final ServerInfo serverInfo;
private final DBSystem dbSystem;
private final NicknameCache nicknameCache;
private ErrorHandler errorHandler;
@Inject
public SpongeChatListener(
PlayerProcessors processorFactory,
Processing processing,
ServerInfo serverInfo,
DBSystem dbSystem,
NicknameCache nicknameCache,
ErrorHandler errorHandler
) {
this.processorFactory = processorFactory;
this.processing = processing;
this.serverInfo = serverInfo;
this.dbSystem = dbSystem;
this.nicknameCache = nicknameCache;
this.errorHandler = errorHandler;
}
@ -65,9 +71,16 @@ public class SpongeChatListener {
}
private void actOnChatEvent(@First Player player) {
long time = System.currentTimeMillis();
UUID uuid = player.getUniqueId();
String displayName = player.getDisplayNameData().displayName().get().toPlain();
processing.submit(processorFactory.nameProcessor(uuid, displayName));
}
if (displayName.equals(nicknameCache.getDisplayName(uuid))) {
return;
}
dbSystem.getDatabase().executeTransaction(
new NicknameStoreTransaction(uuid, new Nickname(displayName, time, serverInfo.getServerUUID()))
);
}
}

View File

@ -17,8 +17,10 @@
package com.djrapitops.plan.system.listeners.sponge;
import com.djrapitops.plan.data.container.Session;
import com.djrapitops.plan.data.store.objects.Nickname;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.events.GeoInfoStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.NicknameStoreTransaction;
import com.djrapitops.plan.db.access.transactions.events.PlayerServerRegisterTransaction;
import com.djrapitops.plan.db.access.transactions.events.WorldNameStoreTransaction;
import com.djrapitops.plan.system.cache.GeolocationCache;
@ -170,7 +172,13 @@ public class SpongePlayerListener {
database.executeTransaction(new PlayerServerRegisterTransaction(uuid, () -> time, playerName, serverUUID));
processing.submitCritical(() -> sessionCache.cacheSession(uuid, new Session(uuid, serverUUID, time, world, gm)));
processing.submitNonCritical(processors.player().nameProcessor(uuid, displayName));
if (!displayName.equals(nicknameCache.getDisplayName(uuid))) {
database.executeTransaction(
new NicknameStoreTransaction(uuid, new Nickname(displayName, time, serverUUID))
);
}
processing.submitNonCritical(processors.info().playerPageUpdateProcessor(uuid));
}