Skip to content

Commit

Permalink
[NOID] fixes tests and format-checks
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Nov 27, 2024
1 parent cae5f13 commit 58ba38c
Show file tree
Hide file tree
Showing 24 changed files with 1,398 additions and 1,186 deletions.
264 changes: 152 additions & 112 deletions full-it/src/test/java/apoc/full/it/vectordb/ChromaDbTest.java

Large diffs are not rendered by default.

304 changes: 169 additions & 135 deletions full-it/src/test/java/apoc/full/it/vectordb/QdrantTest.java

Large diffs are not rendered by default.

379 changes: 224 additions & 155 deletions full-it/src/test/java/apoc/full/it/vectordb/WeaviateTest.java

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions full/src/main/java/apoc/diff/DiffExtended.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package apoc.diff;


import apoc.Extended;
import apoc.util.Util;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;
import org.neo4j.procedure.Context;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Extended
public class DiffExtended {
Expand All @@ -23,7 +21,8 @@ public class DiffExtended {

@UserFunction("apoc.diff.relationships")
@Description("Returns a Map detailing the property differences between the two given relationships")
public Map<String, Object> relationships(@Name("leftRel") Relationship leftRel, @Name("rightRel") Relationship rightRel) {
public Map<String, Object> relationships(
@Name("leftRel") Relationship leftRel, @Name("rightRel") Relationship rightRel) {
leftRel = Util.rebind(tx, leftRel);
rightRel = Util.rebind(tx, rightRel);
Map<String, Object> allLeftProperties = leftRel.getAllProperties();
Expand Down
63 changes: 33 additions & 30 deletions full/src/main/java/apoc/graph/GraphsExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import apoc.result.GraphResult;
import apoc.result.VirtualNode;
import apoc.result.VirtualRelationship;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.neo4j.driver.internal.util.Iterables;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
Expand All @@ -17,13 +23,6 @@
import org.neo4j.procedure.UserAggregationResult;
import org.neo4j.procedure.UserAggregationUpdate;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@Extended
public class GraphsExtended {

Expand All @@ -32,15 +31,17 @@ public class GraphsExtended {
"CALL apoc.graph.filterProperties(anyEntityObject, nodePropertiesToRemove, relPropertiesToRemove) YIELD nodes, relationships - returns a set of virtual nodes and relationships without the properties defined in nodePropertiesToRemove and relPropertiesToRemove")
public Stream<GraphResult> fromData(
@Name("value") Object value,
@Name(value = "nodePropertiesToRemove", defaultValue = "{}") Map<String, List<String>> nodePropertiesToRemove,
@Name(value = "relPropertiesToRemove", defaultValue = "{}") Map<String, List<String>> relPropertiesToRemove) {

@Name(value = "nodePropertiesToRemove", defaultValue = "{}")
Map<String, List<String>> nodePropertiesToRemove,
@Name(value = "relPropertiesToRemove", defaultValue = "{}")
Map<String, List<String>> relPropertiesToRemove) {

VirtualGraphExtractor extractor = new VirtualGraphExtractor(nodePropertiesToRemove, relPropertiesToRemove);
extractor.extract(value);
GraphResult result = new GraphResult( extractor.nodes(), extractor.rels() );
GraphResult result = new GraphResult(extractor.nodes(), extractor.rels());
return Stream.of(result);
}

@UserAggregationFunction("apoc.graph.filterProperties")
@Description(
"apoc.graph.filterProperties(anyEntityObject, nodePropertiesToRemove, relPropertiesToRemove) - aggregation function which returns an object {node: [virtual nodes], relationships: [virtual relationships]} without the properties defined in nodePropertiesToRemove and relPropertiesToRemove")
Expand All @@ -57,9 +58,11 @@ public static class GraphFunction {
@UserAggregationUpdate
public void filterProperties(
@Name("value") Object value,
@Name(value = "nodePropertiesToRemove", defaultValue = "{}") Map<String, List<String>> nodePropertiesToRemove,
@Name(value = "relPropertiesToRemove", defaultValue = "{}") Map<String, List<String>> relPropertiesToRemove) {

@Name(value = "nodePropertiesToRemove", defaultValue = "{}")
Map<String, List<String>> nodePropertiesToRemove,
@Name(value = "relPropertiesToRemove", defaultValue = "{}")
Map<String, List<String>> relPropertiesToRemove) {

if (virtualGraphExtractor == null) {
virtualGraphExtractor = new VirtualGraphExtractor(nodePropertiesToRemove, relPropertiesToRemove);
}
Expand All @@ -72,20 +75,20 @@ public Object result() {
Collection<Relationship> relationships = virtualGraphExtractor.rels();
return Map.of(
NODES, nodes,
RELATIONSHIPS, relationships
);
RELATIONSHIPS, relationships);
}
}

public static class VirtualGraphExtractor {
private static final String ALL_FILTER = "_all";

private final Map<Long, Node> nodes;
private final Map<Long, Relationship> rels;
private final Map<String, List<String>> nodePropertiesToRemove;
private final Map<String, List<String>> relPropertiesToRemove;

public VirtualGraphExtractor(Map<String, List<String>> nodePropertiesToRemove, Map<String, List<String>> relPropertiesToRemove) {
public VirtualGraphExtractor(
Map<String, List<String>> nodePropertiesToRemove, Map<String, List<String>> relPropertiesToRemove) {
this.nodes = new HashMap<>();
this.rels = new HashMap<>();
this.nodePropertiesToRemove = nodePropertiesToRemove;
Expand All @@ -99,26 +102,26 @@ public void extract(Object value) {
if (value instanceof Node) {
Node node = (Node) value;
addVirtualNode(node);

} else if (value instanceof Relationship) {
Relationship rel = (Relationship) value;
addVirtualRel(rel);

} else if (value instanceof Path) {
Path path = (Path) value;
path.nodes().forEach(this::addVirtualNode);
path.relationships().forEach(this::addVirtualRel);

} else if (value instanceof Iterable) {
((Iterable<?>) value).forEach(this::extract);

} else if (value instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) value;
map.values().forEach(this::extract);

} else if (value instanceof Iterator) {
((Iterator<?>) value).forEachRemaining(this::extract);

} else if (value instanceof Object[]) {
Object[] array = (Object[]) value;
for (Object i : array) {
Expand All @@ -128,7 +131,7 @@ public void extract(Object value) {
}

/**
* We can use the elementId as a unique key for virtual nodes/relations,
* We can use the elementId as a unique key for virtual nodes/relations,
* as it is the same as the analogue for real nodes/relations.
*/
private void addVirtualRel(Relationship rel) {
Expand All @@ -141,7 +144,7 @@ private void addVirtualNode(Node node) {

private Node createVirtualNode(Node startNode) {
List<String> props = Iterables.asList(startNode.getPropertyKeys());
nodePropertiesToRemove.forEach((k,v) -> {
nodePropertiesToRemove.forEach((k, v) -> {
if (k.equals(ALL_FILTER) || startNode.hasLabel(Label.label(k))) {
props.removeAll(v);
}
Expand All @@ -156,10 +159,10 @@ private Relationship createVirtualRel(Relationship rel) {

Node endNode = rel.getEndNode();
endNode = nodes.putIfAbsent(endNode.getId(), createVirtualNode(endNode));

Map<String, Object> props = rel.getAllProperties();
relPropertiesToRemove.forEach((k,v) -> {

relPropertiesToRemove.forEach((k, v) -> {
if (k.equals(ALL_FILTER) || rel.isType(RelationshipType.withName(k))) {
v.forEach(props.keySet()::remove);
}
Expand Down
34 changes: 16 additions & 18 deletions full/src/main/java/apoc/map/MapsExtended.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

import apoc.Extended;
import apoc.util.Util;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
import org.neo4j.procedure.UserFunction;

@Extended
public class MapsExtended {

@UserFunction("apoc.map.renameKey")
@Description("Rename the given key(s) in the `MAP`.")
public Map<String, Object> renameKeyRecursively(@Name("map") Map<String, Object> map,
@Name("keyFrom") String keyFrom,
@Name("keyTo") String keyTo,
@Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
public Map<String, Object> renameKeyRecursively(
@Name("map") Map<String, Object> map,
@Name("keyFrom") String keyFrom,
@Name("keyTo") String keyTo,
@Name(value = "config", defaultValue = "{}") Map<String, Object> config) {
boolean recursive = Util.toBoolean(config.getOrDefault("recursive", true));
if (recursive) {
return (Map<String, Object>) renameKeyRecursively(map, keyFrom, keyTo);
Expand All @@ -32,15 +32,14 @@ public Map<String, Object> renameKeyRecursively(@Name("map") Map<String, Object>

private Object renameKeyRecursively(Object object, String keyFrom, String keyTo) {
if (object instanceof Map<?, ?>) {
return ((Map<String, Object>) object).entrySet()
.stream()
.collect(Collectors.toMap(
e -> {
String key = e.getKey();
return key.equals(keyFrom) ? keyTo : key;
},
e -> renameKeyRecursively(e.getValue(), keyFrom, keyTo))
);
return ((Map<String, Object>) object)
.entrySet().stream()
.collect(Collectors.toMap(
e -> {
String key = e.getKey();
return key.equals(keyFrom) ? keyTo : key;
},
e -> renameKeyRecursively(e.getValue(), keyFrom, keyTo)));
}
if (object instanceof List<?>) {
List<?> subList = (List<?>) object;
Expand All @@ -50,5 +49,4 @@ private Object renameKeyRecursively(Object object, String keyFrom, String keyTo)
}
return object;
}

}
16 changes: 8 additions & 8 deletions full/src/main/java/apoc/ml/RestAPIConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package apoc.ml;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -13,11 +12,11 @@ public class RestAPIConfig {
public static final String JSON_PATH_KEY = "jsonPath";
public static final String BODY_KEY = "body";
public static final String BASE_URL_KEY = "baseUrl";

// used internally to handle multiple endpoints, like in `apoc.vectordb.weaviate.get`
// the config documented is the `endpoint` one
private final String baseUrl;

private Map<String, Object> headers;
private Map body;
private String endpoint;
Expand All @@ -26,7 +25,7 @@ public class RestAPIConfig {
public RestAPIConfig(Map<String, Object> config) {
this(config, Map.of(), Map.of());
}

public RestAPIConfig(Map<String, Object> config, Map additionalHeaders, Map additionalBodies) {
if (config == null) {
config = Collections.emptyMap();
Expand All @@ -42,8 +41,9 @@ public RestAPIConfig(Map<String, Object> config, Map additionalHeaders, Map addi
this.jsonPath = (String) config.get(JSON_PATH_KEY);
this.body = populateBody(config, additionalBodies);
}

private static Map<String, Object> populateHeaders(Map<String, Object> config, Map additionalHeaders, String httpMethod) {

private static Map<String, Object> populateHeaders(
Map<String, Object> config, Map additionalHeaders, String httpMethod) {
Map headerConf = (Map<String, Object>) config.getOrDefault(HEADERS_KEY, new HashMap<>());
headerConf.putIfAbsent("content-type", "application/json");
headerConf.putIfAbsent(METHOD_KEY, httpMethod);
Expand All @@ -55,7 +55,7 @@ private static Map populateBody(Map<String, Object> config, Map additionalBodies
Map bodyConf = (Map<String, Object>) config.getOrDefault(BODY_KEY, new HashMap<>());

// if we force body to be null, e.g. with Http GET operations that doesn't allow payloads,
// we skip additional body addition
// we skip additional body addition
if (bodyConf != null) {
additionalBodies.forEach(bodyConf::putIfAbsent);
}
Expand All @@ -73,7 +73,7 @@ public Map<String, Object> getBody() {
public String getEndpoint() {
return endpoint;
}

public String getBaseUrl() {
return baseUrl;
}
Expand Down
Loading

0 comments on commit 58ba38c

Please sign in to comment.