Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
neo4j: optimize performance for count() method (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarmbruster committed Mar 13, 2012
1 parent 135513f commit 2d7ea6c
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import org.slf4j.LoggerFactory
import org.slf4j.Logger
import org.apache.lucene.search.MatchAllDocsQuery
import org.grails.datastore.mapping.model.types.Simple
import org.apache.lucene.search.MultiPhraseQuery
import org.neo4j.kernel.AbstractGraphDatabase
import org.neo4j.helpers.collection.IteratorUtil

/**
* perform criteria queries on a Neo4j backend
Expand All @@ -60,7 +60,7 @@ class Neo4jQuery extends Query {
protected List executeQuery(PersistentEntity entity, Junction criteria) {

Assert.notNull( entity, "Entity must not be null" )

if (indexQueryPossible(entity, criteria)) {
try {
executeQueryViaIndex(entity, criteria)
Expand Down Expand Up @@ -162,6 +162,15 @@ class Neo4jQuery extends Query {
def result = []
List<Node> subReferenceNodes = getSubreferencesOfSelfAndDerived(entity)
List<String> validClassNames = subReferenceNodes.collect { it.getProperty(Neo4jSession.SUBREFERENCE_PROPERTY_NAME)}

// shortcut for count()
if (criteria.empty && (projections.projectionList?.size()==1) && projections.projectionList[0] instanceof CountProjection) {
log.error "shortcut for count"
return [ subReferenceNodes.sum {
IteratorUtil.count((Iterable)it.getRelationships(Direction.OUTGOING, GrailsRelationshipTypes.INSTANCE))
} ]
}

for (Node subReferenceNode in subReferenceNodes) {
for (Relationship rel in subReferenceNode.getRelationships(GrailsRelationshipTypes.INSTANCE, Direction.OUTGOING).iterator()) {
Node n = rel.endNode
Expand Down

0 comments on commit 2d7ea6c

Please sign in to comment.