Update ranks sql system to block a race condition and improve performance

This commit is contained in:
AlexTheCoder 2017-09-02 17:08:29 -04:00
parent b860a69d46
commit 871f178419

View File

@ -42,7 +42,7 @@ public class AccountRepository extends MinecraftRepository
private static String CREATE_RANKS_TABLE = "CREATE TABLE IF NOT EXISTS accountRanks (id INT NOT NULL AUTO_INCREMENT, accountId INT NOT NULL, rankIdentifier VARCHAR(40), primaryGroup BOOL, PRIMARY KEY(id), INDEX accountIndex (accountId), INDEX rankIndex (rankIdentifier), UNIQUE INDEX additionalIndex (accountId, rankIdentifier, primaryGroup), FOREIGN KEY (accountId) REFERENCES accounts(id));";
private static String UPDATE_PRIMARY_RANK = "UPDATE accountRanks SET rankIdentifier=? WHERE accountId=? AND primaryGroup=true;";
private static String ADD_PRIMARY_RANK = "INSERT INTO accountRanks (accountId, rankIdentifier, primaryGroup) VALUES (?, NULL, true);";
private static String ADD_PRIMARY_RANK = "INSERT INTO accountRanks (accountId, rankIdentifier, primaryGroup) VALUES (?, 'NULL', true);";
private static String ADD_ADDITIONAL_RANK = "INSERT INTO accountRanks (accountId, rankIdentifier, primaryGroup) VALUES (?, ?, false);";
private static String REMOVE_ADDITIONAL_RANK = "DELETE FROM accountRanks WHERE accountId=? AND rankIdentifier=? AND primaryGroup=false;";
private static String REMOVE_ADDITIONAL_RANKS = "DELETE FROM accountRanks WHERE accountId=? AND primaryGroup=false;";
@ -100,11 +100,10 @@ public class AccountRepository extends MinecraftRepository
statement.execute("SELECT * FROM accountRanks WHERE accountId=" + accountId + ";");
try (ResultSet rankSet = statement.getResultSet())
{
boolean anyRows = false;
while (rankSet.next())
{
anyRows = true;
PermissionGroup group = PermissionGroup.valueOf(rankSet.getString("rankIdentifier"));
String identifier = rankSet.getString("rankIdentifier");
PermissionGroup group = identifier.equals("NULL") ? null : PermissionGroup.valueOf(identifier);
boolean primary = rankSet.getBoolean("primaryGroup");
if (primary)
@ -113,14 +112,12 @@ public class AccountRepository extends MinecraftRepository
}
else
{
extraRanks.add(group);
if (group != null)
{
extraRanks.add(group);
}
}
}
if (!anyRows)
{
statement.execute(UPDATE_PRIMARY_RANK.replace("?", "" + accountId));
}
}
final int finalId = accountId;
@ -239,7 +236,7 @@ public class AccountRepository extends MinecraftRepository
{
if (!rs.next() || rs.getInt(1) == 0)
{
s.execute(ADD_PRIMARY_RANK.replace("?", "" + accountId));
s.execute(ADD_PRIMARY_RANK.replace("?", String.valueOf(accountId)));
}
}
@ -341,13 +338,7 @@ public class AccountRepository extends MinecraftRepository
{
try (Statement s = c.createStatement())
{
try (ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM accountRanks WHERE accountId=" + accountId + " AND primaryGroup=true;"))
{
if (!rs.next())
{
s.execute(ADD_PRIMARY_RANK.replace("?", "" + accountId));
}
}
int primaryFound = 0;
try (ResultSet rs = s.executeQuery("SELECT * FROM accountRanks WHERE accountId=" + accountId + ";"))
{
while (rs.next())
@ -356,11 +347,19 @@ public class AccountRepository extends MinecraftRepository
{
removed.add(PermissionGroup.valueOf(rs.getString("rankIdentifier")));
}
else
{
primaryFound++;
}
}
}
if (primaryFound < 1)
{
s.execute(ADD_PRIMARY_RANK.replace("?", String.valueOf(accountId)));
}
}
executeUpdate(c, UPDATE_PRIMARY_RANK, () -> {}, new ColumnVarChar("rankIdentifier", 255, "PLAYER"), new ColumnInt("accountId", accountId));
executeUpdate(c, UPDATE_PRIMARY_RANK, () -> {}, new ColumnVarChar("rankIdentifier", 255, PermissionGroup.PLAYER.name()), new ColumnInt("accountId", accountId));
executeUpdate(c, REMOVE_ADDITIONAL_RANKS, () -> success.set(false), new ColumnInt("accountId", accountId));
}
catch (SQLException e)
@ -406,7 +405,14 @@ public class AccountRepository extends MinecraftRepository
{
if (rs.getBoolean("primaryGroup"))
{
primary.set(PermissionGroup.valueOf(rs.getString("rankIdentifier")));
if (rs.getString("rankIdentifier").equals("NULL"))
{
primary.set(null);
}
else
{
primary.set(PermissionGroup.valueOf(rs.getString("rankIdentifier")));
}
}
else
{