Skip to content

Commit

Permalink
[NOID] Ignore vector indexes when getting indexes from Neo4j.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Aug 3, 2023
1 parent 42005bb commit 00162d4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 15 deletions.
14 changes: 11 additions & 3 deletions common/src/main/java/apoc/export/util/NodesAndRelsSubGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package apoc.export.util;

import apoc.util.Util;
import apoc.util.collection.Iterables;
import java.util.Comparator;
import java.util.function.BiFunction;
Expand All @@ -29,6 +30,7 @@
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.schema.Schema;

import java.util.ArrayList;
Expand All @@ -38,6 +40,7 @@
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* @author mh
Expand Down Expand Up @@ -75,7 +78,12 @@ public Iterable<Relationship> getRelationships() {

@Override
public Iterable<IndexDefinition> getIndexes() {
return getDefinitions(Schema::getIndexes);
return getDefinitions((schema, label) ->
StreamSupport
.stream(schema.getIndexes(label).spliterator(), false)
.filter(indexDefinition -> indexDefinition.getIndexType() != IndexType.VECTOR)
.toList()
);
}

@Override
Expand Down Expand Up @@ -116,15 +124,15 @@ public Iterable<IndexDefinition> getIndexes(Label label) {
if (!labels.contains(label.name())) {
return Collections.emptyList();
}
return tx.schema().getIndexes(label);
return Util.getIndexes(tx, label);
}

@Override
public Iterable<IndexDefinition> getIndexes(RelationshipType type) {
if (!types.contains(type.name())) {
return Collections.emptyList();
}
return tx.schema().getIndexes(type);
return Util.getIndexes(tx, type);
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions common/src/main/java/apoc/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.neo4j.graphdb.ExecutionPlanDescription;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Result;
import org.neo4j.graphdb.schema.ConstraintType;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.graphdb.schema.IndexType;
import org.neo4j.internal.schema.ConstraintDescriptor;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.kernel.impl.util.ValueUtils;
Expand Down Expand Up @@ -1140,4 +1143,37 @@ public static int indexOf(List<Object> list, Object value) {
(i) -> Util.valueEquals(i, value)
);
}

/*
* Get all indexes from Neo4j
* Currently filters out vector indexes
* When vector indexes are supported, this can be changed to transaction.schema().getIndexes()
*/
public static Iterable<IndexDefinition> getIndexes(Transaction transaction)
{
return StreamSupport.stream(transaction.schema().getIndexes().spliterator(), false)
.filter(indexDefinition -> indexDefinition.getIndexType() != IndexType.VECTOR).toList();
}

/*
* Get all indexes from Neo4j for a given label
* Currently filters out vector indexes
* When vector indexes are supported, this can be changed to transaction.schema().getIndexes(label)
*/
public static Iterable<IndexDefinition> getIndexes(Transaction transaction, Label label)
{
return StreamSupport.stream(transaction.schema().getIndexes(label).spliterator(), false)
.filter(indexDefinition -> indexDefinition.getIndexType() != IndexType.VECTOR).toList();
}

/*
* Get all indexes from Neo4j for a given relationship type
* Currently filters out vector indexes
* When vector indexes are supported, this can be changed to transaction.schema().getIndexes(relType)
*/
public static Iterable<IndexDefinition> getIndexes(Transaction transaction, RelationshipType relType)
{
return StreamSupport.stream(transaction.schema().getIndexes(relType).spliterator(), false)
.filter(indexDefinition -> indexDefinition.getIndexType() != IndexType.VECTOR).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static SubGraph from(Transaction tx, Result result, boolean addBetween, b
} catch (AuthorizationViolationException e) {
throw new RuntimeException(INVALID_QUERY_MODE_ERROR);
}
for (IndexDefinition def : tx.schema().getIndexes()) {
for (IndexDefinition def : Util.getIndexes(tx)) {
if (def.getIndexType() != IndexType.LOOKUP) {
if (def.isNodeIndex()) {
for (Label label : def.getLabels()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.Comparator;
import java.util.stream.StreamSupport;

import apoc.util.Util;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
Expand Down Expand Up @@ -57,7 +59,7 @@ public Iterable<Relationship> getRelationships()
@Override
public Iterable<IndexDefinition> getIndexes()
{
return transaction.schema().getIndexes();
return Util.getIndexes(transaction);
}

@Override
Expand All @@ -79,12 +81,12 @@ public Iterable<ConstraintDefinition> getConstraints(RelationshipType type) {

@Override
public Iterable<IndexDefinition> getIndexes(Label label) {
return transaction.schema().getIndexes(label);
return Util.getIndexes(transaction, label);
}

@Override
public Iterable<IndexDefinition> getIndexes(RelationshipType type) {
return transaction.schema().getIndexes(type);
return Util.getIndexes(transaction, type);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion common/src/test/java/apoc/util/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.neo4j.graphdb.schema.IndexType.POINT;
import static org.neo4j.graphdb.schema.IndexType.RANGE;
import static org.neo4j.graphdb.schema.IndexType.TEXT;
import static org.neo4j.graphdb.schema.IndexType.VECTOR;

public class UtilTest {

Expand Down Expand Up @@ -69,7 +70,8 @@ public void testAPOCisAwareOfAllIndexes() {
LOOKUP,
TEXT,
RANGE,
POINT
POINT,
VECTOR
));
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/index/SchemaIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public Stream<ListResult> distinct(@Name("label") String label, @Name("key") St
public Stream<PropertyValueCount> distinctCount(@Name(value = "label", defaultValue = "") String labelName, @Name(value = "key", defaultValue = "") String keyName) {

BlockingQueue<PropertyValueCount> queue = new LinkedBlockingDeque<>(100);
Iterable<IndexDefinition> indexDefinitions = (labelName.isEmpty()) ? tx.schema().getIndexes() : tx.schema().getIndexes(Label.label(labelName));
Iterable<IndexDefinition> indexDefinitions = (labelName.isEmpty()) ? Util.getIndexes(tx) : Util.getIndexes(tx, Label.label(labelName));

Util.newDaemonThread(() ->
StreamSupport.stream(indexDefinitions.spliterator(), true)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/apoc/periodic/Periodic.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void truncate(@Name(value = "config", defaultValue = "{}") Map<String,Obj
if (Util.toBoolean(config.get("dropSchema"))) {
Schema schema = tx.schema();
schema.getConstraints().forEach(ConstraintDefinition::drop);
schema.getIndexes().forEach(IndexDefinition::drop);
Util.getIndexes(tx).forEach(IndexDefinition::drop);
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/apoc/refactor/rename/Rename.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private Stream<BatchAndTotalResultWithInfo> getResultOfBatchAndTotalWithInfo(Str
constraintsForLabel.forEach((c) -> {
constraints.add(c.toString());
});
Iterable<IndexDefinition> idxs = transaction.schema().getIndexes(Label.label(label));
Iterable<IndexDefinition> idxs = Util.getIndexes(transaction, Label.label(label));
idxs.forEach((i) -> {
indexes.add(i.toString());
});
Expand All @@ -216,7 +216,7 @@ private Stream<BatchAndTotalResultWithInfo> getResultOfBatchAndTotalWithInfo(Str
}
});
});
Iterable<IndexDefinition> idxs = transaction.schema().getIndexes();
Iterable<IndexDefinition> idxs = Util.getIndexes(transaction);
idxs.forEach((i) -> {
i.getPropertyKeys().forEach((p) -> {
if(p.equals(prop)){
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/apoc/schema/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public List<AssertSchemaResult> assertIndexes(Map<String, List<Object>> indexes0
Map<String, List<Object>> indexes = copyMapOfObjects(indexes0);
List<AssertSchemaResult> result = new ArrayList<>(indexes.size());

for (IndexDefinition definition : schema.getIndexes()) {
for (IndexDefinition definition : Util.getIndexes(tx)) {
if (definition.getIndexType() == IndexType.LOOKUP)
continue;
if (definition.isConstraintIndex())
Expand Down Expand Up @@ -289,12 +289,12 @@ private Map<String, List<Object>> copyMapOfObjects(Map<String, List<Object>> inp
* @return true if the index exists otherwise it returns false
*/
private Boolean indexExists(String labelName, List<String> propertyNames) {
Iterable<IndexDefinition> nodeIndexes = tx.schema().getIndexes(label(labelName));
Iterable<IndexDefinition> nodeIndexes = Util.getIndexes(tx, label(labelName));
return isIndexExistent(propertyNames, nodeIndexes);
}

private Boolean indexExistsForRelationship(String relName, List<String> propertyNames) {
Iterable<IndexDefinition> relIndexes = tx.schema().getIndexes(RelationshipType.withName(relName));
Iterable<IndexDefinition> relIndexes = Util.getIndexes(tx, RelationshipType.withName(relName));
return isIndexExistent(propertyNames, relIndexes);
}

Expand Down

0 comments on commit 00162d4

Please sign in to comment.