private long createNodeWithRelationships( int count ) { GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); for ( int i = 0; i < count; i++ ) { node.createRelationshipTo( db.createNode(), MyRelTypes.TEST ); } tx.success(); return node.getId(); } }
private void createNodeWith( String key ) { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); node.setProperty( key, 1 ); tx.success(); } } }
public GraphDatabaseAPI restartDatabase( RestartAction action, String... configChanges ) throws IOException { FileSystemAbstraction fs = resolveDependency( FileSystemAbstraction.class ); database.shutdown(); action.run( fs, databaseLayout ); database = null; // This DatabaseBuilder has already been configured with the global settings as well as any test-specific settings, // so just apply these additional settings. databaseBuilder.setConfig( stringMap( configChanges ) ); return getGraphDatabaseAPI(); }
private Node createSomeData() { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); node.createRelationshipTo( db.createNode(), MyRelTypes.TEST ); node.createRelationshipTo( db.createNode(), MyRelTypes.TEST2 ); tx.success(); return node; } } }
@Test public void shouldAccountForDeletedNodes() { // given GraphDatabaseService graphDb = db.getGraphDatabaseAPI(); Node node; try ( Transaction tx = graphDb.beginTx() ) { node = graphDb.createNode( label( "Foo" ) ); graphDb.createNode( label( "Foo" ) ); tx.success(); } try ( Transaction tx = graphDb.beginTx() ) { node.delete(); tx.success(); } // when long fooCount = numberOfNodesWith( label( "Foo" ) ); // then assertEquals( 1, fooCount ); }
@Test public void shouldCountRelationshipsByType() { // given final GraphDatabaseService graphDb = db.getGraphDatabaseAPI(); try ( Transaction tx = graphDb.beginTx() ) { graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "FOO" ) ); graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "FOO" ) ); graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "BAR" ) ); graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "BAR" ) ); graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "BAR" ) ); graphDb.createNode().createRelationshipTo( graphDb.createNode(), withName( "BAZ" ) ); tx.success(); } // when long total = numberOfRelationships( ); long foo = numberOfRelationships( withName( "FOO" ) ); long bar = numberOfRelationships( withName( "BAR" ) ); long baz = numberOfRelationships( withName( "BAZ" ) ); long qux = numberOfRelationships( withName( "QUX" ) ); // then assertEquals( 2, foo ); assertEquals( 3, bar ); assertEquals( 1, baz ); assertEquals( 0, qux ); assertEquals( 6, total ); }
@Test public void shouldMaintainCountsOnRelationshipDelete() { // given Relationship relationship; try ( Transaction tx = db.beginTx() ) { relationship = db.createNode( label( "Foo" ) ).createRelationshipTo( db.createNode( label( "Bar" ) ), withName( "KNOWS" ) ); tx.success(); } // when try ( Transaction tx = db.beginTx() ) { relationship.delete(); tx.success(); } // then numberOfRelationshipsMatching( label( "Foo" ), withName( "KNOWS" ), null ).shouldBe( 0 ); numberOfRelationshipsMatching( null, withName( "KNOWS" ), label( "Foo" ) ).shouldBe( 0 ); numberOfRelationshipsMatching( null, withName( "KNOWS" ), label( "Bar" ) ).shouldBe( 0 ); numberOfRelationshipsMatching( label( "Bar" ), withName( "KNOWS" ), null ).shouldBe( 0 ); }
@Test public void canAvoidDeadlockThatWouldHappenIfTheRelationshipTypeCreationTransactionModifiedData() { GraphDatabaseService graphdb = database.getGraphDatabaseAPI(); Node node = null; try ( Transaction tx = graphdb.beginTx() ) { node = graphdb.createNode(); node.setProperty( "counter", 0L ); tx.success(); } graphdb.registerTransactionEventHandler( new RelationshipCounterTransactionEventHandler( node ) ); try ( Transaction tx = graphdb.beginTx() ) { node.setProperty( "state", "not broken yet" ); node.createRelationshipTo( graphdb.createNode(), RelationshipType.withName( "TEST" ) ); node.removeProperty( "state" ); tx.success(); } assertThat( node, inTx( graphdb, hasProperty( "counter" ).withValue( 1L ) ) ); }
@Test public void shouldUpdateRelationshipWithLabelCountsWhenDeletingNodeWithRelationship() { // given Node foo; try ( Transaction tx = db.beginTx() ) { foo = db.createNode( label( "Foo" ) ); Node bar = db.createNode( label( "Bar" ) ); foo.createRelationshipTo( bar, withName( "BAZ" ) ); tx.success(); } long before = numberOfRelationshipsMatching( label( "Foo" ), withName( "BAZ" ), null ); // when try ( Transaction tx = db.beginTx() ) { for ( Relationship relationship : foo.getRelationships() ) { relationship.delete(); } foo.delete(); tx.success(); } long after = numberOfRelationshipsMatching( label( "Foo" ), withName( "BAZ" ), null ); // then assertEquals( before - 1, after ); }
@Test public void lookupWithinTransaction() { try ( Transaction tx = db.beginTx() ) { // when db.createNode( label( "Node" ) ).setProperty( "prop", store ); // then assertEquals( 1, count( db.findNodes( label( "Node" ), "prop", lookup ) ) ); // no need to actually commit this node } }
@Before public void prepDB() { try ( Transaction transaction = db.beginTx() ) { node1 = db.createNode( label( "hej" ), label( "ha" ), label( "he" ) ); node1.setProperty( "hej", "value" ); node1.setProperty( "ha", "value1" ); node1.setProperty( "he", "value2" ); node1.setProperty( "ho", "value3" ); node1.setProperty( "hi", "value4" ); node2 = db.createNode(); Relationship rel = node1.createRelationshipTo( node2, RelationshipType.withName( "hej" ) ); rel.setProperty( "hej", "valuuu" ); rel.setProperty( "ha", "value1" ); rel.setProperty( "he", "value2" ); rel.setProperty( "ho", "value3" ); rel.setProperty( "hi", "value4" ); transaction.success(); } }
@Test public void shouldPrintCypherEsqueRelationshipToString() { // GIVEN Node start; Node end; RelationshipType type = RelationshipType.withName( "NICE" ); Relationship relationship; try ( Transaction tx = db.beginTx() ) { // GIVEN start = db.createNode(); end = db.createNode(); relationship = start.createRelationshipTo( end, type ); tx.success(); // WHEN String toString = relationship.toString(); // THEN assertEquals( "(" + start.getId() + ")-[" + type + "," + relationship.getId() + "]->(" + end.getId() + ")", toString ); } }
@Test public void makeSureHandlersCantBeRegisteredTwice() { DummyTransactionEventHandler<Object> handler = new DummyTransactionEventHandler<>( null ); GraphDatabaseService db = dbRule.getGraphDatabaseAPI(); db.registerTransactionEventHandler( handler ); db.registerTransactionEventHandler( handler ); try ( Transaction tx = db.beginTx() ) { db.createNode().delete(); tx.success(); } assertEquals( Integer.valueOf( 0 ), handler.beforeCommit ); assertEquals( Integer.valueOf( 1 ), handler.afterCommit ); assertNull( handler.afterRollback ); db.unregisterTransactionEventHandler( handler ); }
@Before public void createDataset() { GraphDatabaseAPI db = dbr.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { node = db.createNode( label ); node.setProperty( "short", 123 ); node.setProperty( "long", longString( 300 ) ); relationship = node.createRelationshipTo( db.createNode(), MyRelTypes.TEST ); relationship.setProperty( "short", 123 ); relationship.setProperty( "long", longString( 300 ) ); tx.success(); } logEntriesWrittenBeforeReadOperations = countLogEntries(); }
@Test public void createAndQueryFulltextRelationshipIndex() throws Exception { FulltextIndexProvider provider = (FulltextIndexProvider) db.resolveDependency( IndexProviderMap.class ).lookup( DESCRIPTOR ); IndexReference indexReference; try ( KernelTransactionImplementation transaction = getKernelTransaction() ) { MultiTokenSchemaDescriptor multiTokenSchemaDescriptor = multiToken( new int[]{0, 1, 2}, EntityType.RELATIONSHIP, 0, 1, 2, 3 ); FulltextSchemaDescriptor schema = new FulltextSchemaDescriptor( multiTokenSchemaDescriptor, new Properties() ); indexReference = transaction.schemaWrite().indexCreate( schema, DESCRIPTOR.name(), Optional.of( "fulltext" ) ); transaction.success(); } await( indexReference ); long secondRelId; try ( Transaction transaction = db.beginTx() ) { Relationship ho = node1.createRelationshipTo( node2, RelationshipType.withName( "ho" ) ); secondRelId = ho.getId(); ho.setProperty( "hej", "villa" ); ho.setProperty( "ho", "value3" ); transaction.success(); } verifyRelationshipData( provider, secondRelId ); db.restartDatabase( DatabaseRule.RestartAction.EMPTY ); provider = (FulltextIndexProvider) db.resolveDependency( IndexProviderMap.class ).lookup( DESCRIPTOR ); verifyRelationshipData( provider, secondRelId ); }
@Test public void testRecovery() throws Exception { try ( Transaction tx = db.beginTx() ) { Node node = db.createNode(); Node otherNode = db.createNode(); Relationship rel = node.createRelationshipTo( otherNode, withName( "recovery" ) ); db.index().forNodes( "node-index" ).add( node, "key1", "string value" ); db.index().forNodes( "node-index" ).add( node, "key2", 12345 ); db.index().forRelationships( "rel-index" ).add( rel, "key1", "string value" ); db.index().forRelationships( "rel-index" ).add( rel, "key2", 12345 ); tx.success(); } forceRecover(); }
@Test public void shouldAllowConcurrentCreationOfNonConflictingData() throws Exception { // given database.executeAndCommit( createUniquenessConstraint( "Label1", "key1" ) ); // when Future<Boolean> created = database.executeAndCommit( db -> { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); return otherThread.execute( createNode( db, "Label1", "key1", "value2" ) ); } ); // then assertTrue( "Node creation should succeed", created.get() ); }
@Test public void shouldOnlyReturnTypeOnce() { // Given Node node; try ( Transaction tx = db.beginTx() ) { node = db.createNode(); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); node.createRelationshipTo( db.createNode(), RelationshipType.withName( "R" ) ); tx.success(); } // Then try ( Transaction tx = db.beginTx() ) { assertThat( Iterables.asList( node.getRelationshipTypes() ), equalTo( singletonList( RelationshipType.withName( "R" ) ) ) ); } }
/** * Transactional version of {@link #countsForRelationship(Label, RelationshipType, Label)} */ private MatchingRelationships numberOfRelationshipsMatching( Label lhs, RelationshipType type, Label rhs ) { try ( Transaction tx = db.getGraphDatabaseAPI().beginTx() ) { long nodeCount = countsForRelationship( lhs, type, rhs ); tx.success(); return new MatchingRelationships( String.format( "(%s)-%s->(%s)", lhs == null ? "" : ":" + lhs.name(), type == null ? "" : "[:" + type.name() + "]", rhs == null ? "" : ":" + rhs.name() ), nodeCount ); } }