From 1bf61005724d113426ccac7a5a06e97fc60c8b5b Mon Sep 17 00:00:00 2001 From: Christopher Bohn Date: Sun, 11 Feb 2024 16:19:35 -0800 Subject: [PATCH] Avoid type reconstruction during json serialization --- .../java/org/popcraft/bolt/data/SQLStore.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/bukkit/src/main/java/org/popcraft/bolt/data/SQLStore.java b/bukkit/src/main/java/org/popcraft/bolt/data/SQLStore.java index b41171fc..69e47bf3 100644 --- a/bukkit/src/main/java/org/popcraft/bolt/data/SQLStore.java +++ b/bukkit/src/main/java/org/popcraft/bolt/data/SQLStore.java @@ -11,6 +11,7 @@ import org.popcraft.bolt.util.Metrics; import java.io.IOException; +import java.lang.reflect.Type; import java.nio.file.Files; import java.nio.file.Path; import java.sql.Connection; @@ -35,6 +36,12 @@ public class SQLStore implements Store { private static final Gson GSON = new Gson(); + private static final TypeToken> ACCESS_LIST_TYPE_TOKEN = new TypeToken<>() { + }; + private static final TypeToken> PLAYER_LIST_TYPE_TOKEN = new TypeToken<>() { + }; + private static final Type ACCESS_LIST_TYPE = ACCESS_LIST_TYPE_TOKEN.getType(); + private static final Type PLAYER_LIST_TYPE = PLAYER_LIST_TYPE_TOKEN.getType(); private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); private final Map saveBlocks = new HashMap<>(); private final Map removeBlocks = new HashMap<>(); @@ -161,8 +168,7 @@ private BlockProtection blockProtectionFromResultSet(final ResultSet resultSet) final long created = resultSet.getLong(4); final long accessed = resultSet.getLong(5); final String accessText = resultSet.getString(6); - final Map access = Objects.requireNonNullElse(GSON.fromJson(accessText, new TypeToken>() { - }.getType()), new HashMap<>()); + final Map access = Objects.requireNonNullElse(GSON.fromJson(accessText, ACCESS_LIST_TYPE_TOKEN), new HashMap<>()); final String world = resultSet.getString(7); final int x = resultSet.getInt(8); final int y = resultSet.getInt(9); @@ -183,8 +189,7 @@ private void saveBlockProtectionNow(BlockProtection protection) { replaceBlock.setString(3, protection.getType()); replaceBlock.setLong(4, protection.getCreated()); replaceBlock.setLong(5, protection.getAccessed()); - replaceBlock.setString(6, GSON.toJson(protection.getAccess(), new TypeToken>() { - }.getType())); + replaceBlock.setString(6, GSON.toJson(protection.getAccess(), ACCESS_LIST_TYPE)); replaceBlock.setString(7, protection.getWorld()); replaceBlock.setInt(8, protection.getX()); replaceBlock.setInt(9, protection.getY()); @@ -265,8 +270,7 @@ private EntityProtection entityProtectionFromResultSet(final ResultSet resultSet final long created = resultSet.getLong(4); final long accessed = resultSet.getLong(5); final String accessText = resultSet.getString(6); - final Map access = Objects.requireNonNullElse(GSON.fromJson(accessText, new TypeToken>() { - }.getType()), new HashMap<>()); + final Map access = Objects.requireNonNullElse(GSON.fromJson(accessText, ACCESS_LIST_TYPE_TOKEN), new HashMap<>()); final String entity = resultSet.getString(7); return new EntityProtection(UUID.fromString(id), UUID.fromString(owner), type, created, accessed, access, entity); } @@ -283,8 +287,7 @@ private void saveEntityProtectionNow(EntityProtection protection) { replaceEntity.setString(3, protection.getType()); replaceEntity.setLong(4, protection.getCreated()); replaceEntity.setLong(5, protection.getAccessed()); - replaceEntity.setString(6, GSON.toJson(protection.getAccess(), new TypeToken>() { - }.getType())); + replaceEntity.setString(6, GSON.toJson(protection.getAccess(), ACCESS_LIST_TYPE)); replaceEntity.setString(7, protection.getEntity()); replaceEntity.execute(); } catch (SQLException e) { @@ -350,8 +353,7 @@ private Group groupFromResultSet(final ResultSet resultSet) throws SQLException final String name = resultSet.getString(1); final String owner = resultSet.getString(2); final String membersText = resultSet.getString(3); - final List membersRaw = Objects.requireNonNullElse(GSON.fromJson(membersText, new TypeToken>() { - }.getType()), new ArrayList<>()); + final List membersRaw = Objects.requireNonNullElse(GSON.fromJson(membersText, PLAYER_LIST_TYPE_TOKEN), new ArrayList<>()); final List members = new ArrayList<>(); membersRaw.forEach(memberRaw -> members.add(UUID.fromString(memberRaw))); return new Group(name, UUID.fromString(owner), members); @@ -366,8 +368,7 @@ private void saveGroupNow(Group group) { try (final PreparedStatement replaceGroup = connection.prepareStatement(Statements.REPLACE_GROUP.get(configuration.type()).formatted(configuration.prefix()))) { replaceGroup.setString(1, group.getName()); replaceGroup.setString(2, group.getOwner().toString()); - replaceGroup.setString(3, GSON.toJson(group.getMembers(), new TypeToken>() { - }.getType())); + replaceGroup.setString(3, GSON.toJson(group.getMembers(), PLAYER_LIST_TYPE)); replaceGroup.execute(); } catch (SQLException e) { e.printStackTrace(); @@ -428,8 +429,7 @@ public CompletableFuture> loadAccessLists() { private AccessList accessListFromResultSet(final ResultSet resultSet) throws SQLException { final String owner = resultSet.getString(1); final String accessListText = resultSet.getString(2); - final Map access = Objects.requireNonNullElse(GSON.fromJson(accessListText, new TypeToken>() { - }.getType()), new HashMap<>()); + final Map access = Objects.requireNonNullElse(GSON.fromJson(accessListText, ACCESS_LIST_TYPE_TOKEN), new HashMap<>()); return new AccessList(UUID.fromString(owner), access); } @@ -441,8 +441,7 @@ public void saveAccessList(AccessList accessList) { private void saveAccessListNow(AccessList accessList) { try (final PreparedStatement replaceAccessList = connection.prepareStatement(Statements.REPLACE_ACCESS_LIST.get(configuration.type()).formatted(configuration.prefix()))) { replaceAccessList.setString(1, accessList.getOwner().toString()); - replaceAccessList.setString(2, GSON.toJson(accessList.getAccess(), new TypeToken>() { - }.getType())); + replaceAccessList.setString(2, GSON.toJson(accessList.getAccess(), ACCESS_LIST_TYPE)); replaceAccessList.execute(); } catch (SQLException e) { e.printStackTrace();