Skip to content

Commit

Permalink
[Gc8EZpDO] Clean up some usages of getId() which is deprecated (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
gem-neo4j authored Oct 2, 2023
1 parent be11e34 commit b762a88
Show file tree
Hide file tree
Showing 15 changed files with 550 additions and 268 deletions.
6 changes: 3 additions & 3 deletions common/src/main/java/apoc/get/Get.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
import apoc.result.NodeResult;
import apoc.result.RelationshipResult;
import apoc.util.Util;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.procedure.Name;

import java.util.stream.Stream;

public class Get {

public Transaction tx;
public InternalTransaction tx;

public Get(Transaction tx) {
public Get(InternalTransaction tx) {
this.tx = tx;
}

Expand Down
27 changes: 17 additions & 10 deletions common/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,31 @@ public static Stream<Object> stream(Object values) {
return ConvertUtils.convertToList(values).stream();
}

public static Stream<Node> nodeStream(Transaction tx, Object ids) {
public static Stream<Node> nodeStream(InternalTransaction tx, Object ids) {
return stream(ids).map(id -> node(tx, id));
}

public static Node node(Transaction tx, Object id) {
if (id instanceof Node) return rebind(tx, (Node)id);
if (id instanceof Number) return tx.getNodeById(((Number)id).longValue());
throw new RuntimeException("Can't convert "+id.getClass()+" to a Node");
public static List<Node> nodeList(InternalTransaction tx, Object ids) {
if (ids == null) return List.of();
return stream(ids).map(id -> node(tx, id)).toList();
}

public static Stream<Relationship> relsStream(Transaction tx, Object ids) {
public static Node node(InternalTransaction tx, Object id) {
if (id instanceof Node node) return rebind(tx, node);
if (id instanceof Number nodeId) return tx.getNodeByElementId(tx.elementIdMapper().nodeElementId(nodeId.longValue()));
if (id instanceof String elementId) return tx.getNodeByElementId(elementId);
throw new RuntimeException("Can't convert " + id.getClass() + " to a Node");
}

public static Stream<Relationship> relsStream(InternalTransaction tx, Object ids) {
return stream(ids).map(id -> relationship(tx, id));
}

public static Relationship relationship(Transaction tx, Object id) {
if (id instanceof Relationship) return rebind(tx, (Relationship)id);
if (id instanceof Number) return tx.getRelationshipById(((Number)id).longValue());
throw new RuntimeException("Can't convert "+id.getClass()+" to a Relationship");
public static Relationship relationship(InternalTransaction tx, Object id) {
if (id instanceof Relationship rel) return rebind(tx, rel);
if (id instanceof Number relId) return tx.getRelationshipByElementId(tx.elementIdMapper().relationshipElementId(relId.longValue()));
if (id instanceof String elementId) return tx.getRelationshipByElementId(elementId);
throw new RuntimeException("Can't convert " + id.getClass() + " to a Relationship");
}

public static <T> T retryInTx(Log log, GraphDatabaseService db, Function<Transaction, T> function, long retry, long maxRetries, Consumer<Long> callbackForRetry) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/apoc/algo/Cover.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
Expand All @@ -44,7 +45,7 @@ public class Cover {
@Procedure("apoc.algo.cover")
@Description("Returns all `RELATIONSHIP` values connecting the given set of `NODE` values.")
public Stream<RelationshipResult> cover(@Name("nodes") Object nodes) {
Set<Node> nodeSet = Util.nodeStream(tx, nodes).collect(Collectors.toSet());
Set<Node> nodeSet = Util.nodeStream((InternalTransaction) tx, nodes).collect(Collectors.toSet());
return coverNodes(nodeSet).map(RelationshipResult::new);
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/coll/Coll.java
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,9 @@ public static int compare(Object o1, Object o2) {
return Double.compare(((Number) o1).doubleValue(), ((Number) o2).doubleValue());
return Long.compare(((Number) o1).longValue(), ((Number) o2).longValue());
}
if (o1 instanceof Boolean && o2 instanceof Boolean) return ((Boolean) o1) ? 1 : -1;
if (o1 instanceof Node && o2 instanceof Node) return Long.compare(((Node)o1).getId(),((Node)o2).getId());
if (o1 instanceof Relationship && o2 instanceof Relationship) return Long.compare(((Relationship)o1).getId(),((Relationship)o2).getId());
if (o1 instanceof Boolean b1 && o2 instanceof Boolean) return b1 ? 1 : -1;
if (o1 instanceof Node n1 && o2 instanceof Node n2) return n1.getElementId().compareTo(n2.getElementId());
if (o1 instanceof Relationship rel1 && o2 instanceof Relationship rel2) return rel1.getElementId().compareTo(rel2.getElementId());
return o1.toString().compareTo(o2.toString());
}

Expand Down
19 changes: 10 additions & 9 deletions core/src/main/java/apoc/create/Create.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import apoc.util.collection.Iterables;
import apoc.uuid.UuidUtil;
import org.neo4j.graphdb.*;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.procedure.*;

import java.util.HashMap;
Expand Down Expand Up @@ -53,7 +54,7 @@ public Stream<NodeResult> node(@Name("labels") List<String> labelNames, @Name("p
@Description("Adds the given labels to the given `NODE` values.")
public Stream<NodeResult> addLabels(@Name("nodes") Object nodes, @Name("labels") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : labels) {
node.addLabel(label);
Expand All @@ -65,7 +66,7 @@ public Stream<NodeResult> addLabels(@Name("nodes") Object nodes, @Name("labels")
@Procedure(name = "apoc.create.setProperty", mode = Mode.WRITE)
@Description("Sets the given property to the given `NODE` values.")
public Stream<NodeResult> setProperty(@Name("nodes") Object nodes, @Name("key") String key, @Name("value") Object value) {
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
setProperty(r.node, key,toPropertyValue(value));
return r;
});
Expand All @@ -74,7 +75,7 @@ public Stream<NodeResult> setProperty(@Name("nodes") Object nodes, @Name("key")
@Procedure(name = "apoc.create.setRelProperty", mode = Mode.WRITE)
@Description("Sets the given property on the `RELATIONSHIP` values.")
public Stream<RelationshipResult> setRelProperty(@Name("rels") Object rels, @Name("key") String key, @Name("value") Object value) {
return new Get(tx).rels(rels).map((r) -> {
return new Get((InternalTransaction) tx).rels(rels).map((r) -> {
setProperty(r.rel,key,toPropertyValue(value));
return r;
});
Expand All @@ -83,7 +84,7 @@ public Stream<RelationshipResult> setRelProperty(@Name("rels") Object rels, @Nam
@Procedure(name = "apoc.create.setProperties", mode = Mode.WRITE)
@Description("Sets the given properties to the given `NODE` values.")
public Stream<NodeResult> setProperties(@Name("nodes") Object nodes, @Name("keys") List<String> keys, @Name("values") List<Object> values) {
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
setProperties(r.node, Util.mapFromLists(keys,values));
return r;
});
Expand All @@ -92,7 +93,7 @@ public Stream<NodeResult> setProperties(@Name("nodes") Object nodes, @Name("keys
@Procedure(name = "apoc.create.removeProperties", mode = Mode.WRITE)
@Description("Removes the given properties from the given `NODE` values.")
public Stream<NodeResult> removeProperties(@Name("nodes") Object nodes, @Name("keys") List<String> keys) {
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
keys.forEach( r.node::removeProperty );
return r;
});
Expand All @@ -101,7 +102,7 @@ public Stream<NodeResult> removeProperties(@Name("nodes") Object nodes, @Name("k
@Procedure(name = "apoc.create.setRelProperties", mode = Mode.WRITE)
@Description("Sets the given properties on the `RELATIONSHIP` values.")
public Stream<RelationshipResult> setRelProperties(@Name("rels") Object rels, @Name("keys") List<String> keys, @Name("values") List<Object> values) {
return new Get(tx).rels(rels).map((r) -> {
return new Get((InternalTransaction) tx).rels(rels).map((r) -> {
setProperties(r.rel, Util.mapFromLists(keys,values));
return r;
});
Expand All @@ -110,7 +111,7 @@ public Stream<RelationshipResult> setRelProperties(@Name("rels") Object rels, @N
@Procedure(name = "apoc.create.removeRelProperties", mode = Mode.WRITE)
@Description("Removes the given properties from the given `RELATIONSHIP` values.")
public Stream<RelationshipResult> removeRelProperties(@Name("rels") Object rels, @Name("keys") List<String> keys) {
return new Get(tx).rels(rels).map((r) -> {
return new Get((InternalTransaction) tx).rels(rels).map((r) -> {
keys.forEach( r.rel::removeProperty);
return r;
});
Expand All @@ -120,7 +121,7 @@ public Stream<RelationshipResult> removeRelProperties(@Name("rels") Object rels,
@Description("Sets the given labels to the given `NODE` values. Non-matching labels are removed from the nodes.")
public Stream<NodeResult> setLabels(@Name("nodes") Object nodes, @Name("labels") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : node.getLabels()) {
if (labelNames.contains(label.name())) continue;
Expand All @@ -138,7 +139,7 @@ public Stream<NodeResult> setLabels(@Name("nodes") Object nodes, @Name("labels")
@Description("Removes the given labels from the given `NODE` values.")
public Stream<NodeResult> removeLabels(@Name("nodes") Object nodes, @Name("labels") List<String> labelNames) {
Label[] labels = Util.labels(labelNames);
return new Get(tx).nodes(nodes).map((r) -> {
return new Get((InternalTransaction) tx).nodes(nodes).map((r) -> {
Node node = r.node;
for (Label label : labels) {
node.removeLabel(label);
Expand Down
Loading

0 comments on commit b762a88

Please sign in to comment.