Skip to content

Commit 53892c0

Browse files
committed
Reduce Common Usage between APOC Extended and APOC Core
1 parent d453d5e commit 53892c0

File tree

88 files changed

+5602
-193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+5602
-193
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[submodule "apoc-core"]
22
path = apoc-core
33
url = https://github.com/neo4j/apoc
4-
branch = dev
4+
branch = dev_reduce_common_reliance

apoc-core

Submodule apoc-core updated 63 files

extended/src/main/java/apoc/algo/PathFindingExtended.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import org.neo4j.procedure.Procedure;
1717

1818
import java.util.stream.Stream;
19-
import static apoc.algo.PathFindingUtils.buildPathExpander;
19+
20+
import static apoc.algo.PathFindingUtilsExtended.buildPathExpander;
2021

2122
@Extended
2223
public class PathFindingExtended {
@@ -41,7 +42,7 @@ public Stream<WeightedPathResult> aStarWithPoint(
4142
new BasicEvaluationContext(tx, db),
4243
buildPathExpander(relTypesAndDirs),
4344
CommonEvaluators.doubleCostEvaluator(weightPropertyName),
44-
new PathFindingUtils.GeoEstimateEvaluatorPointCustom(pointPropertyName));
45+
new PathFindingUtilsExtended.GeoEstimateEvaluatorPointCustom(pointPropertyName));
4546
return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo);
4647
}
4748

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package apoc.algo;
20+
21+
import apoc.path.RelationshipTypeAndDirectionsExtended;
22+
import org.apache.commons.lang3.tuple.Pair;
23+
import org.neo4j.graphalgo.EstimateEvaluator;
24+
import org.neo4j.graphdb.Direction;
25+
import org.neo4j.graphdb.Node;
26+
import org.neo4j.graphdb.PathExpander;
27+
import org.neo4j.graphdb.PathExpanderBuilder;
28+
import org.neo4j.graphdb.RelationshipType;
29+
import org.neo4j.values.storable.PointValue;
30+
31+
public class PathFindingUtilsExtended {
32+
public static class GeoEstimateEvaluatorPointCustom implements EstimateEvaluator<Double> {
33+
// -- from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator
34+
private static final double EARTH_RADIUS = 6371 * 1000; // Meters
35+
private Node cachedGoal;
36+
private final String pointPropertyKey;
37+
private double[] cachedGoalCoordinates;
38+
39+
public GeoEstimateEvaluatorPointCustom(String pointPropertyKey) {
40+
this.pointPropertyKey = pointPropertyKey;
41+
}
42+
43+
@Override
44+
public Double getCost(Node node, Node goal) {
45+
double[] nodeCoordinates = getCoordinates(node);
46+
if (cachedGoal == null || !cachedGoal.equals(goal)) {
47+
cachedGoalCoordinates = getCoordinates(goal);
48+
cachedGoal = goal;
49+
}
50+
return distance(nodeCoordinates[0], nodeCoordinates[1], cachedGoalCoordinates[0], cachedGoalCoordinates[1]);
51+
}
52+
53+
private static double distance(double latitude1, double longitude1, double latitude2, double longitude2) {
54+
latitude1 = Math.toRadians(latitude1);
55+
longitude1 = Math.toRadians(longitude1);
56+
latitude2 = Math.toRadians(latitude2);
57+
longitude2 = Math.toRadians(longitude2);
58+
double cLa1 = Math.cos(latitude1);
59+
double xA = EARTH_RADIUS * cLa1 * Math.cos(longitude1);
60+
double yA = EARTH_RADIUS * cLa1 * Math.sin(longitude1);
61+
double zA = EARTH_RADIUS * Math.sin(latitude1);
62+
double cLa2 = Math.cos(latitude2);
63+
double xB = EARTH_RADIUS * cLa2 * Math.cos(longitude2);
64+
double yB = EARTH_RADIUS * cLa2 * Math.sin(longitude2);
65+
double zB = EARTH_RADIUS * Math.sin(latitude2);
66+
return Math.sqrt((xA - xB) * (xA - xB) + (yA - yB) * (yA - yB) + (zA - zB) * (zA - zB));
67+
}
68+
// -- end from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator
69+
70+
private double[] getCoordinates(Node node) {
71+
return ((PointValue) node.getProperty(pointPropertyKey)).coordinate();
72+
}
73+
}
74+
75+
public static PathExpander<Double> buildPathExpander(String relationshipsAndDirections) {
76+
PathExpanderBuilder builder = PathExpanderBuilder.empty();
77+
for (Pair<RelationshipType, Direction> pair : RelationshipTypeAndDirectionsExtended.parse(relationshipsAndDirections)) {
78+
if (pair.getLeft() == null) {
79+
if (pair.getRight() == null) {
80+
builder = PathExpanderBuilder.allTypesAndDirections();
81+
} else {
82+
builder = PathExpanderBuilder.allTypes(pair.getRight());
83+
}
84+
} else {
85+
if (pair.getRight() == null) {
86+
builder = builder.add(pair.getLeft());
87+
} else {
88+
builder = builder.add(pair.getLeft(), pair.getRight());
89+
}
90+
}
91+
}
92+
return builder.build();
93+
}
94+
}

extended/src/main/java/apoc/coll/CollExtended.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package apoc.coll;
22

33
import apoc.Extended;
4-
import apoc.util.CollectionUtils;
4+
import apoc.util.CollectionUtilsExtended;
55
import org.neo4j.procedure.Description;
66
import org.neo4j.procedure.Name;
77
import org.neo4j.procedure.UserFunction;
@@ -17,7 +17,7 @@ public class CollExtended {
1717
@UserFunction
1818
@Description("apoc.coll.avgDuration([duration('P2DT3H'), duration('PT1H45S'), ...]) - returns the average of a list of duration values")
1919
public DurationValue avgDuration(@Name("durations") List<DurationValue> list) {
20-
if (CollectionUtils.isEmpty(list)) return null;
20+
if (CollectionUtilsExtended.isEmpty(list)) return null;
2121

2222
long count = 0;
2323

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package apoc.coll;
20+
21+
import java.util.AbstractSequentialList;
22+
import java.util.Iterator;
23+
import java.util.ListIterator;
24+
import java.util.NoSuchElementException;
25+
import java.util.Set;
26+
import java.util.Spliterator;
27+
28+
/**
29+
* @author mh
30+
* @since 10.04.16
31+
*/
32+
public class SetBackedListExtended<T> extends AbstractSequentialList<T> implements Set<T> {
33+
34+
private final Set<T> set;
35+
36+
public SetBackedListExtended(Set<T> set) {
37+
this.set = set;
38+
}
39+
40+
@Override
41+
public int size() {
42+
return set.size();
43+
}
44+
45+
public ListIterator<T> listIterator(int index) {
46+
return new ListIterator<T>() {
47+
Iterator<T> it = set.iterator();
48+
T current = null;
49+
int idx = 0;
50+
51+
{
52+
moveTo(index);
53+
}
54+
55+
@Override
56+
public boolean hasNext() {
57+
return it.hasNext();
58+
}
59+
60+
@Override
61+
public T next() {
62+
idx++;
63+
return current = it.next();
64+
}
65+
66+
@Override
67+
public boolean hasPrevious() {
68+
return idx > 0;
69+
}
70+
71+
@Override
72+
public T previous() {
73+
if (!hasPrevious()) throw new NoSuchElementException();
74+
T tmp = current;
75+
moveTo(idx - 1);
76+
return tmp;
77+
}
78+
79+
private void moveTo(int pos) {
80+
Iterator<T> it2 = set.iterator();
81+
T value = null;
82+
int i = 0;
83+
while (i++ < pos) {
84+
value = it2.next();
85+
}
86+
;
87+
this.it = it2;
88+
this.idx = pos;
89+
this.current = value;
90+
}
91+
92+
@Override
93+
public int nextIndex() {
94+
return idx;
95+
}
96+
97+
@Override
98+
public int previousIndex() {
99+
return idx - 1;
100+
}
101+
102+
@Override
103+
public void remove() {
104+
throw new UnsupportedOperationException("remove");
105+
}
106+
107+
@Override
108+
public void set(Object o) {
109+
throw new UnsupportedOperationException("set");
110+
}
111+
112+
@Override
113+
public void add(Object o) {
114+
throw new UnsupportedOperationException("add");
115+
}
116+
};
117+
}
118+
119+
@Override
120+
public boolean contains(Object o) {
121+
return set.contains(o);
122+
}
123+
124+
@Override
125+
public int hashCode() {
126+
return set.hashCode();
127+
}
128+
129+
@Override
130+
public boolean equals(Object o) {
131+
if (o instanceof Set) return set.equals(o);
132+
return o instanceof Iterable && super.equals(o);
133+
}
134+
135+
@Override
136+
public Spliterator<T> spliterator() {
137+
return set.spliterator();
138+
}
139+
}

extended/src/main/java/apoc/convert/ConvertExtended.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package apoc.convert;
22

33
import apoc.Extended;
4-
import apoc.meta.Types;
4+
import apoc.meta.TypesExtended;
55
import apoc.util.collection.Iterables;
66
import org.neo4j.graphdb.Node;
77
import org.neo4j.graphdb.Path;
@@ -49,7 +49,7 @@ public Object fromYaml(@Name("value") String value, @Name(value = "config", defa
4949
* which handle complex types, like list/map of nodes/rels/paths
5050
*/
5151
private Object writeYamlResult(Object value) {
52-
Types type = Types.of(value);
52+
TypesExtended type = TypesExtended.of(value);
5353
return switch (type) {
5454
case NODE -> nodeToMap((Node) value);
5555
case RELATIONSHIP -> relToMap((Relationship) value);

extended/src/main/java/apoc/convert/ConvertExtendedUtil.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import apoc.export.util.DurationValueSerializer;
44
import apoc.export.util.PointSerializer;
55
import apoc.export.util.TemporalSerializer;
6+
import apoc.util.collection.Iterables;
7+
import apoc.util.collection.Iterators;
68
import com.fasterxml.jackson.core.JsonProcessingException;
79
import com.fasterxml.jackson.databind.ObjectMapper;
810
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -11,7 +13,12 @@
1113
import org.neo4j.graphdb.spatial.Point;
1214
import org.neo4j.values.storable.DurationValue;
1315

16+
import java.lang.reflect.Array;
1417
import java.time.temporal.Temporal;
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.Collections;
21+
import java.util.Iterator;
1522
import java.util.List;
1623
import java.util.Map;
1724

@@ -26,6 +33,34 @@ public class ConvertExtendedUtil {
2633
YAML_MODULE.addSerializer(DurationValue.class, new DurationValueSerializer());
2734
}
2835

36+
public static List convertToList(Object list) {
37+
if (list == null) return null;
38+
else if (list instanceof List) return (List) list;
39+
else if (list instanceof Collection) return new ArrayList((Collection) list);
40+
else if (list instanceof Iterable) return Iterables.asList((Iterable) list);
41+
else if (list instanceof Iterator) return Iterators.asList((Iterator) list);
42+
else if (list.getClass().isArray()) {
43+
return convertArrayToList(list);
44+
}
45+
return Collections.singletonList(list);
46+
}
47+
48+
public static List convertArrayToList(Object list) {
49+
final Object[] objectArray;
50+
if (list.getClass().getComponentType().isPrimitive()) {
51+
int length = Array.getLength(list);
52+
objectArray = new Object[length];
53+
for (int i = 0; i < length; i++) {
54+
objectArray[i] = Array.get(list, i);
55+
}
56+
} else {
57+
objectArray = (Object[]) list;
58+
}
59+
List result = new ArrayList<>(objectArray.length);
60+
Collections.addAll(result, objectArray);
61+
return result;
62+
}
63+
2964
/**
3065
* get YAMLFactory with configured enable and disable values
3166
*/

0 commit comments

Comments
 (0)