From f6c8edc45b6bae368f1e9c16530a9201264b1a96 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 30 Jan 2022 12:17:07 +0100 Subject: [PATCH 1/3] Add Tabcomplete Cache for Schematic Node --- .../src/de/steamwar/sql/SchematicNode.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 4d31c7c..8f6b0fd 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -22,6 +22,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; import de.steamwar.core.WorldEditWrapper; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.io.ByteArrayInputStream; @@ -65,6 +66,7 @@ public class SchematicNode { private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); private static final SQL.Statement selSchemData = new SQL.Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); private static final SQL.Statement deleteNode = new SQL.Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); + private static String path; public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); @@ -504,6 +506,7 @@ public class SchematicNode { updateDB.update(name, owner, parent, item, type, rank, id); this.lastUpdate = Timestamp.from(Instant.now()); this.brCache.clear(); + TAB_CACHE.clear(); } public void delete() { @@ -581,12 +584,21 @@ public class SchematicNode { return node.getId() == id; } + protected static final Map>> TAB_CACHE = new HashMap<>(); + + static { + Bukkit.getScheduler().runTaskTimer(Core.getInstance(), TAB_CACHE::clear, 20L * 30, 20L * 30); + } + public static List getNodeTabcomplete(SteamwarUser user, String s) { - List list = new ArrayList<>(); boolean sws = s.startsWith("/"); if (sws) { s = s.substring(1); } + if(TAB_CACHE.containsKey(user.getId()) && TAB_CACHE.get(user.getId()).containsKey(s)) { + return new ArrayList<>(TAB_CACHE.get(user.getId()).get(s)); + } + List list = new ArrayList<>(); if (s.contains("/")) { String preTab = s.substring(0, s.lastIndexOf("/") + 1); SchematicNode pa = SchematicNode.getNodeFromPath(user, preTab); @@ -598,6 +610,7 @@ public class SchematicNode { nodes.forEach(node -> list.add((sws ? "/" : "") + node.getName() + (node.isDir() ? "/" : ""))); } list.remove("//copy"); + Objects.requireNonNull(TAB_CACHE.putIfAbsent(user.getId(), new HashMap<>())).putIfAbsent(s, list); return list; } -- 2.39.2 From db9c648eefd38b08a5dc3f062f7a372226e260bc Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 30 Jan 2022 12:17:45 +0100 Subject: [PATCH 2/3] WTF? --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 1 - 1 file changed, 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 8f6b0fd..bb4fe4a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -66,7 +66,6 @@ public class SchematicNode { private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); private static final SQL.Statement selSchemData = new SQL.Statement("SELECT NodeData FROM SchematicNode WHERE NodeId = ?"); private static final SQL.Statement deleteNode = new SQL.Statement("DELETE FROM SchematicNode WHERE NodeId = ?"); - private static String path; public static SchematicNode createSchematic(int owner, String name, Integer parent) { return createSchematicNode(owner, name, parent, SchematicType.Normal.toDB(), ""); -- 2.39.2 From e807bfc793542658e5de01e156e5b0ddddf17799 Mon Sep 17 00:00:00 2001 From: Chaos Date: Sun, 20 Feb 2022 15:11:33 +0100 Subject: [PATCH 3/3] fix(schemnode_cache): Normalize Cache Keys Signed-off-by: Chaos --- SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java index 7640395..9a90105 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SchematicNode.java @@ -450,8 +450,10 @@ public class SchematicNode { if (sws) { s = s.substring(1); } - if(TAB_CACHE.containsKey(user.getId()) && TAB_CACHE.get(user.getId()).containsKey(s)) { - return new ArrayList<>(TAB_CACHE.get(user.getId()).get(s)); + int index = s.lastIndexOf("/"); + String cacheKey = index == -1 ? "" : s.substring(0, index); + if(TAB_CACHE.containsKey(user.getId()) && TAB_CACHE.get(user.getId()).containsKey(cacheKey)) { + return new ArrayList<>(TAB_CACHE.get(user.getId()).get(cacheKey)); } List list = new ArrayList<>(); if (s.contains("/")) { @@ -465,7 +467,7 @@ public class SchematicNode { nodes.forEach(node -> list.add((sws ? "/" : "") + node.getName() + (node.isDir() ? "/" : ""))); } list.remove("//copy"); - Objects.requireNonNull(TAB_CACHE.putIfAbsent(user.getId(), new HashMap<>())).putIfAbsent(s, list); + Objects.requireNonNull(TAB_CACHE.putIfAbsent(user.getId(), new HashMap<>())).putIfAbsent(cacheKey, list); return list; } -- 2.39.2