Skip to content

Instantly share code, notes, and snippets.

@jexp
Forked from anonymous/gist:9601556
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jexp/9607779 to your computer and use it in GitHub Desktop.
Save jexp/9607779 to your computer and use it in GitHub Desktop.
package tests;
/**
*
* @author kb
*
*/
import org.neo4j.cypher.ExecutionEngine;
import org.neo4j.cypher.ExecutionResult;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.graphdb.index.IndexManager;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider;
import org.neo4j.kernel.logging.BufferingLogger;
import org.neo4j.unsafe.batchinsert.BatchInserter;
import org.neo4j.unsafe.batchinsert.BatchInserterIndex;
import org.neo4j.unsafe.batchinsert.BatchInserterIndexProvider;
import org.neo4j.unsafe.batchinsert.BatchInserters;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class App22 {
public static final int RUNS = 100;
public static final String QUERY = "MATCH (m)-[:contains]->(n:node {i:{max}}) return m.id as i LIMIT 1";
public static final Label NODE = DynamicLabel.label("node");
public static final DynamicRelationshipType CONTAINS = DynamicRelationshipType.withName("contains");
public static void main(String[] args) throws Exception {
new App22().run();
}
public void insert(BatchInserter database) {
System.out.println("Inserting...");
BatchInserterIndexProvider p = new LuceneBatchInserterIndexProvider(database);
BatchInserterIndex index = p.nodeIndex("index", MapUtil.stringMap(
IndexManager.PROVIDER, "lucene", "type", "exact"));
Map<String, Object> m = new HashMap<String, Object>();
m.put("id", "indexnode");
//create the "index" node referencing all other nodes
long indexnode = database.createNode(m, DynamicLabel.label("indexnode"));
index.add(indexnode, m);
for (int i = 0; i < 100000; i++) {
m.clear();
m.put("i", i);
//m.put("i2", i * 10);
long node = database.createNode(m, NODE);
database.createRelationship(indexnode, node, CONTAINS, null);
}
database.createDeferredConstraint(NODE).assertPropertyIsUnique("i").create();
p.shutdown();
}
public void cypherQueryPrepared(GraphDatabaseService database, int max, ExecutionEngine engine) throws Exception {
Map<String, Object> m = new HashMap<>();
for (int i = 1; i <= RUNS; i++) {
int query = (max<99995)?max *i:max+i;
long start = System.nanoTime();
m.put("max", query);
ExecutionResult r = engine.execute(QUERY, m);
Object property = r.columnAs("i").next();
long delta = System.nanoTime() - start;
System.out.println("Cypher [prepared with 1 execution engine] (microseconds): "
+ (delta / 1000) + "\t result 'i': " + property + "\t testing equality on: " + query);
}
}
public void javaQuery(GraphDatabaseService database, int max) throws Exception {
for (int i = 1; i <= RUNS; i++) {
int query = (max<99995)?max *i:max+i;
long start = System.nanoTime();
Node node = IteratorUtil.single(database.findNodesByLabelAndProperty(NODE,"i",max));
Relationship rel = node.getSingleRelationship(CONTAINS,Direction.INCOMING);
Node indexNode = rel.getStartNode();
Object prop = indexNode.getProperty("id");
long delta = System.nanoTime() - start;
System.out.println("Java (microseconds): " + (delta / 1000) + "\t result 'i': " + prop + "\t testing equality on: " + query);
}
}
public void run() throws Exception {
String databaseStore = "target/sampledb22";
boolean insert = !new File(databaseStore).exists();
if (insert) {
BatchInserter b = BatchInserters.inserter(databaseStore);
insert(b);
b.shutdown();
//
System.exit(0);
}
GraphDatabaseService database = new GraphDatabaseFactory().newEmbeddedDatabase(databaseStore);
try(Transaction transaction = database.beginTx()){
ExecutionEngine engine = new ExecutionEngine(database, new BufferingLogger());
int number = 1 * 1;
//int number = 1 * 100;
//int number = 1 * 1000;
//int number = 1 + 99995;
javaQuery(database, number);
// cypherQueryPrepared(database, number, engine);
transaction.success();
}
database.shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment