Skip to content

Commit

Permalink
Use Pair from apache instead of Neo4j
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Oct 4, 2022
1 parent 2523252 commit 029b29b
Show file tree
Hide file tree
Showing 20 changed files with 72 additions and 157 deletions.
14 changes: 7 additions & 7 deletions common/src/main/java/apoc/algo/PathFindingUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package apoc.algo;

import apoc.path.RelationshipTypeAndDirections;
import apoc.util.collection.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphalgo.EstimateEvaluator;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
Expand Down Expand Up @@ -62,17 +62,17 @@ public static PathExpander<Double> buildPathExpander(String relationshipsAndDire
PathExpanderBuilder builder = PathExpanderBuilder.empty();
for (Pair<RelationshipType, Direction> pair : RelationshipTypeAndDirections
.parse(relationshipsAndDirections)) {
if (pair.first() == null) {
if (pair.other() == null) {
if (pair.getLeft() == null) {
if (pair.getRight() == null) {
builder = PathExpanderBuilder.allTypesAndDirections();
} else {
builder = PathExpanderBuilder.allTypes(pair.other());
builder = PathExpanderBuilder.allTypes(pair.getRight());
}
} else {
if (pair.other() == null) {
builder = builder.add(pair.first());
if (pair.getRight() == null) {
builder = builder.add(pair.getLeft());
} else {
builder = builder.add(pair.first(), pair.other());
builder = builder.add(pair.getLeft(), pair.getRight());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package apoc.path;

import apoc.util.collection.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.RelationshipType;

Expand All @@ -20,8 +20,8 @@ public abstract class RelationshipTypeAndDirections {
public static final char BACKTICK = '`';

public static String format(Pair<RelationshipType, Direction> typeAndDirection) {
String type = typeAndDirection.first().name();
switch (typeAndDirection.other()) {
String type = typeAndDirection.getLeft().name();
switch (typeAndDirection.getRight()) {
case OUTGOING:
return type + ">";
case INCOMING:
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/apoc/periodic/PeriodicUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package apoc.periodic;

import apoc.Pools;
import apoc.util.collection.Pair;
import apoc.util.Util;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.QueryStatistics;
import org.neo4j.graphdb.Transaction;
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/apoc/trigger/TriggerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import apoc.SystemPropertyKeys;
import apoc.util.MapUtil;
import apoc.util.collection.Iterators;
import apoc.util.collection.Pair;
import apoc.util.Util;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.dbms.api.DatabaseManagementService;
import org.neo4j.function.ThrowingFunction;
import org.neo4j.graphdb.GraphDatabaseService;
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import apoc.result.VirtualNode;
import apoc.result.VirtualRelationship;
import apoc.util.collection.Iterators;
import apoc.util.collection.Pair;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.collections.api.iterator.LongIterator;
import org.neo4j.logging.NullLog;
import org.neo4j.values.storable.CoordinateReferenceSystem;
Expand Down Expand Up @@ -963,11 +963,11 @@ public static <T extends Entity> List<T> rebind(List<T> entities, Transaction tx

public static Node mergeNode(Transaction tx, Label primaryLabel, Label addtionalLabel,
Pair<String, Object>... pairs) {
Node node = Iterators.singleOrNull(tx.findNodes(primaryLabel, pairs[0].first(), pairs[0].other()).stream()
Node node = Iterators.singleOrNull(tx.findNodes(primaryLabel, pairs[0].getLeft(), pairs[0].getRight()).stream()
.filter(n -> addtionalLabel == null || n.hasLabel(addtionalLabel))
.filter( n -> {
for (int i=1; i<pairs.length; i++) {
if (!Objects.deepEquals(pairs[i].other(), n.getProperty(pairs[i].first(), null))) {
if (!Objects.deepEquals(pairs[i].getRight(), n.getProperty(pairs[i].getLeft(), null))) {
return false;
}
}
Expand All @@ -980,7 +980,7 @@ public static Node mergeNode(Transaction tx, Label primaryLabel, Label addtional
new Label[]{primaryLabel, addtionalLabel};
node = tx.createNode(labels);
for (int i=0; i<pairs.length; i++) {
node.setProperty(pairs[i].first(), pairs[i].other());
node.setProperty(pairs[i].getLeft(), pairs[i].getRight());
}
}
return node;
Expand Down
85 changes: 0 additions & 85 deletions common/src/main/java/apoc/util/collection/Pair.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package apoc.path;

import apoc.util.collection.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
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
@@ -1,9 +1,9 @@
package apoc.coll;

import apoc.result.ListResult;
import apoc.util.collection.Pair;
import com.google.common.util.concurrent.AtomicDouble;
import org.apache.commons.lang3.mutable.MutableInt;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import org.apache.commons.math3.util.Combinations;
import org.neo4j.graphdb.GraphDatabaseService;
Expand Down Expand Up @@ -853,12 +853,12 @@ public List<Map<String,Object>> sortMulti(@Name("coll") List<Map<String,Object>>
int a = 0;
for (Pair<String, Boolean> s : fields) {
if (a != 0) break;
String name = s.first();
String name = s.getLeft();
Comparable<Object> v1 = o1.get(name);
Comparable<Object> v2 = o2.get(name);
if (v1 != v2) {
int cmp = (v1 == null) ? -1 : (v2 == null) ? 1 : v1.compareTo(v2);
a = (s.other()) ? cmp : -cmp;
a = (s.getRight()) ? cmp : -cmp;
}
}
return a;
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/apoc/meta/Meta.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import apoc.result.VirtualRelationship;
import apoc.util.MapUtil;
import apoc.util.collection.Iterables;
import apoc.util.collection.Pair;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.cypher.export.CypherResultSubGraph;
import org.neo4j.cypher.export.DatabaseSubGraph;
import org.neo4j.cypher.export.SubGraph;
Expand Down Expand Up @@ -261,7 +261,7 @@ public long count(@Name(value = "nodes", defaultValue = "[]") List<String> nodes
})
.flatMap(pair -> transaction.findNodes(label)
.map(node -> {
if (!visitedNodes.contains(node.getId()) && node.hasRelationship(pair.first(), RelationshipType.withName(pair.other()))) {
if (!visitedNodes.contains(node.getId()) && node.hasRelationship(pair.getLeft(), RelationshipType.withName(pair.getRight()))) {
visitedNodes.add(node.getId());
return 1L;
} else {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/java/apoc/neighbors/Neighbors.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import apoc.result.LongResult;
import apoc.result.NodeListResult;
import apoc.result.NodeResult;
import apoc.util.collection.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.*;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
Expand All @@ -26,13 +26,13 @@ public class Neighbors {

private Iterable<Relationship> getRelationshipsByTypeAndDirection(Node node, Pair<RelationshipType, Direction> typesAndDirection) {
// as policy if both elements in the pair are null we return an empty result
if (typesAndDirection.first() == null) {
return typesAndDirection.other() == null ? Collections.emptyList() : node.getRelationships(typesAndDirection.other());
if (typesAndDirection.getLeft() == null) {
return typesAndDirection.getRight() == null ? Collections.emptyList() : node.getRelationships(typesAndDirection.getRight());
}
if (typesAndDirection.other() == null) {
return typesAndDirection.first() == null ? Collections.emptyList() : node.getRelationships(typesAndDirection.first());
if (typesAndDirection.getRight() == null) {
return typesAndDirection.getLeft() == null ? Collections.emptyList() : node.getRelationships(typesAndDirection.getLeft());
}
return node.getRelationships(typesAndDirection.other(), typesAndDirection.first());
return node.getRelationships(typesAndDirection.getRight(), typesAndDirection.getLeft());
}

@Procedure("apoc.neighbors.tohop")
Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/apoc/nodes/Nodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import apoc.result.VirtualPath;
import apoc.result.VirtualPathResult;
import apoc.util.collection.Iterables;
import apoc.util.collection.Pair;
import apoc.util.Util;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphalgo.BasicEvaluationContext;
import org.neo4j.graphalgo.GraphAlgoFactory;
import org.neo4j.graphalgo.PathFinder;
Expand Down Expand Up @@ -183,8 +183,8 @@ public boolean hasRelationship(@Name("node") Node node, @Name(value = "types", d
TokenRead tokenRead = ktx.tokenRead();

for (Pair<RelationshipType, Direction> pair : parse(types)) {
int typeId = tokenRead.relationshipType(pair.first().name());
Direction direction = pair.other();
int typeId = tokenRead.relationshipType(pair.getLeft().name());
Direction direction = pair.getRight();

int count;
switch (direction) {
Expand Down Expand Up @@ -369,12 +369,12 @@ private int[][] typedDirections(TokenRead ops, List<Pair<RelationshipType, Direc
int outIdx = Direction.OUTGOING.ordinal();
int inIdx = Direction.INCOMING.ordinal();
for (Pair<RelationshipType, Direction> pair : pairs) {
int type = ops.relationshipType(pair.first().name());
int type = ops.relationshipType(pair.getLeft().name());
if (type == -1) continue;
if (pair.other() != Direction.INCOMING) {
if (pair.getRight() != Direction.INCOMING) {
result[outIdx][from++]= type;
}
if (pair.other() != Direction.OUTGOING) {
if (pair.getRight() != Direction.OUTGOING) {
result[inIdx][to++]= type;
}
}
Expand Down Expand Up @@ -539,7 +539,7 @@ public long degree(@Name("node") Node node, @Name(value = "types",defaultValue =
if (types==null || types.isEmpty()) return node.getDegree();
long degree = 0;
for (Pair<RelationshipType, Direction> pair : parse(types)) {
degree += getDegreeSafe(node, pair.first(), pair.other());
degree += getDegreeSafe(node, pair.getLeft(), pair.getRight());
}
return degree;
}
Expand Down Expand Up @@ -577,8 +577,8 @@ public List<String> relationshipTypes(@Name("node") Node node, @Name(value = "ty
if (types == null || types.isEmpty()) return relTypes;
List<String> result = new ArrayList<>(relTypes.size());
for (Pair<RelationshipType, Direction> p : parse(types)) {
String name = p.first().name();
if (relTypes.contains(name) && node.hasRelationship(p.other(),p.first())) {
String name = p.getLeft().name();
if (relTypes.contains(name) && node.hasRelationship(p.getRight(),p.getLeft())) {
result.add(name);
}
}
Expand Down Expand Up @@ -611,8 +611,8 @@ public Map<String,Boolean> relationshipExists(@Name("node") Node node, @Name(val
List<String> relTypes = Iterables.stream(node.getRelationshipTypes()).map(RelationshipType::name).toList();
Map<String,Boolean> result = new HashMap<>();
for (Pair<RelationshipType, Direction> p : parse(types)) {
String name = p.first().name();
boolean hasRelationship = relTypes.contains(name) && node.hasRelationship(p.other(), p.first());
String name = p.getLeft().name();
boolean hasRelationship = relTypes.contains(name) && node.hasRelationship(p.getRight(), p.getLeft());
result.put(format(p), hasRelationship);
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import apoc.util.collection.Iterables;
import apoc.util.collection.Iterators;
import apoc.util.collection.NestingResourceIterator;
import apoc.util.collection.Pair;
import apoc.util.collection.ResourceClosingIterator;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Path;
Expand Down Expand Up @@ -93,8 +93,8 @@ public ResourceIterable<Relationship> expand( Path path, BranchState state ) {
return Iterables.asResourceIterable(Iterators.asList(new NestingResourceIterator<>(stepRels.iterator()) {
@Override
protected ResourceIterator<Relationship> createNestedIterator( Pair<RelationshipType,Direction> entry ) {
RelationshipType type = entry.first();
Direction dir = entry.other();
RelationshipType type = entry.getLeft();
Direction dir = entry.getRight();

ResourceIterable<Relationship> relationships1;
if ( type == null )
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/periodic/Periodic.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import apoc.Pools;
import apoc.util.collection.Iterables;
import apoc.util.collection.Iterators;
import apoc.util.collection.Pair;
import apoc.util.Util;
import apoc.periodic.PeriodicUtils.JobInfo;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.Transaction;
Expand Down Expand Up @@ -263,8 +263,8 @@ public Stream<BatchAndTotalResult> iterate(

try (Result result = tx.execute(slottedRuntime(cypherIterate),params)) {
Pair<String,Boolean> prepared = PeriodicUtils.prepareInnerStatement(cypherAction, batchMode, result.columns(), "_batch");
String innerStatement = applyPlanner(prepared.first(), Planner.valueOf((String) config.getOrDefault("planner", Planner.DEFAULT.name())));
boolean iterateList = prepared.other();
String innerStatement = applyPlanner(prepared.getLeft(), Planner.valueOf((String) config.getOrDefault("planner", Planner.DEFAULT.name())));
boolean iterateList = prepared.getRight();
String periodicId = UUID.randomUUID().toString();
if (log.isDebugEnabled()) {
log.debug("Starting periodic iterate from `%s` operation using iteration `%s` in separate thread with id: `%s`", cypherIterate,cypherAction, periodicId);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/refactor/util/RefactorUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package apoc.refactor.util;

import apoc.util.collection.Pair;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.*;

import java.util.List;
Expand Down
Loading

0 comments on commit 029b29b

Please sign in to comment.