Skip to content

Instantly share code, notes, and snippets.

@sarmbruster
Created April 5, 2016 12:19
Show Gist options
  • Save sarmbruster/a7f7859696d4580fd0e0f4b312daf39d to your computer and use it in GitHub Desktop.
Save sarmbruster/a7f7859696d4580fd0e0f4b312daf39d to your computer and use it in GitHub Desktop.
code sample for batch deletions in groovy
package org.neo4j.extension.tei
import org.junit.Rule
import org.neo4j.extension.spock.Neo4jResource
import org.neo4j.graphdb.DynamicLabel
import org.neo4j.graphdb.GraphDatabaseService
import spock.lang.Specification
import java.util.concurrent.Executors
/**
* @author Stefan Armbruster
*/
class BatchDeleteSpec extends Specification {
@Delegate
@Rule
Neo4jResource neo4j = new Neo4jResource()
def BATCHSIZE = 1000
def "should do batch deletes"() {
when: "create 100k nodes in batches of 10k each"
def label = DynamicLabel.label("Person")
def created = 0
(1..10).each {
withTransaction(graphDatabaseService, {
(1..10000).each { graphDatabaseService.createNode(label); created ++}
})
}
then: "we have 100k nodes"
"match (n) return count(n) as c".cypher()[0].c == 10*10000
when: "delete in batches of BATCHSIZE"
def executor = Executors.newSingleThreadExecutor();
withTransaction(graphDatabaseService, {
def iter = graphDatabaseService.findNodes(label)
while (iter.hasNext()) {
executor.submit( {
withTransaction(graphDatabaseService, {
iter.take(BATCHSIZE).each {it.delete()}
})
}).get()
}
})
then: "db is empty"
"match (n) return count(n) as c".cypher()[0].c == 0
}
static withTransaction(GraphDatabaseService graphDatabaseService, Closure closure) {
def tx = graphDatabaseService.beginTx()
try {
def result = closure.call()
tx.success()
result
} finally {
tx.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment