Skip to content

Commit

Permalink
list 和 clean 性能优化
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Sep 12, 2023
1 parent 65337bf commit 8c40e1e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 67 deletions.
93 changes: 45 additions & 48 deletions src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -339,21 +341,21 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
// 准备查询
Type type = Type.valueOf(inp1);
int maxLine = Integer.parseInt(inp2);

AtomicInteger i = new AtomicInteger();
CompletableFuture.runAsync(() ->
whileDataForList(type, maxLine, (pd) -> {
i.getAndIncrement();
sender.sendMessage(plugin.getConfig().getString("message.command.list", "")
.replace("%num%", i.toString())
.replace("%ID%", String.valueOf(pd.ID))
.replace("%Type%", pd.Type.getName())
.replace("%Ban%", pd.Ban.getName())
.replace("%UUID%", pd.UUID)
.replace("%Name%", pd.Name)
.replace("%Time%", String.valueOf(pd.Time)));
}, () -> {

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> whileDataForList(type, maxLine, (pd) -> {
i.getAndIncrement();
sender.sendMessage(plugin.getConfig().getString("message.command.list", "")
.replace("%num%", i.toString())
.replace("%ID%", String.valueOf(pd.ID))
.replace("%Type%", pd.Type.getName())
.replace("%Ban%", pd.Ban.getName())
.replace("%UUID%", pd.UUID)
.replace("%Name%", pd.Name)
.replace("%Time%", String.valueOf(pd.Time)));
}));
executor.shutdown();

return true;
}
Expand Down Expand Up @@ -389,7 +391,6 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

sender.sendMessage(plugin.getConfig().getString("message.command.clean", ""));
getLogger().info("[IpacWhitelist.clean] >>> ----------");

// 是否禁用参观账户
if(!plugin.getConfig().getBoolean("dev.deletePlayerDataAll.deletingLockPlayer", true)){
Expand All @@ -414,32 +415,37 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// 遍历所有数据
AtomicInteger i = new AtomicInteger();
CompletableFuture.runAsync(() -> whileDataForList(sql, (pd) -> {
// 跳过数据不完整和玩家在线
if(pd.UUID.isEmpty() || pd.Name.isEmpty() || Bukkit.getPlayer(pd.Name) != null){
return;
}
// 锁定这个参观账户
onVisitPlayerJoin.cleanVisitList.add(pd.UUID);

// 开始清理数据 //
i.getAndIncrement();
// 删除文件和运行指令
deletePlayerDataAll(plugin, i, pd);
// 删除账户
pd.Type = Type.NOT;
pd.save();
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
whileDataForList(sql, (pd) -> {
// 跳过数据不完整和玩家在线
if(pd.UUID.isEmpty() || pd.Name.isEmpty() || Bukkit.getPlayer(pd.Name) != null){
return;
}

// 清理结束, 等待指定时间
try {
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("dev.deletePlayerDataAll.intervalTime", 100));
} catch (InterruptedException ignored) {}
getLogger().info("[IpacWhitelist.clean] <<< ----------");

// 取消锁定
onVisitPlayerJoin.cleanVisitList.remove(pd.UUID);
// 锁定这个参观账户
onVisitPlayerJoin.cleanVisitList.add(pd.UUID);

// 开始清理数据 //
i.getAndIncrement();
// 删除文件和运行指令
deletePlayerDataAll(plugin, i, pd);
// 删除账户
pd.Type = Type.NOT;
pd.save();

}, () -> {
// 运行结束 //
// 清理结束, 等待指定时间
try {
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("dev.deletePlayerDataAll.intervalTime", 100));
} catch (InterruptedException ignored) {}

// 取消锁定
onVisitPlayerJoin.cleanVisitList.remove(pd.UUID);

});

// 恢复参观账户
if (!plugin.getConfig().getBoolean("dev.deletePlayerDataAll.deletingLockPlayer", true)) {
Expand All @@ -449,7 +455,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
getLogger().info("[IpacWhitelist.clean] <<< ----------");
sender.sendMessage(plugin.getConfig().getString("message.command.clean-ok", "")
.replace("%num%", i.toString()));
}));
});
executor.shutdown();
} // <- VISIT.. END

// 删除任意玩家数据
Expand Down Expand Up @@ -505,7 +512,6 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// 删除数据
sender.sendMessage(plugin.getConfig().getString("message.command.clean", ""));
getLogger().info("[IpacWhitelist.clean] >>> ----------");

// 是否全局禁用连接
if(!plugin.getConfig().getBoolean("dev.deletePlayerDataAll.deletingLockPlayer", true)){
Expand All @@ -515,6 +521,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
onPlayerJoin.cleanList.add(pd.UUID);

CompletableFuture.runAsync(() -> {
getLogger().info("[IpacWhitelist.clean] <<< ----------");
// 删除文件和运行指令
deletePlayerDataAll(plugin, new AtomicInteger(), pd);
// 删除账户
Expand Down Expand Up @@ -587,16 +594,6 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bu
"PLAYER"
);
}
// else if(args.length == 3){
// switch(args[1].toUpperCase()){
// case "VISIT", "NOT" -> {
//
// }
// case "PLAYER" -> {
//
// }
// }
// }
}
}
return null;
Expand Down
30 changes: 11 additions & 19 deletions src/main/java/aplini/ipacwhitelist/util/SQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.io.File;
import java.sql.*;
import java.util.concurrent.CompletableFuture;

import static aplini.ipacwhitelist.IpacWhitelist.getPlugin;
import static aplini.ipacwhitelist.util.Type.*;
Expand Down Expand Up @@ -208,26 +207,19 @@ public static PlayerData isInWhitelisted(Player player){

// 遍历数据
public interface whileDataForListInterface { void test(PlayerData pd);}
public interface whileDataForListInterfaceEnd { void test();}
public static void whileDataForList(PreparedStatement sql, whileDataForListInterface func, whileDataForListInterfaceEnd funcEnd){
public static void whileDataForList(PreparedStatement sql, whileDataForListInterface func){
// 查询
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
ResultSet results = sql.executeQuery();
while(results.next()){
PlayerData pd = packPlayerData(results);
func.test(pd);
}
} catch (Exception e) {
getLogger().warning(e.getMessage());
try {
ResultSet results = sql.executeQuery();
while(results.next()){
PlayerData pd = packPlayerData(results);
func.test(pd);
}
});
future.join(); // 等待运行完毕

// 运行结束
funcEnd.test();
} catch (Exception e) {
getLogger().warning(e.getMessage());
}
}
public static void whileDataForList(Type type, int maxLine, whileDataForListInterface func, whileDataForListInterfaceEnd funcEnd){
public static void whileDataForList(Type type, int maxLine, whileDataForListInterface func){
String query;
String limit = maxLine != -1 ? ("LIMIT "+ maxLine) : "";

Expand All @@ -239,7 +231,7 @@ public static void whileDataForList(Type type, int maxLine, whileDataForListInte

try {
PreparedStatement sql = connection.prepareStatement(query);
whileDataForList(sql, func, funcEnd);
whileDataForList(sql, func);

} catch (SQLException e) {
getLogger().warning(e.getMessage());
Expand Down

0 comments on commit 8c40e1e

Please sign in to comment.