Skip to content

Instantly share code, notes, and snippets.

@jcheype
Created March 22, 2013 18:27
Show Gist options
  • Select an option

  • Save jcheype/5223607 to your computer and use it in GitHub Desktop.

Select an option

Save jcheype/5223607 to your computer and use it in GitHub Desktop.
package com.jcheype.neoTest;
import com.google.common.base.Predicates;
import com.google.common.collect.Maps;
import com.jcheype.neoTest.manager.MonographieManager;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import org.bson.types.ObjectId;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* Created with IntelliJ IDEA.
* User: juliencheype
* Date: 5/2/13
* Time: 22:08
* To change this template use File | Settings | File Templates.
*/
public class App {
public static void main(String[] args) throws UnknownHostException {
GraphDatabaseService graphDb = new GraphDatabaseFactory().newEmbeddedDatabase("test");
ExecutionEngine engine = new ExecutionEngine(graphDb);
registerShutdownHook(graphDb);
RootNodes rootNodes = new RootNodes(graphDb);
MonographieManager monographie = new MonographieManager(graphDb, rootNodes);
// HashMap monoMap = new HashMap();
// monoMap.put("docid", "123");
// monoMap.put("object_name", "doliprane");
// monoMap.put("publication_cible", new String[]{"V12", "v13"});
// Node mono = monographie.createOrUpdate(monoMap);
// System.out.println(mono.getId());
// System.out.println(mono.getProperty("version"));
// test1(graphDb, monographie);
test3(engine, "DOL*");
test3(engine, "vip*");
test3(engine, "pol*");
test3(engine, "*sano*");
test3(engine, "*DOL*");
// test4(graphDb, monographie);
}
private static void test4(GraphDatabaseService graphDb, MonographieManager monographie) throws UnknownHostException {
Mongo mongo = new Mongo();
DBCursor dbObjects = mongo.getDB("vdmsDEV").getCollection("monographies").find();
int i = 0;
for (DBObject dbo : dbObjects) {
Transaction transaction = graphDb.beginTx();
try {
Map map = Maps.filterValues(dbo.toMap(), Predicates.not(Predicates.instanceOf(ObjectId.class)));
Node mono = monographie.createOrUpdate(map);
monographie.index.add(mono, "object_name", map.get("object_name"));
monographie.index.add(mono, "laboratoire", map.get("laboratoire"));
System.out.println(++i);
transaction.success();
} finally {
transaction.finish();
}
}
}
private static void test3(ExecutionEngine engine, String t) {
long start = System.currentTimeMillis();
ExecutionResult result = engine.execute("START n=node:monographies('object_name: "+t+" AND laboratoire:*e*')" +
// "MATCH (n)<-[:MONOGRAPHIE]-(x) " +
// "WHERE (x.object_name =~ 'S.*' )" +
"RETURN count(n)");
System.out.println("TIME: " + (System.currentTimeMillis() - start));
System.out.println("NUMBER: " + result.iterator().next());
// for(Map<String, Object> node : result){
// System.out.println(((Node)node.get("n")).getProperty("object_name"));
// }
}
private static void test2(ExecutionEngine engine) {
long start = System.currentTimeMillis();
ExecutionResult result = engine.execute("START n=node:RootNodes(name = \"Monographie_ROOT\") " +
"MATCH (n)<-[:MONOGRAPHIE]-(x) " +
"RETURN count(x)");
System.out.println("TIME: " + (System.currentTimeMillis() - start));
System.out.println(result.toString());
}
private static void test1(GraphDatabaseService graphDb, MonographieManager monographie) {
long start = System.currentTimeMillis();
Transaction transaction = graphDb.beginTx();
try {
generate(monographie, 20000);
transaction.success();
} finally {
transaction.finish();
}
System.out.println("TIME: " + (System.currentTimeMillis() - start));
}
private static void generate(MonographieManager monographie, int size) {
for (int i = 0; i < size; i++) {
HashMap monoMap = new HashMap();
monoMap.put("docid", "__" + UUID.randomUUID().toString());
monoMap.put("object_name", UUID.randomUUID().toString());
monoMap.put("publication_cible", new String[]{"V12", "v13"});
monographie.createOrUpdate(monoMap);
}
}
private static void registerShutdownHook(final GraphDatabaseService graphDb) {
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running example before it's completed)
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
graphDb.shutdown();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment