@Override public QueryStatistics queryStatistics() { return originalResult.getQueryStatistics(); }
@Override public QueryStatistics getQueryStatistics() { return originalResult.getQueryStatistics(); }
@Before public void setUp() throws Exception { GraphDatabaseQueryService cypherService = new GraphDatabaseCypherService( this.database.getGraphDatabaseAPI() ); compilerFactory = mock( CompilerFactory.class ); transactionalContext = mock( TransactionalContext.class ); KernelStatement kernelStatement = mock( KernelStatement.class ); executor = mock( SnapshotExecutionEngine.ParametrizedQueryExecutor.class ); versionContext = mock( VersionContext.class ); executionEngine = createExecutionEngine(cypherService); when( kernelStatement.getVersionContext() ).thenReturn( versionContext ); when( transactionalContext.statement() ).thenReturn( kernelStatement ); Result result = mock( Result.class ); QueryStatistics statistics = mock( QueryStatistics.class ); when( result.getQueryStatistics() ).thenReturn( statistics ); when( executor.execute( any(), anyMap(), any() ) ).thenReturn( result ); }
public CypherResultRepresentation( final Result result, boolean includeStats, boolean includePlan ) { super( RepresentationType.STRING ); resultRepresentation = createResultRepresentation( result ); columns = ListRepresentation.string( result.columns() ); statsRepresentation = includeStats ? new CypherStatisticsRepresentation( result.getQueryStatistics() ) : null; plan = includePlan ? CypherPlanRepresentation.newFromProvider(planProvider(result)) : null; }
@Test public void eagerResultHaveQueryStatistic() { Result result = database.execute( "MATCH (n) RETURN n.c" ); assertEquals( 1, testCursorContext.getAdditionalAttempts() ); assertFalse( result.getQueryStatistics().containsUpdates() ); }
protected <T> Result executeWithRetries( String query, T parameters, TransactionalContext context, ParametrizedQueryExecutor<T> executor ) throws QueryExecutionKernelException { VersionContext versionContext = getCursorContext( context ); EagerResult eagerResult; int attempt = 0; boolean dirtySnapshot; do { if ( attempt == maxQueryExecutionAttempts ) { return throwQueryExecutionException( "Unable to get clean data snapshot for query '%s' after %d attempts.", query, attempt ); } attempt++; versionContext.initRead(); Result result = executor.execute( query, parameters, context ); eagerResult = new EagerResult( result, versionContext ); eagerResult.consume(); dirtySnapshot = versionContext.isDirty(); if ( dirtySnapshot && result.getQueryStatistics().containsUpdates() ) { return throwQueryExecutionException( "Unable to get clean data snapshot for query '%s' that perform updates.", query, attempt ); } } while ( dirtySnapshot ); return eagerResult; }
if ( includeStats ) writeStats( result.getQueryStatistics() );
private Object executeStatement(BlockingQueue<RowResult> queue, String stmt, Map<String, Object> params, boolean addStatistics, long timeout) throws InterruptedException { try (Result result = db.execute(stmt,params)) { long time = System.currentTimeMillis(); int row = 0; while (result.hasNext()) { terminationGuard.check(); queue.put(new RowResult(row++, result.next())); } if (addStatistics) { queue.offer(new RowResult(-1, toMap(result.getQueryStatistics(), System.currentTimeMillis() - time, row)), timeout,TimeUnit.SECONDS); } return row; } }
private void addEventData(Map<String, Object> event) { Map<String, Object> params = map("params", event); Result result = db.execute("CREATE (e:Event {params})", params); int created = result.getQueryStatistics().getNodesCreated(); assertTrue("Expected a node to be created", created == 1); String name = event.get("name").toString(); if (!event.containsKey("toofar")) { spaceNodes.put(name, event); if (!event.containsKey("toosoon") && !event.containsKey("toolate")) { spaceTimeNodes.put(name, event); } } eventNodes.put(name, event); }
@Procedure(mode = Mode.WRITE) @Description("apoc.example.movies() | Creates the sample movies graph") public Stream<ProgressInfo> movies() { long start = System.currentTimeMillis(); String file = "movies.cypher"; Result result = db.execute(Util.readResourceFile(file)); QueryStatistics stats = result.getQueryStatistics(); ProgressInfo progress = new ProgressInfo(file, "example movie database from themoviedb.org", "cypher") .update(stats.getNodesCreated(), stats.getRelationshipsCreated(), stats.getPropertiesSet()) .done(start); result.close(); return Stream.of(progress); } }
public void expireNodes(long limit) { try { if (!Util.isWriteableInstance(db)) return; Result result = db.execute("MATCH (t:TTL) where t.ttl < timestamp() WITH t LIMIT {limit} DETACH DELETE t", Util.map("limit", limit)); QueryStatistics stats = result.getQueryStatistics(); result.close(); if (stats.getNodesDeleted()>0) { log.info("TTL: Expired %d nodes %d relationships", stats.getNodesDeleted(), stats.getRelationshipsDeleted()); } } catch (Exception e) { log.error("TTL: Error deleting expired nodes", e); } }
@Override public QueryStatistics queryStatistics() { return originalResult.getQueryStatistics(); }
@Override public QueryStatistics getQueryStatistics() { return originalResult.getQueryStatistics(); }
private int summaryUpdates(Result result) { int updates = 0; updates += result.getQueryStatistics().getConstraintsAdded(); updates += result.getQueryStatistics().getConstraintsRemoved(); updates += result.getQueryStatistics().getNodesCreated(); updates += result.getQueryStatistics().getNodesDeleted(); updates += result.getQueryStatistics().getRelationshipsCreated(); updates += result.getQueryStatistics().getRelationshipsDeleted(); updates += result.getQueryStatistics().getLabelsAdded(); updates += result.getQueryStatistics().getLabelsRemoved(); updates += result.getQueryStatistics().getPropertiesSet(); return updates; }
@Override public QueryStatisticsModel adapt(Result response) { try { org.neo4j.graphdb.QueryStatistics statistics = response.getQueryStatistics(); String stats = mapper.writeValueAsString(statistics); stats = stats.replace("Deleted", "_deleted"); stats = stats.replace("Added", "_added"); stats = stats.replace("Updates", "_updates"); stats = stats.replace("Created", "_created"); stats = stats.replace("Set", "_set"); stats = stats.replace("Removed", "_removed"); stats = stats.replace("deletedNodes", "nodes_deleted"); stats = stats.replace("deletedRelationships", "relationships_deleted"); //Modify the string to include contains_updates as it is a calculated value String containsUpdates = ",\"contains_updates\":" + statistics.containsUpdates(); int closingBraceIndex = stats.lastIndexOf("}"); stats = stats.substring(0, closingBraceIndex) + containsUpdates + "}"; return mapper.readValue(stats, QueryStatisticsModel.class); } catch (Exception e) { throw new ResultProcessingException("Could not read response statistics", e); } } }
@Override public ElementCounter load(Reader reader, Reporter reporter, Config config) { Scanner scanner = new Scanner(reader).useDelimiter(READ_FILE_PATTERN); HashMap<String, Object> params = new HashMap<>(); try (BatchTransaction tx = new BatchTransaction(db,BATCH_SIZE,reporter)) { while (scanner.hasNext()) { String query = scanner.next(); if (query.trim().isEmpty()) { // begin or commit line tx.commit(); continue; } params.clear(); String queryWithParams = extractor.extract(query, params); Result result = db.execute(queryWithParams, params); ProgressReporter.update(result.getQueryStatistics(), reporter); tx.increment(); } } return reporter.getTotal(); }
public CypherResultRepresentation( final Result result, boolean includeStats, boolean includePlan ) { super( RepresentationType.STRING ); resultRepresentation = createResultRepresentation( result ); columns = ListRepresentation.string( result.columns() ); statsRepresentation = includeStats ? new CypherStatisticsRepresentation( result.getQueryStatistics() ) : null; plan = includePlan ? CypherPlanRepresentation.newFromProvider(planProvider(result)) : null; }
private CypherResult doExecuteQuery(String query, Map<String, Object> params, boolean canProfile) { params = params == null ? Collections.<String,Object>emptyMap() : params; long time=System.currentTimeMillis(); Transaction tx = gdb.beginTx(); KernelTransaction resumeTx; try { resumeTx = suspendTx(query); Result result = canProfile ? gdb.execute("PROFILE "+query,params) : gdb.execute(query,params); final Collection<Map<String, Object>> data = Iterators.asCollection(result); time = System.currentTimeMillis() - time; resumeTransaction(resumeTx); CypherResult cypherResult = new CypherResult(result.columns(), data, result.getQueryStatistics(), time, canProfile ? result.getExecutionPlanDescription() : null, prettify(query)); tx.success(); return cypherResult; } finally { tx.close(); awaitIndexOnline(query); } }
@Test public void explain_returns_plan() throws Exception { // START SNIPPET: explain_returns_plan Result result = db.execute( "EXPLAIN CREATE (user:User{name:{name}}) RETURN user" ); assert result.getQueryExecutionType().isExplained(); assert result.getQueryExecutionType().requestedExecutionPlanDescription(); assert !result.hasNext(); assert !result.getQueryStatistics().containsUpdates(); assert result.columns().isEmpty(); assert !result.getExecutionPlanDescription().hasProfilerStatistics(); // END SNIPPET: explain_returns_plan }
private int executeOnInput(CSVReader reader, String query, CSVWriter writer, Config config, ProgressReporter reporter) throws IOException { Map<String, Object> params = createParams(reader); Map<String, Type> types = extractTypes(params); Map<String, String> replacements = computeReplacements(params, query); String[] input; boolean first = true; int outCount = 0; try (BatchTransaction tx = new BatchTransaction(getServer().getDb(),config.getBatchSize(),reporter)) { while ((input = reader.readNext()) != null) { Map<String, Object> queryParams = update(params, types, input); String newQuery = applyReplacements(query, replacements, queryParams); Result result = getEngine().execute(newQuery, queryParams); outCount += writeResult(result, writer, first); first = false; ProgressReporter.update(result.getQueryStatistics(), reporter); tx.increment(); } } return outCount; }