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 ) ); } }
SchemaCalculator( Transaction ktx ) { this.dataRead = ktx.dataRead(); this.tokenRead = ktx.tokenRead(); this.cursors = ktx.cursors(); // the only one that is common for both nodes and rels so thats why we can do it here propertyIdToPropertyNameMapping = new HashMap<>( tokenRead.propertyKeyCount() ); addNamesToCollection( tokenRead.propertyKeyGetAllTokens(), propertyIdToPropertyNameMapping ); }
public int getNumberOfNodes() { Kernel kernel = database.getGraph().getDependencyResolver().resolveDependency( Kernel.class ); try ( org.neo4j.internal.kernel.api.Transaction tx = kernel.beginTransaction( implicit, AnonymousContext.read() ) ) { return Math.toIntExact( tx.dataRead().nodesGetCount() ); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } }
public int getNumberOfRelationships() { Kernel kernel = database.getGraph().getDependencyResolver().resolveDependency( Kernel.class ); try ( org.neo4j.internal.kernel.api.Transaction tx = kernel.beginTransaction( implicit, AnonymousContext.read() ) ) { return Math.toIntExact( tx.dataRead().relationshipsGetCount() ); } catch ( TransactionFailureException e ) { throw new RuntimeException( e ); } }
@Test public void shouldNotSeeNonExistingNode() throws Exception { // Given, empty db // Then try ( Transaction tx = beginTransaction() ) { assertFalse( tx.dataRead().nodeExists( 1337L ) ); } }
protected int countNodes( Transaction transaction ) { int result = 0; try ( NodeCursor cursor = transaction.cursors().allocateNodeCursor() ) { transaction.dataRead().allNodesScan( cursor ); while ( cursor.next() ) { result++; } } return result; }
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 shouldSeeNewGraphPropertyInTransaction() throws Exception { try ( Transaction tx = beginTransaction(); PropertyCursor cursor = tx.cursors().allocatePropertyCursor() ) { int prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.dataRead().graphProperties( cursor ); assertTrue( cursor.next() ); assertThat( cursor.propertyKey(), equalTo( prop ) ); assertThat( cursor.propertyValue(), equalTo( stringValue( "hello" ) ) ); } }
@Test public void shouldThrowIfTransactionTerminated() throws Exception { try ( Transaction tx = beginTransaction() ) { // given terminate( tx ); // expect exception.expect( TransactionTerminatedException.class ); // when tx.dataRead().nodeExists( 42 ); } }
private void assertNoNode( long nodeId ) throws TransactionFailureException { try ( Transaction tx = beginTransaction(); NodeCursor cursor = tx.cursors().allocateNodeCursor() ) { tx.dataRead().singleNode( nodeId, cursor ); assertFalse( cursor.next() ); } } }
@Test public void shouldSeeNodeExistingInTxOnly() throws Exception { try ( Transaction tx = beginTransaction() ) { long node = tx.dataWrite().nodeCreate(); assertTrue( tx.dataRead().nodeExists( node ) ); } }
@Test public void shouldNotFindDeletedNodeInLabelScan() throws Exception { // Given Node node = createNode( "label" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeDelete( node.node ); tx.dataRead().nodeLabelScan( node.labels[0], cursor ); // then assertFalse( cursor.next() ); } }
@Test public void shouldNotFindNodeWithRemovedLabelInLabelScan() throws Exception { // Given Node node = createNode( "label" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeRemoveLabel( node.node, node.labels[0] ); tx.dataRead().nodeLabelScan( node.labels[0], cursor ); // then assertFalse( cursor.next() ); } }
@Ignore public void shouldNotFindDeletedNodeInConjunctionLabelScan() throws Exception { // Given Node node = createNode( "label1", "label2" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeDelete( node.node ); tx.dataRead().nodeLabelIntersectionScan( cursor, node.labels ); // then assertFalse( cursor.next() ); } }
@Test public void shouldFindMatchingNode() throws Exception { // given IndexReference index = createUniquenessConstraint( labelId, propertyId1 ); Value value = Values.of( "value" ); long nodeId = createNodeWithValue( value ); // when looking for it Read read = newTransaction().dataRead(); int propertyId = index.properties()[0]; long foundId = read.lockingNodeUniqueIndexSeek( index, exact( propertyId, value ) ); commit(); // then assertEquals( "Created node was not found", nodeId, foundId ); }
@Ignore public void shouldNotFindDeletedNodeInDisjunctionLabelScan() throws Exception { // Given Node node = createNode( "label1", "label2" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeDelete( node.node ); tx.dataRead().nodeLabelUnionScan( cursor, node.labels ); // then assertFalse( cursor.next() ); } }
@Ignore public void shouldNotFindNodeWithRemovedLabelInConjunctionLabelScan() throws Exception { // Given Node node = createNode( "label1", "label2" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeRemoveLabel( node.node, node.labels[1] ); tx.dataRead().nodeLabelIntersectionScan( cursor, node.labels ); // then assertFalse( cursor.next() ); } }
@Test public void shouldNotFindNonMatchingNode() throws Exception { // given IndexReference index = createUniquenessConstraint( labelId, propertyId1 ); Value value = Values.of( "value" ); createNodeWithValue( Values.of( "other_" + value ) ); // when looking for it Transaction transaction = newTransaction(); long foundId = transaction.dataRead().lockingNodeUniqueIndexSeek( index, exact( propertyId1, value ) ); commit(); // then assertTrue( "Non-matching created node was found", isNoSuchNode( foundId ) ); }
@Test public void txReturnsCorrectIdWhenReadOnly() throws Exception { executeDummyTxs( db, 42 ); org.neo4j.internal.kernel.api.Transaction tx = newTransaction(); try ( NodeCursor node = tx.cursors().allocateNodeCursor() ) { tx.dataRead().singleNode( 1, node ); node.next(); } tx.success(); assertEquals( KernelTransaction.READ_ONLY, tx.closeTransaction() ); assertFalse( tx.isOpen() ); }
@Ignore public void shouldFindNodeWithOneRemovedLabelInDisjunctionLabelScan() throws Exception { // Given Node node = createNode( "label1", "label2" ); try ( org.neo4j.internal.kernel.api.Transaction tx = beginTransaction(); NodeLabelIndexCursor cursor = tx.cursors().allocateNodeLabelIndexCursor() ) { // when tx.dataWrite().nodeRemoveLabel( node.node, node.labels[1] ); tx.dataRead().nodeLabelUnionScan( cursor, node.labels ); // then assertTrue( cursor.next() ); assertEquals( node.node, cursor.nodeReference() ); } }