public Future<Node> getOrCreate( final String key, final Object value, final Object initialValue ) throws Exception { return executeDontWait( new WorkerCommand<CommandState, Node>() { @Override public Node doWork( CommandState state ) { UniqueFactory.UniqueNodeFactory factory = new UniqueFactory.UniqueNodeFactory( state.index ) { @Override protected void initialize( Node node, Map<String, Object> properties ) { node.setProperty( key, initialValue ); } }; return factory.getOrCreate( key, value ); } } ); }
private Node getOrCreateWithUniqueFactory(String nodeName) { UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory(graphDb, "index") { @Override protected void initialize(Node created, Map<String, Object> properties) { created.setProperty("className", properties.get("name")); } }; return factory.getOrCreate("name", nodeName); }
@Test public void getOrCreateNodeWithUniqueFactory() throws Exception { final String key = "name"; final String value = "Mattias"; final String property = "counter"; final Index<Node> index = nodeIndex( LuceneIndexImplementation.EXACT_CONFIG ); final AtomicInteger counter = new AtomicInteger(); UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory( index ) { @Override protected void initialize( Node node, Map<String, Object> properties ) { assertEquals( value, properties.get( key ) ); assertEquals( 1, properties.size() ); node.setProperty( property, counter.getAndIncrement() ); } }; Node unique = factory.getOrCreate( key, value ); assertNotNull( unique ); assertEquals( "not initialized", 0, unique.getProperty( property, null ) ); assertEquals( unique, index.get( key, value ).getSingle() ); assertEquals( unique, factory.getOrCreate( key, value ) ); assertEquals( "initialized more than once", 0, unique.getProperty( property ) ); assertEquals( unique, index.get( key, value ).getSingle() ); finishTx( false ); }
@Test public void getOrCreateRelationshipWithUniqueFactory() throws Exception { final String key = "name"; final String value = "Mattias"; final Node root = graphDb.createNode(); final Index<Relationship> index = relationshipIndex( LuceneIndexImplementation.EXACT_CONFIG ); final DynamicRelationshipType type = DynamicRelationshipType.withName( "SINGLE" ); UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory( index ) { @Override protected Relationship create( Map<String, Object> properties ) { assertEquals( value, properties.get( key ) ); assertEquals( 1, properties.size() ); return root.createRelationshipTo( graphDatabase().createNode(), type ); } }; Relationship unique = factory.getOrCreate( key, value ); assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) ); assertNotNull( unique ); assertEquals( unique, index.get( key, value ).getSingle() ); assertEquals( unique, factory.getOrCreate( key, value ) ); assertEquals( unique, root.getSingleRelationship( type, Direction.BOTH ) ); assertEquals( unique, index.get( key, value ).getSingle() ); finishTx( false ); }
/** * The node factory is used for caching. * @param db The Neo4j GraphDatabaseService that contains the pattern recognition hierarchy. */ private void createNodeFactory(GraphDatabaseService db) { if (patternFactory == null) { patternFactory = new UniqueFactory.UniqueNodeFactory(db, label) { @Override protected void initialize(Node created, Map<String, Object> properties) { created.setProperty(propertyKey, properties.get(propertyKey)); } }; } }
private void createNodeFactory(GraphDatabaseService db) { if (classFactory == null) { classFactory = new UniqueFactory.UniqueNodeFactory(db, label) { @Override protected void initialize(Node created, Map<String, Object> properties) { created.setProperty(propertyKey, properties.get(propertyKey)); } }; } }
private void createNodeFactory(GraphDatabaseService db) { if (dataFactory == null) { dataFactory = new UniqueFactory.UniqueNodeFactory(db, label) { @Override protected void initialize(Node created, Map<String, Object> properties) { created.setProperty(propertyKey, properties.get(propertyKey)); } }; } }