Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #21 from lucyydotp/fix/mysql-no-uuid
Browse files Browse the repository at this point in the history
Draft: Use binary instead of uuid data type for mysql
  • Loading branch information
lucyydotp committed Mar 8, 2023
2 parents e2471e4 + 3d1552d commit b2f28df
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package net.lucypoulton.pronouns.common.store;

import com.zaxxer.hikari.HikariDataSource;
import net.lucypoulton.pronouns.api.PronounParser;
import net.lucypoulton.pronouns.api.set.PronounSet;
import net.lucypoulton.pronouns.api.supplier.PronounSupplier;
import net.lucypoulton.pronouns.api.PronounParser;
import net.lucypoulton.pronouns.common.ProNouns;
import net.lucypoulton.pronouns.common.platform.config.Config;
import net.lucypoulton.pronouns.common.util.UuidUtil;
import org.jetbrains.annotations.NotNull;

import java.sql.SQLException;
Expand Down Expand Up @@ -49,9 +50,9 @@ public MySqlPronounStore(final ProNouns plugin, final Config.MySqlConnectionInfo
try (final var con = dataSource.getConnection()) {
con.prepareStatement("""
CREATE TABLE IF NOT EXISTS pronouns (
player UUID PRIMARY KEY,
player BINARY(16) PRIMARY KEY,
pronouns TEXT NOT NULL,
last_updated_from UUID NOT NULL,
last_updated_from TEXT NOT NULL,
last_updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)
""").execute();
Expand All @@ -67,12 +68,12 @@ private void push(UUID uuid, List<PronounSet> sets) {
try (final var con = dataSource.getConnection()) {
if (sets.size() == 0) {
final var stmt = con.prepareStatement("DELETE FROM pronouns WHERE player=?");
stmt.setString(1, uuid.toString());
stmt.setBytes(1, UuidUtil.toBytes(uuid));
stmt.execute();
return;
}
final var stmt = con.prepareStatement("REPLACE INTO pronouns (player, pronouns, last_updated_from) VALUES (?, ?, ?)");
stmt.setString(1, uuid.toString());
stmt.setBytes(1, UuidUtil.toBytes(uuid));
stmt.setString(2, parser.toString(sets));
stmt.setString(3, plugin.meta().identifier());
stmt.execute();
Expand All @@ -85,7 +86,7 @@ private void pushAll(Map<UUID, List<PronounSet>> sets) {
try (final var con = dataSource.getConnection()) {
final var stmt = con.prepareStatement("REPLACE INTO pronouns (player, pronouns, last_updated_from) VALUES (?, ?, ?)");
for (final var entry : sets.entrySet()) {
stmt.setString(1, entry.getKey().toString());
stmt.setBytes(1, UuidUtil.toBytes(entry.getKey()));
stmt.setString(2, parser.toString(entry.getValue()));
stmt.setString(3, plugin.meta().identifier());
stmt.addBatch();
Expand Down Expand Up @@ -115,7 +116,7 @@ private void poll() {
stmt.setString(2, plugin.meta().identifier());
final var results = stmt.executeQuery();
while (results.next()) {
final var uuid = UUID.fromString(results.getString("player"));
final var uuid = UuidUtil.fromBytes(results.getBytes("player"));
if (!cache.containsKey(uuid)) continue;
final var newSets = parser.parse(results.getString("pronouns"));
cache.put(uuid, newSets);
Expand Down Expand Up @@ -164,7 +165,7 @@ public Map<UUID, List<PronounSet>> dump() {
public void onPlayerJoin(UUID uuid) {
try (final var con = dataSource.getConnection()) {
final var stmt = con.prepareStatement("SELECT pronouns FROM pronouns WHERE player=?");
stmt.setString(1, uuid.toString());
stmt.setBytes(1, UuidUtil.toBytes(uuid));
final var resultSet = stmt.executeQuery();
if (!resultSet.next()) return;
cache.put(uuid, parser.parse(resultSet.getString("pronouns")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.lucypoulton.pronouns.common.util;

import java.nio.ByteBuffer;
import java.util.UUID;

public class UuidUtil {
public static byte[] toBytes(UUID uuid) {
final var buffer = ByteBuffer.allocate(16);
buffer.putLong(uuid.getMostSignificantBits());
buffer.putLong(uuid.getLeastSignificantBits());
return buffer.array();
}

public static UUID fromBytes(byte[] bytes) {
final var buffer = ByteBuffer.wrap(bytes);
return new UUID(buffer.getLong(), buffer.getLong());
}
}

0 comments on commit b2f28df

Please sign in to comment.