From 2a01b3a422fcb77b90a2c1cf55ad15bf5be888d5 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Wed, 16 Oct 2024 15:29:57 -0400 Subject: [PATCH] return existing node if already discovered --- .../io/cryostat/discovery/DiscoveryNode.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/src/main/java/io/cryostat/discovery/DiscoveryNode.java b/src/main/java/io/cryostat/discovery/DiscoveryNode.java index 3446ad51b..6bf524349 100644 --- a/src/main/java/io/cryostat/discovery/DiscoveryNode.java +++ b/src/main/java/io/cryostat/discovery/DiscoveryNode.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.annotation.JsonView; import io.quarkus.hibernate.orm.panache.PanacheEntity; import io.quarkus.narayana.jta.QuarkusTransaction; +import io.quarkus.panache.common.Parameters; import io.vertx.mutiny.core.eventbus.EventBus; import jakarta.annotation.Nullable; import jakarta.enterprise.context.ApplicationScoped; @@ -42,6 +43,8 @@ import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; +import jakarta.persistence.NamedQueries; +import jakarta.persistence.NamedQuery; import jakarta.persistence.OneToMany; import jakarta.persistence.OneToOne; import jakarta.persistence.PostPersist; @@ -56,6 +59,11 @@ @Entity @EntityListeners(DiscoveryNode.Listener.class) +@NamedQueries({ + @NamedQuery( + name = "DiscoveryNode.byTypeWithName", + query = "from DiscoveryNode where nodeType = :nodeType and name = :name") +}) public class DiscoveryNode extends PanacheEntity { public static final String NODE_TYPE = "nodeType"; @@ -129,33 +137,48 @@ public static List findAllByNodeType(NodeType nodeType) { } public static DiscoveryNode environment(String name, NodeType nodeType) { - return QuarkusTransaction.joiningExisting() - .call( - () -> { - DiscoveryNode node = new DiscoveryNode(); - node.name = name; - node.nodeType = nodeType.getKind(); - node.labels = new HashMap<>(); - node.children = new ArrayList<>(); - node.target = null; - node.persist(); - return node; - }); + var kind = nodeType.getKind(); + return DiscoveryNode.find( + "#DiscoveryNode.byTypeWithName", + Parameters.with("nodeType", kind).and("name", name)) + .firstResultOptional() + .orElseGet( + () -> + QuarkusTransaction.joiningExisting() + .call( + () -> { + DiscoveryNode node = new DiscoveryNode(); + node.name = name; + node.nodeType = kind; + node.labels = new HashMap<>(); + node.children = new ArrayList<>(); + node.target = null; + node.persist(); + return node; + })); } public static DiscoveryNode target(Target target, NodeType nodeType) { - return QuarkusTransaction.joiningExisting() - .call( - () -> { - DiscoveryNode node = new DiscoveryNode(); - node.name = target.connectUrl.toString(); - node.nodeType = nodeType.getKind(); - node.labels = new HashMap<>(target.labels); - node.children = null; - node.target = target; - node.persist(); - return node; - }); + var kind = nodeType.getKind(); + var connectUrl = target.connectUrl.toString(); + return DiscoveryNode.find( + "#DiscoveryNode.byTypeWithName", + Parameters.with("nodeType", kind).and("name", connectUrl)) + .firstResultOptional() + .orElseGet( + () -> + QuarkusTransaction.joiningExisting() + .call( + () -> { + DiscoveryNode node = new DiscoveryNode(); + node.name = connectUrl; + node.nodeType = kind; + node.labels = new HashMap<>(target.labels); + node.children = null; + node.target = target; + node.persist(); + return node; + })); } @Override