Skip to content

Commit

Permalink
Avoid type reconstruction during json serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Feb 12, 2024
1 parent e818e9c commit 1bf6100
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions bukkit/src/main/java/org/popcraft/bolt/data/SQLStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +36,12 @@

public class SQLStore implements Store {
private static final Gson GSON = new Gson();
private static final TypeToken<HashMap<String, String>> ACCESS_LIST_TYPE_TOKEN = new TypeToken<>() {
};
private static final TypeToken<List<String>> 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<UUID, BlockProtection> saveBlocks = new HashMap<>();
private final Map<UUID, BlockProtection> removeBlocks = new HashMap<>();
Expand Down Expand Up @@ -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<String, String> access = Objects.requireNonNullElse(GSON.fromJson(accessText, new TypeToken<HashMap<String, String>>() {
}.getType()), new HashMap<>());
final Map<String, String> 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);
Expand All @@ -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<Map<String, String>>() {
}.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());
Expand Down Expand Up @@ -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<String, String> access = Objects.requireNonNullElse(GSON.fromJson(accessText, new TypeToken<HashMap<String, String>>() {
}.getType()), new HashMap<>());
final Map<String, String> 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);
}
Expand All @@ -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<Map<String, String>>() {
}.getType()));
replaceEntity.setString(6, GSON.toJson(protection.getAccess(), ACCESS_LIST_TYPE));
replaceEntity.setString(7, protection.getEntity());
replaceEntity.execute();
} catch (SQLException e) {
Expand Down Expand Up @@ -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<String> membersRaw = Objects.requireNonNullElse(GSON.fromJson(membersText, new TypeToken<List<String>>() {
}.getType()), new ArrayList<>());
final List<String> membersRaw = Objects.requireNonNullElse(GSON.fromJson(membersText, PLAYER_LIST_TYPE_TOKEN), new ArrayList<>());
final List<UUID> members = new ArrayList<>();
membersRaw.forEach(memberRaw -> members.add(UUID.fromString(memberRaw)));
return new Group(name, UUID.fromString(owner), members);
Expand All @@ -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<List<String>>() {
}.getType()));
replaceGroup.setString(3, GSON.toJson(group.getMembers(), PLAYER_LIST_TYPE));
replaceGroup.execute();
} catch (SQLException e) {
e.printStackTrace();
Expand Down Expand Up @@ -428,8 +429,7 @@ public CompletableFuture<Collection<AccessList>> loadAccessLists() {
private AccessList accessListFromResultSet(final ResultSet resultSet) throws SQLException {
final String owner = resultSet.getString(1);
final String accessListText = resultSet.getString(2);
final Map<String, String> access = Objects.requireNonNullElse(GSON.fromJson(accessListText, new TypeToken<HashMap<String, String>>() {
}.getType()), new HashMap<>());
final Map<String, String> access = Objects.requireNonNullElse(GSON.fromJson(accessListText, ACCESS_LIST_TYPE_TOKEN), new HashMap<>());
return new AccessList(UUID.fromString(owner), access);
}

Expand All @@ -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<Map<String, String>>() {
}.getType()));
replaceAccessList.setString(2, GSON.toJson(accessList.getAccess(), ACCESS_LIST_TYPE));
replaceAccessList.execute();
} catch (SQLException e) {
e.printStackTrace();
Expand Down

0 comments on commit 1bf6100

Please sign in to comment.