Skip to content

Commit

Permalink
Fixes neo4j-contrib#2016: Add apoc.schema.relationship.indexExists fu…
Browse files Browse the repository at this point in the history
…nction (neo4j-contrib#2260)
  • Loading branch information
vga91 committed Mar 17, 2022
1 parent 4ab2153 commit 1aafbc6
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 5 deletions.
19 changes: 16 additions & 3 deletions core/src/main/java/apoc/schema/Schemas.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ public Boolean indexExistsOnNode(@Name("labelName") String labelName, @Name("pro
return indexExists(labelName, propertyNames);
}

@UserFunction(value = "apoc.schema.relationship.indexExists")
@Description("RETURN apoc.schema.relationship.indexExists(relName, propertyNames)")
public Boolean indexExistsOnRelationship(@Name("labelName") String relName, @Name("propertyName") List<String> propertyNames) {
return indexExistsForRelationship(relName, propertyNames);
}

@UserFunction(value = "apoc.schema.node.constraintExists")
@Description("RETURN apoc.schema.node.constraintExists(labelName, propertyNames)")
public Boolean constraintExistsOnNode(@Name("labelName") String labelName, @Name("propertyName") List<String> propertyNames) {
Expand Down Expand Up @@ -245,16 +251,23 @@ 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) {
Schema schema = tx.schema();
Iterable<IndexDefinition> nodeIndexes = tx.schema().getIndexes(label(labelName));
return isIndexExistent(propertyNames, nodeIndexes);
}

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

for (IndexDefinition indexDefinition : Iterables.asList(schema.getIndexes(Label.label(labelName)))) {
private Boolean isIndexExistent(List<String> propertyNames, Iterable<IndexDefinition> indexes) {
for (IndexDefinition indexDefinition : indexes) {
List<String> properties = Iterables.asList(indexDefinition.getPropertyKeys());

if (properties.equals(propertyNames)) {
return true;
}
}

return false;
}

Expand Down
15 changes: 13 additions & 2 deletions core/src/test/java/apoc/schema/SchemasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,19 @@ public void testIndexNotExists() {
assertEquals(false, r.entrySet().iterator().next().getValue());
});
}



@Test
public void testRelationshipExists() {
db.executeTransactionally("CREATE INDEX rel_index_simple FOR ()-[r:KNOWS]-() ON (r.since)");
db.executeTransactionally("CREATE INDEX rel_index_composite FOR ()-[r:PURCHASED]-() ON (r.date, r.amount)");
awaitIndexesOnline();

assertTrue(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('KNOWS', ['since'])"));
assertFalse(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('KNOWS', ['dunno'])"));
// - composite index
assertTrue(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('PURCHASED', ['date', 'amount'])"));
assertFalse(singleResultFirstColumn(db, "RETURN apoc.schema.relationship.indexExists('PURCHASED', ['date', 'another'])"));
}

@Test
public void testUniquenessConstraintOnNode() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
¦signature
¦apoc.schema.relationship.constraintExists(type :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)
¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@
RETURN apoc.schema.relationship.constraintExists(type, propertyNames)
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.schema.relationship/apoc.schema.adoc[apoc.schema.relationship.indexExists icon:book[]]

RETURN apoc.schema.relationship.indexExists(relName, propertyNames)
|label:function[]
|label:apoc-core[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
¦signature
¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
¦xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]] +


¦label:function[]
¦label:apoc-core[]
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ for the provided `label` and `uuidProperty`, in case the UUID handler is already
¦function¦apoc.schema.node.constraintExists¦apoc.schema.node.constraintExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.node.constraintExists(labelName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc
¦function¦apoc.schema.node.indexExists¦apoc.schema.node.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.node.indexExists(labelName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc
¦function¦apoc.schema.relationship.constraintExists¦apoc.schema.relationship.constraintExists(type :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.relationship.constraintExists(type, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc
¦function¦apoc.schema.relationship.indexExists¦apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)¦RETURN apoc.schema.relationship.indexExists(relName, propertyNames)¦true¦xref::indexes/schema-index-operations.adoc
¦function¦apoc.scoring.existence¦apoc.scoring.existence(score :: INTEGER?, exists :: BOOLEAN?) :: (FLOAT?)¦apoc.scoring.existence(5, true) returns the provided score if true, 0 if false¦true¦
¦function¦apoc.scoring.pareto¦apoc.scoring.pareto(minimumThreshold :: INTEGER?, eightyPercentValue :: INTEGER?, maximumValue :: INTEGER?, score :: INTEGER?) :: (FLOAT?)¦apoc.scoring.pareto(10, 20, 100, 11) applies a Pareto scoring function over the inputs¦true¦
¦function¦apoc.static.get¦apoc.static.get(key :: STRING?) :: (ANY?)¦apoc.static.get(name) - returns statically stored value from config (apoc.static.<key>) or server lifetime storage¦false¦xref::misc/static-values.adoc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
////
This file is generated by DocsTest, so don't change it!
////

= apoc.schema.relationship.indexExists
:description: This section contains reference documentation for the apoc.schema.relationship.indexExists function.

label:function[] label:apoc-core[]

[.emphasis]
RETURN apoc.schema.relationship.indexExists(relName, propertyNames)

== Signature

[source]
----
apoc.schema.relationship.indexExists(labelName :: STRING?, propertyName :: LIST? OF STRING?) :: (BOOLEAN?)
----

== Input parameters
[.procedures, opts=header]
|===
| Name | Type | Default
|labelName|STRING?|null
|propertyName|LIST? OF STRING?|null
|===

xref::indexes/schema-index-operations.adoc[More documentation of apoc.schema.relationship.indexExists,role=more information]

Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ RETURN apoc.schema.node.indexExists(labelName, propertyNames)
RETURN apoc.schema.relationship.constraintExists(type, propertyNames)
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]]

RETURN apoc.schema.relationship.indexExists(relName, propertyNames)
|label:function[]
|label:apoc-core[]
|===

Original file line number Diff line number Diff line change
Expand Up @@ -2910,6 +2910,11 @@ RETURN apoc.schema.node.indexExists(labelName, propertyNames)
RETURN apoc.schema.relationship.constraintExists(type, propertyNames)
|label:function[]
|label:apoc-core[]
|xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[apoc.schema.relationship.indexExists icon:book[]]

RETURN apoc.schema.relationship.indexExists(relName, propertyNames)
|label:function[]
|label:apoc-core[]
|===

[discrete]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ This file is generated by DocsTest, so don't change it!
*** xref::overview/apoc.schema/apoc.schema.node.constraintExists.adoc[]
*** xref::overview/apoc.schema/apoc.schema.node.indexExists.adoc[]
*** xref::overview/apoc.schema/apoc.schema.relationship.constraintExists.adoc[]
*** xref::overview/apoc.schema/apoc.schema.relationship.indexExists.adoc[]
** xref::overview/apoc.scoring/index.adoc[]
*** xref::overview/apoc.scoring/apoc.scoring.existence.adoc[]
*** xref::overview/apoc.scoring/apoc.scoring.pareto.adoc[]
Expand Down

0 comments on commit 1aafbc6

Please sign in to comment.