static Stream<RetrieveResult> retrieve( Kernel kernel, Anonymizer anonymizer ) throws TransactionFailureException, IndexNotFoundKernelException { try ( Transaction tx = kernel.beginTransaction( Transaction.Type.explicit, LoginContext.AUTH_DISABLED ) ) { TokenRead tokens = tx.tokenRead(); Read read = tx.dataRead(); Map<String,Object> data = new HashMap<>(); data.put( "nodes", nodeCounts( tokens, read, anonymizer ) ); data.put( "relationships", relationshipCounts( tokens, read, anonymizer ) ); data.put( "indexes", indexes( tokens, tx.schemaRead(), anonymizer ) ); data.put( "constraints", constraints( tokens, tx.schemaRead(), anonymizer ) ); return Stream.of( new RetrieveResult( Sections.GRAPH_COUNTS, data ) ); } }
private String getOrCreateFromState( Transaction tx, String key, final String value ) { return tx.schemaRead().schemaStateGetOrCreate( key, from -> value ); }
@Test public void shouldNotFindNonExistentIndex() throws Exception { try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertThat( schemaRead.index( label, prop1 ), equalTo( IndexReference.NO_INDEX ) ); } }
private void awaitSchemaStateCleared( String keyForProbing ) throws TransactionFailureException { try ( Transaction transaction = kernel.beginTransaction( implicit, AUTH_DISABLED ) ) { while ( transaction.schemaRead().schemaStateGetOrCreate( keyForProbing, ignored -> null ) != null ) { LockSupport.parkNanos( MILLISECONDS.toNanos( 10 ) ); } transaction.success(); } }
@Test public void shouldFindAllConstraints() throws Exception { // GIVEN addConstraints( "FOO", "prop1", "BAR", "prop2", "BAZ", "prop3" ); try ( Transaction tx = beginTransaction() ) { //WHEN List<ConstraintDescriptor> constraints = asList( tx.schemaRead().constraintsGetAll() ); // THEN assertThat( constraints, hasSize( 3 ) ); } }
@Test public void createdIndexShouldPopulateInTx() throws Exception { IndexReference index; try ( Transaction tx = beginTransaction() ) { index = tx.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); assertThat( tx.schemaRead().indexGetState( index ), equalTo( InternalIndexState.POPULATING ) ); tx.success(); } }
private void awaitIndexOnline( IndexReference descriptor, String keyForProbing ) throws IndexNotFoundKernelException, TransactionFailureException { try ( Transaction transaction = kernel.beginTransaction( implicit, AUTH_DISABLED ) ) { SchemaIndexTestHelper.awaitIndexOnline( transaction.schemaRead(), descriptor ); transaction.success(); } awaitSchemaStateCleared( keyForProbing ); }
@Test public void shouldFindConstraintsByLabel() throws Exception { // GIVEN addConstraints( "FOO", "prop1", "FOO", "prop2" ); try ( Transaction tx = beginTransaction() ) { int label = tx.tokenWrite().labelGetOrCreateForName( "FOO" ); //WHEN List<ConstraintDescriptor> constraints = asList( tx.schemaRead().constraintsGetForLabel( label ) ); // THEN assertThat( constraints, hasSize( 2 ) ); } }
@Test public void shouldCreateIndex() throws Exception { IndexReference index; try ( Transaction transaction = beginTransaction() ) { index = transaction.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertThat( schemaRead.index( label, prop1 ), equalTo( index ) ); } }
private IndexReference indexId( org.neo4j.internal.kernel.api.Transaction tx ) { int labelId = tx.tokenRead().nodeLabel( label.name() ); int propertyKeyId = tx.tokenRead().propertyKey( property ); return tx.schemaRead().index( labelId, propertyKeyId ); }
@Test public void shouldCreateNodePropertyExistenceConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().nodePropertyExistenceConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertTrue( schemaRead.constraintExists( constraint ) ); Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetForLabel( label ); assertThat( asList( constraints ), equalTo( singletonList( constraint ) ) ); } }
@Test public void shouldCreateUniquePropertyConstraint() throws Exception { ConstraintDescriptor constraint; try ( Transaction transaction = beginTransaction() ) { constraint = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { SchemaRead schemaRead = transaction.schemaRead(); assertTrue( schemaRead.constraintExists( constraint ) ); Iterator<ConstraintDescriptor> constraints = schemaRead.constraintsGetForLabel( label ); assertThat( asList( constraints ), equalTo( singletonList( constraint ) ) ); } }
@Test public void shouldListAll() throws Exception { // given SchemaWrite schemaWrite = schemaWriteInNewTransaction(); IndexReference index1 = schemaWrite.indexCreate( descriptor ); IndexReference index2 = ((IndexBackedConstraintDescriptor) schemaWrite.uniquePropertyConstraintCreate( descriptor2 )) .ownedIndexDescriptor(); commit(); // then/when SchemaRead schemaRead = newTransaction().schemaRead(); List<IndexReference> indexes = Iterators.asList( schemaRead.indexesGetAll() ); assertThat( indexes, containsInAnyOrder( index1, index2 ) ); commit(); }
@Test public void shouldNotSeeDroppedIndexFromTransaction() throws Exception { IndexReference index; try ( Transaction transaction = beginTransaction() ) { index = transaction.schemaWrite().indexCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().indexDrop( index ); SchemaRead schemaRead = transaction.schemaRead(); assertThat( schemaRead.index( label, prop2 ), equalTo( NO_INDEX ) ); } }
@Test public void rollBackIndexRuleShouldNotBeCommitted() throws Exception { // GIVEN SchemaWrite schemaWrite = schemaWriteInNewTransaction(); // WHEN schemaWrite.indexCreate( descriptor ); // don't mark as success rollback(); // THEN Transaction transaction = newTransaction(); assertEquals( emptySet(), asSet( transaction.schemaRead().indexesGetForLabel( labelId ) ) ); commit(); }
private IndexReference createUniquenessConstraint( int labelId, int... propertyIds ) throws Exception { Transaction transaction = newTransaction( LoginContext.AUTH_DISABLED ); LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( labelId, propertyIds ); transaction.schemaWrite().uniquePropertyConstraintCreate( descriptor ); IndexReference result = transaction.schemaRead().index( descriptor.getLabelId(), descriptor.getPropertyIds() ); commit(); return result; } }
@Test public void shouldNotSeeDroppedUniqueConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().uniquePropertyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Test public void shouldNotSeeDroppedNodeKeyConstraintFromTransaction() throws Exception { ConstraintDescriptor existing; try ( Transaction transaction = beginTransaction() ) { existing = transaction.schemaWrite().nodeKeyConstraintCreate( labelDescriptor( label, prop1 ) ); transaction.success(); } try ( Transaction transaction = beginTransaction() ) { transaction.schemaWrite().constraintDrop( existing ); SchemaRead schemaRead = transaction.schemaRead(); assertFalse( schemaRead.constraintExists( existing ) ); assertThat( asList( schemaRead.constraintsGetForLabel( label ) ), empty() ); } }
@Before public void setupGraph() throws IOException, KernelException { if ( testSupport == null ) { folder.create(); testSupport = newTestSupport(); testSupport.setup( folder.getRoot(), this::createTestGraph ); } Kernel kernel = testSupport.kernelToTest(); tx = kernel.beginTransaction( Transaction.Type.implicit, LoginContext.AUTH_DISABLED ); token = tx.token(); read = tx.dataRead(); indexRead = tx.indexRead(); schemaRead = tx.schemaRead(); cursors = new ManagedTestCursors( tx.cursors() ); }
@Test public void addIndexRuleInATransaction() throws Exception { // GIVEN SchemaWrite schemaWriteOperations = schemaWriteInNewTransaction(); // WHEN IndexReference expectedRule = schemaWriteOperations.indexCreate( descriptor ); commit(); // THEN SchemaRead schemaRead = newTransaction().schemaRead(); assertEquals( asSet( expectedRule ), asSet( schemaRead.indexesGetForLabel( labelId ) ) ); assertEquals( expectedRule, schemaRead.index( descriptor.getLabelId(), descriptor.getPropertyIds() ) ); commit(); }