Created
December 23, 2013 13:03
-
-
Save mneedham/8096822 to your computer and use it in GitHub Desktop.
Showing how cypher's Merge function works with unique constraint indexes
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
import org.neo4j.cypher.javacompat.ExecutionEngine; | |
import org.neo4j.cypher.javacompat.ExecutionResult; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.factory.GraphDatabaseFactory; | |
import org.neo4j.helpers.collection.MapUtil; | |
import org.neo4j.kernel.impl.util.FileUtils; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Random; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Future; | |
public class MergeTime | |
{ | |
public static void main(String[] args) throws Exception | |
{ | |
String pathToDb = "/tmp/foo"; | |
FileUtils.deleteRecursively(new File(pathToDb)); | |
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase( pathToDb ); | |
final ExecutionEngine engine = new ExecutionEngine( db ); | |
// withIndexes( engine ); | |
// withUniqueConstraints( engine ); | |
ExecutorService executor = Executors.newFixedThreadPool( 50 ); | |
final Random random = new Random(); | |
final int numberOfUsers = 10; | |
final int numberOfEvents = 50; | |
int iterations = 100; | |
final List<Integer> userIds = generateIds( numberOfUsers ); | |
final List<Integer> eventIds = generateIds( numberOfEvents ); | |
List<Future> merges = new ArrayList<>( ); | |
for ( int i = 0; i < iterations; i++ ) | |
{ | |
Integer userId = userIds.get(random.nextInt(numberOfUsers)); | |
Integer eventId = eventIds.get(random.nextInt(numberOfEvents)); | |
merges.add(executor.submit(mergeAway( engine, userId, eventId) )); | |
} | |
for ( Future merge : merges ) | |
{ | |
merge.get(); | |
} | |
executor.shutdown(); | |
ExecutionResult userResult = engine.execute("MATCH (u:User) RETURN u.id as userId, COUNT(u) AS count ORDER BY userId"); | |
System.out.println(userResult.dumpToString()); | |
} | |
private static void withIndexes( ExecutionEngine engine ) | |
{ | |
engine.execute( "CREATE INDEX ON :User(id)" ).dumpToString(); | |
engine.execute( "CREATE INDEX ON :Event(id)" ).dumpToString(); | |
} | |
private static void withUniqueConstraints( ExecutionEngine engine ) | |
{ | |
engine.execute( "CREATE CONSTRAINT ON (user:User) ASSERT user.id IS UNIQUE" ).dumpToString(); | |
engine.execute( "CREATE CONSTRAINT ON (event:Event) ASSERT event.id IS UNIQUE" ).dumpToString(); | |
} | |
private static Runnable mergeAway(final ExecutionEngine engine, | |
final Integer userId, final Integer eventId) | |
{ | |
return new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
try | |
{ | |
ExecutionResult result = engine.execute( | |
"MERGE (u:User {id: {userId}})\n" + | |
"MERGE (e:Event {id: {eventId}})\n" + | |
"MERGE (u)-[:CREATED_EVENT]->(m)\n" + | |
"RETURN u, e", | |
MapUtil.map( "userId", userId, "eventId", eventId) ); | |
// throw away | |
for ( Map<String, Object> row : result ) { } | |
} | |
catch ( Exception e ) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
}; | |
} | |
private static List<Integer> generateIds( int amount ) | |
{ | |
List<Integer> ids = new ArrayList<>(); | |
for ( int i = 1; i <= amount; i++ ) | |
{ | |
ids.add( i ); | |
} | |
return ids; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment