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 ) ); } }
@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(); } }
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 ); }
@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" ) ) ); } }
@Ignore public void shouldNotFindNodeWithAllRemovedLabelsInDisjunctionLabelScan() 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[0] ); tx.dataWrite().nodeRemoveLabel( node.node, node.labels[1] ); tx.dataRead().nodeLabelUnionScan( cursor, node.labels ); // then assertFalse( cursor.next() ); } }
@Test public void shouldPerformStringRangeSearch() throws Exception { // given Set<Pair<Long,Value>> expected = new HashSet<>(); try ( Transaction tx = beginTransaction() ) { expected.add( nodeWithProp( tx, "banana" ) ); nodeWithProp( tx, "apple" ); tx.success(); } createIndex(); // when try ( Transaction tx = beginTransaction() ) { int label = tx.tokenRead().nodeLabel( "Node" ); int prop = tx.tokenRead().propertyKey( "prop" ); expected.add( nodeWithProp( tx, "cherry" ) ); nodeWithProp( tx, "dragonfruit" ); IndexReference index = tx.schemaRead().index( label, prop ); assertNodeAndValueForSeek( expected, tx, index, needsValues, "berry", IndexQuery.range( prop, "b", true, "d", false ) ); } }
@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() ); }
private long createLabeledNode( Transaction transaction, String label ) throws KernelException { long node = transaction.dataWrite().nodeCreate(); int labelId = transaction.tokenWrite().labelGetOrCreateForName( label ); transaction.dataWrite().nodeAddLabel( node, labelId ); return node; }
private void dropConstraint( ConstraintDescriptor descriptor ) throws KernelException { try ( Transaction transaction = kernel.beginTransaction( implicit, AUTH_DISABLED ) ) { transaction.schemaWrite().constraintDrop( descriptor ); transaction.success(); } }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given int prop; Value theValue = stringValue( "The Value" ); try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); tx.dataWrite().graphSetProperty( prop, theValue ); tx.success(); } // When Transaction tx = beginTransaction(); assertThat( tx.dataWrite().graphSetProperty( prop, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); } }
@Test public void shouldNotWriteWhenSettingPropertyToSameValue() throws Exception { // Given Value theValue = stringValue( "The Value" ); long nodeId = createNodeWithProperty( propertyKey, theValue.asObject() ); // When Transaction tx = beginTransaction(); int property = tx.token().propertyKeyGetOrCreateForName( propertyKey ); assertThat( tx.dataWrite().nodeSetProperty( nodeId, property, theValue ), equalTo( theValue ) ); tx.success(); assertThat( tx.closeTransaction(), equalTo( Transaction.READ_ONLY ) ); }
@Test public void txReturnsCorrectIdWhenFailedAndMarkedForTermination() throws Exception { executeDummyTxs( db, 42 ); org.neo4j.internal.kernel.api.Transaction tx = newTransaction( AUTH_DISABLED ); tx.dataWrite().nodeCreate(); tx.failure(); tx.markForTermination( Status.Transaction.Terminated ); assertEquals( KernelTransaction.ROLLBACK, tx.closeTransaction() ); assertFalse( tx.isOpen() ); }
@Test public void shouldBeAbleToWriteNewGraphProperty() throws Exception { int prop; try ( Transaction tx = beginTransaction() ) { prop = tx.tokenWrite().propertyKeyGetOrCreateForName( "prop" ); assertThat( tx.dataWrite().graphSetProperty( prop, stringValue( "hello" ) ), equalTo( NO_VALUE ) ); tx.success(); } try ( org.neo4j.graphdb.Transaction ignore = graphDb.beginTx() ) { assertThat( testSupport.graphProperties().getProperty( "prop" ), equalTo( "hello" ) ); } }
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 ); } }
static void putTokenCounts( Map<String,Object> metaData, Kernel kernel ) { try ( Transaction tx = kernel.beginTransaction( Transaction.Type.explicit, LoginContext.AUTH_DISABLED ) ) { TokenRead tokens = tx.tokenRead(); metaData.put( "labelCount", tokens.labelCount() ); metaData.put( "relationshipTypeCount", tokens.relationshipTypeCount() ); metaData.put( "propertyKeyCount", tokens.propertyKeyCount() ); tx.success(); } catch ( TransactionFailureException e ) { throw new UnsupportedOperationException( "How to handle errors?" ); } } }
@Test public void txReturnsCorrectIdWhenCommitted() throws Exception { executeDummyTxs( db, 42 ); org.neo4j.internal.kernel.api.Transaction tx = newTransaction( AUTH_DISABLED ); tx.dataWrite().nodeCreate(); tx.success(); long previousCommittedTxId = lastCommittedTxId( db ); assertEquals( previousCommittedTxId + 1, tx.closeTransaction() ); assertFalse( tx.isOpen() ); }
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 shouldAddLabelNodeOnce() throws Exception { long node = createNodeWithLabel( labelName ); try ( Transaction tx = beginTransaction() ) { int labelId = tx.token().labelGetOrCreateForName( labelName ); assertFalse( tx.dataWrite().nodeAddLabel( node, labelId ) ); tx.success(); } assertLabels( node, labelName ); }