Refine search
private Node getNodeByIdInTx( int nodeId ) { try ( Transaction ignored = db.beginTx() ) { return db.getNodeById( nodeId ); } }
private Node getNodeInTx( long nodeId, GraphDatabaseService db ) { try ( Transaction ignored = db.beginTx() ) { return db.getNodeById( nodeId ); } }
private void assertProperty( long node, String propertyKey, Object value ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getNodeById( node ).getProperty( propertyKey ), equalTo( value ) ); } } }
private Thread newThreadForNodeAction( final long nodeId, final Consumer<Node> nodeConsumer ) { return new Thread( () -> { try ( Transaction tx = db.beginTx() ) { Node node = db.getNodeById( nodeId ); barrier.await(); nodeConsumer.accept( node ); tx.success(); } catch ( Exception e ) { ex.set( e ); } } ); }
private void assertLabels( long nodeId, String label ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getNodeById( nodeId ).getLabels(), containsInAnyOrder( label( label ) ) ); } }
private void assertNoLabels( long nodeId ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( graphDb.getNodeById( nodeId ).getLabels(), equalTo( Iterables.empty() ) ); } }
private void deleteNode( long node ) { try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { graphDb.getNodeById( node ).delete(); ctx.success(); } }
private void assertNoProperty( long node, String propertyKey ) { try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertFalse( graphDb.getNodeById( node ).hasProperty( propertyKey ) ); } }
private void deleteNode( long nodeId ) { try ( Transaction tx = db.beginTx() ) { db.getNodeById( nodeId ).delete(); tx.success(); } }
private Node getOrCreateANode( GraphDatabaseService db ) { try ( Transaction tx = db.beginTx() ) { Node node; try { node = db.getNodeById( 0L ); } catch ( NotFoundException e ) { node = db.createNode(); } tx.success(); return node; } }
private boolean changeName( long nodeId, Object newValue ) { boolean changeIndexedNode = false; try ( Transaction tx = db.beginTx() ) { Node node = db.getNodeById( nodeId ); Object oldValue = node.getProperty( NAME_PROPERTY ); if ( !oldValue.equals( newValue ) ) { // Changes are only propagated when the value actually change changeIndexedNode = true; } node.setProperty( NAME_PROPERTY, newValue ); tx.success(); } return changeIndexedNode; }
@Override public void createTestGraph( GraphDatabaseService graphDb ) { try ( Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < N_NODES; i++ ) { nodeIds.add( graphDb.createNode( Label.label( "LABEL" + i ) ).getId() ); } tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { for ( int i = 0; i < N_RELATIONSHIPS; i++ ) { Long source = nodeIds.get( random.nextInt( N_NODES ) ); Long target = nodeIds.get( random.nextInt( N_NODES ) ); graphDb.getNodeById( source ).createRelationshipTo( graphDb.getNodeById( target ), RelationshipType.withName( "REL" + (i % 10) ) ); } tx.success(); } }
@PluginTarget( GraphDatabaseService.class ) public Node methodWithIntParam( @Source GraphDatabaseService db, @Parameter( name = "id", optional = false ) int id ) { try ( Transaction tx = db.beginTx() ) { Node node = db.getNodeById( id ); tx.success(); return node; } }
@PluginTarget( Node.class ) public Node getThisNodeOrById( @Source Node start, @Parameter( name = "id", optional = true ) Long id ) { optional = id; if ( id == null ) { return start; } try ( Transaction tx = start.getGraphDatabase().beginTx() ) { Node node = start.getGraphDatabase().getNodeById( id ); tx.success(); return node; } }
@Test public void shouldRollbackOnFailure() throws Exception { long node; try ( Transaction tx = beginTransaction() ) { node = tx.dataWrite().nodeCreate(); tx.failure(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { graphDb.getNodeById( node ); fail( "There should be no node" ); } catch ( NotFoundException e ) { // expected } }
@Test public void shouldCreateNode() throws Exception { long node; try ( Transaction tx = beginTransaction() ) { node = tx.dataWrite().nodeCreate(); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertEquals( node, graphDb.getNodeById( node ).getId() ); } }
@Test public void shouldBeAbleToMakeRepeatedCallsToSetNodePropertyWithMultiplePropertiesPerBlock() throws Exception { File file = dbRule.databaseLayout().databaseDirectory(); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); long nodeId = inserter.createNode( Collections.emptyMap() ); final Object finalValue1 = 87; final Object finalValue2 = 3.14; inserter.setNodeProperty( nodeId, "a", "some property value" ); inserter.setNodeProperty( nodeId, "a", 42 ); inserter.setNodeProperty( nodeId, "b", finalValue2 ); inserter.setNodeProperty( nodeId, "a", finalValue2 ); inserter.setNodeProperty( nodeId, "a", true ); inserter.setNodeProperty( nodeId, "a", finalValue1 ); inserter.shutdown(); GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction ignored = db.beginTx() ) { assertThat( db.getNodeById( nodeId ).getProperty( "a" ), equalTo( finalValue1 ) ); assertThat( db.getNodeById( nodeId ).getProperty( "b" ), equalTo( finalValue2 ) ); } finally { db.shutdown(); } }
@Test public void shouldRemoveNode() throws Exception { long node = createNode(); try ( Transaction tx = beginTransaction() ) { tx.dataWrite().nodeDelete( node ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { try { graphDb.getNodeById( node ); fail( "Did not remove node" ); } catch ( NotFoundException e ) { // expected } } }
private void changeRandomNode( GraphDatabaseService db, int nodeCount, RandomValues random ) { try ( Transaction tx = db.beginTx() ) { long nodeId = random.nextInt( nodeCount ); Node node = db.getNodeById( nodeId ); Object[] keys = Iterables.asCollection( node.getPropertyKeys() ).toArray(); String key = (String) random.among( keys ); if ( random.nextFloat() < 0.1 ) { // REMOVE node.removeProperty( key ); } else { // CHANGE node.setProperty( key, random.nextValue().asObject() ); } tx.success(); } catch ( NotFoundException e ) { // It's OK, it happens if some other thread deleted that property in between us reading it and // removing or setting it } }
@Test public void shouldBeAbleToMakeRepeatedCallsToSetNodeProperty() throws Exception { File file = dbRule.databaseLayout().databaseDirectory(); BatchInserter inserter = BatchInserters.inserter( file, fileSystemRule.get() ); long nodeId = inserter.createNode( Collections.emptyMap() ); final Object finalValue = 87; inserter.setNodeProperty( nodeId, "a", "some property value" ); inserter.setNodeProperty( nodeId, "a", 42 ); inserter.setNodeProperty( nodeId, "a", 3.14 ); inserter.setNodeProperty( nodeId, "a", true ); inserter.setNodeProperty( nodeId, "a", finalValue ); inserter.shutdown(); GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction ignored = db.beginTx() ) { assertThat( db.getNodeById( nodeId ).getProperty( "a" ), equalTo( finalValue ) ); } finally { db.shutdown(); } }