SteamWar/SpigotCore
Archiviert
13
0

Some Crazy-Ass SQL-Statements and Performance!
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Signed-off-by: Chaoscaot <chaoscaot444@gmail.com>
Dieser Commit ist enthalten in:
Chaoscaot 2021-11-13 15:09:53 +01:00
Ursprung 32a9e2f619
Commit c49daba060
2 geänderte Dateien mit 34 neuen und 31 gelöschten Zeilen

Datei anzeigen

@ -93,7 +93,7 @@ public class Schematic {
public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){ public static List<Schematic> getSchemsOfType(int schemOwner, SchematicType schemType){
List<Schematic> schematics = new ArrayList<>(); List<Schematic> schematics = new ArrayList<>();
SchematicNode.getSchematicsOfType(schemOwner, schemType.toDB(), null) SchematicNode.getAllAccessibleSchematicsOfType(schemOwner, schemType.toDB())
.forEach(node1 -> { .forEach(node1 -> {
if (!node1.isDir()) schematics.add(new Schematic(node1)); if (!node1.isDir()) schematics.add(new Schematic(node1));
}); });

Datei anzeigen

@ -35,6 +35,7 @@ import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
@ -53,6 +54,11 @@ public class SchematicNode {
private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?"); private static final SQL.Statement getAllSchemsOfTypeOwner = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeOwner = ? AND NodeType = ?");
private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?"); private static final SQL.Statement getAllSchemsOfType = new SQL.Statement("SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeType = ?");
private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName"); private static final SQL.Statement getAccessibleByUser = new SQL.Statement("SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) AND s.ParentNode is NULL GROUP BY s.NodeId ORDER BY s.NodeName");
private static final SQL.Statement getAccessibleByUserByTypeInNode = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode = ?");
private static final SQL.Statement getAccessibleByUserByTypeInNode_Null = new SQL.Statement("WITH RECURSIVE RSNB AS (WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSNB WHERE SN.NodeId = RSNB.ParentNode)SELECT * FROM RSNB WHERE ParentNode is null");
private static final SQL.Statement getAccessibleByUserByType = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType = ?");
private static final SQL.Statement getAllSchematicsAccessibleByUser = new SQL.Statement("WITH RECURSIVE RSN as (SELECT s.NodeId, s.NodeName, s.NodeOwner, s.NodeItem, s.NodeType, s.ParentNode, s.NodeRank, s.NodeFormat, s.LastUpdate FROM SchematicNode s LEFT JOIN NodeMember n ON s.NodeId = n.NodeId WHERE (s.NodeOwner = ? OR n.UserId = ?) GROUP BY s.NodeId union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.NodeItem, SN.NodeType, SN.ParentNode, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode AS SN, RSN WHERE SN.ParentNode = RSN.NodeId) SELECT * FROM RSN WHERE NodeType is not null");
private static final SQL.Statement isSchematicAccessibleForUser = new SQL.Statement("WITH RECURSIVE RSN AS (SELECT NodeId, NodeName, NodeOwner, ParentNode, NodeType, NodeItem, NodeRank, NodeFormat, LastUpdate FROM SchematicNode WHERE NodeId = ? union select SN.NodeId, SN.NodeName, SN.NodeOwner, SN.ParentNode, SN.NodeType, SN.NodeItem, SN.NodeRank, SN.NodeFormat, SN.LastUpdate FROM SchematicNode SN, RSN WHERE RSN.ParentNode = SN.NodeId) SELECT COUNT(RSN.NodeId) AS `Accessible` FROM RSN Join NodeMember NM On NM.NodeId = RSN.NodeId WHERE NodeOwner = ? OR UserId = ? LIMIT 1");
private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode"); private static final SQL.Statement countNodes = new SQL.Statement("SELECT COUNT(NodeId) AS 'count' FROM SchematicNode");
private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?"); private static final SQL.Statement updateDB = new SQL.Statement("UPDATE SchematicNode SET NodeName = ?, NodeOwner = ?, ParentNode = ?, NodeItem = ?, NodeType = ?, NodeRank = ? WHERE NodeId = ?");
private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?"); private static final SQL.Statement updateDatabase = new SQL.Statement("UPDATE SchematicNode SET NodeData = ?, NodeFormat = ? WHERE NodeId = ?");
@ -182,18 +188,29 @@ public class SchematicNode {
}); });
} }
public static List<SchematicNode> getSchematicsOfType(int owner, String schemType, Integer parent) { public static List<SchematicNode> getAccessibleSchematicsOfTypeInParent(int owner, String schemType, Integer parent) {
List<SchematicNode> schems = getAllSchematicsAccessibleByUser(owner); SQL.Statement.ResultSetUser<List<SchematicNode>> user = rs -> {
schems.removeIf(node -> node.isDir() || !node.getType().equals(schemType)); List<SchematicNode> nodes = new ArrayList<>();
Map<Integer, SchematicNode> nodesInParent = new LinkedHashMap<>(); while (rs.next()) {
for (SchematicNode schematicNode : schems) { nodes.add(new SchematicNode(rs));
SchematicNode currentNode = schematicNode;
while (currentNode.getParent() != parent) {
currentNode = currentNode.getParentNode();
} }
nodesInParent.putIfAbsent(currentNode.getId(), currentNode); return nodes;
};
if(parent == null || parent == 0) {
return getAccessibleByUserByTypeInNode_Null.select(user, owner, owner, schemType);
} else {
return getAccessibleByUserByTypeInNode.select(user, owner, owner, parent, schemType);
} }
return new ArrayList<>(nodesInParent.values()); }
public static List<SchematicNode> getAllAccessibleSchematicsOfType(int user, String schemType) {
return getAccessibleByUserByType.select(rs -> {
List<SchematicNode> nodes = new ArrayList<>();
while (rs.next()) {
nodes.add(new SchematicNode(rs));
}
return nodes;
}, user, user, schemType);
} }
public static List<SchematicNode> getAllSchematicsOfType(int owner, String schemType) { public static List<SchematicNode> getAllSchematicsOfType(int owner, String schemType) {
@ -205,7 +222,6 @@ public class SchematicNode {
}, owner, schemType); }, owner, schemType);
} }
public static List<SchematicNode> getAllSchematicsOfType(String schemType) { public static List<SchematicNode> getAllSchematicsOfType(String schemType) {
return getAllSchemsOfType.select(rs -> { return getAllSchemsOfType.select(rs -> {
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
@ -231,18 +247,10 @@ public class SchematicNode {
public static List<SchematicNode> getSchematicsAccessibleByUser(int user, Integer parent) { public static List<SchematicNode> getSchematicsAccessibleByUser(int user, Integer parent) {
if (parent != null && parent != 0) { if (parent != null && parent != 0) {
SchematicNode node = SchematicNode.getSchematicNode(parent); if(isSchematicAccessibleForUser.select(rs -> {
boolean isAdded = false; rs.next();
while (node.getId() != 0) { return rs.getInt("Accessible") > 0;
for (NodeMember member:node.getMembers()) { }, parent, user, user))
if (member.getMember() == user) {
isAdded = true;
break;
}
}
node = SchematicNode.getSchematicNode(node.getParent());
}
if(isAdded)
return getSchematicNodeInNode(parent); return getSchematicNodeInNode(parent);
} else { } else {
return getAccessibleByUser.select(rs -> { return getAccessibleByUser.select(rs -> {
@ -256,15 +264,10 @@ public class SchematicNode {
} }
public static List<SchematicNode> getAllSchematicsAccessibleByUser(int user) { public static List<SchematicNode> getAllSchematicsAccessibleByUser(int user) {
return getAccessibleByUser.select(rs -> { return getAllSchematicsAccessibleByUser.select(rs -> {
List<SchematicNode> nodes = new ArrayList<>(); List<SchematicNode> nodes = new ArrayList<>();
while(rs.next()) { while(rs.next()) {
SchematicNode node = new SchematicNode(rs); nodes.add(new SchematicNode(rs));
if(node.isDir()) {
nodes.addAll(deepGet(node.getId(), node1 -> true));
} else {
nodes.add(node);
}
} }
return nodes; return nodes;
}, user, user); }, user, user);