private static long createRelationship( GraphDatabaseService db ) { long relationshipId; try ( Transaction tx = db.beginTx() ) { Node start = db.createNode( Label.label( System.currentTimeMillis() + "" ) ); Node end = db.createNode( Label.label( System.currentTimeMillis() + "" ) ); relationshipId = start.createRelationshipTo( end, withName( "KNOWS" ) ).getId(); tx.success(); } return relationshipId; }
private void deleteNode( long nodeId ) { try ( Transaction tx = db.beginTx() ) { db.getNodeById( nodeId ).delete(); tx.success(); } }
private static void addNode( GraphDatabaseService db ) { if ( USE_CYPHER ) { Result result = db.execute( "create (:Person {name: 'Sneaky Steve'})" ); System.out.println( result.resultAsString() ); } else { try ( Transaction tx = db.beginTx() ) { db.createNode( label( "Person" ) ).setProperty( "name", "Sneaky Steve" ); tx.success(); } } }
private Node getNodeInTx( long nodeId, GraphDatabaseService db ) { try ( Transaction ignored = db.beginTx() ) { return db.getNodeById( nodeId ); } }
private long initWithRel( GraphDatabaseService db ) { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); node.setProperty( "a", "prop" ); Relationship rel = node.createRelationshipTo( db.createNode(), RelationshipType.withName( "T" ) ); long id = rel.getId(); tx.success(); return id; } }
private static Node createNode( GraphDatabaseService db, String propertyKey, Object value ) { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode( label( LABEL ) ); node.setProperty( propertyKey, value ); tx.success(); return node; } }
@Test public void mustBeAbleToConsistencycheckRelationshipIndexThatIsMissingRelationshipsBecauseTheirPropertyValuesaAreNotStrings() throws Exception { GraphDatabaseService db = createDatabase(); try ( Transaction tx = db.beginTx() ) { db.execute( format( RELATIONSHIP_CREATE, "rels", array( "R1" ), array( "p1" ) ) ).close(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); Node node = db.createNode(); node.createRelationshipTo( node, RelationshipType.withName( "R1" ) ).setProperty( "p1", 1 ); tx.success(); } db.shutdown(); assertIsConsistent( checkConsistency() ); }
private void createIndex() { try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { graphDb.schema().indexFor( Label.label( "Node" ) ).on( "prop" ).create(); tx.success(); } try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() ) { graphDb.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); } }
private static void createConstraint( GraphDatabaseService db ) { if ( USE_CYPHER ) { db.execute( "create constraint on (p:Person) assert p.name is unique" ); } else { try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( label( "Person" ) ).assertPropertyIsUnique( "name" ).create(); tx.success(); } } }
@Test public void mustBeAbleToConsistencyCheckNodeIndexWithOneLabelAndOneProperty() throws Exception { GraphDatabaseService db = createDatabase(); try ( Transaction tx = db.beginTx() ) { db.execute( format( NODE_CREATE, "nodes", array( "Label" ), array( "prop" ) ) ).close(); tx.success(); } try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); db.createNode( Label.label( "Label" ) ).setProperty( "prop", "value" ); tx.success(); } db.shutdown(); assertIsConsistent( checkConsistency() ); }
private void createData( GraphDatabaseService database, int numberOfNodes ) { for ( int i = 0; i < numberOfNodes; i++ ) { try ( Transaction transaction = database.beginTx() ) { Node node = database.createNode( Label.label( FOOD_LABEL ), Label.label( CLOTHES_LABEL ), Label.label( WEATHER_LABEL ) ); node.setProperty( PROPERTY_NAME, "Node" + i ); Relationship relationship = node.createRelationshipTo( node, RelationshipType.withName( FOOD_LABEL ) ); relationship.setProperty( PROPERTY_NAME, "Relationship" + i ); transaction.success(); } } }
private long createNodeWithLabel( String labelName ) { long node; try ( org.neo4j.graphdb.Transaction ctx = graphDb.beginTx() ) { node = graphDb.createNode( label( labelName ) ).getId(); ctx.success(); } return node; }
public static void destroy( Map<String, Node> nodes ) { if ( nodes.isEmpty() ) { return; } GraphDatabaseService db = nodes.values().iterator().next().getGraphDatabase(); try ( Transaction tx = db.beginTx() ) { for ( Node node : db.getAllNodes() ) { for ( Relationship rel : node.getRelationships() ) { rel.delete(); } node.delete(); } tx.success(); } }
@Test void latestRecordNotMigratedWhenFormatBumped() { GraphDatabaseService database = startDatabaseWithFormatUnspecifiedUpgrade( storeDir, StandardV3_2.NAME ); try ( Transaction transaction = database.beginTx() ) { Node node = database.createNode(); node.setProperty( "a", "b" ); transaction.success(); } database.shutdown(); Throwable exception = assertThrows( Throwable.class, () -> startDatabaseWithFormatUnspecifiedUpgrade( storeDir, Standard.LATEST_NAME ) ); assertSame( UpgradeNotAllowedByConfigurationException.class, Exceptions.rootCause( exception ).getClass() ); }
@Test public void testRollbackDeleteRelationship() { Node node1 = getGraphDb().createNode(); Node node2 = getGraphDb().createNode(); Relationship rel1 = node1.createRelationshipTo( node2, TEST ); newTransaction(); node1.delete(); rel1.delete(); getTransaction().failure(); getTransaction().close(); setTransaction( getGraphDb().beginTx() ); node1.delete(); node2.delete(); rel1.delete(); }
public static void cleanupAllRelationshipsAndNodes( GraphDatabaseService db ) { try ( Transaction tx = db.beginTx() ) { for ( Relationship relationship : db.getAllRelationships() ) { relationship.delete(); } for ( Node node : db.getAllNodes() ) { node.delete(); } tx.success(); } } }
private void createRelationshipsOnNode( GraphDatabaseService db, Node root, int numberOfRelationships ) { for ( int i = 0; i < numberOfRelationships; i++ ) { root.createRelationshipTo( db.createNode(), RelationshipType.withName( "Type" + (i % 4) ) ) .setProperty( "" + i, i ); } }
private long nodeWithWhatever( GraphDatabaseService graphDb, Object value ) { Node node = graphDb.createNode( label( "What" ) ); node.setProperty( "ever", value ); return node.getId(); }
@Name( GET_CONNECTED_NODES ) @PluginTarget( Node.class ) public Iterable<Node> getAllConnectedNodes( @Source Node start ) { ArrayList<Node> nodes = new ArrayList<>(); try ( Transaction tx = start.getGraphDatabase().beginTx() ) { for ( Relationship rel : start.getRelationships() ) { nodes.add( rel.getOtherNode( start ) ); } tx.success(); } return nodes; }