Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2016: Add apoc.schema.relationship.indexExists function #2260

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -71,6 +71,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 @@ -224,16 +230,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 @@ -342,8 +342,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 @@ -480,6 +480,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 @@ -2592,6 +2592,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 @@ -497,6 +497,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