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(); } } }
while ( relationshipScanCursor.next() ) int typeId = relationshipScanCursor.type(); relationshipScanCursor.properties( propertyCursor ); MutableIntSet propertyIds = IntSets.mutable.empty(); relationshipScanCursor.close();
@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 ); }
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 ); } } }
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; } } }
@Override public Iterable<String> getPropertyKeys() { KernelTransaction transaction = spi.kernelTransaction(); List<String> keys = new ArrayList<>(); try { RelationshipScanCursor relationships = transaction.ambientRelationshipCursor(); PropertyCursor properties = transaction.ambientPropertyCursor(); singleRelationship( transaction, relationships ); TokenRead token = transaction.tokenRead(); relationships.properties( properties ); while ( properties.next() ) { keys.add( token.propertyKeyName( properties.propertyKey() )); } } catch ( PropertyKeyIdNotFoundKernelException e ) { throw new IllegalStateException( "Property key retrieved through kernel API should exist.", e ); } return keys; }
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 ); } } }
@Override public void validateRelationshipPropertyExistenceConstraint( RelationshipScanCursor relationshipCursor, PropertyCursor propertyCursor, RelationTypeSchemaDescriptor descriptor ) throws CreateConstraintFailureException { while ( relationshipCursor.next() ) { relationshipCursor.properties( propertyCursor ); for ( int propertyKey : descriptor.getPropertyIds() ) { if ( relationshipCursor.type() == descriptor.getRelTypeId() && !hasProperty( propertyCursor, propertyKey ) ) { throw createConstraintFailure( new RelationshipPropertyExistenceException( descriptor, VERIFICATION, relationshipCursor.relationshipReference() ) ); } } } }
@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() ); } } }
@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 ); }
@Override public void compute(int iterations, RelationshipType... relationshipTypes) { stats.iterations = iterations; long start = System.currentTimeMillis(); final int[] src = new int[nodeCount]; dst = new AtomicIntegerArray(nodeCount); final int[] degrees = computeDegrees(); stats.readNodeMillis = System.currentTimeMillis() - start; stats.nodes = nodeCount; start = System.currentTimeMillis(); int[] relationshipTypesIds = fetchRelationshipTypeIds(relationshipTypes); stats.readRelationshipMillis = System.currentTimeMillis() - start; stats.relationships = relCount; start = System.currentTimeMillis(); for (int iteration = 0; iteration < iterations; iteration++) { startIteration(src, dst, degrees); MultiThreadedGlobalGraphOperations.forAllRelationships(db, pool, BATCH_SIZE, (ktx, relationshipScanCursor) -> { if (relationshipTypes.length == 0 || contains(relationshipTypesIds, relationshipScanCursor.type())) { int endNode = (int) relationshipScanCursor.targetNodeReference(); int startNode = (int) relationshipScanCursor.sourceNodeReference(); dst.addAndGet(endNode, src[startNode]); } }); } stats.computeMillis = System.currentTimeMillis() - start; }
@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 ) ); } } }
PropertyCursor propertyCursor = transaction.ambientPropertyCursor(); singleRelationship( transaction, relationships ); relationships.properties( propertyCursor ); int propertiesToFind = itemsToReturn; while ( propertiesToFind > 0 && propertyCursor.next() )
private void singleRelationship( KernelTransaction transaction, RelationshipScanCursor relationships ) { transaction.dataRead().singleRelationship( id, relationships ); if ( !relationships.next() ) { throw new NotFoundException( new EntityNotFoundException( EntityType.RELATIONSHIP, id ) ); } } }
assertTrue( "should access relationship", relationship.next() ); relationship.properties( property ); assertTrue( property.next() ); assertEquals( propToken, property.propertyKey() ); assertFalse( "should only find one relationship", relationship.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 ); }
while ( relationshipScanCursor.next() ) int typeId = relationshipScanCursor.type(); relationshipScanCursor.properties( propertyCursor ); MutableIntSet propertyIds = IntSets.mutable.empty(); relationshipScanCursor.close();
/** * Fetches a given property from a relationship * * @param read The current Read instance * @param relationship The node cursor to use * @param node The id of the node * @param propertyCursor The property cursor to use * @param prop The id of the property to find * @return The value of the given property * @throws EntityNotFoundException If the node cannot be find. */ public static Value relationshipGetProperty( Read read, RelationshipScanCursor relationship, long node, PropertyCursor propertyCursor, int prop ) throws EntityNotFoundException { if ( prop == StatementConstants.NO_SUCH_PROPERTY_KEY ) { return Values.NO_VALUE; } singleRelationship( read, relationship, node ); relationship.properties( propertyCursor ); while ( propertyCursor.next() ) { if ( propertyCursor.propertyKey() == prop ) { return propertyCursor.propertyValue(); } } return Values.NO_VALUE; }
@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 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() ); } } }