From ac6707d1a1551353f91a9c93356934901984bfcc Mon Sep 17 00:00:00 2001 From: Gemma Lamont Date: Mon, 16 Dec 2024 15:45:24 +0100 Subject: [PATCH] Add Extended versions of common classes removed from common --- .gitmodules | 2 +- .../java/apoc/algo/PathFindingExtended.java | 5 +- .../apoc/algo/PathFindingUtilsExtended.java | 94 +++ .../src/main/java/apoc/coll/CollExtended.java | 4 +- .../java/apoc/coll/SetBackedListExtended.java | 139 ++++ .../java/apoc/convert/ConvertExtended.java | 4 +- .../apoc/convert/ConvertExtendedUtil.java | 35 + .../main/java/apoc/cypher/CypherExtended.java | 18 +- .../java/apoc/cypher/CypherUtilsExtended.java | 50 ++ .../export/CypherResultSubGraphExtended.java | 274 +++++++ .../export/DatabaseSubGraphExtended.java | 167 +++++ .../java/apoc/date/DateUtilsExtended.java | 55 ++ .../java/apoc/entities/EntitiesExtended.java | 4 +- .../src/main/java/apoc/es/ElasticSearch.java | 4 +- .../export/arrow/ExportArrowExtended.java | 24 +- .../export/arrow/ExportArrowFileStrategy.java | 12 +- .../apoc/export/arrow/ExportArrowService.java | 4 +- .../export/arrow/ExportArrowStrategy.java | 6 +- .../export/arrow/ExportGraphFileStrategy.java | 4 +- .../export/arrow/ExportResultStrategy.java | 4 +- .../java/apoc/export/arrow/ImportArrow.java | 14 +- .../cypher/FileManagerFactoryExtended.java | 155 ++++ .../AbstractCypherFormatterExtended.java | 680 ++++++++++++++++++ .../AddStructureCypherFormatterExtended.java | 109 +++ .../CreateCypherFormatterExtended.java | 103 +++ .../formatter/CypherFormatExtended.java | 74 ++ .../formatter/CypherFormatterExtended.java | 87 +++ .../CypherFormatterUtilsExtended.java | 318 ++++++++ .../UpdateAllCypherFormatterExtended.java | 80 +++ ...pdateStructureCypherFormatterExtended.java | 106 +++ .../apoc/export/parquet/ExportParquet.java | 28 +- .../parquet/ExportParquetFileStrategy.java | 20 +- .../ExportParquetGraphFileStrategy.java | 4 +- .../ExportParquetResultFileStrategy.java | 4 +- .../parquet/ExportParquetStreamStrategy.java | 12 +- .../apoc/export/parquet/ImportParquet.java | 14 +- .../export/parquet/ParquetExportType.java | 4 +- .../java/apoc/export/parquet/ParquetUtil.java | 6 +- .../export/util/BatchTransactionExtended.java | 78 ++ .../export/util/ExportConfigExtended.java | 306 ++++++++ .../apoc/export/util/FormatUtilsExtended.java | 151 ++++ .../util/NodesAndRelsSubGraphExtended.java | 182 +++++ .../main/java/apoc/export/xls/ExportXls.java | 22 +- .../apoc/export/xls/ExportXlsHandler.java | 8 +- extended/src/main/java/apoc/gephi/Gephi.java | 6 +- .../src/main/java/apoc/get/GetExtended.java | 59 ++ .../src/main/java/apoc/get/GetProcedures.java | 4 +- .../builder/DocumentToGraphExtended.java | 436 +++++++++++ .../builder/LabelBuilderExtended.java | 51 ++ .../builder/RelationshipBuilderExtended.java | 62 ++ .../java/apoc/load/CSVResultExtended.java | 103 +++ extended/src/main/java/apoc/load/Gexf.java | 10 +- extended/src/main/java/apoc/load/LoadCsv.java | 22 +- .../main/java/apoc/load/LoadJsonExtended.java | 2 +- .../java/apoc/load/LoadJsonUtilsExtended.java | 74 ++ .../main/java/apoc/load/MappingExtended.java | 132 ++++ .../java/apoc/load/util/LoadCsvConfig.java | 12 +- .../main/java/apoc/load/util/XmlReadUtil.java | 4 +- .../src/main/java/apoc/load/xls/LoadXls.java | 8 +- .../main/java/apoc/meta/TypesExtended.java | 194 +++++ .../src/main/java/apoc/metrics/Metrics.java | 6 +- ...RelationshipTypeAndDirectionsExtended.java | 77 ++ .../result/ExportProgressInfoExtended.java | 137 ++++ .../result/ImportProgressInfoExtended.java | 133 ++++ .../java/apoc/result/KernelInfoResult.java | 2 +- .../src/main/java/apoc/systemdb/SystemDb.java | 12 +- .../java/apoc/trigger/TriggerExtended.java | 6 +- extended/src/main/java/apoc/ttl/TTL.java | 2 +- .../apoc/util/CollectionUtilsExtended.java | 72 ++ .../java/apoc/util/DateParseUtilExtended.java | 93 +++ .../java/apoc/util/EntityUtilExtended.java | 55 ++ .../src/main/java/apoc/util/ExtendedUtil.java | 17 +- .../util/FixedSizeStringWriterExtended.java | 67 ++ .../util/QueueBasedSpliteratorExtended.java | 71 ++ .../java/apoc/util/QueueUtilExtended.java | 89 +++ .../src/main/java/apoc/uuid/UuidHandler.java | 2 +- .../main/java/apoc/uuid/UuidUtilExtended.java | 79 ++ .../src/main/java/apoc/vectordb/ChromaDb.java | 10 +- .../main/java/apoc/vectordb/VectorDbUtil.java | 4 +- .../src/main/java/apoc/vectordb/Weaviate.java | 4 +- .../export/DatabaseSubGraphExtended.java | 166 +++++ .../apoc/nlp/aws/AWSVirtualEntitiesGraph.kt | 6 +- .../apoc/nlp/aws/AWSVirtualKeyPhrasesGraph.kt | 6 +- .../azure/azure/AzureVirtualEntitiesGraph.kt | 6 +- .../azure/AzureVirtualKeyPhrasesGraph.kt | 6 +- .../nlp/gcp/GCPVirtualClassificationGraph.kt | 6 +- .../apoc/nlp/gcp/GCPVirtualEntitiesGraph.kt | 6 +- 87 files changed, 5601 insertions(+), 192 deletions(-) create mode 100644 extended/src/main/java/apoc/algo/PathFindingUtilsExtended.java create mode 100644 extended/src/main/java/apoc/coll/SetBackedListExtended.java create mode 100644 extended/src/main/java/apoc/cypher/CypherUtilsExtended.java create mode 100644 extended/src/main/java/apoc/cypher/export/CypherResultSubGraphExtended.java create mode 100644 extended/src/main/java/apoc/cypher/export/DatabaseSubGraphExtended.java create mode 100644 extended/src/main/java/apoc/date/DateUtilsExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/FileManagerFactoryExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/AbstractCypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/AddStructureCypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/CreateCypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/CypherFormatExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/CypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/CypherFormatterUtilsExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/UpdateAllCypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/cypher/formatter/UpdateStructureCypherFormatterExtended.java create mode 100644 extended/src/main/java/apoc/export/util/BatchTransactionExtended.java create mode 100644 extended/src/main/java/apoc/export/util/ExportConfigExtended.java create mode 100644 extended/src/main/java/apoc/export/util/FormatUtilsExtended.java create mode 100644 extended/src/main/java/apoc/export/util/NodesAndRelsSubGraphExtended.java create mode 100644 extended/src/main/java/apoc/get/GetExtended.java create mode 100644 extended/src/main/java/apoc/graph/document/builder/DocumentToGraphExtended.java create mode 100644 extended/src/main/java/apoc/graph/document/builder/LabelBuilderExtended.java create mode 100644 extended/src/main/java/apoc/graph/document/builder/RelationshipBuilderExtended.java create mode 100644 extended/src/main/java/apoc/load/CSVResultExtended.java create mode 100644 extended/src/main/java/apoc/load/LoadJsonUtilsExtended.java create mode 100644 extended/src/main/java/apoc/load/MappingExtended.java create mode 100644 extended/src/main/java/apoc/meta/TypesExtended.java create mode 100644 extended/src/main/java/apoc/path/RelationshipTypeAndDirectionsExtended.java create mode 100644 extended/src/main/java/apoc/result/ExportProgressInfoExtended.java create mode 100644 extended/src/main/java/apoc/result/ImportProgressInfoExtended.java create mode 100644 extended/src/main/java/apoc/util/CollectionUtilsExtended.java create mode 100644 extended/src/main/java/apoc/util/DateParseUtilExtended.java create mode 100644 extended/src/main/java/apoc/util/EntityUtilExtended.java create mode 100644 extended/src/main/java/apoc/util/FixedSizeStringWriterExtended.java create mode 100644 extended/src/main/java/apoc/util/QueueBasedSpliteratorExtended.java create mode 100644 extended/src/main/java/apoc/util/QueueUtilExtended.java create mode 100644 extended/src/main/java/apoc/uuid/UuidUtilExtended.java create mode 100644 extended/src/main/java/org/neo4j/cypher/export/DatabaseSubGraphExtended.java diff --git a/.gitmodules b/.gitmodules index ea53a5a847..ce008b8fb4 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,4 +1,4 @@ [submodule "apoc-core"] path = apoc-core url = https://github.com/neo4j/apoc - branch = dev + branch = dev_reduce_common_reliance diff --git a/extended/src/main/java/apoc/algo/PathFindingExtended.java b/extended/src/main/java/apoc/algo/PathFindingExtended.java index f9eb496461..4eba38b25e 100644 --- a/extended/src/main/java/apoc/algo/PathFindingExtended.java +++ b/extended/src/main/java/apoc/algo/PathFindingExtended.java @@ -16,7 +16,8 @@ import org.neo4j.procedure.Procedure; import java.util.stream.Stream; -import static apoc.algo.PathFindingUtils.buildPathExpander; + +import static apoc.algo.PathFindingUtilsExtended.buildPathExpander; @Extended public class PathFindingExtended { @@ -41,7 +42,7 @@ public Stream aStarWithPoint( new BasicEvaluationContext(tx, db), buildPathExpander(relTypesAndDirs), CommonEvaluators.doubleCostEvaluator(weightPropertyName), - new PathFindingUtils.GeoEstimateEvaluatorPointCustom(pointPropertyName)); + new PathFindingUtilsExtended.GeoEstimateEvaluatorPointCustom(pointPropertyName)); return WeightedPathResult.streamWeightedPathResult(startNode, endNode, algo); } diff --git a/extended/src/main/java/apoc/algo/PathFindingUtilsExtended.java b/extended/src/main/java/apoc/algo/PathFindingUtilsExtended.java new file mode 100644 index 0000000000..95e5450c1b --- /dev/null +++ b/extended/src/main/java/apoc/algo/PathFindingUtilsExtended.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package apoc.algo; + +import apoc.path.RelationshipTypeAndDirectionsExtended; +import org.apache.commons.lang3.tuple.Pair; +import org.neo4j.graphalgo.EstimateEvaluator; +import org.neo4j.graphdb.Direction; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.PathExpander; +import org.neo4j.graphdb.PathExpanderBuilder; +import org.neo4j.graphdb.RelationshipType; +import org.neo4j.values.storable.PointValue; + +public class PathFindingUtilsExtended { + public static class GeoEstimateEvaluatorPointCustom implements EstimateEvaluator { + // -- from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator + private static final double EARTH_RADIUS = 6371 * 1000; // Meters + private Node cachedGoal; + private final String pointPropertyKey; + private double[] cachedGoalCoordinates; + + public GeoEstimateEvaluatorPointCustom(String pointPropertyKey) { + this.pointPropertyKey = pointPropertyKey; + } + + @Override + public Double getCost(Node node, Node goal) { + double[] nodeCoordinates = getCoordinates(node); + if (cachedGoal == null || !cachedGoal.equals(goal)) { + cachedGoalCoordinates = getCoordinates(goal); + cachedGoal = goal; + } + return distance(nodeCoordinates[0], nodeCoordinates[1], cachedGoalCoordinates[0], cachedGoalCoordinates[1]); + } + + private static double distance(double latitude1, double longitude1, double latitude2, double longitude2) { + latitude1 = Math.toRadians(latitude1); + longitude1 = Math.toRadians(longitude1); + latitude2 = Math.toRadians(latitude2); + longitude2 = Math.toRadians(longitude2); + double cLa1 = Math.cos(latitude1); + double xA = EARTH_RADIUS * cLa1 * Math.cos(longitude1); + double yA = EARTH_RADIUS * cLa1 * Math.sin(longitude1); + double zA = EARTH_RADIUS * Math.sin(latitude1); + double cLa2 = Math.cos(latitude2); + double xB = EARTH_RADIUS * cLa2 * Math.cos(longitude2); + double yB = EARTH_RADIUS * cLa2 * Math.sin(longitude2); + double zB = EARTH_RADIUS * Math.sin(latitude2); + return Math.sqrt((xA - xB) * (xA - xB) + (yA - yB) * (yA - yB) + (zA - zB) * (zA - zB)); + } + // -- end from org.neo4j.graphalgo.impl.util.GeoEstimateEvaluator + + private double[] getCoordinates(Node node) { + return ((PointValue) node.getProperty(pointPropertyKey)).coordinate(); + } + } + + public static PathExpander buildPathExpander(String relationshipsAndDirections) { + PathExpanderBuilder builder = PathExpanderBuilder.empty(); + for (Pair pair : RelationshipTypeAndDirectionsExtended.parse(relationshipsAndDirections)) { + if (pair.getLeft() == null) { + if (pair.getRight() == null) { + builder = PathExpanderBuilder.allTypesAndDirections(); + } else { + builder = PathExpanderBuilder.allTypes(pair.getRight()); + } + } else { + if (pair.getRight() == null) { + builder = builder.add(pair.getLeft()); + } else { + builder = builder.add(pair.getLeft(), pair.getRight()); + } + } + } + return builder.build(); + } +} diff --git a/extended/src/main/java/apoc/coll/CollExtended.java b/extended/src/main/java/apoc/coll/CollExtended.java index 71ba3c6d0d..9850b67f67 100644 --- a/extended/src/main/java/apoc/coll/CollExtended.java +++ b/extended/src/main/java/apoc/coll/CollExtended.java @@ -1,7 +1,7 @@ package apoc.coll; import apoc.Extended; -import apoc.util.CollectionUtils; +import apoc.util.CollectionUtilsExtended; import org.neo4j.procedure.Description; import org.neo4j.procedure.Name; import org.neo4j.procedure.UserFunction; @@ -17,7 +17,7 @@ public class CollExtended { @UserFunction @Description("apoc.coll.avgDuration([duration('P2DT3H'), duration('PT1H45S'), ...]) - returns the average of a list of duration values") public DurationValue avgDuration(@Name("durations") List list) { - if (CollectionUtils.isEmpty(list)) return null; + if (CollectionUtilsExtended.isEmpty(list)) return null; long count = 0; diff --git a/extended/src/main/java/apoc/coll/SetBackedListExtended.java b/extended/src/main/java/apoc/coll/SetBackedListExtended.java new file mode 100644 index 0000000000..ec2d59ed2c --- /dev/null +++ b/extended/src/main/java/apoc/coll/SetBackedListExtended.java @@ -0,0 +1,139 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package apoc.coll; + +import java.util.AbstractSequentialList; +import java.util.Iterator; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.Spliterator; + +/** + * @author mh + * @since 10.04.16 + */ +public class SetBackedListExtended extends AbstractSequentialList implements Set { + + private final Set set; + + public SetBackedListExtended(Set set) { + this.set = set; + } + + @Override + public int size() { + return set.size(); + } + + public ListIterator listIterator(int index) { + return new ListIterator() { + Iterator it = set.iterator(); + T current = null; + int idx = 0; + + { + moveTo(index); + } + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public T next() { + idx++; + return current = it.next(); + } + + @Override + public boolean hasPrevious() { + return idx > 0; + } + + @Override + public T previous() { + if (!hasPrevious()) throw new NoSuchElementException(); + T tmp = current; + moveTo(idx - 1); + return tmp; + } + + private void moveTo(int pos) { + Iterator it2 = set.iterator(); + T value = null; + int i = 0; + while (i++ < pos) { + value = it2.next(); + } + ; + this.it = it2; + this.idx = pos; + this.current = value; + } + + @Override + public int nextIndex() { + return idx; + } + + @Override + public int previousIndex() { + return idx - 1; + } + + @Override + public void remove() { + throw new UnsupportedOperationException("remove"); + } + + @Override + public void set(Object o) { + throw new UnsupportedOperationException("set"); + } + + @Override + public void add(Object o) { + throw new UnsupportedOperationException("add"); + } + }; + } + + @Override + public boolean contains(Object o) { + return set.contains(o); + } + + @Override + public int hashCode() { + return set.hashCode(); + } + + @Override + public boolean equals(Object o) { + if (o instanceof Set) return set.equals(o); + return o instanceof Iterable && super.equals(o); + } + + @Override + public Spliterator spliterator() { + return set.spliterator(); + } +} diff --git a/extended/src/main/java/apoc/convert/ConvertExtended.java b/extended/src/main/java/apoc/convert/ConvertExtended.java index 502098441d..f282d76811 100644 --- a/extended/src/main/java/apoc/convert/ConvertExtended.java +++ b/extended/src/main/java/apoc/convert/ConvertExtended.java @@ -1,7 +1,7 @@ package apoc.convert; import apoc.Extended; -import apoc.meta.Types; +import apoc.meta.TypesExtended; import apoc.util.collection.Iterables; import org.neo4j.graphdb.Node; import org.neo4j.graphdb.Path; @@ -49,7 +49,7 @@ public Object fromYaml(@Name("value") String value, @Name(value = "config", defa * which handle complex types, like list/map of nodes/rels/paths */ private Object writeYamlResult(Object value) { - Types type = Types.of(value); + TypesExtended type = TypesExtended.of(value); return switch (type) { case NODE -> nodeToMap((Node) value); case RELATIONSHIP -> relToMap((Relationship) value); diff --git a/extended/src/main/java/apoc/convert/ConvertExtendedUtil.java b/extended/src/main/java/apoc/convert/ConvertExtendedUtil.java index 6f5d0faca6..1e3e4249fa 100644 --- a/extended/src/main/java/apoc/convert/ConvertExtendedUtil.java +++ b/extended/src/main/java/apoc/convert/ConvertExtendedUtil.java @@ -3,6 +3,8 @@ import apoc.export.util.DurationValueSerializer; import apoc.export.util.PointSerializer; import apoc.export.util.TemporalSerializer; +import apoc.util.collection.Iterables; +import apoc.util.collection.Iterators; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -11,7 +13,12 @@ import org.neo4j.graphdb.spatial.Point; import org.neo4j.values.storable.DurationValue; +import java.lang.reflect.Array; import java.time.temporal.Temporal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -26,6 +33,34 @@ public class ConvertExtendedUtil { YAML_MODULE.addSerializer(DurationValue.class, new DurationValueSerializer()); } + public static List convertToList(Object list) { + if (list == null) return null; + else if (list instanceof List) return (List) list; + else if (list instanceof Collection) return new ArrayList((Collection) list); + else if (list instanceof Iterable) return Iterables.asList((Iterable) list); + else if (list instanceof Iterator) return Iterators.asList((Iterator) list); + else if (list.getClass().isArray()) { + return convertArrayToList(list); + } + return Collections.singletonList(list); + } + + public static List convertArrayToList(Object list) { + final Object[] objectArray; + if (list.getClass().getComponentType().isPrimitive()) { + int length = Array.getLength(list); + objectArray = new Object[length]; + for (int i = 0; i < length; i++) { + objectArray[i] = Array.get(list, i); + } + } else { + objectArray = (Object[]) list; + } + List result = new ArrayList<>(objectArray.length); + Collections.addAll(result, objectArray); + return result; + } + /** * get YAMLFactory with configured enable and disable values */ diff --git a/extended/src/main/java/apoc/cypher/CypherExtended.java b/extended/src/main/java/apoc/cypher/CypherExtended.java index 6a02e63efb..b47e3fff28 100644 --- a/extended/src/main/java/apoc/cypher/CypherExtended.java +++ b/extended/src/main/java/apoc/cypher/CypherExtended.java @@ -5,10 +5,10 @@ import apoc.result.CypherStatementMapResult; import apoc.result.MapResult; import apoc.util.CompressionAlgo; -import apoc.util.EntityUtil; +import apoc.util.EntityUtilExtended; import apoc.util.FileUtils; -import apoc.util.QueueBasedSpliterator; -import apoc.util.QueueUtil; +import apoc.util.QueueBasedSpliteratorExtended; +import apoc.util.QueueUtilExtended; import apoc.util.Util; import apoc.util.collection.Iterators; import org.apache.commons.lang3.StringUtils; @@ -45,7 +45,7 @@ import java.util.stream.Stream; import java.util.stream.StreamSupport; -import static apoc.cypher.CypherUtils.runCypherQuery; +import static apoc.cypher.CypherUtilsExtended.runCypherQuery; import static apoc.util.MapUtil.map; import static apoc.util.Util.param; import static apoc.util.Util.quote; @@ -153,7 +153,7 @@ private Stream runManyStatements(Scanner scanner, Map runDataStatementsInTx(scanner, internalQueue, params, addStatistics, timeout, reportError, fileName); } }, RowResult.TOMBSTONE); - return StreamSupport.stream(new QueueBasedSpliterator<>(queue, RowResult.TOMBSTONE, terminationGuard, Integer.MAX_VALUE), false); + return StreamSupport.stream(new QueueBasedSpliteratorExtended<>(queue, RowResult.TOMBSTONE, terminationGuard, Integer.MAX_VALUE), false); } @@ -239,7 +239,7 @@ private void collectError(BlockingQueue queue, boolean reportError, E } String error = e.getMessage(); RowResult result = new RowResult(-1, Map.of("error", error), fileName); - QueueUtil.put(queue, result, 10); + QueueUtilExtended.put(queue, result, 10); } private Scanner createScannerFor(Reader reader) { @@ -286,7 +286,7 @@ private Object consumeResult(Result result, BlockingQueue queue, bool AtomicBoolean closed = new AtomicBoolean(false); while (isOpenAndHasNext(result, closed)) { terminationGuard.check(); - Map res = EntityUtil.anyRebind(transaction, result.next()); + Map res = EntityUtilExtended.anyRebind(transaction, result.next()); queue.put(new RowResult(row++, res, fileName)); } if (addStatistics) { @@ -454,7 +454,7 @@ public Stream mapParallel2(@Name("fragment") String fragment, @Name(" return total; }); - return StreamSupport.stream(new QueueBasedSpliterator<>(queue, RowResult.TOMBSTONE, terminationGuard, (int)timeout),true) + return StreamSupport.stream(new QueueBasedSpliteratorExtended<>(queue, RowResult.TOMBSTONE, terminationGuard, (int)timeout),true) .map(rowResult -> new MapResult(rowResult.result)) .onClose(() -> { transactions.forEach(i -> Util.close(i)); @@ -505,7 +505,7 @@ public Stream parallel2(@Name("fragment") String fragm } return futures.stream().flatMap(f -> { try { - return EntityUtil.anyRebind(tx, f.get()).stream().map(CypherStatementMapResult::new); + return EntityUtilExtended.anyRebind(tx, f.get()).stream().map(CypherStatementMapResult::new); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Error executing in parallel " + statement, e); } diff --git a/extended/src/main/java/apoc/cypher/CypherUtilsExtended.java b/extended/src/main/java/apoc/cypher/CypherUtilsExtended.java new file mode 100644 index 0000000000..1cef3bf70a --- /dev/null +++ b/extended/src/main/java/apoc/cypher/CypherUtilsExtended.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package apoc.cypher; + +import apoc.result.CypherStatementMapResult; +import org.neo4j.graphdb.Transaction; +import org.neo4j.procedure.Name; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.stream.Stream; + +import static java.lang.String.format; +import static java.lang.String.join; +import static java.util.stream.Collectors.toList; + +public class CypherUtilsExtended { + public static Stream runCypherQuery( + Transaction tx, @Name("cypher") String statement, @Name("params") Map params) { + if (params == null) params = Collections.emptyMap(); + return tx.execute(withParamMapping(statement, params.keySet()), params).stream() + .map(CypherStatementMapResult::new); + } + + public static String withParamMapping(String fragment, Collection keys) { + if (keys.isEmpty()) return fragment; + String declaration = " WITH " + + join( + ", ", + keys.stream().map(s -> format(" $`%s` as `%s` ", s, s)).collect(toList())); + return declaration + fragment; + } +} diff --git a/extended/src/main/java/apoc/cypher/export/CypherResultSubGraphExtended.java b/extended/src/main/java/apoc/cypher/export/CypherResultSubGraphExtended.java new file mode 100644 index 0000000000..666219230b --- /dev/null +++ b/extended/src/main/java/apoc/cypher/export/CypherResultSubGraphExtended.java @@ -0,0 +1,274 @@ +/* + * Copyright (c) "Neo4j" + * Neo4j Sweden AB [http://neo4j.com] + * + * This file is part of Neo4j. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package apoc.cypher.export; + +import apoc.util.Util; +import apoc.util.collection.Iterables; +import org.neo4j.cypher.export.SubGraph; +import org.neo4j.graphdb.Label; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.Relationship; +import org.neo4j.graphdb.RelationshipType; +import org.neo4j.graphdb.Result; +import org.neo4j.graphdb.Transaction; +import org.neo4j.graphdb.schema.ConstraintDefinition; +import org.neo4j.graphdb.schema.IndexDefinition; +import org.neo4j.graphdb.schema.IndexType; +import org.neo4j.graphdb.security.AuthorizationViolationException; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static apoc.util.Util.INVALID_QUERY_MODE_ERROR; + +public class CypherResultSubGraphExtended implements SubGraph { + + private final SortedMap nodes = new TreeMap<>(); + private final SortedMap relationships = new TreeMap<>(); + private final Collection