Created
March 17, 2014 15:38
-
-
Save anonymous/9601556 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tests; | |
/** | |
* | |
* @author kb | |
* | |
*/ | |
import java.io.File; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.neo4j.cypher.ExecutionEngine; | |
import org.neo4j.cypher.ExecutionResult; | |
import org.neo4j.graphdb.Direction; | |
import org.neo4j.graphdb.DynamicLabel; | |
import org.neo4j.graphdb.DynamicRelationshipType; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Relationship; | |
import org.neo4j.graphdb.Transaction; | |
import org.neo4j.graphdb.factory.GraphDatabaseFactory; | |
import org.neo4j.graphdb.index.Index; | |
import org.neo4j.graphdb.index.IndexManager; | |
import org.neo4j.helpers.collection.MapUtil; | |
import org.neo4j.index.lucene.unsafe.batchinsert.LuceneBatchInserterIndexProvider; | |
import org.neo4j.kernel.logging.BufferingLogger; | |
import org.neo4j.tooling.GlobalGraphOperations; | |
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 scala.collection.Iterator; | |
public class App2 { | |
public static void main(String[] args) throws Exception { | |
new App2().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")); | |
//create the "index" node referencing all other nodes | |
long indexnode = database.createNode(null, DynamicLabel.label("indexnode")); | |
Map<String, Object> m = new HashMap<String, Object>(); | |
m.put("id", "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, DynamicLabel.label("node")); | |
database.createRelationship(indexnode, node, DynamicRelationshipType.withName("contains"), null); | |
} | |
p.shutdown(); | |
} | |
public void cypherQueryPrepared(GraphDatabaseService database, int max, ExecutionEngine engine) throws Exception { | |
for (int i = 1; i <= 10; i++) { | |
int query = (max<99995)?max *i:max+i; | |
long start = System.nanoTime(); | |
Map<String, Object> m = new HashMap<>(); | |
m.put("max", query); | |
//ExecutionResult r = engine.execute("start n=node(*) where n.i = " + "{max}" + " return n", m); | |
ExecutionResult r = engine.execute("START m=node:index('id:indexnode') MATCH (m)-[]->(n) WHERE n.i=" + "{max}" + " return n", m); | |
Iterator<Object> got = r.columnAs("n"); | |
String iter = ""; | |
while (got.hasNext()) | |
iter += ((Node) got.next()).getProperty("i") + ", "; | |
if (iter.length() > 1) | |
iter = iter.substring(0, iter.length() - 2); | |
System.out.println("Cypher [prepared with 1 execution engine] (microseconds): " | |
+ ((System.nanoTime() - start) / 1000) + "\t result 'i': " + iter + "\t testing equality on: " + query); | |
} | |
} | |
public void javaQuery(GraphDatabaseService database, int max) throws Exception { | |
for (int i = 1; i <= 10; i++) { | |
int query = (max<99995)?max *i:max+i; | |
int got = -1; | |
long start = System.nanoTime(); | |
IndexManager m = database.index(); | |
Index<Node> index = m.forNodes("index", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "exact")); | |
Node indexnode = index.get("id", "indexnode").getSingle(); | |
for (Relationship rel : indexnode.getRelationships(Direction.OUTGOING)) { | |
Node node = rel.getEndNode(); | |
int n = (Integer) node.getProperty("i"); | |
if (n == query ) { | |
got = n; | |
break; | |
} | |
} | |
System.out.println("Java (microseconds): " + ((System.nanoTime() - start) / 1000) + "\t result 'i': " + got + "\t testing equality on: " + query); | |
} | |
} | |
public void run() throws Exception { | |
String databaseStore = "sampledb2"; | |
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