private static void singleRelationship( Read read, RelationshipScanCursor relationships, long relationship ) throws EntityNotFoundException { read.singleRelationship( relationship, relationships ); if ( !relationships.next() ) { throw new EntityNotFoundException( EntityType.NODE, relationship ); } } }
private void indexRelationship( long id ) { if ( !visitingNodes ) { read.singleRelationship( id, relationshipCursor ); if ( relationshipCursor.next() && schema.isAffected( new long[]{relationshipCursor.type()} ) ) { relationshipCursor.properties( propertyCursor ); indexProperties( id ); } } }
private void singleRelationship( KernelTransaction transaction, RelationshipScanCursor relationships ) { transaction.dataRead().singleRelationship( id, relationships ); if ( !relationships.next() ) { throw new NotFoundException( new EntityNotFoundException( EntityType.RELATIONSHIP, id ) ); } } }
@Test public void shouldScanRelationships() { // given List<Long> ids = new ArrayList<>(); try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { // when read.allRelationshipsScan( relationships ); while ( relationships.next() ) { ids.add( relationships.relationshipReference() ); } } assertEquals( RELATIONSHIP_IDS, ids ); }
@Test public void shouldAccessRelationshipLabels() { // given Map<Integer,Integer> counts = new HashMap<>(); try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { // when read.allRelationshipsScan( relationships ); while ( relationships.next() ) { counts.compute( relationships.type(), ( k, v ) -> v == null ? 1 : v + 1 ); } } // then assertEquals( 3, counts.size() ); int[] values = new int[3]; int i = 0; for ( int value : counts.values() ) { values[i++] = value; } Arrays.sort( values ); assertArrayEquals( new int[]{1, 6, 6}, values ); }
static int countRelationships( Transaction transaction ) { int result = 0; try ( RelationshipScanCursor cursor = transaction.cursors().allocateRelationshipScanCursor() ) { transaction.dataRead().allRelationshipsScan( cursor ); while ( cursor.next() ) { result++; } } return result; }
@Test public void shouldNotAccessNegativeReferences() { // given try ( RelationshipScanCursor relationship = cursors.allocateRelationshipScanCursor() ) { // when read.singleRelationship( -2L, relationship ); // then assertFalse( "should not access negative reference relationship", relationship.next() ); } }
@Test public void shouldNotAccessDeletedRelationship() { // given try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { // when read.singleRelationship( none, relationships ); // then assertFalse( "should not access deleted relationship", relationships.next() ); } }
@Test public void shouldAccessRelationshipByReference() { // given try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { for ( long id : RELATIONSHIP_IDS ) { // when read.singleRelationship( id, relationships ); // then assertTrue( "should access defined relationship", relationships.next() ); assertEquals( "should access the correct relationship", id, relationships.relationshipReference() ); assertFalse( "should only access a single relationship", relationships.next() ); } } }
private void initializeData() { // it enough to check only start node, since it's absence will indicate that data was not yet loaded if ( startNode == AbstractBaseRecord.NO_ID ) { KernelTransaction transaction = spi.kernelTransaction(); try ( Statement ignore = transaction.acquireStatement() ) { RelationshipScanCursor relationships = transaction.ambientRelationshipCursor(); transaction.dataRead().singleRelationship( id, relationships ); //at this point we don't care if it is there or not just load what we got relationships.next(); this.type = relationships.type(); this.startNode = relationships.sourceNodeReference(); this.endNode = relationships.targetNodeReference(); } } }
@Test public void shouldAccessNodes() { // given try ( RelationshipScanCursor relationships = cursors.allocateRelationshipScanCursor() ) { // when read.singleRelationship( one, relationships ); // then assertTrue( relationships.next() ); assertEquals( c, relationships.sourceNodeReference() ); assertEquals( d, relationships.targetNodeReference() ); assertFalse( relationships.next() ); // when read.singleRelationship( loop, relationships ); // then assertTrue( relationships.next() ); assertEquals( relationships.sourceNodeReference(), relationships.targetNodeReference() ); assertFalse( relationships.next() ); } } }
private void assertCountRelationships( RelationshipScanCursor relationship, int expectedCount, long sourceNode, int type, long targetNode ) { int count = 0; while ( relationship.next() ) { assertEquals( sourceNode, relationship.sourceNodeReference() ); assertEquals( type, relationship.type() ); assertEquals( targetNode, relationship.targetNodeReference() ); count++; } assertEquals( expectedCount, count ); }
Value relationshipGetProperty( Transaction transaction, long relationship, int property ) { try ( RelationshipScanCursor cursor = transaction.cursors().allocateRelationshipScanCursor(); PropertyCursor properties = transaction.cursors().allocatePropertyCursor() ) { transaction.dataRead().singleRelationship( relationship, cursor ); if ( !cursor.next() ) { return NO_VALUE; } else { cursor.properties( properties ); while ( properties.next() ) { if ( properties.propertyKey() == property ) { return properties.propertyValue(); } } return NO_VALUE; } } }
@Test public void propertyTypeShouldBeTxStateAware() throws Exception { // Given long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); tx.success(); } // Then try ( Transaction tx = beginTransaction() ) { try ( RelationshipScanCursor relationships = tx.cursors().allocateRelationshipScanCursor(); PropertyCursor properties = tx.cursors().allocatePropertyCursor() ) { tx.dataRead().singleRelationship( relationship, relationships ); assertTrue( relationships.next() ); assertFalse( hasProperties( relationships, tx ) ); int prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); tx.dataWrite().relationshipSetProperty( relationship, prop, stringValue( "foo" ) ); relationships.properties( properties ); assertTrue( properties.next() ); assertThat( properties.propertyType(), equalTo( ValueGroup.TEXT ) ); } } }
@Test public void hasPropertiesShouldSeeNewlyCreatedPropertiesOnNewlyCreatedRelationship() throws Exception { try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); try ( RelationshipScanCursor cursor = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().singleRelationship( relationship, cursor ); assertTrue( cursor.next() ); assertFalse( hasProperties( cursor, tx ) ); tx.dataWrite().relationshipSetProperty( relationship, tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ), stringValue( "foo" ) ); assertTrue( hasProperties( cursor, tx ) ); } } }
@Test public void shouldNotSeeSingleRelationshipWhichWasDeletedInTransaction() throws Exception { int label; long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long decoyNode = tx.dataWrite().nodeCreate(); tx.dataWrite().relationshipCreate( n2, label, decoyNode ); // to have >1 relationship in the db r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().singleRelationship( r, relationship ); assertFalse( "should not find relationship", relationship.next() ); } tx.success(); } }
@Test public void hasPropertiesShouldSeeNewlyCreatedProperties() throws Exception { // Given long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); int token = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), token, write.nodeCreate() ); tx.success(); } // Then try ( Transaction tx = beginTransaction() ) { try ( RelationshipScanCursor cursor = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().singleRelationship( relationship, cursor ); assertTrue( cursor.next() ); assertFalse( hasProperties( cursor, tx ) ); tx.dataWrite().relationshipSetProperty( relationship, tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ), stringValue( "foo" ) ); assertTrue( hasProperties( cursor, tx ) ); } } }
assertTrue( cursor.next() );