QueryRegistryOperations oldQueryRegistryOperations = statement.queryRegistration(); Statement oldStatement = statement; InternalTransaction oldTransaction = transaction; KernelTransaction oldKernelTx = txBridge.getKernelTransactionBoundToThisThread( true ); txBridge.unbindTransactionFromCurrentThread(); kernelTransaction = txBridge.getKernelTransactionBoundToThisThread( true ); statement = kernelTransaction.acquireStatement(); statement.queryRegistration().registerExecutingQuery( executingQuery ); txBridge.unbindTransactionFromCurrentThread(); try oldStatement.close(); oldTransaction.success(); oldTransaction.close();
@Override public void cleanForReuse() { // close the old statement reference after the statement has been "upgraded" // to either a schema data or a schema statement, so that the locks are "handed over". statement.queryRegistration().unregisterExecutingQuery( executingQuery ); statement.close(); statement = txBridge.get(); statement.queryRegistration().registerExecutingQuery( executingQuery ); }
@Override public void close( boolean success ) { if ( isOpen ) { try { statement.queryRegistration().unregisterExecutingQuery( executingQuery ); statement.close(); if ( success ) { transaction.success(); } else { transaction.failure(); } transaction.close(); } finally { statement = null; kernelTransaction = null; transaction = null; isOpen = false; } } }
@Override public final Neo4jTransactionalContext newContext( ClientConnectionInfo clientConnection, InternalTransaction tx, String queryText, MapValue queryParameters ) { Statement initialStatement = statementSupplier.get(); ClientConnectionInfo connectionWithUserName = clientConnection.withUsername( tx.securityContext().subject().username() ); ExecutingQuery executingQuery = initialStatement.queryRegistration().startQueryExecution( connectionWithUserName, queryText, queryParameters ); return contextCreator.create( tx, initialStatement, executingQuery ); } }
@Override public void compute(String label, String type, int iterations) { float[] src = new float[nodes]; dst = new float[nodes]; try ( Transaction tx = db.beginTx()) { ThreadToStatementContextBridge ctx = this.db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class); ReadOperations ops = ctx.get().readOperations(); int labelId = ops.labelGetForName(label); int typeId = ops.relationshipTypeGetForName(type); int[] degrees = computeDegrees(ops,labelId, typeId); RelationshipVisitor<RuntimeException> visitor = new RelationshipVisitor<RuntimeException>() { public void visit(long relId, int relTypeId, long startNode, long endNode) throws RuntimeException { if (relTypeId == typeId) { dst[((int) endNode)] += src[(int) startNode]; } } }; for (int iteration = 0; iteration < iterations; iteration++) { startIteration(src, dst, degrees); PrimitiveLongIterator rels = ops.relationshipsGetAll(); while (rels.hasNext()) { ops.relationshipVisit(rels.next(), visitor); } } tx.success(); } catch (EntityNotFoundException e) { e.printStackTrace(); } }
@Override public void compute(String label, String type, int iterations) { degree = new int[nodeCount]; Arrays.fill(degree, -1); try ( Transaction tx = db.beginTx()) { ThreadToStatementContextBridge ctx = this.db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class); ReadOperations ops = ctx.get().readOperations(); int labelId = ops.labelGetForName(label); int relationshipTypeId = ops.relationshipTypeGetForName(type); PrimitiveLongIterator it = ops.nodesGetForLabel(labelId); int totalCount = nodeCount; runOperations(pool, it, totalCount, ops, new OpsRunner() { public void run(int id) throws EntityNotFoundException { degree[id] = ops.nodeGetDegree(id, direction, relationshipTypeId); } }); tx.success(); } catch (Exception e) { e.printStackTrace(); } }
public void writeBackResults(GraphDatabaseService db, Algorithm algorithm) { ThreadToStatementContextBridge ctx = ((GraphDatabaseAPI)db).getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class); int propertyNameId; try (Transaction tx = db.beginTx()) { propertyNameId = ctx.get().tokenWriteOperations().propertyKeyGetOrCreateForName(algorithm.getPropertyName()); tx.success(); } catch (IllegalTokenNameException e) { throw new RuntimeException(e);
private void runTransaction( LoginContext loginContext, Map<String,Object> metaData ) { try ( Transaction transaction = db.beginTransaction( KernelTransaction.Type.explicit, loginContext ); Statement statement = db.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class ).get() ) { statement.queryRegistration().setMetaData( metaData ); db.createNode(); transaction.success(); } }
private ResourceIterator<Node> allNodesWithLabel( final Label myLabel ) { KernelTransaction ktx = statementContext.getKernelTransactionBoundToThisThread( true ); Statement statement = ktx.acquireStatement(); int labelId = ktx.tokenRead().nodeLabel( myLabel.name() ); if ( labelId == TokenRead.NO_TOKEN ) { statement.close(); return Iterators.emptyResourceIterator(); } NodeLabelIndexCursor cursor = ktx.cursors().allocateNodeLabelIndexCursor(); ktx.dataRead().nodeLabelScan( labelId, cursor ); return new NodeCursorResourceIterator<>( cursor, statement, this::newNodeProxy ); }
public void run() { try (Transaction tx = db.beginTx()) { DataWriteOperations ops = ctx.get().dataWriteOperations(); for (long i = 0; i < WRITE_BATCH; i++) { long node = i + start; if (node >= nodes) break; double value = algorithm.getResult(node); if (value > 0) { ops.nodeSetProperty(node, DefinedProperty.doubleProperty(propertyNameId, value)); } } tx.success(); } catch (AutoIndexingKernelException | ConstraintValidationKernelException | InvalidTransactionTypeKernelException | EntityNotFoundException e) { e.printStackTrace(); } } });
when( initialStatement.queryRegistration() ).thenReturn( initialQueryRegistry ); when( queryService.beginTransaction( transactionType, securityContext ) ).thenReturn( secondTransaction ); when( txBridge.getKernelTransactionBoundToThisThread( true ) ).thenReturn( initialKTX, initialKTX, secondKTX ); when( secondStatement.queryRegistration() ).thenReturn( secondQueryRegistry ); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( initialKTX ).executionStatistics(); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( txBridge ).unbindTransactionFromCurrentThread(); order.verify( txBridge ).getKernelTransactionBoundToThisThread( true ); order.verify( secondKTX ).acquireStatement( ); order.verify( secondQueryRegistry ).registerExecutingQuery( executingQuery ); order.verify( txBridge ).unbindTransactionFromCurrentThread();
private ResourceIterator<Node> nodesByLabelAndProperty( KernelTransaction transaction, int labelId, IndexQuery query ) { Statement statement = transaction.acquireStatement(); Read read = transaction.dataRead(); if ( query.propertyKeyId() == TokenRead.NO_TOKEN || labelId == TokenRead.NO_TOKEN ) { statement.close(); return emptyResourceIterator(); } IndexReference index = transaction.schemaRead().index( labelId, query.propertyKeyId() ); if ( index != IndexReference.NO_INDEX ) { // Ha! We found an index - let's use it to find matching nodes try { NodeValueIndexCursor cursor = transaction.cursors().allocateNodeValueIndexCursor(); read.nodeIndexSeek( index, cursor, IndexOrder.NONE, false, query ); return new NodeCursorResourceIterator<>( cursor, statement, this::newNodeProxy ); } catch ( KernelException e ) { // weird at this point but ignore and fallback to a label scan } } return getNodesByLabelAndPropertyWithoutIndex( statement, labelId, query ); }
@Override public TransactionalContext getOrBeginNewIfClosed() { checkNotTerminated(); if ( !isOpen ) { transaction = graph.beginTransaction( transactionType, securityContext ); kernelTransaction = txBridge.getKernelTransactionBoundToThisThread( true ); statement = kernelTransaction.acquireStatement(); statement.queryRegistration().registerExecutingQuery( executingQuery ); isOpen = true; } return this; }
KernelTransaction.Type type = KernelTransaction.Type.implicit; QueryRegistryOperations registryOperations = mock( QueryRegistryOperations.class ); when( statement.queryRegistration() ).thenReturn( registryOperations ); when( statementBridge.get() ).thenReturn( statement ); when( kernelTransaction.securityContext() ).thenReturn( loginContext.authorize( s -> -1, GraphDatabaseSettings.DEFAULT_DATABASE_NAME ) ); when( kernelTransaction.transactionType() ).thenReturn( type ); when( database.getGraph() ).thenReturn( databaseFacade ); when( databaseFacade.getDependencyResolver() ).thenReturn( resolver );
@Description( "Attaches a map of data to the transaction. The data will be printed when listing queries, and " + "inserted into the query log." ) @Procedure( name = "dbms.setTXMetaData", mode = DBMS ) public void setTXMetaData( @Name( value = "data" ) Map<String,Object> data ) { securityContext.assertCredentialsNotExpired(); int totalCharSize = data.entrySet().stream() .mapToInt( e -> e.getKey().length() + e.getValue().toString().length() ) .sum(); if ( totalCharSize >= HARD_CHAR_LIMIT ) { throw new IllegalArgumentException( format( "Invalid transaction meta-data, expected the total number of chars for " + "keys and values to be less than %d, got %d", HARD_CHAR_LIMIT, totalCharSize ) ); } try ( Statement statement = getCurrentTx().acquireStatement() ) { statement.queryRegistration().setMetaData( data ); } }
@Test public void closeStatementOnClose() { KernelTransaction kernelTransaction = mock( KernelTransaction.class ); Statement statement = mock( Statement.class ); when( kernelTransaction.acquireStatement() ).thenReturn( statement ); //noinspection EmptyTryBlock try ( IndexProcedures ignored = new IndexProcedures( kernelTransaction, null ) ) { } verify( statement ).close(); } }
@Override public void close() { statement.close(); }
@Override public final Neo4jTransactionalContext newContext( ClientConnectionInfo clientConnection, InternalTransaction tx, String queryText, MapValue queryParameters ) { Statement initialStatement = statementSupplier.get(); ClientConnectionInfo connectionWithUserName = clientConnection.withUsername( tx.securityContext().subject().username() ); ExecutingQuery executingQuery = initialStatement.queryRegistration().startQueryExecution( connectionWithUserName, queryText, queryParameters ); return contextCreator.create( tx, initialStatement, executingQuery ); } }
dst = new AtomicIntegerArray(nodeCount); try ( Transaction tx = db.beginTx()) { ThreadToStatementContextBridge ctx = this.db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class); ReadOperations ops = ctx.get().readOperations(); int labelId = ops.labelGetForName(label); int typeId = ops.relationshipTypeGetForName(type); startIteration(src, dst, degrees); PrimitiveLongIterator rels = ops.relationshipsGetAll(); runOperations(pool, rels, relCount , ops, new OpsRunner() { public void run(int id) throws EntityNotFoundException { tx.success(); } catch (EntityNotFoundException e) { e.printStackTrace();
private ResourceIterator<Node> allNodesWithLabel( final Label myLabel ) { KernelTransaction ktx = statementContext.getKernelTransactionBoundToThisThread( true ); Statement statement = ktx.acquireStatement(); int labelId = ktx.tokenRead().nodeLabel( myLabel.name() ); if ( labelId == TokenRead.NO_TOKEN ) { statement.close(); return Iterators.emptyResourceIterator(); } NodeLabelIndexCursor cursor = ktx.cursors().allocateNodeLabelIndexCursor(); ktx.dataRead().nodeLabelScan( labelId, cursor ); return new NodeCursorResourceIterator<>( cursor, statement, this::newNodeProxy ); }