Skip to content

Commit

Permalink
Improve challenge conversion speed and storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed May 14, 2020
1 parent a168cf0 commit f7fc22f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public boolean createChallenges(Challenge.Difficulty diff) {
}
}
Challenges.challengesEasy.put(challengeName, challenge);
addChallengeToDatabase(currentChallenge);
slotsEasy.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand All @@ -115,7 +114,6 @@ public boolean createChallenges(Challenge.Difficulty diff) {
}
}
Challenges.challengesMedium.put(challengeName, challenge);
addChallengeToDatabase(currentChallenge);
slotsMedium.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand All @@ -128,7 +126,6 @@ public boolean createChallenges(Challenge.Difficulty diff) {
}
}
Challenges.challengesHard.put(challengeName, challenge);
addChallengeToDatabase(currentChallenge);
slotsHard.put(challenge.getSlot(), challenge);
if (challenge.getSlot() > highestSlot) {
highestSlot = challenge.getSlot();
Expand Down Expand Up @@ -276,17 +273,4 @@ private boolean isChallengeValid(Challenge challenge) {
}
return false;
}

public void addChallengeToDatabase(String challenge) {
Connection connection = plugin.getdb().getConnection();
try {
String statement = "ALTER TABLE VSkyblock_Challenges ADD COLUMN IF NOT EXISTS (" + challenge + " BIGINT DEFAULT 0)";
PreparedStatement preparedStatement = connection.prepareStatement(statement);
preparedStatement.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
plugin.getdb().closeConnection(connection);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ public void initTables() {
+ "PRIMARY KEY (islandid));");
connection.createStatement().execute(
"CREATE TABLE IF NOT EXISTS VSkyblock_Challenges("
+ "islandid BIGINT UNIQUE NOT NULL);");
+ "islandid BIGINT NOT NULL,"
+ "challenge VARCHAR(100) NOT NULL,"
+ "count BIGINT DEFAULT 0,"
+ "INDEX (islandid), UNIQUE (challenge, islandid));");
//Auto adds new columns (if they are implemented in future updates and the plugin is already running on the server)
connection.createStatement().execute(
"ALTER TABLE VSkyblock_Player ADD COLUMN IF NOT EXISTS("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void setChallengeCount(String challenge, int count) {
}

public int getChallengeCount (String challenge) {
return challengeCounts.get(challenge);
return challengeCounts.getOrDefault(challenge, 0);
}

public HashMap<String, Integer> getAllChallengeCounts() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import com.github.Viduality.VSkyblock.Commands.Challenges.Challenge;
import com.github.Viduality.VSkyblock.Commands.Challenges.Challenges;
import com.github.Viduality.VSkyblock.VSkyblock;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -49,33 +50,16 @@ public void convertAllChallenges() {
preparedStatement.close();
if (!islands.isEmpty()) {
for (int currentisland : islands) {
PreparedStatement addToChallenges = connection.prepareStatement("INSERT IGNORE INTO VSkyblock_Challenges(islandid) VALUES (?)");
addToChallenges.setInt(1, currentisland);
addToChallenges.executeUpdate();
addToChallenges.close();

System.out.println("Converting Challenges of Island " + currentisland + "...");
List<String> uuids = new ArrayList<>();
PreparedStatement getMembers = connection.prepareStatement("SELECT * FROM VSkyblock_Player WHERE islandid = ?");
getMembers.setInt(1, currentisland);
ResultSet membersResult = getMembers.executeQuery();
while (membersResult.next()) {
uuids.add(membersResult.getString("uuid"));
}
getMembers.close();
if (!uuids.isEmpty()) {
ChallengesCache challenges = new ChallengesCache();
PreparedStatement getIslandChallenges;
getIslandChallenges = connection.prepareStatement("SELECT * FROM VSkyblock_Challenges WHERE islandid = ?");
getIslandChallenges.setInt(1, currentisland);
ResultSet r = getIslandChallenges.executeQuery();
ResultSetMetaData rm = r.getMetaData();
while (r.next()) {
for (int i = 2; i <= rm.getColumnCount(); i++) {
challenges.setChallengeCount(rm.getColumnName(i), r.getInt(i));
}
}
r.close();

if (!uuids.isEmpty()) {

ChallengesCache currentIslandChallengesEasy = new ChallengesCache();
ChallengesCache currentIslandChallengesMedium = new ChallengesCache();
Expand Down Expand Up @@ -161,53 +145,11 @@ public void convertAllChallenges() {
e.printStackTrace();
}
}
for (int i = 0; i < 18; i++) {
if (currentIslandChallengesEasy.getCurrentChallengeCount(i + 1) != 0) {
if (Challenges.sortedChallengesEasy.size() > i) {
if (Challenges.sortedChallengesEasy.get(i).getMySQLKey() != null) {
if (challenges.getAllChallengeCounts().containsKey(Challenges.sortedChallengesEasy.get(i).getMySQLKey())) {
int count = 0;
count = challenges.getChallengeCount(Challenges.sortedChallengesEasy.get(i).getMySQLKey());
if (count == 0) {
databaseWriter.updateChallengeCount(currentisland, Challenges.sortedChallengesEasy.get(i).getMySQLKey(), currentIslandChallengesEasy.getCurrentChallengeCount(i + 1));
}
}
}
}
}
}

for (int i = 0; i < 18; i++) {
if (currentIslandChallengesMedium.getCurrentChallengeCount(i + 1) != 0) {
if (Challenges.sortedChallengesMedium.size() > i) {
if (Challenges.sortedChallengesMedium.get(i).getMySQLKey() != null) {
if (challenges.getAllChallengeCounts().containsKey(Challenges.sortedChallengesMedium.get(i).getMySQLKey())) {
int count = 0;
count = challenges.getChallengeCount(Challenges.sortedChallengesMedium.get(i).getMySQLKey());
if (count == 0) {
databaseWriter.updateChallengeCount(currentisland, Challenges.sortedChallengesMedium.get(i).getMySQLKey(), currentIslandChallengesMedium.getCurrentChallengeCount(i + 1));
}
}
}
}
}
}

for (int i = 0; i < 18; i++) {
if (currentIslandChallengesHard.getCurrentChallengeCount(i + 1) != 0) {
if (Challenges.sortedChallengesHard.size() > i) {
if (Challenges.sortedChallengesHard.get(i).getMySQLKey() != null) {
if (challenges.getAllChallengeCounts().containsKey(Challenges.sortedChallengesHard.get(i).getMySQLKey())) {
int count = 0;
count = challenges.getChallengeCount(Challenges.sortedChallengesHard.get(i).getMySQLKey());
if (count == 0) {
databaseWriter.updateChallengeCount(currentisland, Challenges.sortedChallengesHard.get(i).getMySQLKey(), currentIslandChallengesHard.getCurrentChallengeCount(i + 1));
}
}
}
}
}
}
PreparedStatement updateStatement = connection.prepareStatement("INSERT IGNORE INTO VSkyblock_Challenges(islandid, count, challenge) VALUES (?, ?, ?)");
convertChallenges(currentisland, currentIslandChallengesEasy, Challenges.sortedChallengesEasy, updateStatement);
convertChallenges(currentisland, currentIslandChallengesMedium, Challenges.sortedChallengesMedium, updateStatement);
convertChallenges(currentisland, currentIslandChallengesHard, Challenges.sortedChallengesHard, updateStatement);
updateStatement.executeBatch();
}
}
}
Expand All @@ -230,4 +172,20 @@ public void convertAllChallenges() {
}
});
}

private void convertChallenges(int islandId, ChallengesCache challengesCache, List<Challenge> challengeList, PreparedStatement updateStatement) throws SQLException {
for (int i = 0; i < 18; i++) {
if (challengesCache.getCurrentChallengeCount(i + 1) != 0) {
if (challengeList.size() > i) {
String mysqlKey = challengeList.get(i).getMySQLKey();
if (mysqlKey != null) {
updateStatement.setInt(1, islandId);
updateStatement.setInt(2, challengesCache.getCurrentChallengeCount(i + 1));
updateStatement.setString(3, mysqlKey);
updateStatement.addBatch();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;


Expand Down Expand Up @@ -442,11 +443,8 @@ public void getIslandChallenges(final int islandid, final cCallback callback) {
preparedStatement = connection.prepareStatement("SELECT * FROM VSkyblock_Challenges WHERE islandid = ?");
preparedStatement.setInt(1, islandid);
ResultSet r = preparedStatement.executeQuery();
ResultSetMetaData rm = r.getMetaData();
while (r.next()) {
for (int i = 2; i <= rm.getColumnCount(); i++) {
cache.setChallengeCount(rm.getColumnName(i), r.getInt(i));
}
cache.setChallengeCount(r.getString("challenge"), r.getInt("count"));
}
preparedStatement.close();

Expand Down Expand Up @@ -863,43 +861,21 @@ public void getlastLocation(final UUID uuid, final CallbackLocation callback) {
* @param callback Returns the points the island will get for all finished challenges.
*/
public void getIslandsChallengePoints(int islandid, CallbackINT callback) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
getIslandChallenges(islandid, (cache) -> {
int challengeValueFirstComplete = getChallengeValueFirstComplete();
int challengeValueAfterFirstComplete = getChallengeValueAfterFirstComplete();
int challengeValueRepeats = getChallengeValueRepeats();
int totalChallengePoints = 0;
Connection connection = plugin.getdb().getConnection();
try {
ChallengesCache cache = new ChallengesCache();
PreparedStatement getChallengeCountEasy;
getChallengeCountEasy = connection.prepareStatement("SELECT * FROM VSkyblock_Challenges WHERE islandid = ?");
getChallengeCountEasy.setInt(1, islandid);
ResultSet r = getChallengeCountEasy.executeQuery();
ResultSetMetaData rm = r.getMetaData();
while (r.next()) {
for (int i = 1; rm.getColumnCount() <= i; i++) {
cache.setChallengeCount(rm.getColumnName(i), r.getInt(i));
}
for (int currentc : cache.getAllChallengeCounts().values()) {
if (currentc > challengeValueRepeats) {
currentc = challengeValueRepeats;
}
r.close();
for (String c : cache.getAllChallengeCounts().keySet()) {
int currentc = cache.getChallengeCount(c);
if (currentc > challengeValueRepeats) {
currentc = challengeValueRepeats;
}
if (currentc > 0) {
int repeatedPoints = (currentc - 1) * challengeValueAfterFirstComplete;
totalChallengePoints = totalChallengePoints + challengeValueFirstComplete + repeatedPoints;
}
if (currentc > 0) {
int repeatedPoints = (currentc - 1) * challengeValueAfterFirstComplete;
totalChallengePoints = totalChallengePoints + challengeValueFirstComplete + repeatedPoints;
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
plugin.getdb().closeConnection(connection);
}
final int finalchallengepoints = totalChallengePoints;
Bukkit.getScheduler().runTask(plugin, () -> callback.onQueryDone(finalchallengepoints));
callback.onQueryDone(totalChallengePoints);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ public void addPlayer(UUID uuid, String name) {
preparedStatement.executeUpdate();
preparedStatement.close();


PreparedStatement preparedStatement1;

preparedStatement1 = connection.prepareStatement("INSERT INTO VSkyblock_Challenges_Easy(uuid) VALUES (?)");
preparedStatement1.setString(1, uuid.toString());
preparedStatement1.executeUpdate();
preparedStatement1 = connection.prepareStatement("INSERT INTO VSkyblock_Challenges_Medium(uuid) VALUES (?)");
preparedStatement1.setString(1, uuid.toString());
preparedStatement1.executeUpdate();
preparedStatement1 = connection.prepareStatement("INSERT INTO VSkyblock_Challenges_Hard(uuid) VALUES (?)");
preparedStatement1.setString(1, uuid.toString());
preparedStatement1.executeUpdate();
preparedStatement1.close();

} catch (SQLException e) {
e.printStackTrace();
} finally {
Expand Down Expand Up @@ -122,12 +108,6 @@ public void run() {
preparedStatement1.executeUpdate();
preparedStatement1.close();

PreparedStatement preparedStatement2;
preparedStatement2 = connection.prepareStatement("INSERT IGNORE INTO VSkyblock_Challenges(islandid) VALUES (?)");
preparedStatement2.setInt(1, islandid);
preparedStatement2.executeUpdate();
preparedStatement2.close();

} catch (SQLException e) {
e.printStackTrace();
} finally {
Expand Down Expand Up @@ -360,9 +340,8 @@ public void updateChallengeCount(int islandid, String mySQLKey, int count) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
Connection connection = plugin.getdb().getConnection();
try {
String statement = "UPDATE VSkyblock_Challenges SET " + mySQLKey + " = ? WHERE islandid = ?";
PreparedStatement updateCount;
updateCount = connection.prepareStatement(statement);
String statement = "INSERT INTO VSkyblock_Challenges(islandid, count, challenge) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE count = count + VALUES(count)";
PreparedStatement updateCount = connection.prepareStatement(statement);
updateCount.setInt(1, count);
updateCount.setInt(2, islandid);
updateCount.executeUpdate();
Expand Down

0 comments on commit f7fc22f

Please sign in to comment.