@Description( "Create a RelationshipType" ) @Procedure( name = "db.createRelationshipType", mode = WRITE ) public void createRelationshipType( @Name( "newRelationshipType" ) String newRelationshipType ) throws IllegalTokenNameException { tx.tokenWrite().relationshipTypeGetOrCreateForName( newRelationshipType ); }
private void createRelationship( long startNode, String type, long endNode, List<String> propKeys, List<Value> propValues ) throws Throwable { assert type != null && !type.equals( "" ); assert propKeys.size() == propValues.size(); Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int typeId = transaction.tokenWrite().relationshipTypeGetOrCreateForName( type ); long relId = transaction.dataWrite().relationshipCreate( startNode, typeId, endNode ); for ( int i = 0; i < propKeys.size(); i++ ) { String propKeyName = propKeys.get( i ); Value propValue = propValues.get( i ); int propKeyId = transaction.tokenWrite().propertyKeyGetOrCreateForName( propKeyName ); transaction.dataWrite().relationshipSetProperty( relId, propKeyId, propValue ); } commit(); }
@Before public void createLabelAndProperty() throws Exception { TokenWrite tokenWrites = tokenWriteInNewTransaction(); labelId = tokenWrites.labelGetOrCreateForName( LABEL ); labelId2 = tokenWrites.labelGetOrCreateForName( LABEL2 ); relType = tokenWrites.relationshipTypeGetOrCreateForName( REL_TYPE ); relType2 = tokenWrites.relationshipTypeGetOrCreateForName( REL_TYPE2 ); propertyKeyId = tokenWrites.propertyKeyGetOrCreateForName( PROPERTY_KEY ); propertyKeyId2 = tokenWrites.propertyKeyGetOrCreateForName( PROPERTY_KEY2 ); descriptor = SchemaDescriptorFactory.forLabel( labelId, propertyKeyId ); descriptor2 = SchemaDescriptorFactory.forLabel( labelId, propertyKeyId2 ); commit(); executorService = Executors.newCachedThreadPool(); }
@Test public void shouldReturnRelsWhenAskingForRelsWhereOnlySomeTypesExistInCurrentRel() throws Exception { Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int relType1 = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type1" ); int relType2 = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type2" ); long refNode = transaction.dataWrite().nodeCreate(); long otherNode = transaction.dataWrite().nodeCreate(); long theRel = transaction.dataWrite().relationshipCreate( refNode, relType1, otherNode ); assertRels( nodeGetRelationships( transaction, refNode, OUTGOING, new int[]{relType2,relType1} ), theRel ); commit(); }
@Before public void setUp() throws Exception { try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); SchemaWrite schemaWrite = transaction.schemaWrite(); Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetAll(); while ( constraints.hasNext() ) { schemaWrite.constraintDrop( constraints.next() ); } Iterator<IndexReference> indexes = schemaRead.indexesGetAll(); while ( indexes.hasNext() ) { schemaWrite.indexDrop( indexes.next() ); } TokenWrite tokenWrite = transaction.tokenWrite(); label = tokenWrite.labelGetOrCreateForName( "label" ); label2 = tokenWrite.labelGetOrCreateForName( "label2" ); type = tokenWrite.relationshipTypeGetOrCreateForName( "relationship" ); prop1 = tokenWrite.propertyKeyGetOrCreateForName( "prop1" ); prop2 = tokenWrite.propertyKeyGetOrCreateForName( "prop2" ); prop3 = tokenWrite.propertyKeyGetOrCreateForName( "prop3" ); transaction.success(); } }
@Test public void listRelationshipTypes() throws Throwable { // Given Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int relType = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "MyRelType" ); long startNodeId = transaction.dataWrite().nodeCreate(); long endNodeId = transaction.dataWrite().nodeCreate(); transaction.dataWrite().relationshipCreate( startNodeId, relType, endNodeId ); commit(); // When RawIterator<Object[],ProcedureException> stream = procs().procedureCallRead( procs().procedureGet( procedureName( "db", "relationshipTypes" ) ).id(), new Object[0] ); // Then assertThat( asList( stream ), contains( equalTo( new Object[]{"MyRelType"} ) ) ); }
private void getOrCreateIds() throws KernelException { try ( Transaction tx = graphDb.beginTx() ) { ThreadToStatementContextBridge bridge = graphDb.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ); TokenWrite tokenWrite = bridge.getKernelTransactionBoundToThisThread( true ).tokenWrite(); labelId = tokenWrite.labelGetOrCreateForName( "Person" ); relTypeId = tokenWrite.relationshipTypeGetOrCreateForName( "Knows" ); propertyKeyId = tokenWrite.propertyKeyGetOrCreateForName( "name" ); relPropertyKeyId = tokenWrite.propertyKeyGetOrCreateForName( "duration" ); tx.success(); } }
@Test public void askingForNonExistantReltypeOnDenseNodeShouldNotCorruptState() throws Exception { // Given a dense node with one type of rels long[] rels = new long[200]; long refNode; int relTypeTheNodeDoesUse; int relTypeTheNodeDoesNotUse; { Transaction transaction = newTransaction( AnonymousContext.writeToken() ); relTypeTheNodeDoesUse = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type1" ); relTypeTheNodeDoesNotUse = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "Type2" ); refNode = transaction.dataWrite().nodeCreate(); long otherNode = transaction.dataWrite().nodeCreate(); for ( int i = 0; i < rels.length; i++ ) { rels[i] = transaction.dataWrite().relationshipCreate( refNode, relTypeTheNodeDoesUse, otherNode ); } commit(); } Transaction transaction = newTransaction(); // When I've asked for rels that the node does not have assertRels( nodeGetRelationships( transaction, refNode, Direction.INCOMING, new int[]{relTypeTheNodeDoesNotUse} ) ); // Then the node should still load the real rels assertRels( nodeGetRelationships( transaction, refNode, Direction.BOTH, new int[]{relTypeTheNodeDoesUse} ), rels ); commit(); }
@BeforeClass public static void initStorage() throws Exception { try ( Transaction transaction = db.beginTx() ) { TokenWrite tokenWrite = getTransaction().tokenWrite(); tokenWrite.propertyKeyGetOrCreateForName( PROP1 ); tokenWrite.propertyKeyGetOrCreateForName( PROP2 ); tokenWrite.labelGetOrCreateForName( LABEL1 ); tokenWrite.labelGetOrCreateForName( LABEL2 ); tokenWrite.relationshipTypeGetOrCreateForName( TYPE1 ); transaction.success(); } SchemaStore schemaStore = resolveDependency( RecordStorageEngine.class ).testAccessNeoStores().getSchemaStore(); storage = new SchemaStorage( schemaStore ); }
@Test public void shouldNotAllowModifyingPropertiesOnDeletedRelationship() throws Exception { // given Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int prop1 = transaction.tokenWrite().propertyKeyGetOrCreateForName( "prop1" ); int type = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "RELATED" ); long startNodeId = transaction.dataWrite().nodeCreate(); long endNodeId = transaction.dataWrite().nodeCreate(); long rel = transaction.dataWrite().relationshipCreate( startNodeId, type, endNodeId ); transaction.dataWrite().relationshipSetProperty( rel, prop1, Values.stringValue( "As" ) ); transaction.dataWrite().relationshipDelete( rel ); // When try { transaction.dataWrite().relationshipRemoveProperty( rel, prop1 ); fail( "Should have failed." ); } catch ( EntityNotFoundException e ) { assertThat( e.getMessage(), equalTo( "Unable to load RELATIONSHIP with id " + rel + "." ) ); } commit(); }
@Test public void shouldNotCountRemovedRelationships() throws Exception { int relationshipId; long relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); relationshipId = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relationship = write.relationshipCreate( write.nodeCreate(), relationshipId, write.nodeCreate() ); tx.success(); } try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); write.relationshipDelete( relationship ); long countsTxState = tx.dataRead().countsForRelationship( -1, relationshipId, -1 ); long countsNoTxState = tx.dataRead().countsForRelationshipWithoutTxState( -1, relationshipId, -1 ); assertEquals( 0, countsTxState ); assertEquals( 1, countsNoTxState ); } }
@Test public void shouldCountNewRelationships() throws Exception { int relationship; try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); relationship = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); write.relationshipCreate( write.nodeCreate(), relationship, write.nodeCreate() ); tx.success(); } try ( Transaction tx = beginTransaction() ) { Write write = tx.dataWrite(); write.relationshipCreate( write.nodeCreate(), relationship, write.nodeCreate() ); long countsTxState = tx.dataRead().countsForRelationship( -1, relationship, -1 ); long countsNoTxState = tx.dataRead().countsForRelationshipWithoutTxState( -1, relationship, -1 ); assertEquals( 2, countsTxState ); assertEquals( 1, countsNoTxState ); } }
@Test public void shouldScanRelationshipInTransaction() throws Exception { final int nRelationshipsInStore = 10; int type; long n1, n2; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); // setup some in store relationships type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relateNTimes( nRelationshipsInStore, type, n1, n2, tx ); tx.success(); } try ( Transaction tx = beginTransaction() ) { long r = tx.dataWrite().relationshipCreate( n1, type, n2 ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().allRelationshipsScan( relationship ); assertCountRelationships( relationship, nRelationshipsInStore + 1, n1, type, n2 ); } tx.success(); } }
@Test public void shouldBeAbleToRemoveResetAndTwiceRemovePropertyOnRelationship() throws Exception { // given Transaction transaction = newTransaction( AnonymousContext.writeToken() ); int prop = transaction.tokenWrite().propertyKeyGetOrCreateForName( "foo" ); int type = transaction.tokenWrite().relationshipTypeGetOrCreateForName( "RELATED" ); long startNodeId = transaction.dataWrite().nodeCreate(); long endNodeId = transaction.dataWrite().nodeCreate(); long rel = transaction.dataWrite().relationshipCreate( startNodeId, type, endNodeId ); transaction.dataWrite().relationshipSetProperty( rel, prop, Values.of( "bar" ) ); commit(); // when Write write = dataWriteInNewTransaction(); write.relationshipRemoveProperty( rel, prop ); write.relationshipSetProperty( rel, prop, Values.of( "bar" ) ); write.relationshipRemoveProperty( rel, prop ); write.relationshipRemoveProperty( rel, prop ); commit(); // then transaction = newTransaction(); assertThat( relationshipGetProperty(transaction, rel, prop ), equalTo( Values.NO_VALUE ) ); commit(); } }
@Test public void testManualRelationshipIndexPopulation() throws Exception { RelationTypeSchemaDescriptor descriptor; try ( org.neo4j.internal.kernel.api.Transaction tx = ((GraphDatabaseAPI) database).getDependencyResolver().resolveDependency( Kernel.class ).beginTransaction( explicit, AUTH_DISABLED ) ) { int foodId = tx.tokenWrite().relationshipTypeGetOrCreateForName( FOOD_LABEL ); int propertyId = tx.tokenWrite().propertyKeyGetOrCreateForName( PROPERTY_NAME ); descriptor = forRelType( foodId, propertyId ); tx.schemaWrite().indexCreate( descriptor ); tx.success(); } IndexingService indexingService = getIndexingService( database ); IndexProxy indexProxy = indexingService.getIndexProxy( descriptor ); waitIndexOnline( indexProxy ); assertEquals( InternalIndexState.ONLINE, indexProxy.getState() ); PopulationProgress progress = indexProxy.getIndexPopulationProgress(); assertEquals( progress.getCompleted(), progress.getTotal() ); }
@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 shouldNotScanRelationshipWhichWasDeletedInTransaction() throws Exception { final int nRelationshipsInStore = 5 + 1 + 5; int type; long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); type = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); relateNTimes( 5, type, n1, n2, tx ); r = tx.dataWrite().relationshipCreate( n1, type, n2 ); relateNTimes( 5, type, n1, n2, tx ); tx.success(); } try ( Transaction tx = beginTransaction() ) { assertTrue( "should delete relationship", tx.dataWrite().relationshipDelete( r ) ); try ( RelationshipScanCursor relationship = tx.cursors().allocateRelationshipScanCursor() ) { tx.dataRead().allRelationshipsScan( relationship ); assertCountRelationships( relationship, nRelationshipsInStore - 1, n1, type, n2 ); } tx.success(); } }
@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 shouldNotSeeRelationshipDeletedInTransaction() throws Exception { long n1, n2, r; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); int label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); r = tx.dataWrite().relationshipCreate( n1, label, n2 ); tx.success(); } try ( Transaction tx = beginTransaction() ) { tx.dataWrite().relationshipDelete( r ); try ( NodeCursor node = tx.cursors().allocateNodeCursor(); RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor() ) { tx.dataRead().singleNode( n1, node ); assertTrue( "should access node", node.next() ); node.allRelationships( relationship ); assertFalse( "should not find relationship", relationship.next() ); } tx.success(); } }
@Test public void shouldSeeRelationshipInTransaction() throws Exception { long n1, n2; try ( Transaction tx = beginTransaction() ) { n1 = tx.dataWrite().nodeCreate(); n2 = tx.dataWrite().nodeCreate(); tx.success(); } try ( Transaction tx = beginTransaction() ) { int label = tx.tokenWrite().relationshipTypeGetOrCreateForName( "R" ); long r = tx.dataWrite().relationshipCreate( n1, label, n2 ); try ( NodeCursor node = tx.cursors().allocateNodeCursor(); RelationshipTraversalCursor relationship = tx.cursors().allocateRelationshipTraversalCursor() ) { tx.dataRead().singleNode( n1, node ); assertTrue( "should access node", node.next() ); node.allRelationships( relationship ); assertTrue( "should find relationship", relationship.next() ); assertEquals( r, relationship.relationshipReference() ); assertFalse( "should only find one relationship", relationship.next() ); } tx.success(); } }